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
47 #define UWRAP_DEBUG(...)
49 #define UWRAP_DEBUG(...) fprintf(stderr, __VA_ARGS__)
52 #define UWRAP_DLIST_ADD(list,item) do { \
54 (item)->prev = NULL; \
55 (item)->next = NULL; \
58 (item)->prev = NULL; \
59 (item)->next = (list); \
60 (list)->prev = (item); \
65 #define UWRAP_DLIST_REMOVE(list,item) do { \
66 if ((list) == (item)) { \
67 (list) = (item)->next; \
69 (list)->prev = NULL; \
73 (item)->prev->next = (item)->next; \
76 (item)->next->prev = (item)->prev; \
79 (item)->prev = NULL; \
80 (item)->next = NULL; \
83 #define LIBC_NAME "libc.so"
85 struct uwrap_libc_fns {
86 int (*_libc_setuid)(uid_t uid);
87 uid_t (*_libc_getuid)(void);
90 int (*_libc_seteuid)(uid_t euid);
93 int (*_libc_setreuid)(uid_t ruid, uid_t euid);
96 int (*_libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
98 uid_t (*_libc_geteuid)(void);
100 int (*_libc_setgid)(gid_t gid);
101 gid_t (*_libc_getgid)(void);
103 int (*_libc_setegid)(uid_t egid);
106 int (*_libc_setregid)(uid_t rgid, uid_t egid);
109 int (*_libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
111 gid_t (*_libc_getegid)(void);
112 int (*_libc_getgroups)(int size, gid_t list[]);
113 int (*_libc_setgroups)(size_t size, const gid_t *list);
115 long int (*_libc_syscall)(long int sysno, ...);
120 * We keep the virtualised euid/egid/groups information here
122 struct uwrap_thread {
137 struct uwrap_thread *next;
138 struct uwrap_thread *prev;
144 struct uwrap_libc_fns fns;
153 struct uwrap_thread *ids;
156 static struct uwrap uwrap;
158 /* Shortcut to the list item */
159 static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
161 /* The mutex or accessing the id */
162 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
164 /*********************************************************
165 * UWRAP LIBC LOADER FUNCTIONS
166 *********************************************************/
174 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
176 int flags = RTLD_LAZY;
185 flags |= RTLD_DEEPBIND;
191 case UWRAP_LIBSOCKET:
194 if (handle == NULL) {
195 for (handle = NULL, i = 10; handle == NULL && i >= 0; i--) {
196 char soname[256] = {0};
198 snprintf(soname, sizeof(soname), "libc.so.%d", i);
199 handle = dlopen(soname, flags);
202 uwrap.libc.handle = handle;
204 handle = uwrap.libc.handle;
209 if (handle == NULL) {
211 "Failed to dlopen library: %s\n",
219 static void *_uwrap_load_lib_function(enum uwrap_lib lib, const char *fn_name)
224 handle = uwrap_load_lib_handle(lib);
226 func = dlsym(handle, fn_name);
229 "Failed to find %s: %s\n",
237 #define uwrap_load_lib_function(lib, fn_name) \
238 if (uwrap.libc.fns._libc_##fn_name == NULL) { \
239 *(void **) (&uwrap.libc.fns._libc_##fn_name) = \
240 _uwrap_load_lib_function(lib, #fn_name); \
243 static void *uwrap_libc_fn(struct uwrap *u, const char *fn_name)
248 func = dlsym(RTLD_NEXT, fn_name);
250 if (u->libc.handle == NULL) {
254 func = dlsym(u->libc.handle, fn_name);
257 printf("Failed to find %s in %s: %s\n",
258 fn_name, LIBC_NAME, dlerror());
265 static void uwrap_libc_init(struct uwrap *u)
269 int flags = RTLD_LAZY;
272 flags |= RTLD_DEEPBIND;
275 for (u->libc.handle = NULL, i = 10; u->libc.handle == NULL; i--) {
276 char soname[256] = {0};
278 snprintf(soname, sizeof(soname), "%s.%u", LIBC_NAME, i);
279 u->libc.handle = dlopen(soname, flags);
282 if (u->libc.handle == NULL) {
283 printf("Failed to dlopen %s.%u: %s\n", LIBC_NAME, i, dlerror());
288 *(void **) (&u->libc.fns._libc_setuid) = uwrap_libc_fn(u, "setuid");
289 *(void **) (&u->libc.fns._libc_getuid) = uwrap_libc_fn(u, "getuid");
292 *(void **) (&u->libc.fns._libc_seteuid) = uwrap_libc_fn(u, "seteuid");
295 *(void **) (&u->libc.fns._libc_setreuid) = uwrap_libc_fn(u, "setreuid");
297 #ifdef HAVE_SETRESUID
298 *(void **) (&u->libc.fns._libc_setresuid) = uwrap_libc_fn(u, "setresuid");
300 *(void **) (&u->libc.fns._libc_geteuid) = uwrap_libc_fn(u, "geteuid");
302 *(void **) (&u->libc.fns._libc_setgid) = uwrap_libc_fn(u, "setgid");
303 *(void **) (&u->libc.fns._libc_getgid) = uwrap_libc_fn(u, "getgid");
305 *(void **) (&u->libc.fns._libc_setegid) = uwrap_libc_fn(u, "setegid");
308 *(void **) (&u->libc.fns._libc_setregid) = uwrap_libc_fn(u, "setregid");
310 #ifdef HAVE_SETRESGID
311 *(void **) (&u->libc.fns._libc_setresgid) = uwrap_libc_fn(u, "setresgid");
313 *(void **) (&u->libc.fns._libc_getegid) = uwrap_libc_fn(u, "getegid");
314 *(void **) (&u->libc.fns._libc_getgroups) = uwrap_libc_fn(u, "getgroups");
315 *(void **) (&u->libc.fns._libc_setgroups) = uwrap_libc_fn(u, "setgroups");
316 *(void **) (&u->libc.fns._libc_getuid) = uwrap_libc_fn(u, "getuid");
317 *(void **) (&u->libc.fns._libc_getgid) = uwrap_libc_fn(u, "getgid");
319 *(void **) (&u->libc.fns._libc_syscall) = uwrap_libc_fn(u, "syscall");
323 static struct uwrap_thread *find_uwrap_id(pthread_t tid)
325 struct uwrap_thread *id;
327 for (id = uwrap.ids; id; id = id->next) {
328 if (pthread_equal(id->tid, tid)) {
336 static int uwrap_new_id(pthread_t tid, bool do_alloc)
338 struct uwrap_thread *id = uwrap_tls_id;
341 id = malloc(sizeof(struct uwrap_thread));
351 id->ruid = id->euid = id->suid = uwrap.myuid;
352 id->rgid = id->egid = id->sgid = uwrap.mygid;
355 id->groups = malloc(sizeof(gid_t) * id->ngroups);
356 id->groups[0] = uwrap.mygid;
359 UWRAP_DLIST_ADD(uwrap.ids, id);
366 static void uwrap_thread_prepare(void)
368 pthread_mutex_lock(&uwrap_id_mutex);
371 * What happens if another atfork prepare functions calls a uwrap
372 * function? So disable it in case another atfork prepare function
373 * calls a (s)uid function.
375 uwrap.enabled = false;
378 static void uwrap_thread_parent(void)
380 uwrap.enabled = true;
382 pthread_mutex_unlock(&uwrap_id_mutex);
385 static void uwrap_thread_child(void)
387 uwrap.enabled = true;
389 pthread_mutex_unlock(&uwrap_id_mutex);
392 static void uwrap_init(void)
394 const char *env = getenv("UID_WRAPPER");
395 pthread_t tid = pthread_self();
399 if (uwrap.initialised) {
400 struct uwrap_thread *id = uwrap_tls_id;
407 pthread_mutex_lock(&uwrap_id_mutex);
408 id = find_uwrap_id(tid);
410 rc = uwrap_new_id(tid, 1);
415 /* We reuse an old thread id */
418 uwrap_new_id(tid, 0);
420 pthread_mutex_unlock(&uwrap_id_mutex);
426 * If we hold a lock and the application forks, then the child
427 * is not able to unlock the mutex and we are in a deadlock.
428 * This should prevent such deadlocks.
430 pthread_atfork(&uwrap_thread_prepare,
431 &uwrap_thread_parent,
432 &uwrap_thread_child);
434 pthread_mutex_lock(&uwrap_id_mutex);
436 uwrap_libc_init(&uwrap);
438 uwrap.initialised = true;
439 uwrap.enabled = false;
441 if (env != NULL && env[0] == '1') {
442 const char *root = getenv("UID_WRAPPER_ROOT");
445 /* put us in one group */
446 if (root != NULL && root[0] == '1') {
450 uwrap.myuid = uwrap.libc.fns._libc_geteuid();
451 uwrap.mygid = uwrap.libc.fns._libc_getegid();
454 rc = uwrap_new_id(tid, 1);
459 uwrap.enabled = true;
462 pthread_mutex_unlock(&uwrap_id_mutex);
465 static int uwrap_enabled(void)
469 return uwrap.enabled ? 1 : 0;
472 static int uwrap_setresuid_thread(uid_t ruid, uid_t euid, uid_t suid)
474 struct uwrap_thread *id = uwrap_tls_id;
476 if (ruid == (uid_t)-1 && euid == (uid_t)-1 && suid == (uid_t)-1) {
481 pthread_mutex_lock(&uwrap_id_mutex);
482 if (ruid != (uid_t)-1) {
486 if (euid != (uid_t)-1) {
490 if (suid != (uid_t)-1) {
493 pthread_mutex_unlock(&uwrap_id_mutex);
498 static int uwrap_setresuid(uid_t ruid, uid_t euid, uid_t suid)
500 struct uwrap_thread *id;
502 if (ruid == (uid_t)-1 && euid == (uid_t)-1 && suid == (uid_t)-1) {
507 pthread_mutex_lock(&uwrap_id_mutex);
508 for (id = uwrap.ids; id; id = id->next) {
513 if (ruid != (uid_t)-1) {
517 if (euid != (uid_t)-1) {
521 if (suid != (uid_t)-1) {
525 pthread_mutex_unlock(&uwrap_id_mutex);
533 int setuid(uid_t uid)
535 if (!uwrap_enabled()) {
536 return uwrap.libc.fns._libc_setuid(uid);
539 return uwrap_setresuid(uid, -1, -1);
543 int seteuid(uid_t euid)
545 if (euid == (uid_t)-1) {
550 if (!uwrap_enabled()) {
551 return uwrap.libc.fns._libc_seteuid(euid);
554 return uwrap_setresuid(-1, euid, -1);
559 int setreuid(uid_t ruid, uid_t euid)
561 if (ruid == (uid_t)-1 && euid == (uid_t)-1) {
566 if (!uwrap_enabled()) {
567 return uwrap.libc.fns._libc_setreuid(ruid, euid);
570 return uwrap_setresuid(ruid, euid, -1);
574 #ifdef HAVE_SETRESUID
575 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
577 if (!uwrap_enabled()) {
578 return uwrap.libc.fns._libc_setresuid(ruid, euid, suid);
581 return uwrap_setresuid(ruid, euid, suid);
588 static uid_t uwrap_getuid(void)
590 struct uwrap_thread *id = uwrap_tls_id;
593 pthread_mutex_lock(&uwrap_id_mutex);
595 pthread_mutex_unlock(&uwrap_id_mutex);
602 if (!uwrap_enabled()) {
603 return uwrap.libc.fns._libc_getuid();
606 return uwrap_getuid();
612 static uid_t uwrap_geteuid(void)
614 const char *env = getenv("UID_WRAPPER_ROOT");
615 struct uwrap_thread *id = uwrap_tls_id;
618 pthread_mutex_lock(&uwrap_id_mutex);
620 pthread_mutex_unlock(&uwrap_id_mutex);
622 /* Disable root and return myuid */
623 if (env != NULL && env[0] == '2') {
632 if (!uwrap_enabled()) {
633 return uwrap.libc.fns._libc_geteuid();
636 return uwrap_geteuid();
639 static int uwrap_setresgid_thread(gid_t rgid, gid_t egid, gid_t sgid)
641 struct uwrap_thread *id = uwrap_tls_id;
643 if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
648 pthread_mutex_lock(&uwrap_id_mutex);
649 if (rgid != (gid_t)-1) {
653 if (egid != (gid_t)-1) {
657 if (sgid != (gid_t)-1) {
660 pthread_mutex_unlock(&uwrap_id_mutex);
665 static int uwrap_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
667 struct uwrap_thread *id;
669 if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
674 pthread_mutex_lock(&uwrap_id_mutex);
675 for (id = uwrap.ids; id; id = id->next) {
680 if (rgid != (gid_t)-1) {
684 if (egid != (gid_t)-1) {
688 if (sgid != (gid_t)-1) {
692 pthread_mutex_unlock(&uwrap_id_mutex);
700 int setgid(gid_t gid)
702 if (!uwrap_enabled()) {
703 return uwrap.libc.fns._libc_setgid(gid);
706 return uwrap_setresgid(gid, -1, -1);
710 int setegid(gid_t egid)
712 if (!uwrap_enabled()) {
713 return uwrap.libc.fns._libc_setegid(egid);
716 return uwrap_setresgid(-1, egid, -1);
721 int setregid(gid_t rgid, gid_t egid)
723 if (!uwrap_enabled()) {
724 return uwrap.libc.fns._libc_setregid(rgid, egid);
727 return uwrap_setresgid(rgid, egid, -1);
731 #ifdef HAVE_SETRESGID
732 int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
734 if (!uwrap_enabled()) {
735 return uwrap.libc.fns._libc_setregid(rgid, egid, sgid);
738 return uwrap_setresgid(rgid, egid, sgid);
745 static gid_t uwrap_getgid(void)
747 struct uwrap_thread *id = uwrap_tls_id;
750 pthread_mutex_lock(&uwrap_id_mutex);
752 pthread_mutex_unlock(&uwrap_id_mutex);
759 if (!uwrap_enabled()) {
760 return uwrap.libc.fns._libc_getgid();
763 return uwrap_getgid();
769 static uid_t uwrap_getegid(void)
771 struct uwrap_thread *id = uwrap_tls_id;
774 pthread_mutex_lock(&uwrap_id_mutex);
776 pthread_mutex_unlock(&uwrap_id_mutex);
783 if (!uwrap_enabled()) {
784 return uwrap.libc.fns._libc_getegid();
787 return uwrap_getegid();
790 static int uwrap_setgroups_thread(size_t size, const gid_t *list)
792 struct uwrap_thread *id = uwrap_tls_id;
795 pthread_mutex_lock(&uwrap_id_mutex);
801 id->groups = malloc(sizeof(gid_t) * size);
802 if (id->groups == NULL) {
807 memcpy(id->groups, list, size * sizeof(gid_t));
812 pthread_mutex_unlock(&uwrap_id_mutex);
817 static int uwrap_setgroups(size_t size, const gid_t *list)
819 struct uwrap_thread *id;
822 pthread_mutex_lock(&uwrap_id_mutex);
823 for (id = uwrap.ids; id; id = id->next) {
829 id->groups = malloc(sizeof(gid_t) * size);
830 if (id->groups == NULL) {
835 memcpy(id->groups, list, size * sizeof(gid_t));
841 pthread_mutex_unlock(&uwrap_id_mutex);
846 #ifdef HAVE_SETGROUPS_INT
847 int setgroups(int size, const gid_t *list)
849 int setgroups(size_t size, const gid_t *list)
852 if (!uwrap_enabled()) {
853 return uwrap.libc.fns._libc_setgroups(size, list);
856 return uwrap_setgroups(size, list);
859 static int uwrap_getgroups(int size, gid_t *list)
861 struct uwrap_thread *id = uwrap_tls_id;
864 pthread_mutex_lock(&uwrap_id_mutex);
865 ngroups = id->ngroups;
867 if (size > ngroups) {
873 if (size < ngroups) {
877 memcpy(list, id->groups, size * sizeof(gid_t));
880 pthread_mutex_unlock(&uwrap_id_mutex);
885 int getgroups(int size, gid_t *list)
887 if (!uwrap_enabled()) {
888 return uwrap.libc.fns._libc_getgroups(size, list);
891 return uwrap_getgroups(size, list);
894 static long int libc_vsyscall(long int sysno, va_list va)
900 for (i = 0; i < 8; i++) {
901 args[i] = va_arg(va, long int);
904 rc = uwrap.libc.fns._libc_syscall(sysno,
917 #if (defined(HAVE_SYS_SYSCALL_H) || defined(HAVE_SYSCALL_H)) \
918 && (defined(SYS_setreuid) || defined(SYS_setreuid32))
919 static long int uwrap_syscall (long int sysno, va_list vp)
926 #ifdef HAVE_LINUX_32BIT_SYSCALLS
935 #ifdef HAVE_LINUX_32BIT_SYSCALLS
939 rc = uwrap_getegid();
942 #endif /* SYS_getegid */
944 #ifdef HAVE_LINUX_32BIT_SYSCALLS
948 gid_t gid = (gid_t) va_arg(vp, int);
950 rc = uwrap_setresgid_thread(gid, -1, -1);
954 #ifdef HAVE_LINUX_32BIT_SYSCALLS
958 uid_t rgid = (uid_t) va_arg(vp, int);
959 uid_t egid = (uid_t) va_arg(vp, int);
961 rc = uwrap_setresgid_thread(rgid, egid, -1);
966 #ifdef HAVE_LINUX_32BIT_SYSCALLS
967 case SYS_setresgid32:
970 uid_t rgid = (uid_t) va_arg(vp, int);
971 uid_t egid = (uid_t) va_arg(vp, int);
972 uid_t sgid = (uid_t) va_arg(vp, int);
974 rc = uwrap_setresgid_thread(rgid, egid, sgid);
977 #endif /* SYS_setresgid */
981 #ifdef HAVE_LINUX_32BIT_SYSCALLS
990 #ifdef HAVE_LINUX_32BIT_SYSCALLS
994 rc = uwrap_geteuid();
997 #endif /* SYS_geteuid */
999 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1003 uid_t uid = (uid_t) va_arg(vp, int);
1005 rc = uwrap_setresuid_thread(uid, -1, -1);
1009 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1010 case SYS_setreuid32:
1013 uid_t ruid = (uid_t) va_arg(vp, int);
1014 uid_t euid = (uid_t) va_arg(vp, int);
1016 rc = uwrap_setresuid_thread(ruid, euid, -1);
1019 #ifdef SYS_setresuid
1021 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1022 case SYS_setresuid32:
1025 uid_t ruid = (uid_t) va_arg(vp, int);
1026 uid_t euid = (uid_t) va_arg(vp, int);
1027 uid_t suid = (uid_t) va_arg(vp, int);
1029 rc = uwrap_setresuid_thread(ruid, euid, suid);
1032 #endif /* SYS_setresuid */
1036 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1037 case SYS_setgroups32:
1040 size_t size = (size_t) va_arg(vp, size_t);
1041 gid_t *list = (gid_t *) va_arg(vp, int *);
1043 rc = uwrap_setgroups_thread(size, list);
1047 UWRAP_DEBUG("UID_WRAPPER calling non-wrapped syscall "
1050 rc = libc_vsyscall(sysno, vp);
1058 #ifdef HAVE_SYSCALL_INT
1059 int syscall (int sysno, ...)
1061 long int syscall (long int sysno, ...)
1064 #ifdef HAVE_SYSCALL_INT
1071 va_start(va, sysno);
1073 if (!uwrap_enabled()) {
1074 rc = libc_vsyscall(sysno, va);
1079 rc = uwrap_syscall(sysno, va);
1084 #endif /* HAVE_SYSCALL */
1085 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */