The branch, master has been updated
       via  a9bb877 uwrap: Pass bool values to uwrap_new_id().
       via  8ac9882 uwrap: Fix a memory leak when we reuse an allocated id.
       via  8bcb0ab uwrap: Add a destructor to free resources.
       via  0d0adbf cmake: Add check for HAVE_DESTRUCTOR_ATTRIBUTE.
       via  b20dbe2 uwrap: Use realloc for setgroups().
       via  f7bedda uwrap: Fix loading the system libraries.
      from  623bed5 tests: Add test_uwrap_disabled.

http://gitweb.samba.org/?p=uid_wrapper.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit a9bb87790557a74328b77f423c6b62b3814cd157
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 13:03:04 2013 +0100

    uwrap: Pass bool values to uwrap_new_id().

commit 8ac9882e33c8df7c6c985673495d208265e409dd
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 13:02:37 2013 +0100

    uwrap: Fix a memory leak when we reuse an allocated id.

commit 8bcb0ab1c9db8d03f1bdc998b6765eb2d76a4357
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 13:02:03 2013 +0100

    uwrap: Add a destructor to free resources.

commit 0d0adbf10a5f6359a5f63f611bf5e4b71a801e13
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 13:01:27 2013 +0100

    cmake: Add check for HAVE_DESTRUCTOR_ATTRIBUTE.

commit b20dbe2122988a2aaace30ba010de87af8b5a755
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 12:54:14 2013 +0100

    uwrap: Use realloc for setgroups().

commit f7bedda4695cb84aacf5140e5784758e4db65ef3
Author: Andreas Schneider <[email protected]>
Date:   Fri Dec 20 11:09:15 2013 +0100

    uwrap: Fix loading the system libraries.
    
    Thanks metze!

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

Summary of changes:
 ConfigureChecks.cmake |   11 ++++++
 config.h.cmake        |    1 +
 src/uid_wrapper.c     |   94 ++++++++++++++++++++++++++++++++++++------------
 3 files changed, 82 insertions(+), 24 deletions(-)


Changeset truncated at 500 lines:

diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake
index 77a701f..123128f 100644
--- a/ConfigureChecks.cmake
+++ b/ConfigureChecks.cmake
@@ -131,6 +131,17 @@ int main(void) {
     return 0;
 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
 
+check_c_source_compiles("
+void test_destructor_attribute(void) __attribute__ ((destructor));
+
+void test_destructor_attribute(void)
+{
+    return;
+}
+
+int main(void) {
+    return 0;
+}" HAVE_DESTRUCTOR_ATTRIBUTE)
 
 # SYSTEM LIBRARIES
 
diff --git a/config.h.cmake b/config.h.cmake
index 679390c..0e67b23 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -57,6 +57,7 @@
 #cmakedefine HAVE_LINUX_32BIT_SYSCALLS 1
 
 #cmakedefine HAVE_GCC_THREAD_LOCAL_STORAGE 1
+#cmakedefine HAVE_DESTRUCTOR_ATTRIBUTE 1
 
 /*************************** ENDIAN *****************************/
 
diff --git a/src/uid_wrapper.c b/src/uid_wrapper.c
index ff2639f..077bef7 100644
--- a/src/uid_wrapper.c
+++ b/src/uid_wrapper.c
@@ -43,6 +43,12 @@
 # define UWRAP_THREAD
 #endif
 
+#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
+#define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
+#else
+#define DESTRUCTOR_ATTRIBUTE
+#endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
+
 #ifdef NDEBUG
 #define UWRAP_DEBUG(...)
 #else
@@ -80,6 +86,10 @@
        (item)->next    = NULL; \
 } while (0)
 
+#ifndef SAFE_FREE
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
+#endif
+
 #define LIBC_NAME "libc.so"
 
 struct uwrap_libc_fns {
@@ -162,6 +172,12 @@ static UWRAP_THREAD struct uwrap_thread *uwrap_tls_id;
 static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /*********************************************************
+ * UWRAP PROTOTYPES
+ *********************************************************/
+
+void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
+
+/*********************************************************
  * UWRAP LIBC LOADER FUNCTIONS
  *********************************************************/
 
@@ -191,6 +207,7 @@ static void *uwrap_load_lib_handle(enum uwrap_lib lib)
        case UWRAP_LIBSOCKET:
                /* FALL TROUGH */
        case UWRAP_LIBC:
+               handle = uwrap.libc.handle;
                if (handle == NULL) {
                        for (handle = NULL, i = 10; handle == NULL && i >= 0; 
i--) {
                                char soname[256] = {0};
@@ -200,8 +217,6 @@ static void *uwrap_load_lib_handle(enum uwrap_lib lib)
                        }
 
                        uwrap.libc.handle = handle;
-               } else {
-                       handle = uwrap.libc.handle;
                }
                break;
        }
@@ -412,6 +427,15 @@ static int uwrap_new_id(pthread_t tid, bool do_alloc)
                        errno = ENOMEM;
                        return -1;
                }
