Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2192777ybt; Mon, 15 Jun 2020 22:31:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLjaT5JYDaOePg4yQuJC0DLhuQK/wsSbG3qt/PtMOlEjKkqcVG7OzkH1GRkh/LO1PGzYen X-Received: by 2002:a17:906:a402:: with SMTP id l2mr1271861ejz.14.1592285510604; Mon, 15 Jun 2020 22:31:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592285510; cv=none; d=google.com; s=arc-20160816; b=wLpI9Am6x1ILYpW7hVJ/rHoCn66z9YnysjZQfwMOGAZZHNhG7Nql5vA4tJk/6EEFOv oljcv6K6InpJXE08I+gS3DKCdwiZf5VezzrTQR64Fn8cWklZ0Xu2iECrMintpyovTCdF 3XcZrC/J9E3cmO/T2pIyNNKEbKh+qyuY2N/HP2GZK72uT16zx9FHwo7kFeamHmLRGvhJ uKh2CdvAabUS8+r1eP7YdEe9ljpTgZZUsYj1L+3H43DQsuX83CZR82SvhCKI0vz3E/Ae bMP5FaQ/cL+wr5CyC5GZOtXcJbI7QbVnS0o1mGP//FH6qqHK0/cjpFtr+seYifAmrNlD 9Z9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=/atL1Ve60OqcVOW1A17eOHeeXaoo7IDTY4Z8Un8jaH8=; b=rxLAyYR2z0QMryENU5g9LhihicEA91eckCeIJN6yaQ5LuU2ovLr0qxjJointTH3snp cAMyYWi668w7fmpDYorOj2qUA+0K6XcrHB83i+AaZO2kFCmvX1WWZlS+djKMXbfjMA+Q qSsX006FX5IYgIzwD8w7HVIIVUvTc/r0IC+/6TFkjFr5GkcPT8mve7Et8Y0YnM7H8Eht lBtelh9uPTP1vTCnfuuRq93m/myDxJia8JsGVSuAzDqAzedUto1sKxjDuyDUh+ZZh/12 Hpvxp2/DqaCJSTzxg4fmIe8+OfAG3Yjvyd0sqdj1SukjUof+4XeDjr2kz+36Bhwy5QjS CIlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=193OWy+w; 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 r22si10576955eji.209.2020.06.15.22.31.27; Mon, 15 Jun 2020 22:31:50 -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=@sargun.me header.s=google header.b=193OWy+w; 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 S1726649AbgFPF3p (ORCPT + 99 others); Tue, 16 Jun 2020 01:29:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726392AbgFPF3o (ORCPT ); Tue, 16 Jun 2020 01:29:44 -0400 Received: from mail-il1-x142.google.com (mail-il1-x142.google.com [IPv6:2607:f8b0:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73193C08C5C2 for ; Mon, 15 Jun 2020 22:29:44 -0700 (PDT) Received: by mail-il1-x142.google.com with SMTP id x18so17688128ilp.1 for ; Mon, 15 Jun 2020 22:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=/atL1Ve60OqcVOW1A17eOHeeXaoo7IDTY4Z8Un8jaH8=; b=193OWy+wrWHpQHLeiYDs4HwLjRR3lzkgnsBt2V+dkl6TagtCtHrBhNG24ihvS9W6Zf zasBHbcZhYtRMZisoQXuaE9BeVT715PI0S1hhwE0J7wByQ95ip3Y+PHUh72iIzi+jHkx GEj69+viDrq4GN/szP7+GnPsyYy7nNibYDgc0= 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:user-agent; bh=/atL1Ve60OqcVOW1A17eOHeeXaoo7IDTY4Z8Un8jaH8=; b=VSfZEX6WZdjdzkYUkmOqiSQ+mD5UGFde4rQB47vpw6zDcm2Gm/L9s75yJPv4Hshlea fmQOzsjMjcuFWs6RMaFVSYCYFMlGal+oy5lfiiEdPv17xxaacBfG9Adzxi6WYDuDzhE/ B/RSvC/E5JrGgQoBnMErC7DX4w1AXaH3MBuY1c9/RMWgCQ1Iqv6dmISfxP5MqECmZdbt YLhxgd5Y3DyY6KSXvcX1/ehUiZHQXiHQK+9zBp2CFMeV5iRsJV4Q6L1vaHOvbSCKaxej g4Y/dL+tBn6KloVnXtDpdyD0k4Os/lw+Hv8/BgHFc1b+GlVkbMjoamKawpl/W5FyiJES XU9Q== X-Gm-Message-State: AOAM5301A8Q+JN5l3Lgx5sB+DK0U5xkWCnFVZOE/RPgPugJSzuGt7qw4 svzS5Uf3pRWWMMG+1uiAxxpzyg== X-Received: by 2002:a92:c7c6:: with SMTP id g6mr1539412ilk.49.1592285383432; Mon, 15 Jun 2020 22:29:43 -0700 (PDT) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id z16sm9204945ilz.64.2020.06.15.22.29.43 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Jun 2020 22:29:43 -0700 (PDT) Date: Tue, 16 Jun 2020 05:29:41 +0000 From: Sargun Dhillon To: Kees Cook Cc: linux-kernel@vger.kernel.org, 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: <20200616052941.GB16032@ircssh-2.c.rugged-nimbus-611.internal> References: <20200616032524.460144-1-keescook@chromium.org> <20200616032524.460144-3-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200616032524.460144-3-keescook@chromium.org> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Probably doesn't matter, but this function doesn't take the fd, or ufd_required argument in this patch. > + * > + * Installs a received file into the file descriptor table, with appropriate > + * checks and count updates. Optionally writes the fd number to userspace. ufd does not apppear options here. > + * > + * 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); > +} > + > extern void flush_delayed_fput(void); > extern void __fput_sync(struct file *); > > diff --git a/include/net/scm.h b/include/net/scm.h > index 581a94d6c613..1ce365f4c256 100644 > --- a/include/net/scm.h > +++ b/include/net/scm.h > @@ -37,7 +37,6 @@ struct scm_cookie { > #endif > }; > > -int __scm_install_fd(struct file *file, int __user *ufd, unsigned int o_flags); > void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm); > void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm); > int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm); > diff --git a/net/compat.c b/net/compat.c > index 27d477fdcaa0..94f288e8dac5 100644 > --- a/net/compat.c > +++ b/net/compat.c > @@ -298,7 +298,7 @@ void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm) > int err = 0, i; > > for (i = 0; i < fdmax; i++) { > - err = __scm_install_fd(scm->fp->fp[i], cmsg_data + i, o_flags); > + err = fd_install_received_user(scm->fp->fp[i], cmsg_data + i, o_flags); > if (err) > break; > } > diff --git a/net/core/scm.c b/net/core/scm.c > index 6151678c73ed..df190f1fdd28 100644 > --- a/net/core/scm.c > +++ b/net/core/scm.c > @@ -280,36 +280,6 @@ void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_inter > } > EXPORT_SYMBOL(put_cmsg_scm_timestamping); > > -int __scm_install_fd(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 scm_max_fds(struct msghdr *msg) > { > if (msg->msg_controllen <= sizeof(struct cmsghdr)) > @@ -336,7 +306,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) > } > > for (i = 0; i < fdmax; i++) { > - err = __scm_install_fd(scm->fp->fp[i], cmsg_data + i, o_flags); > + err = fd_install_received_user(scm->fp->fp[i], cmsg_data + i, o_flags); > if (err) > break; > } > -- > 2.25.1 >