uwrap: Fix a possible null pointer dereference
[uid_wrapper.git] / src / uid_wrapper.c
1 /*
2  * Copyright (c) 2009      Andrew Tridgell
3  * Copyright (c) 2011-2013 Andreas Schneider <asn@samba.org>
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "config.h"
20
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>
37
38 #include <pthread.h>
39
40 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
41 # define UWRAP_THREAD __thread
42 #else
43 # define UWRAP_THREAD
44 #endif
45
46 # define UWRAP_LOCK(m) do { \
47         pthread_mutex_lock(&( m ## _mutex)); \
48 } while(0)
49
50 # define UWRAP_UNLOCK(m) do { \
51         pthread_mutex_unlock(&( m ## _mutex)); \
52 } while(0)
53
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)
59
60 # define UWRAP_UNLOCK_ALL \
61         UWRAP_UNLOCK(libpthread_symbol_binding); \
62         UWRAP_UNLOCK(libc_symbol_binding); \
63         UWRAP_UNLOCK(uwrap_id)
64
65 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
66 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
67 #else
68 #define CONSTRUCTOR_ATTRIBUTE
69 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
70
71 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
72 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
73 #else
74 #define DESTRUCTOR_ATTRIBUTE
75 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
76
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 */
82
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 */
89
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); \
100         } \
101 } while (0)
102
103 #define UWRAP_DLIST_REMOVE(list,item) do { \
104         if ((list) == (item)) { \
105                 (list)          = (item)->next; \
106                 if (list) { \
107                         (list)->prev    = NULL; \
108                 } \
109         } else { \
110                 if ((item)->prev) { \
111                         (item)->prev->next      = (item)->next; \
112                 } \
113                 if ((item)->next) { \
114                         (item)->next->prev      = (item)->prev; \
115                 } \
116         } \
117         (item)->prev    = NULL; \
118         (item)->next    = NULL; \
119 } while (0)
120
121 #ifndef SAFE_FREE
122 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
123 #endif
124
125 /*****************
126  * LOGGING
127  *****************/
128
129 enum uwrap_dbglvl_e {
130         UWRAP_LOG_ERROR = 0,
131         UWRAP_LOG_WARN,
132         UWRAP_LOG_DEBUG,
133         UWRAP_LOG_TRACE
134 };
135
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__)
141
142 static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *function, const char *format, ...)
143 {
144         char buffer[1024];
145         va_list va;
146         const char *d;
147         unsigned int lvl = 0;
148
149         d = getenv("UID_WRAPPER_DEBUGLEVEL");
150         if (d != NULL) {
151                 lvl = atoi(d);
152         }
153
154         va_start(va, format);
155         vsnprintf(buffer, sizeof(buffer), format, va);
156         va_end(va);
157
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;
173                 }
174
175                 fprintf(stderr,
176                         "%s(%d) - %s: %s\n",
177                         prefix,
178                         (int)getpid(),
179                         function,
180                         buffer);
181         }
182 }
183 #endif /* NDEBUG */
184
185 /*****************
186  * LIBC
187  *****************/
188
189 #define LIBC_NAME "libc.so"
190
191 typedef int (*__libc_setuid)(uid_t uid);
192
193 typedef uid_t (*__libc_getuid)(void);
194
195 #ifdef HAVE_SETEUID
196 typedef int (*__libc_seteuid)(uid_t euid);
197 #endif
198
199 #ifdef HAVE_SETREUID
200 typedef int (*__libc_setreuid)(uid_t ruid, uid_t euid);
201 #endif
202
203 #ifdef HAVE_SETRESUID
204 typedef int (*__libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
205 #endif
206
207 #ifdef HAVE_GETRESUID
208 typedef int (*__libc_getresuid)(uid_t *ruid, uid_t *euid, uid_t *suid);
209 #endif
210
211 typedef uid_t (*__libc_geteuid)(void);
212
213 typedef int (*__libc_setgid)(gid_t gid);
214
215 typedef gid_t (*__libc_getgid)(void);
216
217 #ifdef HAVE_SETEGID
218 typedef int (*__libc_setegid)(uid_t egid);
219 #endif
220
221 #ifdef HAVE_SETREGID
222 typedef int (*__libc_setregid)(uid_t rgid, uid_t egid);
223 #endif
224
225 #ifdef HAVE_SETRESGID
226 typedef int (*__libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
227 #endif
228
229 #ifdef HAVE_GETRESGID
230 typedef int (*__libc_getresgid)(gid_t *rgid, gid_t *egid, gid_t *sgid);
231 #endif
232
233 typedef gid_t (*__libc_getegid)(void);
234
235 typedef int (*__libc_getgroups)(int size, gid_t list[]);
236
237 typedef int (*__libc_setgroups)(size_t size, const gid_t *list);
238
239 #ifdef HAVE_SYSCALL
240 typedef long int (*__libc_syscall)(long int sysno, ...);
241 #endif
242
243 #define UWRAP_SYMBOL_ENTRY(i) \
244         union { \
245                 __libc_##i f; \
246                 void *obj; \
247         } _libc_##i
248
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
285 };
286 #undef UWRAP_SYMBOL_ENTRY
287
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
297
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);
303
304 struct uwrap_libpthread_symbols {
305         UWRAP_SYMBOL_ENTRY(pthread_create);
306         UWRAP_SYMBOL_ENTRY(pthread_exit);
307 };
308 #undef UWRAP_SYMBOL_ENTRY
309
310 /*
311  * We keep the virtualised euid/egid/groups information here
312  */
313 struct uwrap_thread {
314         bool enabled;
315
316         uid_t ruid;
317         uid_t euid;
318         uid_t suid;
319
320         gid_t rgid;
321         gid_t egid;
322         gid_t sgid;
323
324         int ngroups;
325         gid_t *groups;
326
327         struct uwrap_thread *next;
328         struct uwrap_thread *prev;
329 };
330
331 struct uwrap {
332         struct {
333                 void *handle;
334                 struct uwrap_libc_symbols symbols;
335         } libc;
336
337         struct {
338                 void *handle;
339                 struct uwrap_libpthread_symbols symbols;
340         } libpthread;
341
342         bool initialised;
343
344         /* Real uid and gid of user who run uid wrapper */
345         uid_t myuid;
346         gid_t mygid;
347
348         struct uwrap_thread *ids;
349 };
350
351 static struct uwrap uwrap;
352
353 /* Shortcut to the list item */
354 static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
355
356 /* The mutex or accessing the id */
357 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
358
359 /* The mutex for accessing the global libc.symbols */
360 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
361
362 /* The mutex for accessing the global libpthread.symbols */
363 static pthread_mutex_t libpthread_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
364
365 /*********************************************************
366  * UWRAP PROTOTYPES
367  *********************************************************/
368
369 bool uid_wrapper_enabled(void);
370 void uwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
371 void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
372
373 /*********************************************************
374  * UWRAP LIBC LOADER FUNCTIONS
375  *********************************************************/
376
377 enum uwrap_lib {
378     UWRAP_LIBC,
379     UWRAP_LIBNSL,
380     UWRAP_LIBSOCKET,
381     UWRAP_LIBPTHREAD,
382 };
383
384 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
385 {
386         int flags = RTLD_LAZY;
387         void *handle = NULL;
388         int i;
389
390 #ifdef RTLD_DEEPBIND
391         flags |= RTLD_DEEPBIND;
392 #endif
393
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};
404
405                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
406                                 handle = dlopen(soname, flags);
407                                 if (handle != NULL) {
408                                         break;
409                                 }
410                         }
411
412                         uwrap.libc.handle = handle;
413                 }
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;
421                         }
422                 }
423                 break;
424         }
425
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
435         }
436
437         return handle;
438 }
439
440 static void *_uwrap_bind_symbol(enum uwrap_lib lib, const char *fn_name)
441 {
442         void *handle;
443         void *func;
444
445         handle = uwrap_load_lib_handle(lib);
446
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);
453         }
454
455         return func;
456 }
457
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); \
463         } \
464         UWRAP_UNLOCK(libc_symbol_binding)
465
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); \
471         } \
472         UWRAP_UNLOCK(libpthread_symbol_binding)
473
474 /*
475  * IMPORTANT
476  *
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.
481  */
482 static int libc_setuid(uid_t uid)
483 {
484         uwrap_bind_symbol_libc(setuid);
485
486         return uwrap.libc.symbols._libc_setuid.f(uid);
487 }
488
489 static uid_t libc_getuid(void)
490 {
491         uwrap_bind_symbol_libc(getuid);
492
493         return uwrap.libc.symbols._libc_getuid.f();
494 }
495
496 #ifdef HAVE_SETEUID
497 static int libc_seteuid(uid_t euid)
498 {
499         uwrap_bind_symbol_libc(seteuid);
500
501         return uwrap.libc.symbols._libc_seteuid.f(euid);
502 }
503 #endif
504
505 #ifdef HAVE_SETREUID
506 static int libc_setreuid(uid_t ruid, uid_t euid)
507 {
508         uwrap_bind_symbol_libc(setreuid);
509
510         return uwrap.libc.symbols._libc_setreuid.f(ruid, euid);
511 }
512 #endif
513
514 #ifdef HAVE_SETRESUID
515 static int libc_setresuid(uid_t ruid, uid_t euid, uid_t suid)
516 {
517         uwrap_bind_symbol_libc(setresuid);
518
519         return uwrap.libc.symbols._libc_setresuid.f(ruid, euid, suid);
520 }
521 #endif
522
523 #ifdef HAVE_GETRESUID
524 static int libc_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
525 {
526         uwrap_bind_symbol_libc(getresuid);
527
528         return uwrap.libc.symbols._libc_getresuid.f(ruid, euid, suid);
529 }
530 #endif
531
532 static uid_t libc_geteuid(void)
533 {
534         uwrap_bind_symbol_libc(geteuid);
535
536         return uwrap.libc.symbols._libc_geteuid.f();
537 }
538
539 static int libc_setgid(gid_t gid)
540 {
541         uwrap_bind_symbol_libc(setgid);
542
543         return uwrap.libc.symbols._libc_setgid.f(gid);
544 }
545
546 static gid_t libc_getgid(void)
547 {
548         uwrap_bind_symbol_libc(getgid);
549
550         return uwrap.libc.symbols._libc_getgid.f();
551 }
552
553 #ifdef HAVE_SETEGID
554 static int libc_setegid(gid_t egid)
555 {
556         uwrap_bind_symbol_libc(setegid);
557
558         return uwrap.libc.symbols._libc_setegid.f(egid);
559 }
560 #endif
561
562 #ifdef HAVE_SETREGID
563 static int libc_setregid(gid_t rgid, gid_t egid)
564 {
565         uwrap_bind_symbol_libc(setregid);
566
567         return uwrap.libc.symbols._libc_setregid.f(rgid, egid);
568 }
569 #endif
570
571 #ifdef HAVE_SETRESGID
572 static int libc_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
573 {
574         uwrap_bind_symbol_libc(setresgid);
575
576         return uwrap.libc.symbols._libc_setresgid.f(rgid, egid, sgid);
577 }
578 #endif
579
580 #ifdef HAVE_GETRESGID
581 static int libc_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
582 {
583         uwrap_bind_symbol_libc(setresgid);
584
585         return uwrap.libc.symbols._libc_getresgid.f(rgid, egid, sgid);
586 }
587 #endif
588
589 static gid_t libc_getegid(void)
590 {
591         uwrap_bind_symbol_libc(getegid);
592
593         return uwrap.libc.symbols._libc_getegid.f();
594 }
595
596 static int libc_getgroups(int size, gid_t list[])
597 {
598         uwrap_bind_symbol_libc(getgroups);
599
600         return uwrap.libc.symbols._libc_getgroups.f(size, list);
601 }
602
603 static int libc_setgroups(size_t size, const gid_t *list)
604 {
605         uwrap_bind_symbol_libc(setgroups);
606
607         return uwrap.libc.symbols._libc_setgroups.f(size, list);
608 }
609
610 #ifdef HAVE_SYSCALL
611 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
612 static long int libc_vsyscall(long int sysno, va_list va)
613 {
614         long int args[8];
615         long int rc;
616         int i;
617
618         uwrap_bind_symbol_libc(syscall);
619
620         for (i = 0; i < 8; i++) {
621                 args[i] = va_arg(va, long int);
622         }
623
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]);
633
634         return rc;
635 }
636 #endif
637
638 /*
639  * This part is "optimistic".
640  * Thread can ends without pthread_exit call.
641  */
642 static void libpthread_pthread_exit(void *retval)
643 {
644         uwrap_bind_symbol_libpthread(pthread_exit);
645
646         uwrap.libpthread.symbols._libpthread_pthread_exit.f(retval);
647 }
648
649 static void uwrap_pthread_exit(void *retval)
650 {
651         struct uwrap_thread *id = uwrap_tls_id;
652
653         UWRAP_LOG(UWRAP_LOG_DEBUG, "Cleanup thread");
654
655         UWRAP_LOCK(uwrap_id);
656         if (id == NULL) {
657                 UWRAP_UNLOCK(uwrap_id);
658                 libpthread_pthread_exit(retval);
659                 return;
660         }
661
662         UWRAP_DLIST_REMOVE(uwrap.ids, id);
663         SAFE_FREE(id->groups);
664         SAFE_FREE(id);
665         uwrap_tls_id = NULL;
666
667         UWRAP_UNLOCK(uwrap_id);
668
669         libpthread_pthread_exit(retval);
670 }
671
672 void pthread_exit(void *retval)
673 {
674         if (!uid_wrapper_enabled()) {
675                 libpthread_pthread_exit(retval);
676         };
677
678         uwrap_pthread_exit(retval);
679
680         /* Calm down gcc warning. */
681         exit(666);
682 }
683
684 static int libpthread_pthread_create(pthread_t *thread,
685                                 const pthread_attr_t *attr,
686                                 void *(*start_routine) (void *),
687                                 void *arg)
688 {
689         uwrap_bind_symbol_libpthread(pthread_create);
690         return uwrap.libpthread.symbols._libpthread_pthread_create.f(thread,
691                                                                      attr,
692                                                                      start_routine,
693                                                                      arg);
694 }
695
696 struct uwrap_pthread_create_args {
697         struct uwrap_thread *id;
698         void *(*start_routine) (void *);
699         void *arg;
700 };
701
702 static void *uwrap_pthread_create_start(void *_a)
703 {
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;
709
710         SAFE_FREE(a);
711
712         uwrap_tls_id = id;
713
714         return start_routine(arg);
715 }
716
717 static int uwrap_pthread_create(pthread_t *thread,
718                                  const pthread_attr_t *attr,
719                                  void *(*start_routine) (void *),
720                                  void *arg)
721 {
722         struct uwrap_pthread_create_args *args;
723         struct uwrap_thread *src_id = uwrap_tls_id;
724         int ret;
725
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;
732         }
733         args->start_routine = start_routine;
734         args->arg = arg;
735
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;
743         }
744
745         UWRAP_LOCK(uwrap_id);
746
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;
756         }
757
758         args->id->ruid = src_id->ruid;
759         args->id->euid = src_id->euid;
760         args->id->suid = src_id->suid;
761
762         args->id->rgid = src_id->rgid;
763         args->id->egid = src_id->egid;
764         args->id->sgid = src_id->sgid;
765
766         args->id->enabled = src_id->enabled;
767
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);
774         }
775
776         UWRAP_DLIST_ADD(uwrap.ids, args->id);
777         UWRAP_UNLOCK(uwrap_id);
778
779         ret = libpthread_pthread_create(thread, attr,
780                                         uwrap_pthread_create_start,
781                                         args);
782         if (ret != 0) {
783                 return ret;
784         }
785
786         return ret;
787 }
788
789 int pthread_create(pthread_t *thread,
790                     const pthread_attr_t *attr,
791                     void *(*start_routine) (void *),
792                     void *arg)
793 {
794         if (!uid_wrapper_enabled()) {
795                 return libpthread_pthread_create(thread,
796                                            attr,
797                                            start_routine,
798                                            arg);
799         };
800
801         return uwrap_pthread_create(thread,
802                                     attr,
803                                     start_routine,
804                                     arg);
805 }
806
807 /*********************************************************
808  * UWRAP ID HANDLING
809  *********************************************************/
810
811 static void uwrap_thread_prepare(void)
812 {
813         struct uwrap_thread *id = uwrap_tls_id;
814
815         /* uid_wrapper is loaded but not enabled */
816         if (id == NULL) {
817                 return;
818         }
819
820         UWRAP_LOCK_ALL;
821
822         /*
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.
827          */
828         id->enabled = false;
829 }
830
831 static void uwrap_thread_parent(void)
832 {
833         struct uwrap_thread *id = uwrap_tls_id;
834
835         /* uid_wrapper is loaded but not enabled */
836         if (id == NULL) {
837                 return;
838         }
839
840         id->enabled = true;
841
842         UWRAP_UNLOCK_ALL;
843 }
844
845 static void uwrap_thread_child(void)
846 {
847         struct uwrap_thread *id = uwrap_tls_id;
848         struct uwrap_thread *u = uwrap.ids;
849
850         /* uid_wrapper is loaded but not enabled */
851         if (id == NULL) {
852                 return;
853         }
854
855         /*
856          * "Garbage collector" - Inspired by DESTRUCTOR.
857          * All threads (except one which called fork()) are dead now.. Dave
858          * That's what posix said...
859          */
860         while (u != NULL) {
861                 if (u == id) {
862                         /* Skip this item. */
863                         u = uwrap.ids->next;
864                         continue;
865                 }
866
867                 UWRAP_DLIST_REMOVE(uwrap.ids, u);
868
869                 SAFE_FREE(u->groups);
870                 SAFE_FREE(u);
871
872                 u = uwrap.ids;
873         }
874
875         id->enabled = true;
876
877         UWRAP_UNLOCK_ALL;
878 }
879
880 static void uwrap_init(void)
881 {
882         const char *env;
883
884         UWRAP_LOCK(uwrap_id);
885
886         if (uwrap.initialised) {
887                 struct uwrap_thread *id = uwrap_tls_id;
888
889                 if (uwrap.ids == NULL) {
890                         UWRAP_UNLOCK(uwrap_id);
891                         return;
892                 }
893
894                 if (id == NULL) {
895                         UWRAP_LOG(UWRAP_LOG_ERROR,
896                                   "Invalid id for thread");
897                         exit(-1);
898                 }
899
900                 UWRAP_UNLOCK(uwrap_id);
901                 return;
902         }
903
904         UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize uid_wrapper");
905
906         uwrap.initialised = true;
907
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;
912
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);
918                 }
919
920                 UWRAP_DLIST_ADD(uwrap.ids, id);
921                 uwrap_tls_id = id;
922
923                 uwrap.myuid = libc_geteuid();
924                 uwrap.mygid = libc_getegid();
925
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;
930
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);
936                         }
937
938                         id->ngroups = 1;
939                         id->groups[0] = 0;
940
941                 } else {
942                         id->ruid = id->euid = id->suid = uwrap.myuid;
943                         id->rgid = id->egid = id->sgid = uwrap.mygid;
944
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);
950                         }
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);
955                         }
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;
960                                 /*
961                                  * Deallocation of uwrap.groups is handled by
962                                  * library destructor.
963                                  */
964                                 exit(-1);
965                         }
966                 }
967
968                 id->enabled = true;
969
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);
974         }
975
976         UWRAP_UNLOCK(uwrap_id);
977
978         UWRAP_LOG(UWRAP_LOG_DEBUG, "Succeccfully initialized uid_wrapper");
979 }
980
981 bool uid_wrapper_enabled(void)
982 {
983         struct uwrap_thread *id = uwrap_tls_id;
984         bool enabled;
985
986         if (id == NULL) {
987                 return false;
988         }
989
990         UWRAP_LOCK(uwrap_id);
991         enabled = id->enabled;
992         UWRAP_UNLOCK(uwrap_id);
993
994         return enabled;
995 }
996
997 #ifdef HAVE_GETRESUID
998 static int uwrap_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
999 {
1000         struct uwrap_thread *id = uwrap_tls_id;
1001
1002         UWRAP_LOCK(uwrap_id);
1003
1004         *ruid = id->ruid;
1005         *euid = id->euid;
1006         *suid = id->suid;
1007
1008         UWRAP_UNLOCK(uwrap_id);
1009
1010         return 0;
1011 }
1012 #endif
1013
1014 static int uwrap_setresuid_thread(uid_t ruid, uid_t euid, uid_t suid)
1015 {
1016         struct uwrap_thread *id = uwrap_tls_id;
1017
1018         if (ruid == (uid_t)-1 && euid == (uid_t)-1 && suid == (uid_t)-1) {
1019                 errno = EINVAL;
1020                 return -1;
1021         }
1022
1023         UWRAP_LOCK(uwrap_id);
1024         if (ruid != (uid_t)-1) {
1025                 id->ruid = ruid;
1026         }
1027
1028         if (euid != (uid_t)-1) {
1029                 id->euid = euid;
1030         }
1031
1032         if (suid != (uid_t)-1) {
1033                 id->suid = suid;
1034         }
1035
1036         UWRAP_UNLOCK(uwrap_id);
1037
1038         return 0;
1039 }
1040
1041 #ifdef HAVE_GETRESGID
1042 static int uwrap_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1043 {
1044         struct uwrap_thread *id = uwrap_tls_id;
1045
1046         UWRAP_LOCK(uwrap_id);
1047
1048         *rgid = id->rgid;
1049         *egid = id->egid;
1050         *sgid = id->sgid;
1051
1052         UWRAP_UNLOCK(uwrap_id);
1053
1054         return 0;
1055 }
1056 #endif
1057
1058 static int uwrap_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1059 {
1060         struct uwrap_thread *id;
1061
1062         if (ruid == (uid_t)-1 && euid == (uid_t)-1 && suid == (uid_t)-1) {
1063                 errno = EINVAL;
1064                 return -1;
1065         }
1066
1067         UWRAP_LOCK(uwrap_id);
1068         for (id = uwrap.ids; id; id = id->next) {
1069                 if (ruid != (uid_t)-1) {
1070                         id->ruid = ruid;
1071                 }
1072
1073                 if (euid != (uid_t)-1) {
1074                         id->euid = euid;
1075                 }
1076
1077                 if (suid != (uid_t)-1) {
1078                         id->suid = suid;
1079                 }
1080         }
1081
1082         UWRAP_UNLOCK(uwrap_id);
1083
1084         return 0;
1085 }
1086
1087 /*
1088  * SETUID
1089  */
1090 int setuid(uid_t uid)
1091 {
1092         if (!uid_wrapper_enabled()) {
1093                 return libc_setuid(uid);
1094         }
1095
1096         uwrap_init();
1097         return uwrap_setresuid(uid, -1, -1);
1098 }
1099
1100 #ifdef HAVE_SETEUID
1101 int seteuid(uid_t euid)
1102 {
1103         if (euid == (uid_t)-1) {
1104                 errno = EINVAL;
1105                 return -1;
1106         }
1107
1108         if (!uid_wrapper_enabled()) {
1109                 return libc_seteuid(euid);
1110         }
1111
1112         uwrap_init();
1113         return uwrap_setresuid(-1, euid, -1);
1114 }
1115 #endif
1116
1117 #ifdef HAVE_SETREUID
1118 int setreuid(uid_t ruid, uid_t euid)
1119 {
1120         if (ruid == (uid_t)-1 && euid == (uid_t)-1) {
1121                 errno = EINVAL;
1122                 return -1;
1123         }
1124
1125         if (!uid_wrapper_enabled()) {
1126                 return libc_setreuid(ruid, euid);
1127         }
1128
1129         uwrap_init();
1130         return uwrap_setresuid(ruid, euid, -1);
1131 }
1132 #endif
1133
1134 #ifdef HAVE_SETRESUID
1135 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
1136 {
1137         if (!uid_wrapper_enabled()) {
1138                 return libc_setresuid(ruid, euid, suid);
1139         }
1140
1141         uwrap_init();
1142         return uwrap_setresuid(ruid, euid, suid);
1143 }
1144 #endif
1145
1146 #ifdef HAVE_GETRESUID
1147 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
1148 {
1149         if (!uid_wrapper_enabled()) {
1150                 return libc_getresuid(ruid, euid, suid);
1151         }
1152
1153         uwrap_init();
1154         return uwrap_getresuid(ruid, euid, suid);
1155 }
1156 #endif
1157
1158 /*
1159  * GETUID
1160  */
1161 static uid_t uwrap_getuid(void)
1162 {
1163         struct uwrap_thread *id = uwrap_tls_id;
1164         uid_t uid;
1165
1166         UWRAP_LOCK(uwrap_id);
1167         uid = id->ruid;
1168         UWRAP_UNLOCK(uwrap_id);
1169
1170         return uid;
1171 }
1172
1173 uid_t getuid(void)
1174 {
1175         if (!uid_wrapper_enabled()) {
1176                 return libc_getuid();
1177         }
1178
1179         uwrap_init();
1180         return uwrap_getuid();
1181 }
1182
1183 /*
1184  * GETEUID
1185  */
1186 static uid_t uwrap_geteuid(void)
1187 {
1188         const char *env = getenv("UID_WRAPPER_MYUID");
1189         struct uwrap_thread *id = uwrap_tls_id;
1190         uid_t uid;
1191
1192         UWRAP_LOCK(uwrap_id);
1193         uid = id->euid;
1194         UWRAP_UNLOCK(uwrap_id);
1195
1196         /* Disable root and return myuid */
1197         if (env != NULL && env[0] == '1') {
1198                 uid = uwrap.myuid;
1199         }
1200
1201         return uid;
1202 }
1203
1204 uid_t geteuid(void)
1205 {
1206         if (!uid_wrapper_enabled()) {
1207                 return libc_geteuid();
1208         }
1209
1210         uwrap_init();
1211         return uwrap_geteuid();
1212 }
1213
1214 static int uwrap_setresgid_thread(gid_t rgid, gid_t egid, gid_t sgid)
1215 {
1216         struct uwrap_thread *id = uwrap_tls_id;
1217
1218         if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
1219                 errno = EINVAL;
1220                 return -1;
1221         }
1222
1223         UWRAP_LOCK(uwrap_id);
1224         if (rgid != (gid_t)-1) {
1225                 id->rgid = rgid;
1226         }
1227
1228         if (egid != (gid_t)-1) {
1229                 id->egid = egid;
1230         }
1231
1232         if (sgid != (gid_t)-1) {
1233                 id->sgid = sgid;
1234         }
1235
1236         UWRAP_UNLOCK(uwrap_id);
1237
1238         return 0;
1239 }
1240
1241 static int uwrap_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1242 {
1243         struct uwrap_thread *id;
1244
1245         if (rgid == (gid_t)-1 && egid == (gid_t)-1 && sgid == (gid_t)-1) {
1246                 errno = EINVAL;
1247                 return -1;
1248         }
1249
1250         UWRAP_LOCK(uwrap_id);
1251         for (id = uwrap.ids; id; id = id->next) {
1252                 if (rgid != (gid_t)-1) {
1253                         id->rgid = rgid;
1254                 }
1255
1256                 if (egid != (gid_t)-1) {
1257                         id->egid = egid;
1258                 }
1259
1260                 if (sgid != (gid_t)-1) {
1261                         id->sgid = sgid;
1262                 }
1263         }
1264         UWRAP_UNLOCK(uwrap_id);
1265
1266         return 0;
1267 }
1268
1269 /*
1270  * SETGID
1271  */
1272 int setgid(gid_t gid)
1273 {
1274         if (!uid_wrapper_enabled()) {
1275                 return libc_setgid(gid);
1276         }
1277
1278         uwrap_init();
1279         return uwrap_setresgid(gid, -1, -1);
1280 }
1281
1282 #ifdef HAVE_SETEGID
1283 int setegid(gid_t egid)
1284 {
1285         if (!uid_wrapper_enabled()) {
1286                 return libc_setegid(egid);
1287         }
1288
1289         uwrap_init();
1290         return uwrap_setresgid(-1, egid, -1);
1291 }
1292 #endif
1293
1294 #ifdef HAVE_SETREGID
1295 int setregid(gid_t rgid, gid_t egid)
1296 {
1297         if (!uid_wrapper_enabled()) {
1298                 return libc_setregid(rgid, egid);
1299         }
1300
1301         uwrap_init();
1302         return uwrap_setresgid(rgid, egid, -1);
1303 }
1304 #endif
1305
1306 #ifdef HAVE_SETRESGID
1307 int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1308 {
1309         if (!uid_wrapper_enabled()) {
1310                 return libc_setresgid(rgid, egid, sgid);
1311         }
1312
1313         uwrap_init();
1314         return uwrap_setresgid(rgid, egid, sgid);
1315 }
1316 #endif
1317
1318 #ifdef HAVE_GETRESGID
1319 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
1320 {
1321         if (!uid_wrapper_enabled()) {
1322                 return libc_getresgid(rgid, egid, sgid);
1323         }
1324
1325         uwrap_init();
1326         return uwrap_getresgid(rgid, egid, sgid);
1327 }
1328 #endif
1329
1330 /*
1331  * GETGID
1332  */
1333 static gid_t uwrap_getgid(void)
1334 {
1335         struct uwrap_thread *id = uwrap_tls_id;
1336         gid_t gid;
1337
1338         UWRAP_LOCK(uwrap_id);
1339         gid = id->rgid;
1340         UWRAP_UNLOCK(uwrap_id);
1341
1342         return gid;
1343 }
1344
1345 gid_t getgid(void)
1346 {
1347         if (!uid_wrapper_enabled()) {
1348                 return libc_getgid();
1349         }
1350
1351         uwrap_init();
1352         return uwrap_getgid();
1353 }
1354
1355 /*
1356  * GETEGID
1357  */
1358 static uid_t uwrap_getegid(void)
1359 {
1360         struct uwrap_thread *id = uwrap_tls_id;
1361         gid_t gid;
1362
1363         UWRAP_LOCK(uwrap_id);
1364         gid = id->egid;
1365         UWRAP_UNLOCK(uwrap_id);
1366
1367         return gid;
1368 }
1369
1370 uid_t getegid(void)
1371 {
1372         if (!uid_wrapper_enabled()) {
1373                 return libc_getegid();
1374         }
1375
1376         uwrap_init();
1377         return uwrap_getegid();
1378 }
1379
1380 static int uwrap_setgroups_thread(size_t size, const gid_t *list)
1381 {
1382         struct uwrap_thread *id = uwrap_tls_id;
1383         int rc = -1;
1384
1385         UWRAP_LOCK(uwrap_id);
1386
1387         if (size == 0) {
1388                 SAFE_FREE(id->groups);
1389                 id->ngroups = 0;
1390         } else if (size > 0) {
1391                 gid_t *tmp;
1392
1393                 tmp = realloc(id->groups, sizeof(gid_t) * size);
1394                 if (tmp == NULL) {
1395                         errno = ENOMEM;
1396                         goto out;
1397                 }
1398                 id->groups = tmp;
1399                 id->ngroups = size;
1400                 memcpy(id->groups, list, size * sizeof(gid_t));
1401         }
1402
1403         rc = 0;
1404 out:
1405         UWRAP_UNLOCK(uwrap_id);
1406
1407         return rc;
1408 }
1409
1410 static int uwrap_setgroups(size_t size, const gid_t *list)
1411 {
1412         struct uwrap_thread *id;
1413         int rc = -1;
1414
1415         UWRAP_LOCK(uwrap_id);
1416
1417         if (size == 0) {
1418                 for (id = uwrap.ids; id; id = id->next) {
1419                         SAFE_FREE(id->groups);
1420                         id->ngroups = 0;
1421
1422                 }
1423         } else if (size > 0) {
1424                 gid_t *tmp;
1425
1426                 for (id = uwrap.ids; id; id = id->next) {
1427                         tmp = realloc(id->groups, sizeof(gid_t) * size);
1428                         if (tmp == NULL) {
1429                                 errno = ENOMEM;
1430                                 goto out;
1431                         }
1432                         id->groups = tmp;
1433
1434                         id->ngroups = size;
1435                         memcpy(id->groups, list, size * sizeof(gid_t));
1436                 }
1437         }
1438
1439         rc = 0;
1440 out:
1441         UWRAP_UNLOCK(uwrap_id);
1442
1443         return rc;
1444 }
1445
1446 #ifdef HAVE_SETGROUPS_INT
1447 int setgroups(int size, const gid_t *list)
1448 #else
1449 int setgroups(size_t size, const gid_t *list)
1450 #endif
1451 {
1452         if (!uid_wrapper_enabled()) {
1453                 return libc_setgroups(size, list);
1454         }
1455
1456         uwrap_init();
1457         return uwrap_setgroups(size, list);
1458 }
1459
1460 static int uwrap_getgroups(int size, gid_t *list)
1461 {
1462         struct uwrap_thread *id = uwrap_tls_id;
1463         int ngroups;
1464
1465         UWRAP_LOCK(uwrap_id);
1466         ngroups = id->ngroups;
1467
1468         if (size > ngroups) {
1469                 size = ngroups;
1470         }
1471         if (size == 0) {
1472                 goto out;
1473         }
1474         if (size < ngroups) {
1475                 errno = EINVAL;
1476                 ngroups = -1;
1477         }
1478         memcpy(list, id->groups, size * sizeof(gid_t));
1479
1480 out:
1481         UWRAP_UNLOCK(uwrap_id);
1482
1483         return ngroups;
1484 }
1485
1486 int getgroups(int size, gid_t *list)
1487 {
1488         if (!uid_wrapper_enabled()) {
1489                 return libc_getgroups(size, list);
1490         }
1491
1492         uwrap_init();
1493         return uwrap_getgroups(size, list);
1494 }
1495
1496 #if (defined(HAVE_SYS_SYSCALL_H) || defined(HAVE_SYSCALL_H)) \
1497     && (defined(SYS_setreuid) || defined(SYS_setreuid32))
1498 static long int uwrap_syscall (long int sysno, va_list vp)
1499 {
1500         long int rc;
1501
1502         switch (sysno) {
1503                 /* gid */
1504                 case SYS_getgid:
1505 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1506                 case SYS_getgid32:
1507 #endif
1508                         {
1509                                 rc = uwrap_getgid();
1510                         }
1511                         break;
1512 #ifdef SYS_getegid
1513                 case SYS_getegid:
1514 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1515                 case SYS_getegid32:
1516 #endif
1517                         {
1518                                 rc = uwrap_getegid();
1519                         }
1520                         break;
1521 #endif /* SYS_getegid */
1522                 case SYS_setgid:
1523 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1524                 case SYS_setgid32:
1525 #endif
1526                         {
1527                                 gid_t gid = (gid_t) va_arg(vp, gid_t);
1528
1529                                 rc = uwrap_setresgid_thread(gid, -1, -1);
1530                         }
1531                         break;
1532                 case SYS_setregid:
1533 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1534                 case SYS_setregid32:
1535 #endif
1536                         {
1537                                 gid_t rgid = (gid_t) va_arg(vp, gid_t);
1538                                 gid_t egid = (gid_t) va_arg(vp, gid_t);
1539
1540                                 rc = uwrap_setresgid_thread(rgid, egid, -1);
1541                         }
1542                         break;
1543 #ifdef SYS_setresgid
1544                 case SYS_setresgid:
1545 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1546                 case SYS_setresgid32:
1547 #endif
1548                         {
1549                                 gid_t rgid = (gid_t) va_arg(vp, gid_t);
1550                                 gid_t egid = (gid_t) va_arg(vp, gid_t);
1551                                 gid_t sgid = (gid_t) va_arg(vp, gid_t);
1552
1553                                 rc = uwrap_setresgid_thread(rgid, egid, sgid);
1554                         }
1555                         break;
1556 #endif /* SYS_setresgid */
1557 #if defined(SYS_getresgid) && defined(HAVE_GETRESGID)
1558                 case SYS_getresgid:
1559 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1560                 case SYS_getresgid32:
1561 #endif
1562                         {
1563                                 gid_t *rgid = (gid_t *) va_arg(vp, gid_t *);
1564                                 gid_t *egid = (gid_t *) va_arg(vp, gid_t *);
1565                                 gid_t *sgid = (gid_t *) va_arg(vp, gid_t *);
1566
1567                                 rc = uwrap_getresgid(rgid, egid, sgid);
1568                         }
1569                         break;
1570 #endif /* SYS_getresgid && HAVE_GETRESGID */
1571
1572                 /* uid */
1573                 case SYS_getuid:
1574 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1575                 case SYS_getuid32:
1576 #endif
1577                         {
1578                                 rc = uwrap_getuid();
1579                         }
1580                         break;
1581 #ifdef SYS_geteuid
1582                 case SYS_geteuid:
1583 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1584                 case SYS_geteuid32:
1585 #endif
1586                         {
1587                                 rc = uwrap_geteuid();
1588                         }
1589                         break;
1590 #endif /* SYS_geteuid */
1591                 case SYS_setuid:
1592 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1593                 case SYS_setuid32:
1594 #endif
1595                         {
1596                                 uid_t uid = (uid_t) va_arg(vp, uid_t);
1597
1598                                 rc = uwrap_setresuid_thread(uid, -1, -1);
1599                         }
1600                         break;
1601                 case SYS_setreuid:
1602 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1603                 case SYS_setreuid32:
1604 #endif
1605                         {
1606                                 uid_t ruid = (uid_t) va_arg(vp, uid_t);
1607                                 uid_t euid = (uid_t) va_arg(vp, uid_t);
1608
1609                                 rc = uwrap_setresuid_thread(ruid, euid, -1);
1610                         }
1611                         break;
1612 #ifdef SYS_setresuid
1613                 case SYS_setresuid:
1614 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1615                 case SYS_setresuid32:
1616 #endif
1617                         {
1618                                 uid_t ruid = (uid_t) va_arg(vp, uid_t);
1619                                 uid_t euid = (uid_t) va_arg(vp, uid_t);
1620                                 uid_t suid = (uid_t) va_arg(vp, uid_t);
1621
1622                                 rc = uwrap_setresuid_thread(ruid, euid, suid);
1623                         }
1624                         break;
1625 #endif /* SYS_setresuid */
1626 #if defined(SYS_getresuid) && defined(HAVE_GETRESUID)
1627                 case SYS_getresuid:
1628 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1629                 case SYS_getresuid32:
1630 #endif
1631                         {
1632                                 uid_t *ruid = (uid_t *) va_arg(vp, uid_t *);
1633                                 uid_t *euid = (uid_t *) va_arg(vp, uid_t *);
1634                                 uid_t *suid = (uid_t *) va_arg(vp, uid_t *);
1635
1636                                 rc = uwrap_getresuid(ruid, euid, suid);
1637                         }
1638                         break;
1639 #endif /* SYS_getresuid && HAVE_GETRESUID*/
1640                 /* groups */
1641                 case SYS_setgroups:
1642 #ifdef HAVE_LINUX_32BIT_SYSCALLS
1643                 case SYS_setgroups32:
1644 #endif
1645                         {
1646                                 size_t size = (size_t) va_arg(vp, size_t);
1647                                 gid_t *list = (gid_t *) va_arg(vp, int *);
1648
1649                                 rc = uwrap_setgroups_thread(size, list);
1650                         }
1651                         break;
1652                 default:
1653                         UWRAP_LOG(UWRAP_LOG_DEBUG,
1654                                   "UID_WRAPPER calling non-wrapped syscall %lu",
1655                                   sysno);
1656
1657                         rc = libc_vsyscall(sysno, vp);
1658                         break;
1659         }
1660
1661         return rc;
1662 }
1663
1664 #ifdef HAVE_SYSCALL
1665 #ifdef HAVE_SYSCALL_INT
1666 int syscall (int sysno, ...)
1667 #else
1668 long int syscall (long int sysno, ...)
1669 #endif
1670 {
1671 #ifdef HAVE_SYSCALL_INT
1672         int rc;
1673 #else
1674         long int rc;
1675 #endif
1676         va_list va;
1677
1678         va_start(va, sysno);
1679
1680         if (!uid_wrapper_enabled()) {
1681                 rc = libc_vsyscall(sysno, va);
1682                 va_end(va);
1683                 return rc;
1684         }
1685
1686         uwrap_init();
1687         rc = uwrap_syscall(sysno, va);
1688         va_end(va);
1689
1690         return rc;
1691 }
1692 #endif /* HAVE_SYSCALL */
1693 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
1694
1695 /****************************
1696  * CONSTRUCTOR
1697  ***************************/
1698 void uwrap_constructor(void)
1699 {
1700         /*
1701         * If we hold a lock and the application forks, then the child
1702         * is not able to unlock the mutex and we are in a deadlock.
1703         * This should prevent such deadlocks.
1704         */
1705         pthread_atfork(&uwrap_thread_prepare,
1706                        &uwrap_thread_parent,
1707                        &uwrap_thread_child);
1708
1709         /* Here is safe place to call uwrap_init() and initialize data
1710          * for main process.
1711          */
1712         uwrap_init();
1713 }
1714
1715 /****************************
1716  * DESTRUCTOR
1717  ***************************/
1718
1719 /*
1720  * This function is called when the library is unloaded and makes sure that
1721  * resources are freed.
1722  */
1723 void uwrap_destructor(void)
1724 {
1725         struct uwrap_thread *u = uwrap.ids;
1726
1727         UWRAP_LOCK_ALL;
1728
1729         while (u != NULL) {
1730                 UWRAP_DLIST_REMOVE(uwrap.ids, u);
1731
1732                 SAFE_FREE(u->groups);
1733                 SAFE_FREE(u);
1734
1735                 u = uwrap.ids;
1736         }
1737
1738
1739         if (uwrap.libc.handle != NULL) {
1740                 dlclose(uwrap.libc.handle);
1741         }
1742
1743         if (uwrap.libpthread.handle != NULL) {
1744                 dlclose(uwrap.libpthread.handle);
1745         }
1746
1747         UWRAP_UNLOCK_ALL;
1748 }