+
+               id->groups = malloc(sizeof(gid_t) * 1);
+               if (id->groups == NULL) {
+                       errno = ENOMEM;
+                       return -1;
+               }
+
+               UWRAP_DLIST_ADD(uwrap.ids, id);
+               uwrap_tls_id = id;
        }
 
        id->tid = tid;
@@ -421,14 +445,8 @@ static int uwrap_new_id(pthread_t tid, bool do_alloc)
        id->rgid = id->egid = id->sgid = uwrap.mygid;
 
        id->ngroups = 1;
-       id->groups = malloc(sizeof(gid_t) * id->ngroups);
        id->groups[0] = uwrap.mygid;
 
-       if (do_alloc) {
-               UWRAP_DLIST_ADD(uwrap.ids, id);
-               uwrap_tls_id = id;
-       }
-
        return 0;
 }
 
@@ -476,7 +494,7 @@ static void uwrap_init(void)
                pthread_mutex_lock(&uwrap_id_mutex);
                id = find_uwrap_id(tid);
                if (id == NULL) {
-                       rc = uwrap_new_id(tid, 1);
+                       rc = uwrap_new_id(tid, true);
                        if (rc < 0) {
                                exit(-1);
                        }
@@ -484,7 +502,7 @@ static void uwrap_init(void)
                        /* We reuse an old thread id */
                        uwrap_tls_id = id;
 
-                       uwrap_new_id(tid, 0);
+                       uwrap_new_id(tid, false);
                }
                pthread_mutex_unlock(&uwrap_id_mutex);
 
@@ -518,7 +536,7 @@ static void uwrap_init(void)
                        uwrap.mygid = libc_getegid();
                }
 
-               rc = uwrap_new_id(tid, 1);
+               rc = uwrap_new_id(tid, true);
                if (rc < 0) {
                        exit(-1);
                }
@@ -860,16 +878,17 @@ static int uwrap_setgroups_thread(size_t size, const 
gid_t *list)
        int rc = -1;
 
        pthread_mutex_lock(&uwrap_id_mutex);
-       free(id->groups);
-       id->groups = NULL;
-       id->ngroups = 0;
 
-       if (size != 0) {
-               id->groups = malloc(sizeof(gid_t) * size);
-               if (id->groups == NULL) {
+       if (size > 0) {
+               gid_t *tmp;
+
+               tmp = realloc(id->groups, sizeof(gid_t) * size);
+               if (tmp == NULL) {
                        errno = ENOMEM;
                        goto out;
                }
+               id->groups = tmp;
+
                id->ngroups = size;
                memcpy(id->groups, list, size * sizeof(gid_t));
        }
@@ -887,17 +906,18 @@ static int uwrap_setgroups(size_t size, const gid_t *list)
        int rc = -1;
 
        pthread_mutex_lock(&uwrap_id_mutex);
-       for (id = uwrap.ids; id; id = id->next) {
-               free(id->groups);
-               id->groups = NULL;
-               id->ngroups = 0;
 
-               if (size != 0) {
-                       id->groups = malloc(sizeof(gid_t) * size);
-                       if (id->groups == NULL) {
+       if (size > 0) {
+               for (id = uwrap.ids; id; id = id->next) {
+                       gid_t *tmp;
+
+                       tmp = realloc(id->groups, sizeof(gid_t) * size);
+                       if (tmp == NULL) {
                                errno = ENOMEM;
                                goto out;
                        }
+                       id->groups = tmp;
+
                        id->ngroups = size;
                        memcpy(id->groups, list, size * sizeof(gid_t));
                }
@@ -1127,3 +1147,29 @@ long int syscall (long int sysno, ...)
 }
 #endif /* HAVE_SYSCALL */
 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
+
+/****************************
+ * DESTRUCTOR
+ ***************************/
+
+/*
+ * This function is called when the library is unloaded and makes sure that
+ * resources are freed.
+ */
+void uwrap_destructor(void)
+{
+       struct uwrap_thread *u = uwrap.ids;
+
+       while (u != NULL) {
+               UWRAP_DLIST_REMOVE(uwrap.ids, u);
+
+               SAFE_FREE(u->groups);
+               SAFE_FREE(u);
+
+               u = uwrap.ids;
+       }
+
+       if (uwrap.libc.handle != NULL) {
+               dlclose(uwrap.libc.handle);
+       }
+}


-- 
UID Wrapper Repository

Reply via email to