[glib/wip/smcv/but-what-if-capabilities: 2/2] tests: Don't assume that unprivileged uids are bound by RLIMIT_NPROC



commit 2b8638561bb8548493e37f7512796d91413b8c18
Author: Simon McVittie <smcv collabora com>
Date:   Thu Feb 13 12:54:46 2020 +0000

    tests: Don't assume that unprivileged uids are bound by RLIMIT_NPROC
    
    Some CI platforms invoke tests as euid != 0, but with capabilities that
    include CAP_SYS_RESOURCE and/or CAP_SYS_ADMIN. If we detect this,
    we can't test what happens if our RLIMIT_NPROC is too low to create a
    thread, because RLIMIT_NPROC is bypassed in these cases.
    
    Signed-off-by: Simon McVittie <smcv collabora com>
    Fixes: https://gitlab.gnome.org/GNOME/glib/issues/2029

 glib/tests/thread.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
---
diff --git a/glib/tests/thread.c b/glib/tests/thread.c
index b9f87967b..579ce1fca 100644
--- a/glib/tests/thread.c
+++ b/glib/tests/thread.c
@@ -138,12 +138,6 @@ test_thread4 (void)
   GError *error;
   gint ret;
 
-  /* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
-   * things are true on other systems.
-   */
-  if (getuid () == 0 || geteuid () == 0)
-    return;
-
   getrlimit (RLIMIT_NPROC, &nl);
   nl.rlim_cur = 1;
 
@@ -152,9 +146,26 @@ test_thread4 (void)
 
   error = NULL;
   thread = g_thread_try_new ("a", thread1_func, NULL, &error);
-  g_assert (thread == NULL);
-  g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
-  g_error_free (error);
+
+  if (thread != NULL)
+    {
+      gpointer result;
+
+      /* Privileged processes might be able to create new threads even
+       * though the rlimit is too low. There isn't much we can do about
+       * this; we just can't test this failure mode in this situation. */
+      g_test_skip ("Unable to test g_thread_try_new() failing with EAGAIN "
+                   "while privileged (CAP_SYS_RESOURCE, CAP_SYS_ADMIN or "
+                   "euid 0?)");
+      result = g_thread_join (thread);
+      g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
+    }
+  else
+    {
+      g_assert (thread == NULL);
+      g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
+      g_error_free (error);
+    }
 
   if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
     g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]