Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2143963ybt; Mon, 15 Jun 2020 20:31:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUEsBak2Nayh/0G9VmGYTWn4QbtDsdgSzF25sDxhGQ9++QK3fuxRyWcvGbWeE91YBKp9IW X-Received: by 2002:a05:6402:1c87:: with SMTP id cy7mr790597edb.354.1592278301022; Mon, 15 Jun 2020 20:31:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592278301; cv=none; d=google.com; s=arc-20160816; b=V2Nvik29NE46X1kS+gx3SGZNS9LXI9AbJzRKNTilm92rdFxv+wIk7nYej98jXr/Bg5 ivxYLFDRTtPaOH3OKeBRatuOXr1x+dELumUz/G38p8HWP3plEA7twSs3XE0jokhdO7Cw 5rJYaOcDvbX1R1BfRM1aog8CP6SJxEureFac8NrNPtbcCCRTJExQ1zLgtp+V1wc0FrTn YdMbTwF1VBKHJfY4lNmbi9Xno1HCDllt3pdBPURp6HoGRbRepEHZb3IvAzyvfJpwwjNt PGzjSx+U5B+zriLxJbzfCMnUx1ZjA5THqOB9o/0bg5ZO59lLCrzgDnQH3mocUamCxt8+ l4KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HcknO+2U6NyZmGxMbR3CAHSQ4G454UQIfBRNwJsDAGs=; b=QfvGObMKwuXry4aBUINVcQ3etd29g3ZWUrxYEbGmMT3at9Auqd6di1XEwFq3YjwL1j Rd+99z+37rYhBF/+9IGx2hsYMwBXQkRkN6vJNx5zDFggrJQ3YlkPhzwauZKXOZAwyY5h DhKJdUKiss4nVsfG2AuFcuZqwFNnRtJizMgshqN9dIbnIXpKV8WVG3KN4bqsPVACh96l ZpX1RTX/DwBSMiFYYkgrn45UjnsRo9sVe2OFgSwpDFcKO1AXW8+QQ7IbOU6NzZeq320/ sTB7/d3PcGnKoImyNc10Za4K7PaoaPC7H5Yo0DvpKn6MrxA4ijjL++mbDBVzyGX/9de2 GmMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ni9Nj86T; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h19si9884307edq.313.2020.06.15.20.31.18; Mon, 15 Jun 2020 20:31:41 -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=@chromium.org header.s=google header.b=Ni9Nj86T; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727799AbgFPD0r (ORCPT + 99 others); Mon, 15 Jun 2020 23:26:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726864AbgFPDZf (ORCPT ); Mon, 15 Jun 2020 23:25:35 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C895C08C5C6 for ; Mon, 15 Jun 2020 20:25:34 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id b201so8850153pfb.0 for ; Mon, 15 Jun 2020 20:25:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HcknO+2U6NyZmGxMbR3CAHSQ4G454UQIfBRNwJsDAGs=; b=Ni9Nj86TxOCxN+Q1noSGMuOS4x3lT9BEmWWFkE5gXguwF94uaaqkDDRC0ZDI2dFwqH 6UQCDz5uppetU9OsClPUQVnfL7QLC7Lhie+Nj/0QfNVNZRco/pe3pJZKvxbx48Yw93YL K9aHPzSRk0B6z5wkni3ESg09Vw41qQyXw7c2w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HcknO+2U6NyZmGxMbR3CAHSQ4G454UQIfBRNwJsDAGs=; b=C2spuP/2VNxugZmVXi9e0qK3a3N+LUbFcCA5Gesb422dpK1xKNffoSExAi2gkm1ek6 scB+kDtLQT+SdmqmEtcsRVMhUn5Y5+Up8Siyo/8KDmPAZVsK5RV5eVIXOnKCmJYxsT25 uYabzOf+HqbSZDvIXZIJc/UomTAtMDNqbFFVylMdRm/JtbZrrJseaENmIlm7Wfpnk+Yk hdRHXgX79UlLU8TRH1Xr0JXbvDBTDZrqcuZaqrRsVOs2nQbZgFZmF6TlICSN3pw9Wk6H cujUBOH0kJ16QshecQ1pucRAQ65XOwKmbSSWnrcyWgSq2IkJflGlaaupJ2e/9U2XNKVN 2JqA== X-Gm-Message-State: AOAM533h2RIyXN6RSjYVuZJINpVDz3cArZljpbbDSxKcW/5QlRIN0HOd BAgObA2rctWZKJkXoun2TITKDw== X-Received: by 2002:a05:6a00:84e:: with SMTP id q14mr252444pfk.309.1592277933644; Mon, 15 Jun 2020 20:25:33 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c2sm12623848pgk.77.2020.06.15.20.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 20:25:32 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Sargun Dhillon , Christian Brauner , "David S. Miller" , Christoph Hellwig , Tycho Andersen , Jakub Kicinski , Alexander Viro , Aleksa Sarai , Matt Denton , Jann Horn , Chris Palmer , Robert Sesek , Giuseppe Scrivano , Greg Kroah-Hartman , Andy Lutomirski , Will Drewry , Shuah Khan , netdev@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v4 05/11] fs: Expand __fd_install_received() to accept fd Date: Mon, 15 Jun 2020 20:25:18 -0700 Message-Id: <20200616032524.460144-6-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200616032524.460144-1-keescook@chromium.org> References: <20200616032524.460144-1-keescook@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Expand __fd_install_received() with support for replace_fd() for the coming seccomp "addfd" ioctl(). Add new wrapper fd_replace_received() for the new mode and update existing wrappers to retain old mode. Signed-off-by: Kees Cook --- fs/file.c | 22 ++++++++++++++++------ include/linux/file.h | 10 +++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/fs/file.c b/fs/file.c index 14a8ef74efb2..b583e7c60571 100644 --- a/fs/file.c +++ b/fs/file.c @@ -950,8 +950,8 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) * * Returns newly install fd or -ve on error. */ -int __fd_install_received(struct file *file, bool ufd_required, int __user *ufd, - unsigned int o_flags) +int __fd_install_received(int fd, struct file *file, bool ufd_required, + int __user *ufd, unsigned int o_flags) { struct socket *sock; int new_fd; @@ -961,9 +961,11 @@ int __fd_install_received(struct file *file, bool ufd_required, int __user *ufd, if (error) return error; - new_fd = get_unused_fd_flags(o_flags); - if (new_fd < 0) - return new_fd; + if (fd < 0) { + new_fd = get_unused_fd_flags(o_flags); + if (new_fd < 0) + return new_fd; + } if (ufd_required) { error = put_user(new_fd, ufd); @@ -973,6 +975,15 @@ int __fd_install_received(struct file *file, bool ufd_required, int __user *ufd, } } + if (fd < 0) + fd_install(new_fd, get_file(file)); + else { + new_fd = fd; + error = replace_fd(new_fd, file, o_flags); + if (error) + return error; + } + /* Bump the usage count and install the file. The resulting value of * "error" is ignored here since we only need to take action when * the file is a socket and testing "sock" for NULL is sufficient. @@ -982,7 +993,6 @@ int __fd_install_received(struct file *file, bool ufd_required, int __user *ufd, sock_update_netprioidx(&sock->sk->sk_cgrp_data); sock_update_classid(&sock->sk->sk_cgrp_data); } - fd_install(new_fd, get_file(file)); return new_fd; } diff --git a/include/linux/file.h b/include/linux/file.h index 999a2c56db07..f1d16e24a12e 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -91,16 +91,20 @@ extern void put_unused_fd(unsigned int fd); extern void fd_install(unsigned int fd, struct file *file); -extern int __fd_install_received(struct file *file, bool ufd_required, +extern int __fd_install_received(int fd, struct file *file, bool ufd_required, int __user *ufd, unsigned int o_flags); static inline int fd_install_received_user(struct file *file, int __user *ufd, unsigned int o_flags) { - return __fd_install_received(file, true, ufd, o_flags); + return __fd_install_received(-1, file, true, ufd, o_flags); } static inline int fd_install_received(struct file *file, unsigned int o_flags) { - return __fd_install_received(file, false, NULL, o_flags); + return __fd_install_received(-1, file, false, NULL, o_flags); +} +static inline int fd_replace_received(int fd, struct file *file, unsigned int o_flags) +{ + return __fd_install_received(fd, file, false, NULL, o_flags); } extern void flush_delayed_fput(void); -- 2.25.1