zed: only go up to current limit in close_from() fallback

Consider the following strace log:
  prlimit64(0, RLIMIT_NOFILE,
            NULL, {rlim_cur=1024, rlim_max=1024*1024}) = 0
  dup2(0, 30)                         = 30
  dup2(0, 300)                        = 300
  dup2(0, 3000)                       = -1 EBADF (Bad file descriptor)
  dup2(0, 30000)                      = -1 EBADF (Bad file descriptor)
  dup2(0, 300000)                     = -1 EBADF (Bad file descriptor)
  prlimit64(0, RLIMIT_NOFILE,
            {rlim_cur=1024*1024, rlim_max=1024*1024}, NULL) = 0
  dup2(0, 30)                         = 30
  dup2(0, 300)                        = 300
  dup2(0, 3000)                       = 3000
  dup2(0, 30000)                      = 30000
  dup2(0, 300000)                     = 300000

Even a privileged process needs to bump its rlimit before being able
to use fds higher than rlim_cur.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11834
This commit is contained in:
наб 2021-04-03 12:09:24 +02:00 committed by Brian Behlendorf
parent b3a7e6e7f3
commit 55780d8ec0

View File

@ -17,7 +17,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <sys/resource.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -119,9 +118,7 @@ zed_file_is_locked(int fd)
void void
zed_file_close_from(int lowfd) zed_file_close_from(int lowfd)
{ {
static const int maxfd_def = 256;
int errno_bak = errno; int errno_bak = errno;
struct rlimit rl;
int maxfd = 0; int maxfd = 0;
int fd; int fd;
DIR *fddir; DIR *fddir;
@ -134,11 +131,8 @@ zed_file_close_from(int lowfd)
maxfd = fd; maxfd = fd;
} }
(void) closedir(fddir); (void) closedir(fddir);
} else if (getrlimit(RLIMIT_NOFILE, &rl) < 0 ||
rl.rlim_max == RLIM_INFINITY) {
maxfd = maxfd_def;
} else { } else {
maxfd = rl.rlim_max; maxfd = sysconf(_SC_OPEN_MAX);
} }
for (fd = lowfd; fd < maxfd; fd++) for (fd = lowfd; fd < maxfd; fd++)
(void) close(fd); (void) close(fd);