]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Limit rlim_max in rlimit_nofile_safe() to nr_open 28968/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 25 Aug 2023 11:55:36 +0000 (13:55 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 25 Aug 2023 12:26:11 +0000 (14:26 +0200)
We might inherit a max rlim value that's larger than the kernel's
maximum (nr_open). This will cause setrlimit() to fail as the given
maximum is larger than the kernel's maximum. To get around this,
let's limit the max rlim we pass to rlimit() to the value of nr_open.

Should fix #28965

src/basic/rlimit-util.c

index 91424cd3cced0e121c7d20db22d7cd573839b3ea..a0ffb24626f02210153ab78d909c5e843003590d 100644 (file)
@@ -401,7 +401,11 @@ int rlimit_nofile_safe(void) {
         if (rl.rlim_cur <= FD_SETSIZE)
                 return 0;
 
-        rl.rlim_cur = FD_SETSIZE;
+        /* So we might have inherited a hard limit that's larger than the kernel's maximum limit as stored in
+         * /proc/sys/fs/nr_open. If we pass this hard limit unmodified to setrlimit(), we'll get EPERM. To
+         * make sure that doesn't happen, let's limit our hard limit to the value from nr_open. */
+        rl.rlim_max = MIN(rl.rlim_max, (rlim_t) read_nr_open());
+        rl.rlim_cur = MIN((rlim_t) FD_SETSIZE, rl.rlim_max);
         if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
                 return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur);