uwrap: Allow setresuid calls only for privileged users
[Samba.git] / lib / uid_wrapper / uid_wrapper.c
blobabb0cb364cb8e404b7b61e98e83e92905253fcc7
1 /*
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/>.
19 #include "config.h"
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <grp.h>
30 #ifdef HAVE_SYS_SYSCALL_H
31 #include <sys/syscall.h>
32 #endif
33 #ifdef HAVE_SYSCALL_H
34 #include <syscall.h>
35 #endif
36 #include <dlfcn.h>
38 #include <pthread.h>
40 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
41 # define UWRAP_THREAD __thread
42 #else
43 # define UWRAP_THREAD
44 #endif
46 # define UWRAP_LOCK(m) do { \
47 pthread_mutex_lock(&( m ## _mutex)); \
48 } while(0)
50 # define UWRAP_UNLOCK(m) do { \
51 pthread_mutex_unlock(&( m ## _mutex)); \
52 } while(0)
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))
67 #else
68 #define CONSTRUCTOR_ATTRIBUTE
69 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
71 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
72 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
73 #else
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)))
86 #else
87 #define PRINTF_ATTRIBUTE(a,b)
88 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
90 #define UWRAP_DLIST_ADD(list,item) do { \
91 if (!(list)) { \
92 (item)->prev = NULL; \
93 (item)->next = NULL; \
94 (list) = (item); \
95 } else { \
96 (item)->prev = NULL; \
97 (item)->next = (list); \
98 (list)->prev = (item); \
99 (list) = (item); \
101 } while (0)
103 #define UWRAP_DLIST_REMOVE(list,item) do { \
104 if ((list) == (item)) { \
105 (list) = (item)->next; \
106 if (list) { \
107 (list)->prev = NULL; \
109 } else { \
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; \
119 } while (0)
121 #ifndef SAFE_FREE
122 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
123 #endif
125 /*****************
126 * LOGGING
127 *****************/
129 enum uwrap_dbglvl_e {
130 UWRAP_LOG_ERROR = 0,
131 UWRAP_LOG_WARN,
132 UWRAP_LOG_DEBUG,
133 UWRAP_LOG_TRACE
136 #ifdef NDEBUG
137 # define UWRAP_LOG(...)
138 #else /* NDEBUG */
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, ...)
144 char buffer[1024];
145 va_list va;
146 const char *d;
147 unsigned int lvl = 0;
149 d = getenv("UID_WRAPPER_DEBUGLEVEL");
150 if (d != NULL) {
151 lvl = atoi(d);
154 va_start(va, format);
155 vsnprintf(buffer, sizeof(buffer), format, va);
156 va_end(va);
158 if (lvl >= dbglvl) {
159 const char *prefix;
160 switch (dbglvl) {
161 case UWRAP_LOG_ERROR:
162 prefix = "UWRAP_ERROR";
163 break;
164 case UWRAP_LOG_WARN:
165 prefix = "UWRAP_WARN";
166 break;
167 case UWRAP_LOG_DEBUG:
168 prefix = "UWRAP_DEBUG";
169 break;
170 case UWRAP_LOG_TRACE:
171 prefix = "UWRAP_TRACE";
172 break;
175 fprintf(stderr,
176 "%s(%d) - %s: %s\n",
177 prefix,
178 (int)getpid(),
179 function,
180 buffer);
183 #endif /* NDEBUG */
185 /*****************
186 * LIBC
187 *****************/
189 #define LIBC_NAME "libc.so"
191 typedef int (*__libc_setuid)(uid_t uid);
193 typedef uid_t (*__libc_getuid)(void);
195 #ifdef HAVE_SETEUID
196 typedef int (*__libc_seteuid)(uid_t euid);
197 #endif
199 #ifdef HAVE_SETREUID
200 typedef int (*__libc_setreuid)(uid_t ruid, uid_t euid);
201 #endif
203 #ifdef HAVE_SETRESUID
204 typedef int (*__libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
205 #endif
207 #ifdef HAVE_GETRESUID
208 typedef int (*__libc_getresuid)(uid_t *ruid, uid_t *euid, uid_t *suid);
209 #endif
211 typedef uid_t (*__libc_geteuid)(void);
213 typedef int (*__libc_setgid)(gid_t gid);
215 typedef gid_t (*__libc_getgid)(void);
217 #ifdef HAVE_SETEGID
218 typedef int (*__libc_setegid)(uid_t egid);
219 #endif
221 #ifdef HAVE_SETREGID
222 typedef int (*__libc_setregid)(uid_t rgid, uid_t egid);
223 #endif
225 #ifdef HAVE_SETRESGID
226 typedef int (*__libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
227 #endif
229 #ifdef HAVE_GETRESGID
230 typedef int (*__libc_getresgid)(gid_t *rgid, gid_t *egid, gid_t *sgid);
231 #endif
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);
239 #ifdef HAVE_SYSCALL
240 typedef long int (*__libc_syscall)(long int sysno, ...);
241 #endif
243 #define UWRAP_SYMBOL_ENTRY(i) \
244 union { \
245 __libc_##i f; \
246 void *obj; \
247 } _libc_##i
249 struct uwrap_libc_symbols {
250 UWRAP_SYMBOL_ENTRY(setuid);
251 UWRAP_SYMBOL_ENTRY(getuid);
252 #ifdef HAVE_SETEUID
253 UWRAP_SYMBOL_ENTRY(seteuid);
254 #endif
255 #ifdef HAVE_SETREUID
256 UWRAP_SYMBOL_ENTRY(setreuid);
257 #endif
258 #ifdef HAVE_SETRESUID
259 UWRAP_SYMBOL_ENTRY(setresuid);
260 #endif
261 #ifdef HAVE_GETRESUID
262 UWRAP_SYMBOL_ENTRY(getresuid);
263 #endif
264 UWRAP_SYMBOL_ENTRY(geteuid);
265 UWRAP_SYMBOL_ENTRY(setgid);
266 UWRAP_SYMBOL_ENTRY(getgid);
267 #ifdef HAVE_SETEGID
268 UWRAP_SYMBOL_ENTRY(setegid);
269 #endif
270 #ifdef HAVE_SETREGID
271 UWRAP_SYMBOL_ENTRY(setregid);
272 #endif
273 #ifdef HAVE_SETRESGID
274 UWRAP_SYMBOL_ENTRY(setresgid);
275 #endif
276 #ifdef HAVE_GETRESGID
277 UWRAP_SYMBOL_ENTRY(getresgid);
278 #endif
279 UWRAP_SYMBOL_ENTRY(getegid);
280 UWRAP_SYMBOL_ENTRY(getgroups);
281 UWRAP_SYMBOL_ENTRY(setgroups);
282 #ifdef HAVE_SYSCALL
283 UWRAP_SYMBOL_ENTRY(syscall);
284 #endif
286 #undef UWRAP_SYMBOL_ENTRY
288 /*****************
289 * LIBPTHREAD
290 *****************/
291 /* Yeah... I'm pig. I overloading macro here... So what? */
292 #define UWRAP_SYMBOL_ENTRY(i) \
293 union { \
294 __libpthread_##i f; \
295 void *obj; \
296 } _libpthread_##i
298 typedef int (*__libpthread_pthread_create)(pthread_t *thread,
299 const pthread_attr_t *attr,
300 void *(*start_routine) (void *),
301 void *arg);
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 {
314 bool enabled;
316 uid_t ruid;
317 uid_t euid;
318 uid_t suid;
320 gid_t rgid;
321 gid_t egid;
322 gid_t sgid;
324 int ngroups;
325 gid_t *groups;
327 struct uwrap_thread *next;
328 struct uwrap_thread *prev;
331 struct uwrap {
332 struct {
333 void *handle;
334 struct uwrap_libc_symbols symbols;
335 } libc;
337 struct {
338 void *handle;
339 struct uwrap_libpthread_symbols symbols;
340 } libpthread;
342 bool initialised;
344 /* Real uid and gid of user who run uid wrapper */
345 uid_t myuid;
346 gid_t mygid;
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 /*********************************************************
366 * UWRAP PROTOTYPES
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 *********************************************************/
377 enum uwrap_lib {
378 UWRAP_LIBC,
379 UWRAP_LIBNSL,
380 UWRAP_LIBSOCKET,
381 UWRAP_LIBPTHREAD,
384 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
386 int flags = RTLD_LAZY;
387 void *handle = NULL;
388 int i;
390 #ifdef RTLD_DEEPBIND
391 flags |= RTLD_DEEPBIND;
392 #endif
394 switch (lib) {
395 case UWRAP_LIBNSL:
396 /* FALL TROUGH */
397 case UWRAP_LIBSOCKET:
398 /* FALL TROUGH */
399 case UWRAP_LIBC:
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) {
408 break;
412 uwrap.libc.handle = handle;
414 break;
415 case UWRAP_LIBPTHREAD:
416 handle = uwrap.libpthread.handle;
417 if (handle == NULL) {
418 handle = dlopen("libpthread.so.0", flags);
419 if (handle != NULL) {
420 break;
423 break;
426 if (handle == NULL) {
427 #ifdef RTLD_NEXT
428 handle = uwrap.libc.handle = RTLD_NEXT;
429 #else
430 fprintf(stderr,
431 "Failed to dlopen library: %s\n",
432 dlerror());
433 exit(-1);
434 #endif
437 return handle;
440 static void *_uwrap_bind_symbol(enum uwrap_lib lib, const char *fn_name)
442 void *handle;
443 void *func;
445 handle = uwrap_load_lib_handle(lib);
447 func = dlsym(handle, fn_name);
448 if (func == NULL) {
449 fprintf(stderr,
450 "Failed to find %s: %s\n",
451 fn_name, dlerror());
452 exit(-1);
455 return func;
458 #define uwrap_bind_symbol_libc(sym_name) \
459 UWRAP_LOCK(libc_symbol_binding); \
460 if (uwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
461 uwrap.libc.symbols._libc_##sym_name.obj = \
462 _uwrap_bind_symbol(UWRAP_LIBC, #sym_name); \
464 UWRAP_UNLOCK(libc_symbol_binding)
466 #define uwrap_bind_symbol_libpthread(sym_name) \
467 UWRAP_LOCK(libpthread_symbol_binding); \
468 if (uwrap.libpthread.symbols._libpthread_##sym_name.obj == NULL) { \
469 uwrap.libpthread.symbols._libpthread_##sym_name.obj = \
470 _uwrap_bind_symbol(UWRAP_LIBPTHREAD, #sym_name); \
472 UWRAP_UNLOCK(libpthread_symbol_binding)
475 * IMPORTANT
477 * Functions expeciall from libc need to be loaded individually, you can't load
478 * all at once or gdb will segfault at startup. The same applies to valgrind and
479 * has probably something todo with with the linker.
480 * So we need load each function at the point it is called the first time.
482 static int libc_setuid(uid_t uid)
484 uwrap_bind_symbol_libc(setuid);
486 return uwrap.libc.symbols._libc_setuid.f(uid);
489 static uid_t libc_getuid(void)
491 uwrap_bind_symbol_libc(getuid);
493 return uwrap.libc.symbols._libc_getuid.f();
496 #ifdef HAVE_SETEUID
497 static int libc_seteuid(uid_t euid)
499 uwrap_bind_symbol_libc(seteuid);
501 return uwrap.libc.symbols._libc_seteuid.f(euid);
503 #endif
505 #ifdef HAVE_SETREUID
506 static int libc_setreuid(uid_t ruid, uid_t euid)
508 uwrap_bind_symbol_libc(setreuid);
510 return uwrap.libc.symbols._libc_setreuid.f(ruid, euid);
512 #endif
514 #ifdef HAVE_SETRESUID
515 static int libc_setresuid(uid_t ruid, uid_t euid, uid_t suid)
517 uwrap_bind_symbol_libc(setresuid);
519 return uwrap.libc.symbols._libc_setresuid.f(ruid, euid, suid);
521 #endif
523 #ifdef HAVE_GETRESUID
524 static int libc_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
526 uwrap_bind_symbol_libc(getresuid);
528 return uwrap.libc.symbols._libc_getresuid.f(ruid, euid, suid);
530 #endif
532 static uid_t libc_geteuid(void)
534 uwrap_bind_symbol_libc(geteuid);
536 return uwrap.libc.symbols._libc_geteuid.f();
539 static int libc_setgid(gid_t gid)
541 uwrap_bind_symbol_libc(setgid);
543 return uwrap.libc.symbols._libc_setgid.f(gid);
546 static gid_t libc_getgid(void)
548 uwrap_bind_symbol_libc(getgid);
550 return uwrap.libc.symbols._libc_getgid.f();
553 #ifdef HAVE_SETEGID
554 static int libc_setegid(gid_t egid)
556 uwrap_bind_symbol_libc(setegid);
558 return uwrap.libc.symbols._libc_setegid.f(egid);
560 #endif
562 #ifdef HAVE_SETREGID
563 static int libc_setregid(gid_t rgid, gid_t egid)
565 uwrap_bind_symbol_libc(setregid);
567 return uwrap.libc.symbols._libc_setregid.f(rgid, egid);
569 #endif
571 #ifdef HAVE_SETRESGID
572 static int libc_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
574 uwrap_bind_symbol_libc(setresgid);
576 return uwrap.libc.symbols._libc_setresgid.f(rgid, egid, sgid);
578 #endif
580 #ifdef HAVE_GETRESGID
581 static int libc_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
583 uwrap_bind_symbol_libc(setresgid);
585 return uwrap.libc.symbols._libc_getresgid.f(rgid, egid, sgid);
587 #endif
589 static gid_t libc_getegid(void)
591 uwrap_bind_symbol_libc(getegid);
593 return uwrap.libc.symbols._libc_getegid.f();
596 static int libc_getgroups(int size, gid_t list[])
598 uwrap_bind_symbol_libc(getgroups);
600 return uwrap.libc.symbols._libc_getgroups.f(size, list);
603 static int libc_setgroups(size_t size, const gid_t *list)
605 uwrap_bind_symbol_libc(setgroups);
607 return uwrap.libc.symbols._libc_setgroups.f(size, list);
610 #ifdef HAVE_SYSCALL
611 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
612 static long int libc_vsyscall(long int sysno, va_list va)
614 long int args[8];
615 long int rc;
616 int i;
618 uwrap_bind_symbol_libc(syscall);
620 for (i = 0; i < 8; i++) {
621 args[i] = va_arg(va, long int);
624 rc = uwrap.libc.symbols._libc_syscall.f(sysno,
625 args[0],
626 args[1],
627 args[2],
628 args[3],
629 args[4],
630 args[5],
631 args[6],
632 args[7]);
634 return rc;
636 #endif
639 * This part is "optimistic".
640 * Thread can ends without pthread_exit call.
642 static void libpthread_pthread_exit(void *retval)
644 uwrap_bind_symbol_libpthread(pthread_exit);
646 uwrap.libpthread.symbols._libpthread_pthread_exit.f(retval);
649 static void uwrap_pthread_exit(void *retval)
651 struct uwrap_thread *id = uwrap_tls_id;
653 UWRAP_LOG(UWRAP_LOG_DEBUG, "Cleanup thread");
655 UWRAP_LOCK(uwrap_id);
656 if (id == NULL) {
657 UWRAP_UNLOCK(uwrap_id);
658 libpthread_pthread_exit(retval);
659 return;
662 UWRAP_DLIST_REMOVE(uwrap.ids, id);
663 SAFE_FREE(id->groups);
664 SAFE_FREE(id);
665 uwrap_tls_id = NULL;
667 UWRAP_UNLOCK(uwrap_id);
669 libpthread_pthread_exit(retval);
672 void pthread_exit(void *retval)
674 if (!uid_wrapper_enabled()) {
675 libpthread_pthread_exit(retval);
678 uwrap_pthread_exit(retval);
680 /* Calm down gcc warning. */
681 exit(666);
684 static int libpthread_pthread_create(pthread_t *thread,
685 const pthread_attr_t *attr,
686 void *(*start_routine) (void *),
687 void *arg)
689 uwrap_bind_symbol_libpthread(pthread_create);
690 return uwrap.libpthread.symbols._libpthread_pthread_create.f(thread,
691 attr,
692 start_routine,
693 arg);
696 struct uwrap_pthread_create_args {
697 struct uwrap_thread *id;
698 void *(*start_routine) (void *);
699 void *arg;
702 static void *uwrap_pthread_create_start(void *_a)
704 struct uwrap_pthread_create_args *a =
705 (struct uwrap_pthread_create_args *)_a;
706 void *(*start_routine) (void *) = a->start_routine;
707 void *arg = a->arg;
708 struct uwrap_thread *id = a->id;
710 SAFE_FREE(a);
712 uwrap_tls_id = id;
714 return start_routine(arg);
717 static int uwrap_pthread_create(pthread_t *thread,
718 const pthread_attr_t *attr,
719 void *(*start_routine) (void *),
720 void *arg)
722 struct uwrap_pthread_create_args *args;
723 struct uwrap_thread *src_id = uwrap_tls_id;
724 int ret;
726 args = malloc(sizeof(struct uwrap_pthread_create_args));
727 if (args == NULL) {
728 UWRAP_LOG(UWRAP_LOG_ERROR,
729 "uwrap_pthread_create: Unable to allocate memory");
730 errno = ENOMEM;
731 return -1;
733 args->start_routine = start_routine;
734 args->arg = arg;
736 args->id = calloc(1, sizeof(struct uwrap_thread));
737 if (args->id == NULL) {
738 SAFE_FREE(args);
739 UWRAP_LOG(UWRAP_LOG_ERROR,
740 "uwrap_pthread_create: Unable to allocate memory");
741 errno = ENOMEM;
742 return -1;
745 UWRAP_LOCK(uwrap_id);
747 args->id->groups = malloc(sizeof(gid_t) * src_id->ngroups);
748 if (args->id->groups == NULL) {
749 UWRAP_UNLOCK(uwrap_id);
750 SAFE_FREE(args->id);
751 SAFE_FREE(args);
752 UWRAP_LOG(UWRAP_LOG_ERROR,
753 "uwrap_pthread_create: Unable to allocate memory again");
754 errno = ENOMEM;
755 return -1;
758 args->id->ruid = src_id->ruid;
759 args->id->euid = src_id->euid;
760 args->id->suid = src_id->suid;
762 args->id->rgid = src_id->rgid;
763 args->id->egid = src_id->egid;
764 args->id->sgid = src_id->sgid;
766 args->id->enabled = src_id->enabled;
768 args->id->ngroups = src_id->ngroups;
769 if (src_id->groups != NULL) {
770 memcpy(args->id->groups, src_id->groups,
771 sizeof(gid_t) * src_id->ngroups);
772 } else {
773 SAFE_FREE(args->id->groups);
776 UWRAP_DLIST_ADD(uwrap.ids, args->id);
777 UWRAP_UNLOCK(uwrap_id);
779 ret = libpthread_pthread_create(thread, attr,
780 uwrap_pthread_create_start,
781 args);
782 if (ret != 0) {
783 return ret;
786 return ret;
789 int pthread_create(pthread_t *thread,
790 const pthread_attr_t *attr,
791 void *(*start_routine) (void *),
792 void *arg)
794 if (!uid_wrapper_enabled()) {
795 return libpthread_pthread_create(thread,
796 attr,
797 start_routine,
798 arg);
801 return uwrap_pthread_create(thread,
802 attr,
803 start_routine,
804 arg);
807 /*********************************************************
808 * UWRAP ID HANDLING
809 *********************************************************/
811 static void uwrap_thread_prepare(void)
813 struct uwrap_thread *id = uwrap_tls_id;
815 /* uid_wrapper is loaded but not enabled */
816 if (id == NULL) {
817 return;
820 UWRAP_LOCK_ALL;
823 * What happens if another atfork prepare functions calls a uwrap
824 * function? So disable it in case another atfork prepare function
825 * calls a (s)uid function. We disable uid_wrapper only for thread
826 * (process) which called fork.
828 id->enabled = false;
831 static void uwrap_thread_parent(void)
833 struct uwrap_thread *id = uwrap_tls_id;
835 /* uid_wrapper is loaded but not enabled */
836 if (id == NULL) {
837 return;
840 id->enabled = true;
842 UWRAP_UNLOCK_ALL;
845 static void uwrap_thread_child(void)
847 struct uwrap_thread *id = uwrap_tls_id;
848 struct uwrap_thread *u = uwrap.ids;
850 /* uid_wrapper is loaded but not enabled */
851 if (id == NULL) {
852 return;
856 * "Garbage collector" - Inspired by DESTRUCTOR.
857 * All threads (except one which called fork()) are dead now.. Dave
858 * That's what posix said...
860 while (u != NULL) {
861 if (u == id) {
862 /* Skip this item. */
863 u = uwrap.ids->next;
864 continue;
867 UWRAP_DLIST_REMOVE(uwrap.ids, u);
869 SAFE_FREE(u->groups);
870 SAFE_FREE(u);
872 u = uwrap.ids;
875 id->enabled = true;
877 UWRAP_UNLOCK_ALL;
880 static void uwrap_init(void)
882 const char *env;
884 UWRAP_LOCK(uwrap_id);
886 if (uwrap.initialised) {
887 struct uwrap_thread *id = uwrap_tls_id;
889 if (uwrap.ids == NULL) {
890 UWRAP_UNLOCK(uwrap_id);
891 return;
894 if (id == NULL) {
895 UWRAP_LOG(UWRAP_LOG_ERROR,
896 "Invalid id for thread");
897 exit(-1);
900 UWRAP_UNLOCK(uwrap_id);
901 return;
904 UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize uid_wrapper");
906 uwrap.initialised = true;
908 env = getenv("UID_WRAPPER");
909 if (env != NULL && env[0] == '1') {
910 const char *root = getenv("UID_WRAPPER_ROOT");
911 struct uwrap_thread *id;
913 id = calloc(1, sizeof(struct uwrap_thread));
914 if (id == NULL) {
915 UWRAP_LOG(UWRAP_LOG_ERROR,
916 "Unable to allocate memory for main id");
917 exit(-1);
920 UWRAP_DLIST_ADD(uwrap.ids, id);
921 uwrap_tls_id = id;
923 uwrap.myuid = libc_geteuid();
924 uwrap.mygid = libc_getegid();
926 /* put us in one group */
927 if (root != NULL && root[0] == '1') {
928 id->ruid = id->euid = id->suid = 0;
929 id->rgid = id->egid = id->sgid = 0;
931 id->groups = malloc(sizeof(gid_t) * 1);
932 if (id->groups == NULL) {
933 UWRAP_LOG(UWRAP_LOG_ERROR,
934 "Unable to allocate memory");
935 exit(-1);
938 id->ngroups = 1;
939 id->groups[0] = 0;
941 } else {
942 id->ruid = id->euid = id->suid = uwrap.myuid;
943 id->rgid = id->egid = id->sgid = uwrap.mygid;
945 id->ngroups = libc_getgroups(0, NULL);
946 if (id->ngroups == -1) {
947 UWRAP_LOG(UWRAP_LOG_ERROR,
948 "Unable to call libc_getgroups in uwrap_init.");
949 exit(-1);
951 id->groups = malloc(sizeof(gid_t) * id->ngroups);
952 if (id->groups == NULL) {
953 UWRAP_LOG(UWRAP_LOG_ERROR, "Unable to allocate memory");
954 exit(-1);
956 if (libc_getgroups(id->ngroups, id->groups) == -1) {
957 UWRAP_LOG(UWRAP_LOG_ERROR,
958 "Unable to call libc_getgroups again in uwrap_init.");
959 id->groups = 0;
961 * Deallocation of uwrap.groups is handled by
962 * library destructor.
964 exit(-1);
968 id->enabled = true;
970 UWRAP_LOG(UWRAP_LOG_DEBUG,
971 "Enabled uid_wrapper as %s (real uid=%u)",
972 id->ruid == 0 ? "root" : "user",
973 (unsigned int)uwrap.myuid);
976 UWRAP_UNLOCK(uwrap_id);
978 UWRAP_LOG(UWRAP_LOG_DEBUG, "Succeccfully initialized uid_wrapper");
981 bool uid_wrapper_enabled(void)
983 struct uwrap_thread *id = uwrap_tls_id;
984 bool enabled;
986 if (id == NULL) {
987 return false;
990 UWRAP_LOCK(uwrap_id);
991 enabled = id->enabled;
992 UWRAP_UNLOCK(uwrap_id);
994 return enabled;
998 * UWRAP_SETxUID FUNCTIONS
1001 static int uwrap_setresuid_args(uid_t ruid, uid_t euid, uid_t suid)
1003 struct uwrap_thread *id = uwrap_tls_id;
1005 UWRAP_LOG(UWRAP_LOG_TRACE,
1006 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1007 id->ruid, ruid, id->euid, euid, id->suid, suid);
1009 if (id->euid != 0) {
1010 if (ruid != (uid_t)-1 &&
1011 ruid != id->ruid &&
1012 ruid != id->euid &&
1013 ruid != id->suid) {
1014 errno = EPERM;
1015 return -1;
1017 if (euid != (uid_t)-1 &&
1018 euid != id->ruid &&
1019 euid != id->euid &&
1020 euid != id->suid) {
1021 errno = EPERM;
1022 return -1;
1024 if (suid != (uid_t)-1 &&
1025 suid != id->ruid &&
1026 suid != id->euid &&
1027 suid != id->suid) {
1028 errno = EPERM;
1029 return -1;
1033 return 0;
1036 static int uwrap_setresuid_thread(uid_t ruid, uid_t euid, uid_t suid)
1038 struct uwrap_thread *id = uwrap_tls_id;
1039 int rc;
1041 UWRAP_LOG(UWRAP_LOG_TRACE,
1042 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1043 id->ruid, ruid, id->euid, euid, id->suid, suid);
1045 rc = uwrap_setresuid_args(ruid, euid, suid);
1046 if (rc != 0) {
1047 return rc;
1050 UWRAP_LOCK(uwrap_id);
1052 if (ruid != (uid_t)-1) {
1053 id->ruid = ruid;
1056 if (euid != (uid_t)-1) {
1057 id->euid = euid;
1060 if (suid != (uid_t)-1) {
1061 id->suid = suid;
1064 UWRAP_UNLOCK(uwrap_id);
1066 return 0;
1069 static int uwrap_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1071 struct uwrap_thread *id = uwrap_tls_id;
1072 int rc;
1074 UWRAP_LOG(UWRAP_LOG_TRACE,
1075 "ruid %d -> %d, euid %d -> %d, suid %d -> %d",
1076 id->ruid, ruid, id->euid, euid, id->suid, suid);
1078 rc = uwrap_setresuid_args(ruid, euid, suid);
1079 if (rc != 0) {
1080 return rc;
1083 UWRAP_LOCK(uwrap_id);
1085 for (id = uwrap.ids; id; id = id->next) {
1086 if (ruid != (uid_t)-1) {
1087 id->ruid = ruid;
1090 if (euid != (uid_t)-1) {
1091 id->euid = euid;
1094 if (suid != (uid_t)-1) {
1095 id->suid = suid;
1099 UWRAP_UNLOCK(uwrap_id);
1101 return 0;
1106 * UWRAP_GETxUID FUNCTIONS
1109 #ifdef HAVE_GETRESUID
1110 static int uwrap_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1112 struct uwrap_thread *id = uwrap_tls_id;
1114 UWRAP_LOCK(uwrap_id);
1116 *ruid = id->ruid;
1117 *euid = id->euid;
1118 *suid = id->suid;
1120 UWRAP_UNLOCK(uwrap_id);
1122 return 0;
1124 #endif
1126 #ifdef HAVE_GETRESGID
1127 static int uwrap_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1129 struct uwrap_thread *id = uwrap_tls_id;
1131 UWRAP_LOCK(uwrap_id);
1133 *rgid = id->rgid;
1134 *egid = id->egid;
1135 *sgid = id->sgid;
1137 UWRAP_UNLOCK(uwrap_id);
1139 return 0;
1141 #endif
1144 * SETUID
1146 int setuid(uid_t uid)
1148 if (!uid_wrapper_enabled()) {
1149 return libc_setuid(uid);
1152 uwrap_init();
1153 return uwrap_setresuid(uid, -1, -1);
1156 #ifdef HAVE_SETEUID
1157 int seteuid(uid_t euid)
1159 if (euid == (uid_t)-1) {
1160 errno = EINVAL;
1161 return -1;
1164 if (!uid_wrapper_enabled()) {
1165 return libc_seteuid(euid);
1168 uwrap_init();
1169 return uwrap_setresuid(-1, euid, -1);
1171 #endif
1173 #ifdef HAVE_SETREUID
1174 int setreuid(uid_t ruid, uid_t euid)
1176 if (ruid == (uid_t)-1 && euid == (uid_t)-1) {
1177 errno = EINVAL;
1178 return -1;
1181 if (!uid_wrapper_enabled()) {
1182 return libc_setreuid(ruid, euid);
1185 uwrap_init();
1186 return uwrap_setresuid(ruid, euid, -1);
1188 #endif
1190 #ifdef HAVE_SETRESUID
1191 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
1193 if (!uid_wrapper_enabled()) {
1194 return libc_setresuid(ruid, euid, suid);
1197 uwrap_init();
1198 return uwrap_setresuid(ruid, euid, suid);
1200 #endif
1202 #ifdef HAVE_GETRESUID
1203 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1205 if (!uid_wrapper_enabled()) {
1206 return libc_getresuid(ruid, euid, suid);
1209 uwrap_init();
1210 return uwrap_getresuid(ruid, euid, suid);
1212 #endif
1215 * GETUID
1217 static uid_t uwrap_getuid(void)
1219 struct uwrap_thread *id = uwrap_tls_id;
1220 uid_t uid;
1222 UWRAP_LOCK(uwrap_id);
1223 uid = id->ruid;
1224 UWRAP_UNLOCK(uwrap_id);
1226 return uid;
1229 uid_t getuid(void)
1231 if (!uid_wrapper_enabled()) {
1232 return libc_getuid();
1235 uwrap_init();
1236 return uwrap_getuid();
1240 * GETEUID
1242 static uid_t uwrap_geteuid(void)
1244 const char *env = getenv("UID_WRAPPER_MYUID");
1245 struct uwrap_thread *id = uwrap_tls_id;
1246 uid_t uid;
1248 UWRAP_LOCK(uwrap_id);
1249 uid = id->euid;
1250 UWRAP_UNLOCK(uwrap_id);
1252 /* Disable root and return myuid */
1253 if (env != NULL && env[0] == '1') {
1254 uid = uwrap.myuid;
1257 return uid;
1260 uid_t geteuid(void)
1262 if (!uid_wrapper_enabled()) {
1263 return libc_geteuid();
1266 uwrap_init();
1267 return uwrap_geteuid();
1270 static int uwrap_setresgid_thread(gid_t rgid, gid_t egid, gid_t sgid)
1272 struct uwrap_thread *id = uwrap_tls_id;
1274 if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
1275 errno = EINVAL;
1276 return -1;
1279 UWRAP_LOCK(uwrap_id);
1280 if (rgid != (gid_t)-1) {
1281 id->rgid = rgid;
1284 if (egid != (gid_t)-1) {
1285 id->egid = egid;
1288 if (sgid != (gid_t)-1) {
1289 id->sgid = sgid;
1292 UWRAP_UNLOCK(uwrap_id);
1294 return 0;
1297 static int uwrap_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1299 struct uwrap_thread *id;
1301 if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
1302 errno = EINVAL;
1303 return -1;
1306 UWRAP_LOCK(uwrap_id);
1307 for (id = uwrap.ids; id; id = id->next) {
1308 if (rgid != (gid_t)-1) {
1309 id->rgid = rgid;
1312 if (egid != (gid_t)-1) {
1313 id->egid = egid;
1316 if (sgid != (gid_t)-1) {
1317 id->sgid = sgid;
1320 UWRAP_UNLOCK(uwrap_id);
1322 return 0;
1326 * SETGID
1328 int setgid(gid_t gid)
1330 if (!uid_wrapper_enabled()) {
1331 return libc_setgid(gid);
1334 uwrap_init();
1335 return uwrap_setresgid(gid, -1, -1);
1338 #ifdef HAVE_SETEGID
1339 int setegid(gid_t egid)
1341 if (!uid_wrapper_enabled()) {
1342 return libc_setegid(egid);
1345 uwrap_init();
1346 return uwrap_setresgid(-1, egid, -1);
1348 #endif
1350 #ifdef HAVE_SETREGID
1351 int setregid(gid_t rgid, gid_t egid)
1353 if (!uid_wrapper_enabled()) {
1354 return libc_setregid(rgid, egid);
1357 uwrap_init();
1358 return uwrap_setresgid(rgid, egid, -1);
1360 #endif
1362 #ifdef HAVE_SETRESGID
1363 int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1365 if (!uid_wrapper_enabled()) {
1366 return libc_setresgid(rgid, egid, sgid);
1369 uwrap_init();
1370 return uwrap_setresgid(rgid, egid, sgid);
1372 #endif
1374 #ifdef HAVE_GETRESGID
1375 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1377 if (!uid_wrapper_enabled()) {
1378 return libc_getresgid(rgid, egid, sgid);
1381 uwrap_init();
1382 return uwrap_getresgid(rgid, egid, sgid);
1384 #endif
1387 * GETGID
1389 static gid_t uwrap_getgid(void)
1391 struct uwrap_thread *id = uwrap_tls_id;
1392 gid_t gid;
1394 UWRAP_LOCK(uwrap_id);
1395 gid = id->rgid;
1396 UWRAP_UNLOCK(uwrap_id);
1398 return gid;
1401 gid_t getgid(void)
1403 if (!uid_wrapper_enabled()) {
1404 return libc_getgid();
1407 uwrap_init();
1408 return uwrap_getgid();
1412 * GETEGID
1414 static uid_t uwrap_getegid(void)
1416 struct uwrap_thread *id = uwrap_tls_id;
1417 gid_t gid;
1419 UWRAP_LOCK(uwrap_id);
1420 gid = id->egid;
1421 UWRAP_UNLOCK(uwrap_id);
1423 return gid;
1426 uid_t getegid(void)
1428 if (!uid_wrapper_enabled()) {
1429 return libc_getegid();
1432 uwrap_init();
1433 return uwrap_getegid();
1436 static int uwrap_setgroups_thread(size_t size, const gid_t *list)
1438 struct uwrap_thread *id = uwrap_tls_id;
1439 int rc = -1;
1441 UWRAP_LOCK(uwrap_id);
1443 if (size == 0) {
1444 SAFE_FREE(id->groups);
1445 id->ngroups = 0;
1446 } else if (size > 0) {
1447 gid_t *tmp;
1449 tmp = realloc(id->groups, sizeof(gid_t) * size);
1450 if (tmp == NULL) {
1451 errno = ENOMEM;
1452 goto out;
1454 id->groups = tmp;
1455 id->ngroups = size;
1456 memcpy(id->groups, list, size * sizeof(gid_t));
1459 rc = 0;
1460 out:
1461 UWRAP_UNLOCK(uwrap_id);
1463 return rc;
1466 static int uwrap_setgroups(size_t size, const gid_t *list)
1468 struct uwrap_thread *id;
1469 int rc = -1;
1471 UWRAP_LOCK(uwrap_id);
1473 if (size == 0) {
1474 for (id = uwrap.ids; id; id = id->next) {
1475 SAFE_FREE(id->groups);
1476 id->ngroups = 0;
1479 } else if (size > 0) {
1480 gid_t *tmp;
1482 for (id = uwrap.ids; id; id = id->next) {
1483 tmp = realloc(id->groups, sizeof(gid_t) * size);
1484 if (tmp == NULL) {
1485 errno = ENOMEM;
1486 goto out;
1488 id->groups = tmp;
1490 id->ngroups = size;
1491 memcpy(id->groups, list, size * sizeof(gid_t));
1495 rc = 0;
1496 out:
1497 UWRAP_UNLOCK(uwrap_id);
1499 return rc;
1502 #ifdef HAVE_SETGROUPS_INT
1503 int setgroups(int size, const gid_t *list)
1504 #else
1505 int setgroups(size_t size, const gid_t *list)
1506 #endif
1508 if (!uid_wrapper_enabled()) {
1509 return libc_setgroups(size, list);
1512 uwrap_init();
1513 return uwrap_setgroups(size, list);
1516 static int uwrap_getgroups(int size, gid_t *list)
1518 struct uwrap_thread *id = uwrap_tls_id;
1519 int ngroups;
1521 UWRAP_LOCK(uwrap_id);
1522 ngroups = id->ngroups;
1524 if (size > ngroups) {
1525 size = ngroups;
1527 if (size == 0) {
1528 goto out;
1530 if (size < ngroups) {
1531 errno = EINVAL;
1532 ngroups = -1;
1534 memcpy(list, id->groups, size * sizeof(gid_t));
1536 out:
1537 UWRAP_UNLOCK(uwrap_id);
1539 return ngroups;
1542 int getgroups(int size, gid_t *list)
1544 if (!uid_wrapper_enabled()) {
1545 return libc_getgroups(size, list);
1548 uwrap_init();
1549 return uwrap_getgroups(size, list);
1552 #if (defined(HAVE_SYS_SYSCALL_H) || defined(HAVE_SYSCALL_H)) \
1553 && (defined(SYS_setreuid) || defined(SYS_setreuid32))
1554 static long int uwrap_syscall (long int sysno, va_list vp)
1556 long int rc;
1558 switch (sysno) {
1559 /* gid */
1560 case SYS_getgid:
1561 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1562 case SYS_getgid32:
1563 #endif
1565 rc = uwrap_getgid();
1567 break;
1568 #ifdef SYS_getegid
1569 case SYS_getegid:
1570 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1571 case SYS_getegid32:
1572 #endif
1574 rc = uwrap_getegid();
1576 break;
1577 #endif /* SYS_getegid */
1578 case SYS_setgid:
1579 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1580 case SYS_setgid32:
1581 #endif
1583 gid_t gid = (gid_t) va_arg(vp, gid_t);
1585 rc = uwrap_setresgid_thread(gid, -1, -1);
1587 break;
1588 case SYS_setregid:
1589 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1590 case SYS_setregid32:
1591 #endif
1593 gid_t rgid = (gid_t) va_arg(vp, gid_t);
1594 gid_t egid = (gid_t) va_arg(vp, gid_t);
1596 rc = uwrap_setresgid_thread(rgid, egid, -1);
1598 break;
1599 #ifdef SYS_setresgid
1600 case SYS_setresgid:
1601 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1602 case SYS_setresgid32:
1603 #endif
1605 gid_t rgid = (gid_t) va_arg(vp, gid_t);
1606 gid_t egid = (gid_t) va_arg(vp, gid_t);
1607 gid_t sgid = (gid_t) va_arg(vp, gid_t);
1609 rc = uwrap_setresgid_thread(rgid, egid, sgid);
1611 break;
1612 #endif /* SYS_setresgid */
1613 #if defined(SYS_getresgid) && defined(HAVE_GETRESGID)
1614 case SYS_getresgid:
1615 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1616 case SYS_getresgid32:
1617 #endif
1619 gid_t *rgid = (gid_t *) va_arg(vp, gid_t *);
1620 gid_t *egid = (gid_t *) va_arg(vp, gid_t *);
1621 gid_t *sgid = (gid_t *) va_arg(vp, gid_t *);
1623 rc = uwrap_getresgid(rgid, egid, sgid);
1625 break;
1626 #endif /* SYS_getresgid && HAVE_GETRESGID */
1628 /* uid */
1629 case SYS_getuid:
1630 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1631 case SYS_getuid32:
1632 #endif
1634 rc = uwrap_getuid();
1636 break;
1637 #ifdef SYS_geteuid
1638 case SYS_geteuid:
1639 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1640 case SYS_geteuid32:
1641 #endif
1643 rc = uwrap_geteuid();
1645 break;
1646 #endif /* SYS_geteuid */
1647 case SYS_setuid:
1648 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1649 case SYS_setuid32:
1650 #endif
1652 uid_t uid = (uid_t) va_arg(vp, uid_t);
1654 rc = uwrap_setresuid_thread(uid, -1, -1);
1656 break;
1657 case SYS_setreuid:
1658 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1659 case SYS_setreuid32:
1660 #endif
1662 uid_t ruid = (uid_t) va_arg(vp, uid_t);
1663 uid_t euid = (uid_t) va_arg(vp, uid_t);
1665 rc = uwrap_setresuid_thread(ruid, euid, -1);
1667 break;
1668 #ifdef SYS_setresuid
1669 case SYS_setresuid:
1670 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1671 case SYS_setresuid32:
1672 #endif
1674 uid_t ruid = (uid_t) va_arg(vp, uid_t);
1675 uid_t euid = (uid_t) va_arg(vp, uid_t);
1676 uid_t suid = (uid_t) va_arg(vp, uid_t);
1678 rc = uwrap_setresuid_thread(ruid, euid, suid);
1680 break;
1681 #endif /* SYS_setresuid */
1682 #if defined(SYS_getresuid) && defined(HAVE_GETRESUID)
1683 case SYS_getresuid:
1684 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1685 case SYS_getresuid32:
1686 #endif
1688 uid_t *ruid = (uid_t *) va_arg(vp, uid_t *);
1689 uid_t *euid = (uid_t *) va_arg(vp, uid_t *);
1690 uid_t *suid = (uid_t *) va_arg(vp, uid_t *);
1692 rc = uwrap_getresuid(ruid, euid, suid);
1694 break;
1695 #endif /* SYS_getresuid && HAVE_GETRESUID*/
1696 /* groups */
1697 case SYS_setgroups:
1698 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1699 case SYS_setgroups32:
1700 #endif
1702 size_t size = (size_t) va_arg(vp, size_t);
1703 gid_t *list = (gid_t *) va_arg(vp, int *);
1705 rc = uwrap_setgroups_thread(size, list);
1707 break;
1708 default:
1709 UWRAP_LOG(UWRAP_LOG_DEBUG,
1710 "UID_WRAPPER calling non-wrapped syscall %lu",
1711 sysno);
1713 rc = libc_vsyscall(sysno, vp);
1714 break;
1717 return rc;
1720 #ifdef HAVE_SYSCALL
1721 #ifdef HAVE_SYSCALL_INT
1722 int syscall (int sysno, ...)
1723 #else
1724 long int syscall (long int sysno, ...)
1725 #endif
1727 #ifdef HAVE_SYSCALL_INT
1728 int rc;
1729 #else
1730 long int rc;
1731 #endif
1732 va_list va;
1734 va_start(va, sysno);
1736 if (!uid_wrapper_enabled()) {
1737 rc = libc_vsyscall(sysno, va);
1738 va_end(va);
1739 return rc;
1742 uwrap_init();
1743 rc = uwrap_syscall(sysno, va);
1744 va_end(va);
1746 return rc;
1748 #endif /* HAVE_SYSCALL */
1749 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
1751 /****************************
1752 * CONSTRUCTOR
1753 ***************************/
1754 void uwrap_constructor(void)
1757 * If we hold a lock and the application forks, then the child
1758 * is not able to unlock the mutex and we are in a deadlock.
1759 * This should prevent such deadlocks.
1761 pthread_atfork(&uwrap_thread_prepare,
1762 &uwrap_thread_parent,
1763 &uwrap_thread_child);
1765 /* Here is safe place to call uwrap_init() and initialize data
1766 * for main process.
1768 uwrap_init();
1771 /****************************
1772 * DESTRUCTOR
1773 ***************************/
1776 * This function is called when the library is unloaded and makes sure that
1777 * resources are freed.
1779 void uwrap_destructor(void)
1781 struct uwrap_thread *u = uwrap.ids;
1783 UWRAP_LOCK_ALL;
1785 while (u != NULL) {
1786 UWRAP_DLIST_REMOVE(uwrap.ids, u);
1788 SAFE_FREE(u->groups);
1789 SAFE_FREE(u);
1791 u = uwrap.ids;
1795 if (uwrap.libc.handle != NULL) {
1796 dlclose(uwrap.libc.handle);
1799 if (uwrap.libpthread.handle != NULL) {
1800 dlclose(uwrap.libpthread.handle);
1803 UWRAP_UNLOCK_ALL;