ngompa / rpms / sudo

Forked from rpms/sudo 2 years ago
Clone
72a5571
 changeset 12288:1064b906ca68
72a5571
72a5571
Ignore a failure to restore the RLIMIT_CORE resource limit.
72a5571
Linux containers don't allow RLIMIT_CORE to be set back to RLIM_INFINITY
72a5571
if we set the limit to zero, even for root.  This is not a problem
72a5571
outside the container.
72a5571
author 	Todd C. Miller <Todd.Miller@sudo.ws>
72a5571
date 	Sat, 14 Mar 2020 11:13:55 -0600
72a5571
parents 	72ca06a294b4
72a5571
children 	40629e6fd692
72a5571
files 	src/limits.c
72a5571
diffstat 	1 files changed, 61 insertions(+), 10 deletions(-) [+]
72a5571
line wrap: on
72a5571
 line diff
72a5571
72a5571
--- a/src/limits.c	Thu Mar 12 17:39:56 2020 -0600
72a5571
+++ b/src/limits.c	Sat Mar 14 11:13:55 2020 -0600
72a5571
@@ -114,13 +114,21 @@
72a5571
72a5571
     if (getrlimit(RLIMIT_CORE, &corelimit) == -1)
72a5571
 	sudo_warn("getrlimit(RLIMIT_CORE)");
72a5571
+    sudo_debug_printf(SUDO_DEBUG_INFO, "RLIMIT_CORE [%lld, %lld] -> [0, 0]",
72a5571
+	(long long)corelimit.rlim_cur, (long long)corelimit.rlim_max);
72a5571
     if (setrlimit(RLIMIT_CORE, &rl) == -1)
72a5571
 	sudo_warn("setrlimit(RLIMIT_CORE)");
72a5571
 #ifdef __linux__
72a5571
     /* On Linux, also set PR_SET_DUMPABLE to zero (reset by execve). */
72a5571
-    if ((dumpflag = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) == -1)
72a5571
+    if ((dumpflag = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) == -1) {
72a5571
+	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+	    "prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)");
72a5571
 	dumpflag = 0;
72a5571
-    (void) prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
72a5571
+    }
72a5571
+    if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) {
72a5571
+	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+	    "prctl(PR_SET_DUMPABLE, %d, 0, 0, 0)", dumpflag);
72a5571
+    }
72a5571
 #endif /* __linux__ */
72a5571
     coredump_disabled = true;
72a5571
72a5571
@@ -136,10 +144,20 @@
72a5571
     debug_decl(restore_coredump, SUDO_DEBUG_UTIL);
72a5571
72a5571
     if (coredump_disabled) {
72a5571
-	if (setrlimit(RLIMIT_CORE, &corelimit) == -1)
72a5571
-	    sudo_warn("setrlimit(RLIMIT_CORE)");
72a5571
+	/*
72a5571
+	 * Linux containers don't allow RLIMIT_CORE to be set back to
72a5571
+	 * RLIM_INFINITY if we set the limit to zero, even for root.
72a5571
+	 */
72a5571
+	if (setrlimit(RLIMIT_CORE, &corelimit) == -1) {
72a5571
+	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+		"setrlimit(RLIMIT_CORE, [%lld, %lld])",
72a5571
+		(long long)corelimit.rlim_cur, (long long)corelimit.rlim_max);
72a5571
+	}
72a5571
 #ifdef __linux__
72a5571
-	(void) prctl(PR_SET_DUMPABLE, dumpflag, 0, 0, 0);
72a5571
+	if (prctl(PR_SET_DUMPABLE, dumpflag, 0, 0, 0) == -1) {
72a5571
+	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+		"prctl(PR_SET_DUMPABLE, %d, 0, 0, 0)", dumpflag);
72a5571
+	}
72a5571
 #endif /* __linux__ */
72a5571
     }
72a5571
     debug_return;
72a5571
@@ -162,8 +180,14 @@
72a5571
72a5571
     if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
72a5571
 	sudo_warn("getrlimit(RLIMIT_NPROC)");
