[SCM] Samba Shared Repository - branch master updated

Andreas Schneider asn at samba.org
Wed Jan 28 11:45:03 MST 2015


The branch, master has been updated
       via  62671ad7 uwrap: Bump version to 1.1.0.
       via  0588a58 uwrap: Make sure we leave if the id is NULL.
       via  99a9855 uwrap: Support scenario where threads fork or creates threads.
       via  a5b70c0 uwrap: Prepare for overload of libpthread functions.
       via  526c1d5 uwrap: Introduce UWRAP_LOCK_ALL and UWRAP_UNLOCK_ALL macros
       via  308230d uwrap: Rewrite uwrap_libc_fns struct to pass strict aliasing rules.
       via  2fb08a6 uwrap: Fix wrong data types in syscalls switch.
       via  34062ac uwrap: Add support for getresuid() and getresgid() glibc/syscall.
       via  fbdd2d4 uwrap: Extend support for (set|get)groups libc functions and syscalls.
       via  c6a1e60 uwrap: Extend support for syscalls called from threads or main process.
       via  e1e067f uwrap: Small uwrap_init optimalization.
       via  fa8290e uwrap: Add support for running with address sanitizer.
       via  50b6d94 uwrap: Reflect changes of uid/gid in threads to main process.
       via  bda49a3 uwrap: Small optimalization of uwrap_init().
       via  0830d93 uwrap: Optimalization of uid_wrapper_enabled() function.
       via  b21106a uid_wrapper: Fix race condition - uwrap_init.
       via  199b7b7 uwrap: Fix race condition - glibc lookups.
       via  a79b5cf uwrap: Add library constructor and move pthread_atfork inside.
       via  e279eee uwrap: Use UWRAP_LOCK/UNLOCK macros instead of pthread_mutex_lock/unlock calls.
       via  6ca1cf2 uwrap: Fix the handle loop for older gcc versions.
       via  cf68c1a waf: Add address sanitizer configure option.
      from  ab51f28 ctdb-scripts: Call iptables/ip6tables directly from iptables_wrapper

https://git.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 62671ad7b2985912410a5ae5ed7de2fa3b672e69
Author: Andreas Schneider <asn at samba.org>
Date:   Fri Jan 23 15:32:05 2015 +0100

    uwrap: Bump version to 1.1.0.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>
    
    Autobuild-User(master): Andreas Schneider <asn at cryptomilk.org>
    Autobuild-Date(master): Wed Jan 28 19:44:39 CET 2015 on sn-devel-104

