2 * Copyright (c) 2009 Andrew Tridgell
3 * Copyright (c) 2011-2013 Andreas Schneider <asn@samba.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
30 #ifdef HAVE_SYS_SYSCALL_H
31 #include <sys/syscall.h>
40 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
41 # define UWRAP_THREAD __thread
46 # define UWRAP_LOCK(m) do { \
47 pthread_mutex_lock(&( m ## _mutex)); \
50 # define UWRAP_UNLOCK(m) do { \
51 pthread_mutex_unlock(&( m ## _mutex)); \
54 /* Add new global locks here please */
55 # define UWRAP_LOCK_ALL \
56 UWRAP_LOCK(uwrap_id); \
57 UWRAP_LOCK(libc_symbol_binding); \
58 UWRAP_LOCK(libpthread_symbol_binding)
60 # define UWRAP_UNLOCK_ALL \
61 UWRAP_UNLOCK(libpthread_symbol_binding); \
62 UWRAP_UNLOCK(libc_symbol_binding); \
63 UWRAP_UNLOCK(uwrap_id)
65 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
66 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
68 #define CONSTRUCTOR_ATTRIBUTE
69 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
71 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
72 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
74 #define DESTRUCTOR_ATTRIBUTE
75 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
77 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
78 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
79 #else /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
80 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
81 #endif /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
83 /* GCC have printf type attribute check. */
84 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
85 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
87 #define PRINTF_ATTRIBUTE(a,b)
88 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
90 #define UWRAP_DLIST_ADD(list,item) do { \
92 (item)->prev = NULL; \
93 (item)->next = NULL; \
96 (item)->prev = NULL; \
97 (item)->next = (list); \
98 (list)->prev = (item); \
103 #define UWRAP_DLIST_REMOVE(list,item) do { \
104 if ((list) == (item)) { \
105 (list) = (item)->next; \
107 (list)->prev = NULL; \
110 if ((item)->prev) { \
111 (item)->prev->next = (item)->next; \
113 if ((item)->next) { \
114 (item)->next->prev = (item)->prev; \
117 (item)->prev = NULL; \
118 (item)->next = NULL; \
122 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
129 enum uwrap_dbglvl_e {
137 # define UWRAP_LOG(...)
139 static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *function, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
140 # define UWRAP_LOG(dbglvl, ...) uwrap_log((dbglvl), __func__, __VA_ARGS__)
142 static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *function, const char *format, ...)
147 unsigned int lvl = 0;
149 d = getenv("UID_WRAPPER_DEBUGLEVEL");
154 va_start(va, format);
155 vsnprintf(buffer, sizeof(buffer), format, va);
159 const char *prefix = "UWRAP";
161 case UWRAP_LOG_ERROR:
162 prefix = "UWRAP_ERROR";
165 prefix = "UWRAP_WARN";
167 case UWRAP_LOG_DEBUG:
168 prefix = "UWRAP_DEBUG";
170 case UWRAP_LOG_TRACE:
171 prefix = "UWRAP_TRACE";
189 #define LIBC_NAME "libc.so"
191 typedef int (*__libc_setuid)(uid_t uid);
193 typedef uid_t (*__libc_getuid)(void);
196 typedef int (*__libc_seteuid)(uid_t euid);
200 typedef int (*__libc_setreuid)(uid_t ruid, uid_t euid);
203 #ifdef HAVE_SETRESUID
204 typedef int (*__libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
207 #ifdef HAVE_GETRESUID
208 typedef int (*__libc_getresuid)(uid_t *ruid, uid_t *euid, uid_t *suid);
211 typedef uid_t (*__libc_geteuid)(void);
213 typedef int (*__libc_setgid)(gid_t gid);
215 typedef gid_t (*__libc_getgid)(void);
218 typedef int (*__libc_setegid)(uid_t egid);
222 typedef int (*__libc_setregid)(uid_t rgid, uid_t egid);
225 #ifdef HAVE_SETRESGID
226 typedef int (*__libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
229 #ifdef HAVE_GETRESGID
230 typedef int (*__libc_getresgid)(gid_t *rgid, gid_t *egid, gid_t *sgid);
233 typedef gid_t (*__libc_getegid)(void);
235 typedef int (*__libc_getgroups)(int size, gid_t list[]);
237 typedef int (*__libc_setgroups)(size_t size, const gid_t *list);
240 typedef long int (*__libc_syscall)(long int sysno, ...);
243 #define UWRAP_SYMBOL_ENTRY(i) \
249 struct uwrap_libc_symbols {
250 UWRAP_SYMBOL_ENTRY(setuid);
251 UWRAP_SYMBOL_ENTRY(getuid);
253 UWRAP_SYMBOL_ENTRY(seteuid);
256 UWRAP_SYMBOL_ENTRY(setreuid);
258 #ifdef HAVE_SETRESUID
259 UWRAP_SYMBOL_ENTRY(setresuid);
261 #ifdef HAVE_GETRESUID
262 UWRAP_SYMBOL_ENTRY(getresuid);
264 UWRAP_SYMBOL_ENTRY(geteuid);
265 UWRAP_SYMBOL_ENTRY(setgid);
266 UWRAP_SYMBOL_ENTRY(getgid);
268 UWRAP_SYMBOL_ENTRY(setegid);
271 UWRAP_SYMBOL_ENTRY(setregid);
273 #ifdef HAVE_SETRESGID
274 UWRAP_SYMBOL_ENTRY(setresgid);
276 #ifdef HAVE_GETRESGID
277 UWRAP_SYMBOL_ENTRY(getresgid);
279 UWRAP_SYMBOL_ENTRY(getegid);
280 UWRAP_SYMBOL_ENTRY(getgroups);
281 UWRAP_SYMBOL_ENTRY(setgroups);
283 UWRAP_SYMBOL_ENTRY(syscall);
286 #undef UWRAP_SYMBOL_ENTRY
291 /* Yeah... I'm pig. I overloading macro here... So what? */
292 #define UWRAP_SYMBOL_ENTRY(i) \
294 __libpthread_##i f; \
298 typedef int (*__libpthread_pthread_create)(pthread_t *thread,
299 const pthread_attr_t *attr,
300 void *(*start_routine) (void *),
302 typedef void (*__libpthread_pthread_exit)(void *retval);
304 struct uwrap_libpthread_symbols {
305 UWRAP_SYMBOL_ENTRY(pthread_create);
306 UWRAP_SYMBOL_ENTRY(pthread_exit);
308 #undef UWRAP_SYMBOL_ENTRY
311 * We keep the virtualised euid/egid/groups information here
313 struct uwrap_thread {
327 struct uwrap_thread *next;
328 struct uwrap_thread *prev;
334 struct uwrap_libc_symbols symbols;
339 struct uwrap_libpthread_symbols symbols;
344 /* Real uid and gid of user who run uid wrapper */
348 struct uwrap_thread *ids;
351 static struct uwrap uwrap;
353 /* Shortcut to the list item */
354 static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
356 /* The mutex or accessing the id */
357 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
359 /* The mutex for accessing the global libc.symbols */
360 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
362 /* The mutex for accessing the global libpthread.symbols */
363 static pthread_mutex_t libpthread_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
365 /*********************************************************
367 *********************************************************/
369 bool uid_wrapper_enabled(void);
370 void uwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
371 void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
373 /*********************************************************
374 * UWRAP LIBC LOADER FUNCTIONS
375 *********************************************************/
384 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
386 int flags = RTLD_LAZY;
391 flags |= RTLD_DEEPBIND;
397 case UWRAP_LIBSOCKET:
400 handle = uwrap.libc.handle;
401 if (handle == NULL) {
402 for (i = 10; i >= 0; i--) {
403 char soname[256] = {0};
405 snprintf(soname, sizeof(soname), "libc.so.%d", i);
406 handle = dlopen(soname, flags);
407 if (handle != NULL) {
411 /* glibc on Alpha and IA64 is libc.so.6.1 */
412 snprintf(soname, sizeof(soname), "libc.so.%d.1", i);
413 handle = dlopen(soname, flags);
414 if (handle != NULL) {
419 uwrap.libc.handle = handle;
422 case UWRAP_LIBPTHREAD:
423 handle = uwrap.libpthread.handle;
424 if (handle == NULL) {
425 handle = dlopen("libpthread.so.0", flags);
426 if (handle != NULL) {
433 if (handle == NULL) {
435 handle = uwrap.libc.handle = RTLD_NEXT;
438 "Failed to dlopen library: %s\n",
447 static void *_uwrap_bind_symbol(enum uwrap_lib lib, const char *fn_name)
452 handle = uwrap_load_lib_handle(lib);
454 func = dlsym(handle, fn_name);
457 "Failed to find %s: %s\n",
465 #define uwrap_bind_symbol_libc(sym_name) \
466 UWRAP_LOCK(libc_symbol_binding); \
467 if (uwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
468 uwrap.libc.symbols._libc_##sym_name.obj = \
469 _uwrap_bind_symbol(UWRAP_LIBC, #sym_name); \
471 UWRAP_UNLOCK(libc_symbol_binding)
473 #define uwrap_bind_symbol_libpthread(sym_name) \
474 UWRAP_LOCK(libpthread_symbol_binding); \
475 if (uwrap.libpthread.symbols._libpthread_##sym_name.obj == NULL) { \
476 uwrap.libpthread.symbols._libpthread_##sym_name.obj = \
477 _uwrap_bind_symbol(UWRAP_LIBPTHREAD, #sym_name); \
479 UWRAP_UNLOCK(libpthread_symbol_binding)
484 * Functions expeciall from libc need to be loaded individually, you can't load
485 * all at once or gdb will segfault at startup. The same applies to valgrind and
486 * has probably something todo with with the linker.
487 * So we need load each function at the point it is called the first time.
489 static int libc_setuid(uid_t uid)
491 uwrap_bind_symbol_libc(setuid);
493 return uwrap.libc.symbols._libc_setuid.f(uid);
496 static uid_t libc_getuid(void)
498 uwrap_bind_symbol_libc(getuid);
500 return uwrap.libc.symbols._libc_getuid.f();
504 static int libc_seteuid(uid_t euid)
506 uwrap_bind_symbol_libc(seteuid);
508 return uwrap.libc.symbols._libc_seteuid.f(euid);
513 static int libc_setreuid(uid_t ruid, uid_t euid)
515 uwrap_bind_symbol_libc(setreuid);
517 return uwrap.libc.symbols._libc_setreuid.f(ruid, euid);
521 #ifdef HAVE_SETRESUID
522 static int libc_setresuid(uid_t ruid, uid_t euid, uid_t suid)
524 uwrap_bind_symbol_libc(setresuid);
526 return uwrap.libc.symbols._libc_setresuid.f(ruid, euid, suid);
530 #ifdef HAVE_GETRESUID
531 static int libc_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
533 uwrap_bind_symbol_libc(getresuid);
535 return uwrap.libc.symbols._libc_getresuid.f(ruid, euid, suid);
539 static uid_t libc_geteuid(void)
541 uwrap_bind_symbol_libc(geteuid);
543 return uwrap.libc.symbols._libc_geteuid.f();
546 static int libc_setgid(gid_t gid)
548 uwrap_bind_symbol_libc(setgid);
550 return uwrap.libc.symbols._libc_setgid.f(gid);
553 static gid_t libc_getgid(void)
555 uwrap_bind_symbol_libc(getgid);
557 return uwrap.libc.symbols._libc_getgid.f();
561 static int libc_setegid(gid_t egid)
563 uwrap_bind_symbol_libc(setegid);
565 return uwrap.libc.symbols._libc_setegid.f(egid);
570 static int libc_setregid(gid_t rgid, gid_t egid)
572 uwrap_bind_symbol_libc(setregid);
574 return uwrap.libc.symbols._libc_setregid.f(rgid, egid);
578 #ifdef HAVE_SETRESGID
579 static int libc_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
581 uwrap_bind_symbol_libc(setresgid);
583 return uwrap.libc.symbols._libc_setresgid.f(rgid, egid, sgid);
587 #ifdef HAVE_GETRESGID
588 static int libc_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
590 uwrap_bind_symbol_libc(setresgid);
592 return uwrap.libc.symbols._libc_getresgid.f(rgid, egid, sgid);
596 static gid_t libc_getegid(void)
598 uwrap_bind_symbol_libc(getegid);
600 return uwrap.libc.symbols._libc_getegid.f();
603 static int libc_getgroups(int size, gid_t list[])
605 uwrap_bind_symbol_libc(getgroups);
607 return uwrap.libc.symbols._libc_getgroups.f(size, list);
610 static int libc_setgroups(size_t size, const gid_t *list)
612 uwrap_bind_symbol_libc(setgroups);
614 return uwrap.libc.symbols._libc_setgroups.f(size, list);
618 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
619 static long int libc_vsyscall(long int sysno, va_list va)
625 uwrap_bind_symbol_libc(syscall);
627 for (i = 0; i < 8; i++) {
628 args[i] = va_arg(va, long int);
631 rc = uwrap.libc.symbols._libc_syscall.f(sysno,
646 * This part is "optimistic".
647 * Thread can ends without pthread_exit call.
649 static void libpthread_pthread_exit(void *retval)
651 uwrap_bind_symbol_libpthread(pthread_exit);
653 uwrap.libpthread.symbols._libpthread_pthread_exit.f(retval);
656 static void uwrap_pthread_exit(void *retval)
658 struct uwrap_thread *id = uwrap_tls_id;
660 UWRAP_LOG(UWRAP_LOG_DEBUG, "Cleanup thread");
662 UWRAP_LOCK(uwrap_id);
664 UWRAP_UNLOCK(uwrap_id);
665 libpthread_pthread_exit(retval);
669 UWRAP_DLIST_REMOVE(uwrap.ids, id);
670 SAFE_FREE(id->groups);
674 UWRAP_UNLOCK(uwrap_id);
676 libpthread_pthread_exit(retval);
679 void pthread_exit(void *retval)
681 if (!uid_wrapper_enabled()) {
682 libpthread_pthread_exit(retval);
685 uwrap_pthread_exit(retval);
687 /* Calm down gcc warning. */
691 static int libpthread_pthread_create(pthread_t *thread,
692 const pthread_attr_t *attr,
693 void *(*start_routine) (void *),
696 uwrap_bind_symbol_libpthread(pthread_create);
697 return uwrap.libpthread.symbols._libpthread_pthread_create.f(thread,
703 struct uwrap_pthread_create_args {
704 struct uwrap_thread *id;
705 void *(*start_routine) (void *);
709 static void *uwrap_pthread_create_start(void *_a)
711 struct uwrap_pthread_create_args *a =
712 (struct uwrap_pthread_create_args *)_a;
713 void *(*start_routine) (void *) = a->start_routine;
715 struct uwrap_thread *id = a->id;
721 return start_routine(arg);
724 static int uwrap_pthread_create(pthread_t *thread,
725 const pthread_attr_t *attr,
726 void *(*start_routine) (void *),
729 struct uwrap_pthread_create_args *args;
730 struct uwrap_thread *src_id = uwrap_tls_id;
733 args = malloc(sizeof(struct uwrap_pthread_create_args));
735 UWRAP_LOG(UWRAP_LOG_ERROR,
736 "uwrap_pthread_create: Unable to allocate memory");
740 args->start_routine = start_routine;
743 args->id = calloc(1, sizeof(struct uwrap_thread));
744 if (args->id == NULL) {
746 UWRAP_LOG(UWRAP_LOG_ERROR,
747 "uwrap_pthread_create: Unable to allocate memory");
752 UWRAP_LOCK(uwrap_id);
754 args->id->groups = malloc(sizeof(gid_t) * src_id->ngroups);
755 if (args->id->groups == NULL) {
756 UWRAP_UNLOCK(uwrap_id);
759 UWRAP_LOG(UWRAP_LOG_ERROR,
760 "uwrap_pthread_create: Unable to allocate memory again");
765 args->id->ruid = src_id->ruid;
766 args->id->euid = src_id->euid;
767 args->id->suid = src_id->suid;
769 args->id->rgid = src_id->rgid;
770 args->id->egid = src_id->egid;
771 args->id->sgid = src_id->sgid;
773 args->id->enabled = src_id->enabled;
775 args->id->ngroups = src_id->ngroups;
776 if (src_id->groups != NULL) {
777 memcpy(args->id->groups, src_id->groups,
778 sizeof(gid_t) * src_id->ngroups);
780 SAFE_FREE(args->id->groups);
783 UWRAP_DLIST_ADD(uwrap.ids, args->id);
784 UWRAP_UNLOCK(uwrap_id);
786 ret = libpthread_pthread_create(thread, attr,
787 uwrap_pthread_create_start,
796 int pthread_create(pthread_t *thread,
797 const pthread_attr_t *attr,
798 void *(*start_routine) (void *),
801 if (!uid_wrapper_enabled()) {
802 return libpthread_pthread_create(thread,
808 return uwrap_pthread_create(thread,
814 /*********************************************************
816 *********************************************************/
818 #define GROUP_STRING_SIZE 16384
819 #define GROUP_MAX_COUNT (GROUP_STRING_SIZE / (10 + 1))
822 * This function exports all the IDs of the current user so if
823 * we fork and then exec we can setup uid_wrapper in the new process
826 static void uwrap_export_ids(struct uwrap_thread *id)
828 char groups_str[GROUP_STRING_SIZE] = {0};
829 size_t groups_str_size = sizeof(groups_str);
830 char unsigned_str[32] = {0};
834 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->ruid);
835 setenv("UID_WRAPPER_INITIAL_RUID", unsigned_str, 1);
837 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->euid);
838 setenv("UID_WRAPPER_INITIAL_EUID", unsigned_str, 1);
840 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->suid);
841 setenv("UID_WRAPPER_INITIAL_SUID", unsigned_str, 1);
844 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->rgid);
845 setenv("UID_WRAPPER_INITIAL_RGID", unsigned_str, 1);
847 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->egid);
848 setenv("UID_WRAPPER_INITIAL_EGID", unsigned_str, 1);
850 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->sgid);
851 setenv("UID_WRAPPER_INITIAL_SGID", unsigned_str, 1);
853 if (id->ngroups > GROUP_MAX_COUNT) {
854 UWRAP_LOG(UWRAP_LOG_ERROR,
855 "ERROR: Number of groups (%u) exceeds maximum value "
856 "uid_wrapper will handle (%u).",
863 snprintf(unsigned_str, sizeof(unsigned_str), "%u", id->ngroups);
864 setenv("UID_WRAPPER_INITIAL_GROUPS_COUNT", unsigned_str, 1);
866 for (i = 0; i < id->ngroups; i++) {
867 size_t groups_str_len = strlen(groups_str);
868 size_t groups_str_avail = groups_str_size - groups_str_len;
871 len = snprintf(unsigned_str, sizeof(unsigned_str), ",%u", id->groups[i]);
875 if (len < groups_str_avail) {
876 snprintf(groups_str + groups_str_len,
877 groups_str_size - groups_str_len,
879 i == 0 ? unsigned_str + 1 : unsigned_str);
883 if (id->ngroups > 0) {
884 setenv("UID_WRAPPER_INITIAL_GROUPS", groups_str, 1);
888 static void uwrap_thread_prepare(void)
890 struct uwrap_thread *id = uwrap_tls_id;
892 /* uid_wrapper is loaded but not enabled */
900 * What happens if another atfork prepare functions calls a uwrap
901 * function? So disable it in case another atfork prepare function
902 * calls a (s)uid function. We disable uid_wrapper only for thread
903 * (process) which called fork.
908 static void uwrap_thread_parent(void)
910 struct uwrap_thread *id = uwrap_tls_id;
912 /* uid_wrapper is loaded but not enabled */
922 static void uwrap_thread_child(void)
924 struct uwrap_thread *id = uwrap_tls_id;
925 struct uwrap_thread *u = uwrap.ids;
927 /* uid_wrapper is loaded but not enabled */
932 uwrap_export_ids(id);
935 * "Garbage collector" - Inspired by DESTRUCTOR.
936 * All threads (except one which called fork()) are dead now.. Dave
937 * That's what posix said...
941 /* Skip this item. */
946 UWRAP_DLIST_REMOVE(uwrap.ids, u);
948 SAFE_FREE(u->groups);
960 * This initializes uid_wrapper with the IDs exported to the environment. Those
961 * are normally set after we forked and executed.
963 static void uwrap_init_env(struct uwrap_thread *id)
968 env = getenv("UID_WRAPPER_INITIAL_RUID");
969 if (env != NULL && env[0] != '\0') {
970 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env);
971 id->ruid = strtoul(env, (char **)NULL, 10);
972 unsetenv("UID_WRAPPER_INITIAL_RUID");
975 env = getenv("UID_WRAPPER_INITIAL_EUID");
976 if (env != NULL && env[0] != '\0') {
977 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize euid with %s", env);
978 id->euid = strtoul(env, (char **)NULL, 10);
979 unsetenv("UID_WRAPPER_INITIAL_EUID");
982 env = getenv("UID_WRAPPER_INITIAL_SUID");
983 if (env != NULL && env[0] != '\0') {
984 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize suid with %s", env);
985 id->suid = strtoul(env, (char **)NULL, 10);
986 unsetenv("UID_WRAPPER_INITIAL_SUID");
989 env = getenv("UID_WRAPPER_INITIAL_RGID");
990 if (env != NULL && env[0] != '\0') {
991 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env);
992 id->rgid = strtoul(env, (char **)NULL, 10);
993 unsetenv("UID_WRAPPER_INITIAL_RGID");
996 env = getenv("UID_WRAPPER_INITIAL_EGID");
997 if (env != NULL && env[0] != '\0') {
998 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize egid with %s", env);
999 id->egid = strtoul(env, (char **)NULL, 10);
1000 unsetenv("UID_WRAPPER_INITIAL_EGID");
1003 env = getenv("UID_WRAPPER_INITIAL_SGID");
1004 if (env != NULL && env[0] != '\0') {
1005 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize sgid with %s", env);
1006 id->sgid = strtoul(env, (char **)NULL, 10);
1007 unsetenv("UID_WRAPPER_INITIAL_SGID");
1010 env = getenv("UID_WRAPPER_INITIAL_GROUPS_COUNT");
1011 if (env != NULL && env[0] != '\0') {
1012 ngroups = strtol(env, (char **)NULL, 10);
1013 unsetenv("UID_WRAPPER_INITIAL_GROUPS_COUNT");
1016 if (ngroups > 0 && ngroups < GROUP_MAX_COUNT) {
1022 id->groups = malloc(sizeof(gid_t) * ngroups);
1023 if (id->groups == NULL) {
1024 UWRAP_LOG(UWRAP_LOG_ERROR,
1025 "Unable to allocate memory");
1029 env = getenv("UID_WRAPPER_INITIAL_GROUPS");
1030 if (env != NULL && env[0] != '\0') {
1031 char *groups_str = NULL;
1032 char *saveptr = NULL;
1033 const char *p = NULL;
1035 groups_str = strdup(env);
1036 if (groups_str == NULL) {
1040 p = strtok_r(groups_str, ",", &saveptr);
1042 id->groups[i] = strtol(p, (char **)NULL, 10);
1045 p = strtok_r(NULL, ",", &saveptr);
1047 SAFE_FREE(groups_str);
1054 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize groups with %s", env);
1055 id->ngroups = ngroups;
1059 static void uwrap_init(void)
1063 UWRAP_LOCK(uwrap_id);
1065 if (uwrap.initialised) {
1066 struct uwrap_thread *id = uwrap_tls_id;
1068 if (uwrap.ids == NULL) {
1069 UWRAP_UNLOCK(uwrap_id);
1074 UWRAP_LOG(UWRAP_LOG_ERROR,
1075 "Invalid id for thread");
1079 UWRAP_UNLOCK(uwrap_id);
1083 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize uid_wrapper");
1085 uwrap.initialised = true;
1087 env = getenv("UID_WRAPPER");
1088 if (env != NULL && env[0] == '1') {
1089 const char *root = getenv("UID_WRAPPER_ROOT");
1090 struct uwrap_thread *id;
1092 id = calloc(1, sizeof(struct uwrap_thread));
1094 UWRAP_LOG(UWRAP_LOG_ERROR,
1095 "Unable to allocate memory for main id");
1099 UWRAP_DLIST_ADD(uwrap.ids, id);
1102 uwrap.myuid = libc_geteuid();
1103 uwrap.mygid = libc_getegid();
1105 /* put us in one group */
1106 if (root != NULL && root[0] == '1') {
1107 id->ruid = id->euid = id->suid = 0;
1108 id->rgid = id->egid = id->sgid = 0;
1110 id->groups = malloc(sizeof(gid_t) * 1);
1111 if (id->groups == NULL) {
1112 UWRAP_LOG(UWRAP_LOG_ERROR,
1113 "Unable to allocate memory");
1121 id->ruid = id->euid = id->suid = uwrap.myuid;
1122 id->rgid = id->egid = id->sgid = uwrap.mygid;
1124 id->ngroups = libc_getgroups(0, NULL);
1125 if (id->ngroups == -1) {
1126 UWRAP_LOG(UWRAP_LOG_ERROR,
1127 "Unable to call libc_getgroups in uwrap_init.");
1130 id->groups = malloc(sizeof(gid_t) * id->ngroups);
1131 if (id->groups == NULL) {
1132 UWRAP_LOG(UWRAP_LOG_ERROR, "Unable to allocate memory");
1135 if (libc_getgroups(id->ngroups, id->groups) == -1) {
1136 UWRAP_LOG(UWRAP_LOG_ERROR,
1137 "Unable to call libc_getgroups again in uwrap_init.");
1140 * Deallocation of uwrap.groups is handled by
1141 * library destructor.
1151 UWRAP_LOG(UWRAP_LOG_DEBUG,
1152 "Enabled uid_wrapper as %s (real uid=%u)",
1153 id->ruid == 0 ? "root" : "user",
1154 (unsigned int)uwrap.myuid);
1157 UWRAP_UNLOCK(uwrap_id);
1159 UWRAP_LOG(UWRAP_LOG_DEBUG, "Succeccfully initialized uid_wrapper");
1162 bool uid_wrapper_enabled(void)
1164 struct uwrap_thread *id = uwrap_tls_id;
1171 UWRAP_LOCK(uwrap_id);
1172 enabled = id->enabled;
1173 UWRAP_UNLOCK(uwrap_id);
1179 * UWRAP_SETxUID FUNCTIONS
1182 static int uwrap_setresuid_args(uid_t ruid, uid_t euid, uid_t suid)
1184 struct uwrap_thread *id = uwrap_tls_id;
1186 UWRAP_LOG(UWRAP_LOG_TRACE,
1187 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1188 id->ruid, ruid, id->euid, euid, id->suid, suid);
1190 if (id->euid != 0) {
1191 if (ruid != (uid_t)-1 &&
1198 if (euid != (uid_t)-1 &&
1205 if (suid != (uid_t)-1 &&
1217 static int uwrap_setresuid_thread(uid_t ruid, uid_t euid, uid_t suid)
1219 struct uwrap_thread *id = uwrap_tls_id;
1222 UWRAP_LOG(UWRAP_LOG_TRACE,
1223 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1224 id->ruid, ruid, id->euid, euid, id->suid, suid);
1226 rc = uwrap_setresuid_args(ruid, euid, suid);
1231 UWRAP_LOCK(uwrap_id);
1233 if (ruid != (uid_t)-1) {
1237 if (euid != (uid_t)-1) {
1241 if (suid != (uid_t)-1) {
1245 UWRAP_UNLOCK(uwrap_id);
1250 static int uwrap_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1252 struct uwrap_thread *id = uwrap_tls_id;
1255 UWRAP_LOG(UWRAP_LOG_TRACE,
1256 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1257 id->ruid, ruid, id->euid, euid, id->suid, suid);
1259 rc = uwrap_setresuid_args(ruid, euid, suid);
1264 UWRAP_LOCK(uwrap_id);
1266 for (id = uwrap.ids; id; id = id->next) {
1267 if (ruid != (uid_t)-1) {
1271 if (euid != (uid_t)-1) {
1275 if (suid != (uid_t)-1) {
1280 UWRAP_UNLOCK(uwrap_id);
1285 static int uwrap_setreuid_args(uid_t ruid, uid_t euid,
1290 struct uwrap_thread *id = uwrap_tls_id;
1291 uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1293 UWRAP_LOG(UWRAP_LOG_TRACE,
1294 "ruid %d -> %d, euid %d -> %d",
1295 id->ruid, ruid, id->euid, euid);
1297 if (ruid != (uid_t)-1) {
1299 if (ruid != id->ruid &&
1307 if (euid != (uid_t)-1) {
1309 if (euid != id->ruid &&
1318 if (ruid != (uid_t) -1 ||
1319 (euid != (uid_t)-1 && id->ruid != euid)) {
1320 new_suid = new_euid;
1323 *_new_ruid = new_ruid;
1324 *_new_euid = new_euid;
1325 *_new_suid = new_suid;
1330 static int uwrap_setreuid_thread(uid_t ruid, uid_t euid)
1333 struct uwrap_thread *id = uwrap_tls_id;
1335 uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1338 UWRAP_LOG(UWRAP_LOG_TRACE,
1339 "ruid %d -> %d, euid %d -> %d",
1340 id->ruid, ruid, id->euid, euid);
1342 rc = uwrap_setreuid_args(ruid, euid, &new_ruid, &new_euid, &new_suid);
1347 return uwrap_setresuid_thread(new_ruid, new_euid, new_suid);
1350 #ifdef HAVE_SETREUID
1351 static int uwrap_setreuid(uid_t ruid, uid_t euid)
1354 struct uwrap_thread *id = uwrap_tls_id;
1356 uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1359 UWRAP_LOG(UWRAP_LOG_TRACE,
1360 "ruid %d -> %d, euid %d -> %d",
1361 id->ruid, ruid, id->euid, euid);
1363 rc = uwrap_setreuid_args(ruid, euid, &new_ruid, &new_euid, &new_suid);
1368 return uwrap_setresuid(new_ruid, new_euid, new_suid);
1372 static int uwrap_setuid_args(uid_t uid,
1377 struct uwrap_thread *id = uwrap_tls_id;
1379 UWRAP_LOG(UWRAP_LOG_TRACE,
1383 if (uid == (uid_t)-1) {
1388 if (id->euid == 0) {
1389 *new_suid = *new_ruid = uid;
1390 } else if (uid != id->ruid &&
1401 static int uwrap_setuid_thread(uid_t uid)
1403 uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1406 rc = uwrap_setuid_args(uid, &new_ruid, &new_euid, &new_suid);
1411 return uwrap_setresuid_thread(new_ruid, new_euid, new_suid);
1414 static int uwrap_setuid(uid_t uid)
1416 uid_t new_ruid = -1, new_euid = -1, new_suid = -1;
1419 rc = uwrap_setuid_args(uid, &new_ruid, &new_euid, &new_suid);
1424 return uwrap_setresuid(new_ruid, new_euid, new_suid);
1428 * UWRAP_GETxUID FUNCTIONS
1431 #ifdef HAVE_GETRESUID
1432 static int uwrap_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1434 struct uwrap_thread *id = uwrap_tls_id;
1436 UWRAP_LOCK(uwrap_id);
1442 UWRAP_UNLOCK(uwrap_id);
1448 #ifdef HAVE_GETRESGID
1449 static int uwrap_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1451 struct uwrap_thread *id = uwrap_tls_id;
1453 UWRAP_LOCK(uwrap_id);
1459 UWRAP_UNLOCK(uwrap_id);
1466 * UWRAP_SETxGID FUNCTIONS
1469 static int uwrap_setresgid_args(gid_t rgid, gid_t egid, gid_t sgid)
1471 struct uwrap_thread *id = uwrap_tls_id;
1473 UWRAP_LOG(UWRAP_LOG_TRACE,
1474 "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1475 id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1477 if (id->euid != 0) {
1478 if (rgid != (gid_t)-1 &&
1485 if (egid != (gid_t)-1 &&
1492 if (sgid != (gid_t)-1 &&
1504 static int uwrap_setresgid_thread(gid_t rgid, gid_t egid, gid_t sgid)
1506 struct uwrap_thread *id = uwrap_tls_id;
1509 UWRAP_LOG(UWRAP_LOG_TRACE,
1510 "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1511 id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1513 rc = uwrap_setresgid_args(rgid, egid, sgid);
1518 UWRAP_LOCK(uwrap_id);
1520 if (rgid != (gid_t)-1) {
1524 if (egid != (gid_t)-1) {
1528 if (sgid != (gid_t)-1) {
1532 UWRAP_UNLOCK(uwrap_id);
1537 static int uwrap_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1539 struct uwrap_thread *id = uwrap_tls_id;
1542 UWRAP_LOG(UWRAP_LOG_TRACE,
1543 "rgid %d -> %d, egid %d -> %d, sgid %d -> %d",
1544 id->rgid, rgid, id->egid, egid, id->sgid, sgid);
1546 rc = uwrap_setresgid_args(rgid, egid, sgid);
1551 UWRAP_LOCK(uwrap_id);
1553 for (id = uwrap.ids; id; id = id->next) {
1554 if (rgid != (gid_t)-1) {
1558 if (egid != (gid_t)-1) {
1562 if (sgid != (gid_t)-1) {
1567 UWRAP_UNLOCK(uwrap_id);
1572 static int uwrap_setregid_args(gid_t rgid, gid_t egid,
1577 struct uwrap_thread *id = uwrap_tls_id;
1578 gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1580 UWRAP_LOG(UWRAP_LOG_TRACE,
1581 "rgid %d -> %d, egid %d -> %d",
1582 id->rgid, rgid, id->egid, egid);
1584 if (rgid != (gid_t)-1) {
1586 if (rgid != id->rgid &&
1594 if (egid != (gid_t)-1) {
1596 if (egid != id->rgid &&
1605 if (rgid != (gid_t) -1 ||
1606 (egid != (gid_t)-1 && id->rgid != egid)) {
1607 new_sgid = new_egid;
1610 *_new_rgid = new_rgid;
1611 *_new_egid = new_egid;
1612 *_new_sgid = new_sgid;
1617 static int uwrap_setregid_thread(gid_t rgid, gid_t egid)
1620 struct uwrap_thread *id = uwrap_tls_id;
1622 gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1625 UWRAP_LOG(UWRAP_LOG_TRACE,
1626 "rgid %d -> %d, egid %d -> %d",
1627 id->rgid, rgid, id->egid, egid);
1629 rc = uwrap_setregid_args(rgid, egid, &new_rgid, &new_egid, &new_sgid);
1634 return uwrap_setresgid_thread(new_rgid, new_egid, new_sgid);
1637 #ifdef HAVE_SETREGID
1638 static int uwrap_setregid(gid_t rgid, gid_t egid)
1641 struct uwrap_thread *id = uwrap_tls_id;
1643 gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1646 UWRAP_LOG(UWRAP_LOG_TRACE,
1647 "rgid %d -> %d, egid %d -> %d",
1648 id->rgid, rgid, id->egid, egid);
1650 rc = uwrap_setregid_args(rgid, egid, &new_rgid, &new_egid, &new_sgid);
1655 return uwrap_setresgid(new_rgid, new_egid, new_sgid);
1659 static int uwrap_setgid_args(gid_t gid,
1664 struct uwrap_thread *id = uwrap_tls_id;
1666 UWRAP_LOG(UWRAP_LOG_TRACE,
1670 if (gid == (gid_t)-1) {
1675 if (id->euid == 0) {
1676 *new_sgid = *new_rgid = gid;
1677 } else if (gid != id->rgid &&
1688 static int uwrap_setgid_thread(gid_t gid)
1690 gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1693 rc = uwrap_setgid_args(gid, &new_rgid, &new_egid, &new_sgid);
1698 return uwrap_setresgid_thread(new_rgid, new_egid, new_sgid);
1701 static int uwrap_setgid(gid_t gid)
1703 gid_t new_rgid = -1, new_egid = -1, new_sgid = -1;
1706 rc = uwrap_setgid_args(gid, &new_rgid, &new_egid, &new_sgid);
1711 return uwrap_setresgid(new_rgid, new_egid, new_sgid);
1717 int setuid(uid_t uid)
1719 if (!uid_wrapper_enabled()) {
1720 return libc_setuid(uid);
1724 return uwrap_setuid(uid);
1728 int seteuid(uid_t euid)
1730 if (!uid_wrapper_enabled()) {
1731 return libc_seteuid(euid);
1734 /* On FreeBSD the uid_t -1 is set and doesn't produce and error */
1735 if (euid == (uid_t)-1) {
1741 return uwrap_setresuid(-1, euid, -1);
1745 #ifdef HAVE_SETREUID
1746 int setreuid(uid_t ruid, uid_t euid)
1748 if (!uid_wrapper_enabled()) {
1749 return libc_setreuid(ruid, euid);
1753 return uwrap_setreuid(ruid, euid);
1757 #ifdef HAVE_SETRESUID
1758 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
1760 if (!uid_wrapper_enabled()) {
1761 return libc_setresuid(ruid, euid, suid);
1765 return uwrap_setresuid(ruid, euid, suid);
1769 #ifdef HAVE_GETRESUID
1770 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1772 if (!uid_wrapper_enabled()) {
1773 return libc_getresuid(ruid, euid, suid);
1777 return uwrap_getresuid(ruid, euid, suid);
1784 static uid_t uwrap_getuid(void)
1786 struct uwrap_thread *id = uwrap_tls_id;
1789 UWRAP_LOCK(uwrap_id);
1791 UWRAP_UNLOCK(uwrap_id);
1798 if (!uid_wrapper_enabled()) {
1799 return libc_getuid();
1803 return uwrap_getuid();
1809 static uid_t uwrap_geteuid(void)
1811 const char *env = getenv("UID_WRAPPER_MYUID");
1812 struct uwrap_thread *id = uwrap_tls_id;
1815 UWRAP_LOCK(uwrap_id);
1817 UWRAP_UNLOCK(uwrap_id);
1819 /* Disable root and return myuid */
1820 if (env != NULL && env[0] == '1') {
1829 if (!uid_wrapper_enabled()) {
1830 return libc_geteuid();
1834 return uwrap_geteuid();
1840 int setgid(gid_t gid)
1842 if (!uid_wrapper_enabled()) {
1843 return libc_setgid(gid);
1847 return uwrap_setgid(gid);
1851 int setegid(gid_t egid)
1853 if (!uid_wrapper_enabled()) {
1854 return libc_setegid(egid);
1857 /* On FreeBSD the uid_t -1 is set and doesn't produce and error */
1858 if (egid == (gid_t)-1) {
1864 return uwrap_setresgid(-1, egid, -1);
1868 #ifdef HAVE_SETREGID
1869 int setregid(gid_t rgid, gid_t egid)
1871 if (!uid_wrapper_enabled()) {
1872 return libc_setregid(rgid, egid);
1876 return uwrap_setregid(rgid, egid);
1880 #ifdef HAVE_SETRESGID
1881 int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1883 if (!uid_wrapper_enabled()) {
1884 return libc_setresgid(rgid, egid, sgid);
1888 return uwrap_setresgid(rgid, egid, sgid);
1892 #ifdef HAVE_GETRESGID
1893 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1895 if (!uid_wrapper_enabled()) {
1896 return libc_getresgid(rgid, egid, sgid);
1900 return uwrap_getresgid(rgid, egid, sgid);
1907 static gid_t uwrap_getgid(void)
1909 struct uwrap_thread *id = uwrap_tls_id;
1912 UWRAP_LOCK(uwrap_id);
1914 UWRAP_UNLOCK(uwrap_id);
1921 if (!uid_wrapper_enabled()) {
1922 return libc_getgid();
1926 return uwrap_getgid();
1932 static uid_t uwrap_getegid(void)
1934 struct uwrap_thread *id = uwrap_tls_id;
1937 UWRAP_LOCK(uwrap_id);
1939 UWRAP_UNLOCK(uwrap_id);
1946 if (!uid_wrapper_enabled()) {
1947 return libc_getegid();
1951 return uwrap_getegid();
1954 static int uwrap_setgroups_thread(size_t size, const gid_t *list)
1956 struct uwrap_thread *id = uwrap_tls_id;
1959 UWRAP_LOCK(uwrap_id);
1962 SAFE_FREE(id->groups);
1964 } else if (size > 0) {
1967 tmp = realloc(id->groups, sizeof(gid_t) * size);
1974 memcpy(id->groups, list, size * sizeof(gid_t));
1979 UWRAP_UNLOCK(uwrap_id);
1984 static int uwrap_setgroups(size_t size, const gid_t *list)
1986 struct uwrap_thread *id;
1989 UWRAP_LOCK(uwrap_id);
1992 for (id = uwrap.ids; id; id = id->next) {
1993 SAFE_FREE(id->groups);
1997 } else if (size > 0) {
2000 for (id = uwrap.ids; id; id = id->next) {
2001 tmp = realloc(id->groups, sizeof(gid_t) * size);
2009 memcpy(id->groups, list, size * sizeof(gid_t));
2015 UWRAP_UNLOCK(uwrap_id);
2020 #ifdef HAVE_SETGROUPS_INT
2021 int setgroups(int size, const gid_t *list)
2023 int setgroups(size_t size, const gid_t *list)
2026 if (!uid_wrapper_enabled()) {
2027 return libc_setgroups(size, list);
2031 return uwrap_setgroups(size, list);
2034 static int uwrap_getgroups(int size, gid_t *list)
2036 struct uwrap_thread *id = uwrap_tls_id;
2039 UWRAP_LOCK(uwrap_id);
2040 ngroups = id->ngroups;
2042 if (size > ngroups) {
2048 if (size < ngroups) {
2052 memcpy(list, id->groups, size * sizeof(gid_t));
2055 UWRAP_UNLOCK(uwrap_id);
2060 int getgroups(int size, gid_t *list)
2062 if (!uid_wrapper_enabled()) {
2063 return libc_getgroups(size, list);
2067 return uwrap_getgroups(size, list);
2070 #if (defined(HAVE_SYS_SYSCALL_H) || defined(HAVE_SYSCALL_H)) \
2071 && (defined(SYS_setreuid) || defined(SYS_setreuid32))
2072 static long int uwrap_syscall (long int sysno, va_list vp)
2083 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2087 rc = uwrap_getgid();
2092 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2096 rc = uwrap_getegid();
2099 #endif /* SYS_getegid */
2101 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2105 gid_t gid = (gid_t) va_arg(vp, gid_t);
2107 rc = uwrap_setgid_thread(gid);
2111 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2112 case SYS_setregid32:
2115 gid_t rgid = (gid_t) va_arg(vp, gid_t);
2116 gid_t egid = (gid_t) va_arg(vp, gid_t);
2118 rc = uwrap_setregid_thread(rgid, egid);
2121 #ifdef SYS_setresgid
2123 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2124 case SYS_setresgid32:
2127 gid_t rgid = (gid_t) va_arg(vp, gid_t);
2128 gid_t egid = (gid_t) va_arg(vp, gid_t);
2129 gid_t sgid = (gid_t) va_arg(vp, gid_t);
2131 rc = uwrap_setresgid_thread(rgid, egid, sgid);
2134 #endif /* SYS_setresgid */
2135 #if defined(SYS_getresgid) && defined(HAVE_GETRESGID)
2137 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2138 case SYS_getresgid32:
2141 gid_t *rgid = (gid_t *) va_arg(vp, gid_t *);
2142 gid_t *egid = (gid_t *) va_arg(vp, gid_t *);
2143 gid_t *sgid = (gid_t *) va_arg(vp, gid_t *);
2145 rc = uwrap_getresgid(rgid, egid, sgid);
2148 #endif /* SYS_getresgid && HAVE_GETRESGID */
2156 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2160 rc = uwrap_getuid();
2165 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2169 rc = uwrap_geteuid();
2172 #endif /* SYS_geteuid */
2174 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2178 uid_t uid = (uid_t) va_arg(vp, uid_t);
2180 rc = uwrap_setuid_thread(uid);
2184 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2185 case SYS_setreuid32:
2188 uid_t ruid = (uid_t) va_arg(vp, uid_t);
2189 uid_t euid = (uid_t) va_arg(vp, uid_t);
2191 rc = uwrap_setreuid_thread(ruid, euid);
2194 #ifdef SYS_setresuid
2196 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2197 case SYS_setresuid32:
2200 uid_t ruid = (uid_t) va_arg(vp, uid_t);
2201 uid_t euid = (uid_t) va_arg(vp, uid_t);
2202 uid_t suid = (uid_t) va_arg(vp, uid_t);
2204 rc = uwrap_setresuid_thread(ruid, euid, suid);
2207 #endif /* SYS_setresuid */
2208 #if defined(SYS_getresuid) && defined(HAVE_GETRESUID)
2210 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2211 case SYS_getresuid32:
2214 uid_t *ruid = (uid_t *) va_arg(vp, uid_t *);
2215 uid_t *euid = (uid_t *) va_arg(vp, uid_t *);
2216 uid_t *suid = (uid_t *) va_arg(vp, uid_t *);
2218 rc = uwrap_getresuid(ruid, euid, suid);
2221 #endif /* SYS_getresuid && HAVE_GETRESUID*/
2224 #ifdef HAVE_LINUX_32BIT_SYSCALLS
2225 case SYS_setgroups32:
2228 size_t size = (size_t) va_arg(vp, size_t);
2229 gid_t *list = (gid_t *) va_arg(vp, int *);
2231 rc = uwrap_setgroups_thread(size, list);
2235 UWRAP_LOG(UWRAP_LOG_DEBUG,
2236 "UID_WRAPPER calling non-wrapped syscall %lu",
2239 rc = libc_vsyscall(sysno, vp);
2247 #ifdef HAVE_SYSCALL_INT
2248 int syscall (int sysno, ...)
2250 long int syscall (long int sysno, ...)
2253 #ifdef HAVE_SYSCALL_INT
2260 va_start(va, sysno);
2262 if (!uid_wrapper_enabled()) {
2263 rc = libc_vsyscall(sysno, va);
2269 rc = uwrap_syscall(sysno, va);
2274 #endif /* HAVE_SYSCALL */
2275 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
2277 /****************************
2279 ***************************/
2280 void uwrap_constructor(void)
2283 * If we hold a lock and the application forks, then the child
2284 * is not able to unlock the mutex and we are in a deadlock.
2285 * This should prevent such deadlocks.
2287 pthread_atfork(&uwrap_thread_prepare,
2288 &uwrap_thread_parent,
2289 &uwrap_thread_child);
2291 /* Here is safe place to call uwrap_init() and initialize data
2297 /****************************
2299 ***************************/
2302 * This function is called when the library is unloaded and makes sure that
2303 * resources are freed.
2305 void uwrap_destructor(void)
2307 struct uwrap_thread *u = uwrap.ids;
2312 UWRAP_DLIST_REMOVE(uwrap.ids, u);
2314 SAFE_FREE(u->groups);
2321 if (uwrap.libc.handle != NULL) {
2322 dlclose(uwrap.libc.handle);
2325 if (uwrap.libpthread.handle != NULL) {
2326 dlclose(uwrap.libpthread.handle);