72a5571
+    sudo_debug_printf(SUDO_DEBUG_INFO, "RLIMIT_NPROC [%lld, %lld] -> [inf, inf]",
72a5571
+	(long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max);
72a5571
     if (setrlimit(RLIMIT_NPROC, &rl) == -1) {
72a5571
 	rl.rlim_cur = rl.rlim_max = nproclimit.rlim_max;
72a5571
+	sudo_debug_printf(SUDO_DEBUG_INFO,
72a5571
+	    "RLIMIT_NPROC [%lld, %lld] -> [%lld, %lld]",
72a5571
+	    (long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max,
72a5571
+	    (long long)rl.rlim_cur, (long long)rl.rlim_max);
72a5571
 	if (setrlimit(RLIMIT_NPROC, &rl) != 0)
72a5571
 	    sudo_warn("setrlimit(RLIMIT_NPROC)");
72a5571
     }
72a5571
@@ -180,8 +204,11 @@
72a5571
 #ifdef __linux__
72a5571
     debug_decl(restore_nproc, SUDO_DEBUG_UTIL);
72a5571
72a5571
-    if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0)
72a5571
-	sudo_warn("setrlimit(RLIMIT_NPROC)");
72a5571
+    if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0) {
72a5571
+	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+	    "setrlimit(RLIMIT_NPROC, [%lld, %lld])",
72a5571
+	    (long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max);
72a5571
+    }
72a5571
72a5571
     debug_return;
72a5571
 #endif /* __linux__ */
72a5571
@@ -203,6 +230,11 @@
72a5571
 	struct saved_limit *lim = &saved_limits[idx];
72a5571
 	if (getrlimit(lim->resource, &lim->oldlimit) == -1)
72a5571
 	    continue;
72a5571
+	sudo_debug_printf(SUDO_DEBUG_INFO,
72a5571
+	    "getrlimit(lim->name) -> [%lld, %lld]",
72a5571
+	    (long long)lim->oldlimit.rlim_cur,
72a5571
+	    (long long)lim->oldlimit.rlim_max);
72a5571
+
72a5571
 	lim->saved = true;
72a5571
 	if (lim->newlimit.rlim_cur != RLIM_INFINITY) {
72a5571
 	    /* Don't reduce the soft resource limit. */
72a5571
@@ -217,13 +249,28 @@
72a5571
 		lim->newlimit.rlim_max = lim->oldlimit.rlim_max;
72a5571
 	}
72a5571
 	if ((rc = setrlimit(lim->resource, &lim->newlimit)) == -1) {
72a5571
-	    if (lim->fallback != NULL)
72a5571
-		rc = setrlimit(lim->resource, lim->fallback);
72a5571
+	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+		"setrlimit(%s, [%lld, %lld])", lim->name,
72a5571
+		(long long)lim->newlimit.rlim_cur,
72a5571
+		(long long)lim->newlimit.rlim_max);
72a5571
+	    if (lim->fallback != NULL) {
72a5571
+		if ((rc = setrlimit(lim->resource, lim->fallback)) == -1) {
72a5571
+		    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+			"setrlimit(%s, [%lld, %lld])", lim->name,
72a5571
+			(long long)lim->fallback->rlim_cur,
72a5571
+			(long long)lim->fallback->rlim_max);
72a5571
+		}
72a5571
+	    }
72a5571
 	    if (rc == -1) {
72a5571
 		/* Try setting new rlim_cur to old rlim_max. */
72a5571
 		lim->newlimit.rlim_cur = lim->oldlimit.rlim_max;
72a5571
 		lim->newlimit.rlim_max = lim->oldlimit.rlim_max;
72a5571
-		rc = setrlimit(lim->resource, &lim->newlimit);
72a5571
+		if ((rc = setrlimit(lim->resource, &lim->newlimit)) == -1) {
72a5571
+		    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+			"setrlimit(%s, [%lld, %lld])", lim->name,
72a5571
+			(long long)lim->newlimit.rlim_cur,
72a5571
+			(long long)lim->newlimit.rlim_max);
72a5571
+		}
72a5571
 	    }
72a5571
 	    if (rc == -1)
72a5571
 		sudo_warn("setrlimit(%s)", lim->name);
72a5571
@@ -254,6 +301,10 @@
72a5571
 		if (rc != -1 || errno != EINVAL)
72a5571
 		    break;
72a5571
72a5571
+		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
72a5571
+		    "setrlimit(%s, [%lld, %lld])", lim->name,
72a5571
+		    (long long)rl.rlim_cur, (long long)rl.rlim_max);
72a5571
+
72a5571
 		/*
72a5571
 		 * Soft limit could be lower than current resource usage.
72a5571
 		 * This can be an issue on NetBSD with RLIMIT_STACK and ASLR.