commit 0588a5847a2f2b3786ea6033aae4c652746b0700
Author: Andreas Schneider <asn at samba.org>
Date:   Fri Jan 23 15:31:17 2015 +0100

    uwrap: Make sure we leave if the id is NULL.
    
    CID #97616
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 99a9855d38d8bfaec5bd03b8aaeca01cefa22b00
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:30:30 2015 +0100

    uwrap: Support scenario where threads fork or creates threads.
    
    When fork() is called here there is no need to disable uwrap as a whole.
    This change disables only uwrap for the thread which called fork().
    
    uwrap catches calls of pthread_create() and pthread_exit() functions
    from libpthread library now.
    
    Pair-Programmed-With: Andreas Schneider <asn at samba.org>
    Pair-Programmed-With: Stefan Metzmacher <metze at samba.org>
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit a5b70c0e5fe70fe69e5294ddeadd8981dda9afb0
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:30:03 2015 +0100

    uwrap: Prepare for overload of libpthread functions.
    
    uwrap_bind_symbol are now renamed to uwrap_bind_symbol_libc
    and simlilar uwrap_bind_symbol_libpthread are introduced.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 526c1d514740956c9a9f6d83f99ceeca476130a8
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:29:35 2015 +0100

    uwrap: Introduce UWRAP_LOCK_ALL and UWRAP_UNLOCK_ALL macros
    
    Introduce UWRAP_LOCK_ALL and UWRAP_UNLOCK_ALL which make
    locking easier.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 308230d4fcb90822b81578624504d74ad2fbeab9
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:28:37 2015 +0100

    uwrap: Rewrite uwrap_libc_fns struct to pass strict aliasing rules.
    
    Also rename struct uwrap_libc_fns fns to uwrap_libc_symbols and
    uwrap_load_lib_function to uwrap_bind_symbol (same for _uwrap_load_...
    variant.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 2fb08a6750452ee5e5b95cfea7c8829f8125bfed
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:28:00 2015 +0100

    uwrap: Fix wrong data types in syscalls switch.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 34062ac77a0aab77f0ab427e34d47ce418e78259
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:27:25 2015 +0100

    uwrap: Add support for getresuid() and getresgid() glibc/syscall.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit fbdd2d46249235aac3adaa9f20b707d6aedb0bfc
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:25:16 2015 +0100

    uwrap: Extend support for (set|get)groups libc functions and syscalls.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit c6a1e6058eeb9234f7bd41124cf4532288e8b776
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:24:39 2015 +0100

    uwrap: Extend support for syscalls called from threads or main process.
    
    We need to distinguish if the syscall is called from main process or
    from a thread.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit e1e067f332ff5f4082b5dafdeecd2aa8c6a5b526
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:24:04 2015 +0100

    uwrap: Small uwrap_init optimalization.
    
    Don't call libc_getuid/getgid function twice.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit fa8290e7b9c3981dad94894e492991d7b9e8b3a9
Author: Andreas Schneider <asn at samba.org>
Date:   Fri Jan 23 15:22:18 2015 +0100

    uwrap: Add support for running with address sanitizer.
    
    The address sanitzer will complain about our hack with variable function
    attributes. This disables the checking of it.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 50b6d948671850283d1d8e05c59758c8062c6e14
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:16:34 2015 +0100

    uwrap: Reflect changes of uid/gid in threads to main process.
    
    When thread changes uid/gid this change must be reflected to main
    process.
    
    Syscalls changes only uid/gid of thread. Call of libc functions changes
    also uid/gid of main process.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit bda49a3af43b6dd3447422121f78709397234109
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:15:42 2015 +0100

    uwrap: Small optimalization of uwrap_init().
    
    Don't call getenv("UID_WRAPPER") on start of uwrap_init().
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 0830d93eece7e0d132c1513c195d7735cf3421da
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:15:04 2015 +0100

    uwrap: Optimalization of uid_wrapper_enabled() function.
    
    Check only bool variable inside uwrap structure instead
    of calling whole uid_init().
    
    In the best case only one mutex lock is need when check.
    
    NOTES:
     * This patch uses __atomic_load gcc builtin function.
     * uid_init() were moved outside uid_wrapper_enabled() function.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit b21106a1a1c940151d2e56608aea35dab714b773
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:12:43 2015 +0100

    uid_wrapper: Fix race condition - uwrap_init.
    
    Patch moves uwrap_id_mutex before if (uwrap.initialised) statement
    which can be passed by concurrent threads.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 199b7b7fe87b32daa599b61c595d3148e407861d
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:12:02 2015 +0100

    uwrap: Fix race condition - glibc lookups.
    
    Patch adds libc_symbol_binding_mutex which guards global table of libc
    functions and their lookup.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit a79b5cf41c49a36a88d4ba486171699668a5357a
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 15:10:02 2015 +0100

    uwrap: Add library constructor and move pthread_atfork inside.
    
    Library constructor is used for pthread_atfork call. Moved here because
    pthread_atfork is cumulative and should be called only once.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit e279eee670310530eebe5dcfa3fc16b54d0356f2
Author: Robin Hack <hack.robin at gmail.com>
Date:   Fri Jan 23 14:00:49 2015 +0100

    uwrap: Use UWRAP_LOCK/UNLOCK macros instead of pthread_mutex_lock/unlock calls.
    
    New macros UWRAP_LOCK/UNLOCK has been created and all calls to
    pthread_mutex_lock/unlock has been replaced by these macros.
    
    Signed-off-by: Robin Hack <hack.robin at gmail.com>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

commit 6ca1cf208e0cb0af2153e9d559592730d5acb74c
Author: Andreas Schneider <asn at samba.org>
Date:   Fri Jan 23 13:59:14 2015 +0100

    uwrap: Fix the handle loop for older gcc versions.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Michael Adam <obnox at samba.org>

commit cf68c1accb50f4d8449e993c630559a3d3e9dd36
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Jan 26 16:16:15 2015 +0100

    waf: Add address sanitizer configure option.
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Stefan Metzmacher <metze at samba.org>

-----------------------------------------------------------------------

Summary of changes:
 buildtools/wafsamba/samba_autoconf.py |   5 +
 buildtools/wafsamba/wscript           |   3 +
 lib/uid_wrapper/uid_wrapper.c         | 854 ++++++++++++++++++++++++++--------
 lib/uid_wrapper/wscript               |  54 ++-
 4 files changed, 721 insertions(+), 195 deletions(-)


Changeset truncated at 500 lines:

diff --git a/buildtools/wafsamba/samba_autoconf.py b/buildtools/wafsamba/samba_autoconf.py
index 8d84a5e..c13bfe7 100644
--- a/buildtools/wafsamba/samba_autoconf.py
+++ b/buildtools/wafsamba/samba_autoconf.py
@@ -703,6 +703,11 @@ int main(void) {
     if Options.options.pedantic:
         conf.ADD_CFLAGS('-W', testflags=True)
 
+    if Options.options.address_sanitizer:
+        conf.ADD_CFLAGS('-fno-omit-frame-pointer -O1 -fsanitize=address', testflags=True)
+        conf.ADD_LDFLAGS('-fsanitize=address', testflags=True)
+        conf.env['ADDRESS_SANITIZER'] = True
+
 
     # Let people pass an additional ADDITIONAL_{CFLAGS,LDFLAGS}
     # environment variables which are only used the for final build.
diff --git a/buildtools/wafsamba/wscript b/buildtools/wafsamba/wscript
index c81a7b3..86224d4 100755
--- a/buildtools/wafsamba/wscript
+++ b/buildtools/wafsamba/wscript
@@ -117,6 +117,9 @@ def set_options(opt):
     gr.add_option('--git-local-changes',
 		   help=("mark version with + if local git changes"),
 		   action='store_true', dest='GIT_LOCAL_CHANGES', default=False)
+    gr.add_option('--address-sanitizer',
+                   help=("Enable address sanitizer compile and liker flags"),
+                   action="store_true", dest='address_sanitizer', default=False)
 
     gr.add_option('--abi-check',
 		   help=("Check ABI signatures for libraries"),
diff --git a/lib/uid_wrapper/uid_wrapper.c b/lib/uid_wrapper/uid_wrapper.c
index 2181767..1d49a85 100644
--- a/lib/uid_wrapper/uid_wrapper.c
+++ b/lib/uid_wrapper/uid_wrapper.c
@@ -43,12 +43,43 @@
 # define UWRAP_THREAD
 #endif
 
+# define UWRAP_LOCK(m) do { \
+	pthread_mutex_lock(&( m ## _mutex)); \
+} while(0)
+
+# define UWRAP_UNLOCK(m) do { \
+	pthread_mutex_unlock(&( m ## _mutex)); \
+} while(0)
+
+/* Add new global locks here please */
+# define UWRAP_LOCK_ALL \
+	UWRAP_LOCK(uwrap_id); \
+	UWRAP_LOCK(libc_symbol_binding); \
+	UWRAP_LOCK(libpthread_symbol_binding)
+
+# define UWRAP_UNLOCK_ALL \
+	UWRAP_UNLOCK(libpthread_symbol_binding); \
+	UWRAP_UNLOCK(libc_symbol_binding); \
+	UWRAP_UNLOCK(uwrap_id)
+
+#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
+#define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
+#else
+#define CONSTRUCTOR_ATTRIBUTE
+#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
+
 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
 #else
 #define DESTRUCTOR_ATTRIBUTE
 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
 
+#ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
+#define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
+#else /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
+#define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
+#endif /* DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE */
+
 /* GCC have printf type attribute check. */
 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
@@ -157,46 +188,130 @@ static void uwrap_log(enum uwrap_dbglvl_e dbglvl, const char *format, ...)
 
 #define LIBC_NAME "libc.so"
 
-struct uwrap_libc_fns {
-	int (*_libc_setuid)(uid_t uid);
-	uid_t (*_libc_getuid)(void);
+typedef int (*__libc_setuid)(uid_t uid);
+
+typedef	uid_t (*__libc_getuid)(void);
 
 #ifdef HAVE_SETEUID
-	int (*_libc_seteuid)(uid_t euid);
+typedef int (*__libc_seteuid)(uid_t euid);
 #endif
+
 #ifdef HAVE_SETREUID
-	int (*_libc_setreuid)(uid_t ruid, uid_t euid);
+typedef int (*__libc_setreuid)(uid_t ruid, uid_t euid);
 #endif
+
 #ifdef HAVE_SETRESUID
-	int (*_libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
+typedef int (*__libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
+#endif
+
+#ifdef HAVE_GETRESUID
+typedef int (*__libc_getresuid)(uid_t *ruid, uid_t *euid, uid_t *suid);
 #endif
-	uid_t (*_libc_geteuid)(void);
 
-	int (*_libc_setgid)(gid_t gid);
-	gid_t (*_libc_getgid)(void);
+typedef uid_t (*__libc_geteuid)(void);
+
+typedef int (*__libc_setgid)(gid_t gid);
+
+typedef gid_t (*__libc_getgid)(void);
+
 #ifdef HAVE_SETEGID
-	int (*_libc_setegid)(uid_t egid);
+typedef int (*__libc_setegid)(uid_t egid);
 #endif
+
 #ifdef HAVE_SETREGID
-	int (*_libc_setregid)(uid_t rgid, uid_t egid);
+typedef int (*__libc_setregid)(uid_t rgid, uid_t egid);
 #endif
+
 #ifdef HAVE_SETRESGID
-	int (*_libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
+typedef int (*__libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
 #endif
-	gid_t (*_libc_getegid)(void);
-	int (*_libc_getgroups)(int size, gid_t list[]);
-	int (*_libc_setgroups)(size_t size, const gid_t *list);
+
+#ifdef HAVE_GETRESGID
+typedef int (*__libc_getresgid)(gid_t *rgid, gid_t *egid, gid_t *sgid);
+#endif
+
+typedef gid_t (*__libc_getegid)(void);
+
+typedef int (*__libc_getgroups)(int size, gid_t list[]);
+
+typedef int (*__libc_setgroups)(size_t size, const gid_t *list);
+
 #ifdef HAVE_SYSCALL
-	long int (*_libc_syscall)(long int sysno, ...);
+typedef long int (*__libc_syscall)(long int sysno, ...);
+#endif
+
+#define UWRAP_SYMBOL_ENTRY(i) \
+	union { \
+		__libc_##i f; \
+		void *obj; \
+	} _libc_##i
+
+struct uwrap_libc_symbols {
+	UWRAP_SYMBOL_ENTRY(setuid);
+	UWRAP_SYMBOL_ENTRY(getuid);
+#ifdef HAVE_SETEUID
+	UWRAP_SYMBOL_ENTRY(seteuid);
+#endif
+#ifdef HAVE_SETREUID
+	UWRAP_SYMBOL_ENTRY(setreuid);
+#endif
+#ifdef HAVE_SETRESUID
+	UWRAP_SYMBOL_ENTRY(setresuid);
 #endif
+#ifdef HAVE_GETRESUID
+	UWRAP_SYMBOL_ENTRY(getresuid);
+#endif
+	UWRAP_SYMBOL_ENTRY(geteuid);
+	UWRAP_SYMBOL_ENTRY(setgid);
+	UWRAP_SYMBOL_ENTRY(getgid);
+#ifdef HAVE_SETEGID
+	UWRAP_SYMBOL_ENTRY(setegid);
+#endif
+#ifdef HAVE_SETREGID
+	UWRAP_SYMBOL_ENTRY(setregid);
+#endif
+#ifdef HAVE_SETRESGID
+	UWRAP_SYMBOL_ENTRY(setresgid);
+#endif
+#ifdef HAVE_GETRESGID
+	UWRAP_SYMBOL_ENTRY(getresgid);
+#endif
+	UWRAP_SYMBOL_ENTRY(getegid);
+	UWRAP_SYMBOL_ENTRY(getgroups);
+	UWRAP_SYMBOL_ENTRY(setgroups);
+#ifdef HAVE_SYSCALL
+	UWRAP_SYMBOL_ENTRY(syscall);
+#endif
+};
+#undef UWRAP_SYMBOL_ENTRY
+
+/*****************
+ * LIBPTHREAD
+ *****************/
+/* Yeah... I'm pig. I overloading macro here... So what? */
+#define UWRAP_SYMBOL_ENTRY(i) \
+	union { \
+		__libpthread_##i f; \
+		void *obj; \
+	} _libpthread_##i
+
+typedef int (*__libpthread_pthread_create)(pthread_t *thread,
+				    const pthread_attr_t *attr,
+				    void *(*start_routine) (void *),
+				    void *arg);
+typedef void (*__libpthread_pthread_exit)(void *retval);
+
+struct uwrap_libpthread_symbols {
+	UWRAP_SYMBOL_ENTRY(pthread_create);
+	UWRAP_SYMBOL_ENTRY(pthread_exit);
 };
+#undef UWRAP_SYMBOL_ENTRY
 
 /*
  * We keep the virtualised euid/egid/groups information here
  */
 struct uwrap_thread {
-	pthread_t tid;
-	bool dead;
+	bool enabled;
 
 	uid_t ruid;
 	uid_t euid;
@@ -206,8 +321,8 @@ struct uwrap_thread {
 	gid_t egid;
 	gid_t sgid;
 
-	gid_t *groups;
 	int ngroups;
+	gid_t *groups;
 
 	struct uwrap_thread *next;
 	struct uwrap_thread *prev;
@@ -216,14 +331,19 @@ struct uwrap_thread {
 struct uwrap {
 	struct {
 		void *handle;
-		struct uwrap_libc_fns fns;
+		struct uwrap_libc_symbols symbols;
 	} libc;
 
+	struct {
+		void *handle;
+		struct uwrap_libpthread_symbols symbols;
+	} libpthread;
+
 	bool initialised;
-	bool enabled;
 
+	/* Real uid and gid of user who run uid wrapper */
 	uid_t myuid;
-	uid_t mygid;
+	gid_t mygid;
 
 	struct uwrap_thread *ids;
 };
@@ -236,11 +356,18 @@ static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
 /* The mutex or accessing the id */
 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+/* The mutex for accessing the global libc.symbols */
+static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+/* The mutex for accessing the global libpthread.symbols */
+static pthread_mutex_t libpthread_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 /*********************************************************
  * UWRAP PROTOTYPES
  *********************************************************/
 
 bool uid_wrapper_enabled(void);
+void uwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
 void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
 /*********************************************************
@@ -251,6 +378,7 @@ enum uwrap_lib {
     UWRAP_LIBC,
     UWRAP_LIBNSL,
     UWRAP_LIBSOCKET,
+    UWRAP_LIBPTHREAD,
 };
 
 static void *uwrap_load_lib_handle(enum uwrap_lib lib)
@@ -271,16 +399,28 @@ static void *uwrap_load_lib_handle(enum uwrap_lib lib)
 	case UWRAP_LIBC:
 		handle = uwrap.libc.handle;
 		if (handle == NULL) {
-			for (handle = NULL, i = 10; handle == NULL && i >= 0; i--) {
+			for (i = 10; i >= 0; i--) {
 				char soname[256] = {0};
 
 				snprintf(soname, sizeof(soname), "libc.so.%d", i);
 				handle = dlopen(soname, flags);
+				if (handle != NULL) {
+					break;
+				}
 			}
 
 			uwrap.libc.handle = handle;
 		}
 		break;
+	case UWRAP_LIBPTHREAD:
+		handle = uwrap.libpthread.handle;
+		if (handle == NULL) {
+			handle = dlopen("libpthread.so.0", flags);
+			if (handle != NULL) {
+				break;
+			}
+		}
+		break;
 	}
 
 	if (handle == NULL) {
@@ -297,7 +437,7 @@ static void *uwrap_load_lib_handle(enum uwrap_lib lib)
 	return handle;
 }
 
-static void *_uwrap_load_lib_function(enum uwrap_lib lib, const char *fn_name)
+static void *_uwrap_bind_symbol(enum uwrap_lib lib, const char *fn_name)
 {
 	void *handle;
 	void *func;
@@ -315,11 +455,21 @@ static void *_uwrap_load_lib_function(enum uwrap_lib lib, const char *fn_name)
 	return func;
 }
 
-#define uwrap_load_lib_function(lib, fn_name) \
-	if (uwrap.libc.fns._libc_##fn_name == NULL) { \
-		*(void **) (&uwrap.libc.fns._libc_##fn_name) = \
-			_uwrap_load_lib_function(lib, #fn_name); \
-	}
+#define uwrap_bind_symbol_libc(sym_name) \
+	UWRAP_LOCK(libc_symbol_binding); \
+	if (uwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
+		uwrap.libc.symbols._libc_##sym_name.obj = \
+			_uwrap_bind_symbol(UWRAP_LIBC, #sym_name); \
+	} \
+	UWRAP_UNLOCK(libc_symbol_binding)
+
+#define uwrap_bind_symbol_libpthread(sym_name) \
+	UWRAP_LOCK(libpthread_symbol_binding); \
+	if (uwrap.libpthread.symbols._libpthread_##sym_name.obj == NULL) { \
+		uwrap.libpthread.symbols._libpthread_##sym_name.obj = \
+			_uwrap_bind_symbol(UWRAP_LIBPTHREAD, #sym_name); \
+	} \
+	UWRAP_UNLOCK(libpthread_symbol_binding)
 
 /*
  * IMPORTANT
@@ -331,128 +481,147 @@ static void *_uwrap_load_lib_function(enum uwrap_lib lib, const char *fn_name)
  */
 static int libc_setuid(uid_t uid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setuid);
+	uwrap_bind_symbol_libc(setuid);
 
-	return uwrap.libc.fns._libc_setuid(uid);
+	return uwrap.libc.symbols._libc_setuid.f(uid);
 }
 
 static uid_t libc_getuid(void)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, getuid);
+	uwrap_bind_symbol_libc(getuid);
 
-	return uwrap.libc.fns._libc_getuid();
+	return uwrap.libc.symbols._libc_getuid.f();
 }
 
 #ifdef HAVE_SETEUID
 static int libc_seteuid(uid_t euid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, seteuid);
+	uwrap_bind_symbol_libc(seteuid);
 
-	return uwrap.libc.fns._libc_seteuid(euid);
+	return uwrap.libc.symbols._libc_seteuid.f(euid);
 }
 #endif
 
 #ifdef HAVE_SETREUID
 static int libc_setreuid(uid_t ruid, uid_t euid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setreuid);
+	uwrap_bind_symbol_libc(setreuid);
 
-	return uwrap.libc.fns._libc_setreuid(ruid, euid);
+	return uwrap.libc.symbols._libc_setreuid.f(ruid, euid);
 }
 #endif
 
 #ifdef HAVE_SETRESUID
 static int libc_setresuid(uid_t ruid, uid_t euid, uid_t suid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setresuid);
+	uwrap_bind_symbol_libc(setresuid);
 
-	return uwrap.libc.fns._libc_setresuid(ruid, euid, suid);
+	return uwrap.libc.symbols._libc_setresuid.f(ruid, euid, suid);
+}
+#endif
+
+#ifdef HAVE_GETRESUID
+static int libc_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
+{
+	uwrap_bind_symbol_libc(getresuid);
+
+	return uwrap.libc.symbols._libc_getresuid.f(ruid, euid, suid);
 }
 #endif
 
 static uid_t libc_geteuid(void)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, geteuid);
+	uwrap_bind_symbol_libc(geteuid);
 
-	return uwrap.libc.fns._libc_geteuid();
+	return uwrap.libc.symbols._libc_geteuid.f();
 }
 
 static int libc_setgid(gid_t gid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setgid);
+	uwrap_bind_symbol_libc(setgid);
 
-	return uwrap.libc.fns._libc_setgid(gid);
+	return uwrap.libc.symbols._libc_setgid.f(gid);
 }
 
 static gid_t libc_getgid(void)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, getgid);
+	uwrap_bind_symbol_libc(getgid);
 
-	return uwrap.libc.fns._libc_getgid();
+	return uwrap.libc.symbols._libc_getgid.f();
 }
 
 #ifdef HAVE_SETEGID
 static int libc_setegid(gid_t egid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setegid);
+	uwrap_bind_symbol_libc(setegid);
 
-	return uwrap.libc.fns._libc_setegid(egid);
+	return uwrap.libc.symbols._libc_setegid.f(egid);
 }
 #endif
 
 #ifdef HAVE_SETREGID
 static int libc_setregid(gid_t rgid, gid_t egid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setregid);
+	uwrap_bind_symbol_libc(setregid);
 
-	return uwrap.libc.fns._libc_setregid(rgid, egid);
+	return uwrap.libc.symbols._libc_setregid.f(rgid, egid);
 }
 #endif
 
 #ifdef HAVE_SETRESGID
 static int libc_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, setresgid);
+	uwrap_bind_symbol_libc(setresgid);
+
+	return uwrap.libc.symbols._libc_setresgid.f(rgid, egid, sgid);
+}
+#endif
+
+#ifdef HAVE_GETRESGID
+static int libc_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
+{
+	uwrap_bind_symbol_libc(setresgid);
 
-	return uwrap.libc.fns._libc_setresgid(rgid, egid, sgid);
+	return uwrap.libc.symbols._libc_getresgid.f(rgid, egid, sgid);
 }
 #endif
 
 static gid_t libc_getegid(void)
 {
-	uwrap_load_lib_function(UWRAP_LIBC, getegid);
+	uwrap_bind_symbol_libc(getegid);
 
-	return uwrap.libc.fns._libc_getegid();
+	return uwrap.libc.symbols._libc_getegid.f();
 }
 
 static int libc_getgroups(int size, gid_t list[])
 {
-	uwrap_load_lib_function(UWRAP_LIBC, getgroups);
+	uwrap_bind_symbol_libc(getgroups);
 
-	return uwrap.libc.fns._libc_getgroups(size, list);
+	return uwrap.libc.symbols._libc_getgroups.f(size, list);


-- 
Samba Shared Repository


More information about the samba-cvs mailing list