1
0
Fork 0
forked from nuttx/nuttx-update

sched/group and syscall/: Implement 'real' setuid, getuid, setgid, and getgid interfaces. These will be inheritance by all child task groups.

This commit is contained in:
Gregory Nutt 2019-08-06 14:13:43 -06:00
parent 945e27d85c
commit ec5120f69e
16 changed files with 419 additions and 16 deletions

View file

@ -441,6 +441,13 @@ struct task_group_s
#endif
uint8_t tg_flags; /* See GROUP_FLAG_* definitions */
/* User identity **************************************************************/
#ifdef CONFIG_SCHED_USER_IDENTITY
uid_t tg_uid; /* User identity */
gid_t tg_gid; /* User group identity */
#endif
/* Group membership ***********************************************************/
uint8_t tg_nmembers; /* Number of members in the group */

View file

@ -82,20 +82,33 @@
#define SYS_sched_yield (CONFIG_SYS_RESERVED + 12)
#define SYS_set_errno (CONFIG_SYS_RESERVED + 13)
#define SYS_uname (CONFIG_SYS_RESERVED + 14)
#define __SYS_uid (CONFIG_SYS_RESERVED + 15)
/* User identity */
#ifdef CONFIG_SCHED_USER_IDENTITY
# define SYS_setuid (__SYS_uid + 0)
# define SYS_getuid (__SYS_uid + 1)
# define SYS_setgid (__SYS_uid + 2)
# define SYS_getgid (__SYS_uid + 3)
# define __SYS_sem (__SYS_uid + 4)
#else
# define __SYS_sem __SYS_uid
#endif
/* Semaphores */
#define SYS_sem_destroy (CONFIG_SYS_RESERVED + 15)
#define SYS_sem_post (CONFIG_SYS_RESERVED + 16)
#define SYS_sem_timedwait (CONFIG_SYS_RESERVED + 17)
#define SYS_sem_trywait (CONFIG_SYS_RESERVED + 18)
#define SYS_sem_wait (CONFIG_SYS_RESERVED + 19)
#define SYS_sem_destroy (__SYS_sem + 0)
#define SYS_sem_post (__SYS_sem + 1)
#define SYS_sem_timedwait (__SYS_sem + 2)
#define SYS_sem_trywait (__SYS_sem + 3)
#define SYS_sem_wait (__SYS_sem + 4)
#ifdef CONFIG_PRIORITY_INHERITANCE
# define SYS_sem_setprotocol (CONFIG_SYS_RESERVED + 20)
# define __SYS_named_sem (CONFIG_SYS_RESERVED + 21)
# define SYS_sem_setprotocol (__SYS_sem + 5)
# define __SYS_named_sem (__SYS_sem + 6)
#else
# define __SYS_named_sem (CONFIG_SYS_RESERVED + 20)
# define __SYS_named_sem (__SYS_sem + 5)
#endif
/* Named semaphores */

View file

@ -371,7 +371,9 @@ long sysconf(int name);
/* User and group identity management */
int setuid(uid_t uid);
uid_t getuid(void);
int setgid(gid_t gid);
gid_t getgid(void);
#undef EXTERN
#if defined(__cplusplus)

View file

@ -38,7 +38,10 @@
CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_sysconf.c
CSRCS += lib_getopt.c lib_getoptargp.c lib_getoptindp.c lib_getoptoptp.c
CSRCS += lib_sleep.c lib_usleep.c
ifneq ($(CONFIG_SCHED_USER_IDENTITY),y)
CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c
endif
ifneq ($(CONFIG_DISABLE_ENVIRON),y)
CSRCS += lib_chdir.c lib_getcwd.c

