Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp185810pxf; Thu, 25 Mar 2021 01:24:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3lqvLPjld67vrx5HeQ3qEfNLobNHe+cfgQvT6c2vEAst5A5WnHl4gQe5YHZW7wjMyG99l X-Received: by 2002:a17:906:a413:: with SMTP id l19mr8077276ejz.421.1616660658746; Thu, 25 Mar 2021 01:24:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616660658; cv=none; d=google.com; s=arc-20160816; b=XSdRyFGXjztYooW8PZ/vzth/6NPLO8uvVU4Pcaj+L0IDzAnDiSi9iMATIftNrG4+1V xu3rP51/OCxxweP/RjazxbjtivsAU/PSW/g1uhMy74YSmjtqa0jhUH1Ekz+3V+yZiR7W +My+6M/MzZkeRlFGr8RQIf2mSMblm8cUURaX/fD/ZkQEkrg4CT8+cEGjzv3XYMzvZ5vd DNxEGW+u+IZkaDKG6zcFn8RC3HzUf3XPcKx8qR/5Pi0qcZEihmxKUUllXZtwOvrjVkaR eW5RxGJFL//aZpH6dOPmuFnuHIl1/o0LkB1Oiwcns3Pq2qvadrpLI+rU/pcT/FikGewT LT4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+rMCIoW7pA9LHheP4AD2vUH1j8gzDA0578xBrBuuLkU=; b=ndBZVOh+33cJBC6DBOXBWKqjrr9a8ikvxj3k00xRZD8JiDYzejJxO2AHB7i2wOxB5L pNzz8LxYMvGqkxhmMKyPmFTJDtWA42uo6ktCqaX5Yp8MiytnHVGAOxRL/dhDfe+zOEcG U6FrC3hiEXYPdlALaB+AW0aHd0QeHUTfNgTobfHvaw3JSPcAI0Ea+5PzvGgR3DLlMXBd yY4Ej0aRf4vfNwrSvop68HqnBHgoRrlTLXRE2ycjpYlHCdlPjnWQ8W4jUUCJB/NtqbI8 nqTDxPV5HWEZsV7SgyEAAO1mxbNvihJM4zDQJAbk6e4BLyqTXZ1xiF6LXOYjMxl4uLXe v1AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=0pFdJY9A; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c11si3643384ede.102.2021.03.25.01.23.56; Thu, 25 Mar 2021 01:24:18 -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; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=0pFdJY9A; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229653AbhCYIWi (ORCPT + 99 others); Thu, 25 Mar 2021 04:22:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229719AbhCYIWP (ORCPT ); Thu, 25 Mar 2021 04:22:15 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87580C06175F; Thu, 25 Mar 2021 01:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=+rMCIoW7pA9LHheP4AD2vUH1j8gzDA0578xBrBuuLkU=; b=0pFdJY9A4UdALQUlD9PmA0KXgM WVCQAe5hvjOzQw+9nEmscHTvFKGvdGoyY2wykYvjlAfrByYQqvhp7ZkDM/eqXWYwRpI0+4+Wr02Hh 7dWCo16BSmG9Na9l1ApMPwaU/MpyD1wtTuF6HmK42DKSVkQIJ83KKvBirBowGbsynrMCKiacwgxWg aVx+MVz1WMUvXoJpdQwpmna3IGO1MRI1XXoAtu5zGCftq7gAbzjyUZJJ+faZjzbVt1BHA2p5f/i5S wsf2ImXlxUTGyNEjh2bsV4CCoQh/FEXaFc6DKwsoiyMUNc7MAF1nAZPkKuhddI83WAOrBHH9W6O9y AUaerolg==; Received: from [2001:4bb8:191:f692:9658:56e2:eade:cbfa] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lPLFu-004X66-Oe; Thu, 25 Mar 2021 08:22:15 +0000 From: Christoph Hellwig To: Al Viro Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Andy Lutomirski , Will Drewry Subject: [PATCH] fs: split receive_fd_replace from __receive_fd Date: Thu, 25 Mar 2021 09:22:09 +0100 Message-Id: <20210325082209.1067987-2-hch@lst.de> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210325082209.1067987-1-hch@lst.de> References: <20210325082209.1067987-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org receive_fd_replace shares almost no code with the general case, so split it out. Also remove the "Bump the sock usage counts" comment from both copies, as that is now what __receive_sock actually does. Signed-off-by: Christoph Hellwig --- fs/file.c | 39 +++++++++++++++++++-------------------- include/linux/file.h | 11 ++++------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/fs/file.c b/fs/file.c index f3a4bac2cbe915..d8ccb95a7f4138 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1068,8 +1068,6 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) /** * __receive_fd() - Install received file into file descriptor table - * - * @fd: fd to install into (if negative, a new fd will be allocated) * @file: struct file that was received from another process * @ufd: __user pointer to write new fd number to * @o_flags: the O_* flags to apply to the new fd entry @@ -1083,7 +1081,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) * * Returns newly install fd or -ve on error. */ -int __receive_fd(int fd, struct file *file, int __user *ufd, unsigned int o_flags) +int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) { int new_fd; int error; @@ -1092,32 +1090,33 @@ int __receive_fd(int fd, struct file *file, int __user *ufd, unsigned int o_flag if (error) return error; - if (fd < 0) { - new_fd = get_unused_fd_flags(o_flags); - if (new_fd < 0) - return new_fd; - } else { - new_fd = fd; - } + new_fd = get_unused_fd_flags(o_flags); + if (new_fd < 0) + return new_fd; if (ufd) { error = put_user(new_fd, ufd); if (error) { - if (fd < 0) - put_unused_fd(new_fd); + put_unused_fd(new_fd); return error; } } - if (fd < 0) { - fd_install(new_fd, get_file(file)); - } else { - error = replace_fd(new_fd, file, o_flags); - if (error) - return error; - } + fd_install(new_fd, get_file(file)); + __receive_sock(file); + return new_fd; +} - /* Bump the sock usage counts, if any. */ +int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) +{ + int error; + + error = security_file_receive(file); + if (error) + return error; + error = replace_fd(new_fd, file, o_flags); + if (error) + return error; __receive_sock(file); return new_fd; } diff --git a/include/linux/file.h b/include/linux/file.h index 225982792fa20e..2de2e4613d7bc3 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -92,23 +92,20 @@ extern void put_unused_fd(unsigned int fd); extern void fd_install(unsigned int fd, struct file *file); -extern int __receive_fd(int fd, struct file *file, int __user *ufd, +extern int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags); static inline int receive_fd_user(struct file *file, int __user *ufd, unsigned int o_flags) { if (ufd == NULL) return -EFAULT; - return __receive_fd(-1, file, ufd, o_flags); + return __receive_fd(file, ufd, o_flags); } static inline int receive_fd(struct file *file, unsigned int o_flags) { - return __receive_fd(-1, file, NULL, o_flags); -} -static inline int receive_fd_replace(int fd, struct file *file, unsigned int o_flags) -{ - return __receive_fd(fd, file, NULL, o_flags); + return __receive_fd(file, NULL, o_flags); } +int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags); extern void flush_delayed_fput(void); extern void __fput_sync(struct file *); -- 2.30.1