From: Eric W. Biederman Date: Fri, 20 Nov 2020 23:14:36 +0000 (-0600) Subject: file: In f_dupfd read RLIMIT_NOFILE once. X-Git-Tag: v5.11-rc1~135^2~2^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e06b53c22f31ebba4c46d02fb3a58336135db45c;p=thirdparty%2Flinux.git file: In f_dupfd read RLIMIT_NOFILE once. Simplify the code, and remove the chance of races by reading RLIMIT_NOFILE only once in f_dupfd. Pass the read value of RLIMIT_NOFILE into alloc_fd which is the other location the rlimit was read in f_dupfd. As f_dupfd is the only caller of alloc_fd this changing alloc_fd is trivially safe. Further this causes alloc_fd to take all of the same arguments as __alloc_fd except for the files_struct argument. Acked-by: Christian Brauner v1: https://lkml.kernel.org/r/20200817220425.9389-15-ebiederm@xmission.com Link: https://lkml.kernel.org/r/20201120231441.29911-19-ebiederm@xmission.com Signed-off-by: Eric W. Biederman --- diff --git a/fs/file.c b/fs/file.c index 0d4ec0fa23b34..07e25f1b9dfd5 100644 --- a/fs/file.c +++ b/fs/file.c @@ -538,9 +538,9 @@ out: return error; } -static int alloc_fd(unsigned start, unsigned flags) +static int alloc_fd(unsigned start, unsigned end, unsigned flags) { - return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags); + return __alloc_fd(current->files, start, end, flags); } int __get_unused_fd_flags(unsigned flags, unsigned long nofile) @@ -1175,10 +1175,11 @@ SYSCALL_DEFINE1(dup, unsigned int, fildes) int f_dupfd(unsigned int from, struct file *file, unsigned flags) { + unsigned long nofile = rlimit(RLIMIT_NOFILE); int err; - if (from >= rlimit(RLIMIT_NOFILE)) + if (from >= nofile) return -EINVAL; - err = alloc_fd(from, flags); + err = alloc_fd(from, nofile, flags); if (err >= 0) { get_file(file); fd_install(err, file);