View file

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/unistd/lib_aetgid.c
* libs/libc/unistd/lib_getgid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
@ -56,12 +56,11 @@
* None.
*
* Returned Value:
* The getgid() function is always be successful and no return value is
* reserved to indicate an error..
* The real group ID of the calling task group.
*
****************************************************************************/
int setgid(gid_t gid)
gid_t getgid(void)
{
/* Return group identity 'root' with a gid value of 0. */

View file

@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/unistd/lib_setuid.c
* libs/libc/unistd/lib_getuid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
@ -56,12 +56,11 @@
* None
*
* Returned Value:
* Zero if successful and -1 in case of failure, in which case errno is set
* appropriately.
* The real user ID of the calling task group.
*
****************************************************************************/
int setuid(uid_t uid)
uid_t getuid(void)
{
/* Return the user identity 'root' with a uid value of 0. */

View file

@ -621,6 +621,17 @@ config SCHED_EXIT_KILL_CHILDREN
memory leaks since, for example, memory allocations held by threads
are not automatically freed!
config SCHED_USER_IDENTITY
bool "Support per-task User Identity"
default n
---help---
This selection enables functionality of getuid(), setuid(), getgid(),
setgid(). If this option is not selected, then stub, root-only
versions of these interfaces are avaialbe. When selected, these
interfaces will associate a UID and/or GID with each task group.
Those can then be managed using the interfaces. Child tasks will
inherit the UID and GID of its parent.
endmenu # Tasks and Scheduling
menu "Pthread Options"

View file

@ -48,6 +48,10 @@ CSRCS += group_waiter.c
endif
endif
ifeq ($(CONFIG_SCHED_USER_IDENTITY),y)
CSRCS += group_setuid.c group_setgid.c group_getuid.c group_getgid.c
endif
ifeq ($(CONFIG_ARCH_ADDRENV),y)
CSRCS += group_addrenv.c
endif

View file

@ -50,11 +50,13 @@
#include <nuttx/sched.h>
#include "environ/environ.h"
#include "sched/sched.h"
#include "group/group.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Is this worth making a configuration option? */
#define GROUP_INITIAL_MEMBERS 4
@ -62,6 +64,7 @@
/****************************************************************************
* Private Data
****************************************************************************/
/* This is counter that is used to generate unique task group IDs */
#if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
@ -144,6 +147,40 @@ static void group_assign_grpid(FAR struct task_group_s *group)
}
#endif /* HAVE_GROUP_MEMBERS */
/****************************************************************************
* Name: group_inherit_identity
*
* Description:
* All inherit the user identity from the parent task group.
*
* Input Parameters:
* group - The new task group.
*
* Returned Value:
* None
*
* Assumptions:
* The parent of the new task is the task at the head of the assigned task
* list for the current CPU.
*
****************************************************************************/
#ifdef CONFIG_SCHED_USER_IDENTITY
static inline void group_inherit_identity(FAR struct task_group_s *group)
{
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *rgroup = rtcb->group;
/* Inherit the user identity from the parent task group. */
DEBUGASSERT(group != NULL);
group->tg_uid = rgroup->tg_uid;
group->tg_gid = rgroup->tg_gid;
}
#else
# define group_inherit_identity(group)
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -230,6 +267,10 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
group_assign_grpid(group);
#endif
/* Inherit the user identity from the parent task group */
group_inherit_identity(group);
/* Duplicate the parent tasks environment */
ret = env_dup(group);

View file

@ -0,0 +1,74 @@
/****************************************************************************
* sched/group/group_getgid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <sched/sched.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getgid
*
* Description:
* The getgid() function will the real group ID of the calling task group.
*
* Input Parameters:
* None.
*
* Returned Value:
* The real group ID of the calling task group.
*
****************************************************************************/
gid_t getgid(void)
{
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *rgroup = rtcb->group;
/* Set the task group's group identity. */
DEBUGASSERT(group != NULL);
return rgroup->tg_gid;
}

View file

@ -0,0 +1,74 @@
/****************************************************************************
* sched/group/group_getuid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <sched/sched.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getuid
*
* Description:
* The getuid() function will return the real user ID of the calling process.
*
* Input Parameters:
* None
*
* Returned Value:
* The real user ID of the calling task group.
*
****************************************************************************/
uid_t getuid(void)
{
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *rgroup = rtcb->group;
/* Set the task group's group identity. */
DEBUGASSERT(group != NULL);
return rgroup->tg_uid;
}

View file

@ -0,0 +1,78 @@
/****************************************************************************
* sched/group/group_setgid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <sched/sched.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: setgid
*
* Description:
* The setgid() function sets the real group ID, effective group ID, and
* the saved set-group-ID of the calling process to gid, given appropriate
* privileges.
*
* Input Parameters:
* uid - User identity to set the various process' user ID attributes to.
*
* Returned Value:
* Zero if successful and -1 in case of failure, in which case errno is set
* appropriately.
*
****************************************************************************/
int setgid(gid_t gid)
{
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *rgroup = rtcb->group;
/* Set the task group's group identity. */
DEBUGASSERT(group != NULL);
rgroup->tg_gid = gid;
return OK;
}

View file

@ -0,0 +1,78 @@
/****************************************************************************
* sched/group/group_setuid.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <sched/sched.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: setuid
*
* Description:
* The setuid() function sets the real user ID, effective user ID, and the
* saved set-user-ID of the calling process to uid, given appropriate
* privileges.
*
* Input Parameters:
* uid - User identity to set the various process' user ID attributes to.
*
* Returned Value:
* Zero if successful and -1 in case of failure, in which case errno is set
* appropriately.
*
****************************************************************************/
int setuid(uid_t uid)
{
FAR struct tcb_s *rtcb = this_task();
FAR struct task_group_s *rgroup = rtcb->group;
/* Set the task group's group identity. */
DEBUGASSERT(group != NULL);
rgroup->tg_uid = uid;
return OK;
}

View file

@ -32,11 +32,13 @@
"get_errno","errno.h","!defined(__DIRECT_ERRNO_ACCESS)","int"
"get_errno_ptr","errno.h","defined(__DIRECT_ERRNO_ACCESS)","FAR int*"
"getenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char*","FAR const char*"
"getgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t"
"getpeername","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *"
"getpid","unistd.h","","pid_t"
"getrandom","sys/random.h","defined(CONFIG_CRYPTO_RANDOM_POOL)","void","FAR void*","size_t"
"getsockname","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *"
"getsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR void*","FAR socklen_t*"
"getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
"if_indextoname","net/if.h","defined(CONFIG_NETDEV_IFINDEX)","FAR char *","unsigned int","FAR char *"
"if_nametoindex","net/if.h","defined(CONFIG_NETDEV_IFINDEX)","unsigned int","FAR const char *"
"insmod","nuttx/module.h","defined(CONFIG_MODULE)","FAR void *","FAR const char *","FAR const char *"
@ -141,8 +143,10 @@
"sendto","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR const void*","size_t","int","FAR const struct sockaddr*","socklen_t"
"set_errno","errno.h","!defined(__DIRECT_ERRNO_ACCESS)","void","int"
"setenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char*","FAR const char*","int"
"setgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t"
"sethostname","unistd.h","defined(CONFIG_LIBC_NETDB)","int","FAR const char*","size_t"
"setsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR const void*","socklen_t"
"setuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t"
"shmat", "sys/shm.h", "defined(CONFIG_MM_SHM)", "FAR void *", "int", "FAR const void *", "int"
"shmctl", "sys/shm.h", "defined(CONFIG_MM_SHM)", "int", "int", "int", "FAR struct shmid_ds *"
"shmdt", "sys/shm.h", "defined(CONFIG_MM_SHM)", "int", "FAR const void *"

Can't render this file because it has a wrong number of fields in line 2.

View file

@ -59,6 +59,15 @@ SYSCALL_LOOKUP(sched_yield, 0, STUB_sched_yield)
SYSCALL_LOOKUP(set_errno, 1, STUB_set_errno)
SYSCALL_LOOKUP(uname, 1, STUB_uname)
/* User identity */
#ifdef CONFIG_SCHED_USER_IDENTITY
SYSCALL_LOOKUP(setuid, 1, STUB_setuid)
SYSCALL_LOOKUP(getuid, 0, STUB_getuid)
SYSCALL_LOOKUP(setgid, 1, STUB_setgid)
SYSCALL_LOOKUP(getgid, 0, STUB_getgid)
#endif
/* Semaphores */
SYSCALL_LOOKUP(sem_destroy, 1, STUB_sem_destroy)

View file

@ -78,6 +78,13 @@ uintptr_t STUB_sched_yield(int nbr);
uintptr_t STUB_set_errno(int nbr, uintptr_t parm1);
uintptr_t STUB_uname(int nbr, uintptr_t parm1);
/* User identity */
uintptr_t STUB_setuid(int nbr, uintptr_t parm1);
uintptr_t STUB_getuid(int nbr);
uintptr_t STUB_setgid(int nbr, uintptr_t parm1);
uintptr_t STUB_getgid(int nbr);
/* Semaphores */
uintptr_t STUB_sem_close(int nbr, uintptr_t parm1);