Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2676005pxa; Mon, 17 Aug 2020 16:18:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytcH5Eoap8oPX0DNFd/imo1JEn2q7osznxZmiQc6QRWmB6Zh1xf6rgtHHCxJyQ0vqqvcwb X-Received: by 2002:a17:906:bb0e:: with SMTP id jz14mr18223908ejb.525.1597706306450; Mon, 17 Aug 2020 16:18:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597706306; cv=none; d=google.com; s=arc-20160816; b=STY/5JfqE4NZk1Y1KQwsdk15Q6MbUCEIvL/QzTyFnh4QIGb/m5uNBYPLoyymE3NzHo m1PaMSjBW5W2IfZDUK9AJ5MzvPBEFYJ/zcrNNKM4WeRZyM61Pao8N141AIIEhH3Z4Hy6 cabjqV6pC3d34V9WeCwrhlg5Khi1h22LCA3a18fxwKWffEs3YFk7gcT//gcQiwJzleN0 k4pkQFZL7BKh+ksxXwgJoDJIZnFtqnrjTvpcHY2hhrJ4+YleicElF8PQbr62yuVBZcom zVZvH1ITD1An2Hm2O8Lgj+Os9hDuC3cGs77pf3Db5dQ86iv7GCgPFZ/Nz95Y8fpAtSll KPPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:cc:to:from; bh=y9a4uqiXt9Dx051CxN332CmyxLOKzrv9RR6XqzYYgKs=; b=ydIm65ErtbpdqAF8qGkVjTmHAHViC7wTr8Pd24ABXT1+IOOgCCFPKGyWb8l2sNmuW3 lQ4F3oHKNEUgV7xchbKDPtV/TCOdWNhv9DtpbrCdnLltT2QXFHItDXxKtXcCYQcdl2Wk 0pcS3uIJIMS9MYPLrzteF7siwnQTgCs93M9Z6S42eYV8NvvpZiep235Pzn/lMtUyQkPR 84wcxhkpJQAaPzDxDkcU6LsM0TKUeMW5lCprHIG7hdbBpnsl6cg3weFFf+O7mY496YQA XmiIJBvXRLEF9CfdO3JKkr6up8w/Xhe3KfDzk9v0OxuqdWvSRqO2BT2ylQuLoin/e0uT 4LLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l9si13551455ejq.65.2020.08.17.16.18.02; Mon, 17 Aug 2020 16:18:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730290AbgHQWLw (ORCPT + 99 others); Mon, 17 Aug 2020 18:11:52 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:47158 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730254AbgHQWLu (ORCPT ); Mon, 17 Aug 2020 18:11:50 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out01.mta.xmission.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1k7nM5-004wA5-4v; Mon, 17 Aug 2020 16:11:49 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.int.ebiederm.org) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1k7nKq-0004PB-Nn; Mon, 17 Aug 2020 16:10:33 -0600 From: "Eric W. Biederman" To: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, criu@openvz.org, bpf@vger.kernel.org, Linus Torvalds , Alexander Viro , Christian Brauner , Oleg Nesterov , Cyrill Gorcunov , Jann Horn , Kees Cook , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Jeff Layton , Miklos Szeredi , Matthew Wilcox , "J. Bruce Fields" , Matthew Wilcox , Trond Myklebust , Chris Wright , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , "Eric W. Biederman" Date: Mon, 17 Aug 2020 17:04:23 -0500 Message-Id: <20200817220425.9389-15-ebiederm@xmission.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <87ft8l6ic3.fsf@x220.int.ebiederm.org> References: <87ft8l6ic3.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-XM-SPF: eid=1k7nKq-0004PB-Nn;;;mid=<20200817220425.9389-15-ebiederm@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1+4cpNrHwZELt8oSRiOq1yzCRzmHtQx5rY= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa06.xmission.com X-Spam-Level: * X-Spam-Status: No, score=1.3 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TooManySym_01,T_TooManySym_02,XMNoVowels autolearn=disabled version=3.4.2 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * 1.5 XMNoVowels Alpha-numberic number with no vowels * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa06 0; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_02 5+ unique symbols in subject * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: ; sa06 0; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;linux-kernel@vger.kernel.org X-Spam-Relay-Country: X-Spam-Timing: total 470 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 9 (1.9%), b_tie_ro: 8 (1.6%), parse: 1.07 (0.2%), extract_message_metadata: 11 (2.3%), get_uri_detail_list: 1.07 (0.2%), tests_pri_-1000: 15 (3.1%), tests_pri_-950: 1.34 (0.3%), tests_pri_-900: 1.07 (0.2%), tests_pri_-90: 195 (41.6%), check_bayes: 194 (41.2%), b_tokenize: 9 (1.8%), b_tok_get_all: 5 (1.2%), b_comp_prob: 2.2 (0.5%), b_tok_touch_all: 174 (36.9%), b_finish: 0.93 (0.2%), tests_pri_0: 218 (46.5%), check_dkim_signature: 0.57 (0.1%), check_dkim_adsp: 1.96 (0.4%), poll_dns_idle: 0.38 (0.1%), tests_pri_10: 2.7 (0.6%), tests_pri_500: 13 (2.7%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH 15/17] file: In f_dupfd read RLIMIT_NOFILE once. X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: "Eric W. Biederman" --- fs/file.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/file.c b/fs/file.c index 1a755811669d..505b2e81ad3e 100644 --- a/fs/file.c +++ b/fs/file.c @@ -523,9 +523,9 @@ int __alloc_fd(struct files_struct *files, 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) @@ -1158,10 +1158,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); -- 2.25.0