Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1624684ybt; Thu, 18 Jun 2020 13:10:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVRUGTGrBFsYUMghMj4bkxfSPkRNPpkZ5ifwTqibYNKF9RhvCxO79a37K/PiaQD0rvxjCV X-Received: by 2002:a17:906:b59:: with SMTP id v25mr424995ejg.114.1592511013885; Thu, 18 Jun 2020 13:10:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592511013; cv=none; d=google.com; s=arc-20160816; b=LRT0SUQ+vLBht7zFCXgqEKiIus3eyddGHZnVohS5soOJXxA9Bm0ihZ+jhbUPhUM9yJ mgNa5/+kHEi4lkqtEmDjg8biHFPZkRSAG24JIzX/tPVqrsMaEkZXV5gtxK43GG9Ii49z bvxh/YFEXP2RAmQculmXnG5ZQZcdwKY0Dmr8E4UtblueugiBm9NMwlQpBWEUfaPs9Hzf 17Hv6JFn1C+HDpZN2wTREIo3pDL0rbPAvk8YC5H6WK5pAfiFp+472ema2FP6BzoJJaFW +/jts8vydhL1zuFyiSMEuA00ngdsof5ZJDDG8USsYllR8HLLCDS/1/dM25o6P403iCBO 4lVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=hH5UdqkrUsHHkWkTkrsTpUaZ/SG0CR6QLInGjMheMsQ=; b=biirBSDMFNPxNQ7BgNKrMgDayED4xDiBhX+TZRhkfHjm9EbcxaaR0DGf19VTn+Ti8G O0rk6sxW9j+Qswss7TXw39/aEei5kBqyo5RaZ1Btoc289a2QlbTfUgJhhUO59nY87MnG RJhfpgManNIKS9TkCa8ZJ8NjrOA9R+jmYTYJbdVga/2VWYWJmQXvEMiF2t5e80UE8xJo 6K4n7BRwyW1MVMYR+0ny5/zjIQ5mu2oeed2QfgRWJOfab3aqtLkEHj/M/6GZW/8aYE45 GET2na1W28LlJjMJ8dGXSQlTmj5sHNfN2VdIRj7yanbkmBKxibegEM5HYd8ys6D5B2sk Q/YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=etadw103; 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 89si2738659edo.459.2020.06.18.13.09.51; Thu, 18 Jun 2020 13:10:13 -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=etadw103; 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 S1730966AbgFRUFT (ORCPT + 99 others); Thu, 18 Jun 2020 16:05:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730918AbgFRUFQ (ORCPT ); Thu, 18 Jun 2020 16:05:16 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5464C0613EE for ; Thu, 18 Jun 2020 13:05:16 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id e18so3411247pgn.7 for ; Thu, 18 Jun 2020 13:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=hH5UdqkrUsHHkWkTkrsTpUaZ/SG0CR6QLInGjMheMsQ=; b=etadw103cqJRZdZ59vL7+FSIVCyWqJxsPUoshMP46pJkWYpyPh1Z8z2rOaozPM6T9H 5c7AmqMnocB+A8JJb/7BGlmzUHsa8VUg/Sy09a1I4Lr+wMi+eXoSUP1amVW/VFTdTB0Y Az+830Tp9h9FTrH3X1IX6E56xQAUj6mYytnd0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hH5UdqkrUsHHkWkTkrsTpUaZ/SG0CR6QLInGjMheMsQ=; b=cjFi1Y1ZxtZYAxKg5rM3ErGjCrXGjBl0XlHe1PpzPdQgJWNTnT9KdFuegHsiayR5yt g8jFCkbTRU7+H040KDcgFDFozaYO2CfzwjRKVQ+hl0w2U72MG695tjbRAtaSEE1lrnaT 7rqfvfMYVBY3oLXKIYUTjPXyz9aablNMngsf1QJw5/gVlcJj6AWVM4KLwhRtUa5dSYLa l7pZVzuPV/ObyHCM+94//mUJvuwSbN60dMsgEwkE3GqduYYcaUgMUCB3Rs5SlYVAd9+s 9vyRkVTs+bjHKqi43ZicDtZGSX+p8LD0Jm6fXBB22xvupJwmLRit6yDJ4IorKTF4gz6T ToMw== X-Gm-Message-State: AOAM533Xaoklchrw+HMG+HvL8ic0mXFQprnHrQR+9i1DyC1pwe3t+pRf UQV6p+kOeoRTBH7sBvGIBm2t0g== X-Received: by 2002:a62:1c5:: with SMTP id 188mr4954296pfb.213.1592510716220; Thu, 18 Jun 2020 13:05:16 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p14sm3201600pju.7.2020.06.18.13.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jun 2020 13:05:15 -0700 (PDT) Date: Thu, 18 Jun 2020 13:05:14 -0700 From: Kees Cook To: Christian Brauner Cc: linux-kernel@vger.kernel.org, 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: Re: [PATCH v4 02/11] fs: Move __scm_install_fd() to __fd_install_received() Message-ID: <202006181302.84BFFF52CA@keescook> References: <20200616032524.460144-1-keescook@chromium.org> <20200616032524.460144-3-keescook@chromium.org> <20200618085614.fw3ynalpcipbplf3@wittgenstein> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200618085614.fw3ynalpcipbplf3@wittgenstein> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 18, 2020 at 10:56:14AM +0200, Christian Brauner wrote: > On Mon, Jun 15, 2020 at 08:25:15PM -0700, Kees Cook wrote: > > In preparation for users of the "install a received file" logic outside > > of net/ (pidfd and seccomp), relocate and rename __scm_install_fd() from > > net/core/scm.c to __fd_install_received() in fs/file.c, and provide a > > wrapper named fd_install_received_user(), as future patches will change > > the interface to __fd_install_received(). > > > > Signed-off-by: Kees Cook > > --- > > fs/file.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ > > include/linux/file.h | 8 ++++++++ > > include/net/scm.h | 1 - > > net/compat.c | 2 +- > > net/core/scm.c | 32 +----------------------------- > > 5 files changed, 57 insertions(+), 33 deletions(-) > > > > diff --git a/fs/file.c b/fs/file.c > > index abb8b7081d7a..fcfddae0d252 100644 > > --- a/fs/file.c > > +++ b/fs/file.c > > @@ -11,6 +11,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -18,6 +19,8 @@ > > #include > > #include > > #include > > +#include > > +#include > > > > unsigned int sysctl_nr_open __read_mostly = 1024*1024; > > unsigned int sysctl_nr_open_min = BITS_PER_LONG; > > @@ -931,6 +934,50 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) > > return err; > > } > > > > +/** > > + * __fd_install_received() - 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_required: true to use @ufd for writing fd number to userspace > > + * @ufd: __user pointer to write new fd number to > > + * @o_flags: the O_* flags to apply to the new fd entry > > + * > > + * Installs a received file into the file descriptor table, with appropriate > > + * checks and count updates. Optionally writes the fd number to userspace. > > + * > > + * Returns -ve on error. > > + */ > > +int __fd_install_received(struct file *file, int __user *ufd, unsigned int o_flags) > > +{ > > + struct socket *sock; > > + int new_fd; > > + int error; > > + > > + error = security_file_receive(file); > > + if (error) > > + return error; > > + > > + new_fd = get_unused_fd_flags(o_flags); > > + if (new_fd < 0) > > + return new_fd; > > + > > + error = put_user(new_fd, ufd); > > + if (error) { > > + put_unused_fd(new_fd); > > + return error; > > + } > > + > > + /* Bump the usage count and install the file. */ > > + sock = sock_from_file(file, &error); > > + if (sock) { > > + sock_update_netprioidx(&sock->sk->sk_cgrp_data); > > + sock_update_classid(&sock->sk->sk_cgrp_data); > > + } > > + fd_install(new_fd, get_file(file)); > > + return 0; > > +} > > + > > static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) > > { > > int err = -EBADF; > > diff --git a/include/linux/file.h b/include/linux/file.h > > index 122f80084a3e..fe18a1a0d555 100644 > > --- a/include/linux/file.h > > +++ b/include/linux/file.h > > @@ -91,6 +91,14 @@ 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, 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, ufd, o_flags); > > +} > > Shouldn't this be the other way around such that > fd_install_received_user() is the workhorse that has a "ufd" argument > and fd_install_received() is the static inline function that doesn't? > > extern int fd_install_received_user(struct file *file, int __user *ufd, unsigned int o_flags) > static inline int fd_install_received(struct file *file, unsigned int o_flags) > { > return fd_install_received_user(file, NULL, o_flags); > } So, I think it's all worked out in v5[1], so the helper argument handling is better for the ufd case, as David pointed out earlier. (As in, I think you're reacting to the same general problem here.) > (So I'm on vacation this week some my reviews are selective and spotty > but I promise to be back next week. :)) No worries! -Kees [1] https://lore.kernel.org/lkml/20200617220327.3731559-1-keescook@chromium.org/ -- Kees Cook