Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1066165ybt; Wed, 17 Jun 2020 22:51:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEsL8OqHkoeK5oX2AvuPaiswDDDtkzHX1nF8Cm2+2pbb5mbH5VV0rhJvRat/bTXZM+nBts X-Received: by 2002:a17:906:490f:: with SMTP id b15mr2396024ejq.180.1592459517095; Wed, 17 Jun 2020 22:51:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592459517; cv=none; d=google.com; s=arc-20160816; b=YA30MqnMI8oYTDVd4s4+zBSk1P6K4asSWQ1KrnOgZk4ZIc3/lwr3dV+M5nJKd8w2S5 Deio3xp7+qo0oKscRmrdgpZhWXkpXDWmlZ2PwAQwuhtxetgJ70h+Az21CKpAC5edOwpz hcAzBm5xQ08sv546FwimgD664W/Nmqxupj3gh6joPdrocqij/TCjZPE5ccCCTbBwNW5y rZ1nTFRc4IeA3r3eISzAEixYJYBbXE3B3OSw9oggTYVSAd4MMLwcai9f+izkhyTBFOok T2XZi51EY7m8QRWdQO+PVN/fcK/NkeOFZV4X4H1pNXb8/mZjc+J2U2JZQrYUAFG69ibR SwPw== 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=jM1eoIIAt+grvNiuYi3rzmR7kTBktuLf10g4AO+YUOI=; b=JLRjwRlEUJ3/YUGvYRZR3HBjytQ6se6xvJDDT8Cc3Soe7lpvlQjGZxe5FvItvdNzt5 E+5Z72+Th3iixnOStfoo+MhPtwtNJNXaB3yrzNbimJXCoJSc5//gCyMdGK5jrzYCYue/ cMNqzhGFKwgfIbOrEIWzT7Fl5N0zmErDqDQ2AmIIEyqMk/1kQgyZEMG1o7mH7IdYt+KS t5+c1XHaju793G4x3ealw8iqEyIgMzdhE67MqqDINF6v2gYVkuvmt7f7o5CR9lomxZf9 s8l9ZU/DuJYh0E7yFIMFzOTMbNSMWbLEidBXpzxVWjTfTvidNeu25wBKiCYixSDZaeYR ieDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=0y1bvfIt; 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 c1si1227213edr.485.2020.06.17.22.51.32; Wed, 17 Jun 2020 22:51:57 -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=0y1bvfIt; 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 S1727105AbgFRFt1 (ORCPT + 99 others); Thu, 18 Jun 2020 01:49:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727063AbgFRFtY (ORCPT ); Thu, 18 Jun 2020 01:49:24 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FDB6C0613EF for ; Wed, 17 Jun 2020 22:49:22 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id f23so320575iof.6 for ; Wed, 17 Jun 2020 22:49:22 -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=jM1eoIIAt+grvNiuYi3rzmR7kTBktuLf10g4AO+YUOI=; b=0y1bvfItLik1rf3wmKn7Iu980MFvRglKuXHNUxhRgvYIMadB13+ziJIB8021zVD/3i S8wWZ/I1MB9wNT2Og7foF3D2CWDOmQmGASC8WAOPga/NMs3q6tUJudABNh6zM9yINyAp PttGFB7ur2H99nzntubSzEkQDyVNh4EGlGKgU= 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=jM1eoIIAt+grvNiuYi3rzmR7kTBktuLf10g4AO+YUOI=; b=t8OTpBGyZx8zaI05Gm365h3G1zFbYP8zK05IeMvwxrGw9wZFZnFpbBqMB6Z9oE+NZB cWEgjQBHl5ERl0WN2b9cFZ11WqX63syVZVJqRKpOtJTKA41us1rkecGGjxDsTJZSfN4T I3aZk+uk+JYMy0/8vX7apjxQn7zwAcp2HbcmYX1ohBkc1q0KmlYfBmYtDE3oA1xmolrg 6lYzBRx6iBgZnzdX9Jt8RLfIhAXd66ddoTI7DX39KzR2yGdGDEh44c/XS9F561hU+K5U tGRzT3fUcPpLjjMzrn/3We5eZRkv2mUxYGU90KMCEL5nzCKSD/kCzdyOnzpkEdlIuNJJ uGeg== X-Gm-Message-State: AOAM533HsyGslvEqdSoZBCiYKtJ4CcFjRH0Y4xJalHalXcAel4+RhkAK /AOQ3Zt/+seYrAIuT+Q6V9+Mkg== X-Received: by 2002:a02:5a85:: with SMTP id v127mr2785152jaa.83.1592459361462; Wed, 17 Jun 2020 22:49:21 -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 b22sm1146946ios.21.2020.06.17.22.49.20 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 17 Jun 2020 22:49:20 -0700 (PDT) Date: Thu, 18 Jun 2020 05:49:19 +0000 From: Sargun Dhillon To: Kees Cook Cc: linux-kernel@vger.kernel.org, Christian Brauner , Tycho Andersen , David Laight , Christoph Hellwig , "David S. Miller" , 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 v5 3/7] fs: Add fd_install_received() wrapper for __fd_install_received() Message-ID: <20200618054918.GB18669@ircssh-2.c.rugged-nimbus-611.internal> References: <20200617220327.3731559-1-keescook@chromium.org> <20200617220327.3731559-4-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200617220327.3731559-4-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 Wed, Jun 17, 2020 at 03:03:23PM -0700, Kees Cook wrote: > For both pidfd and seccomp, the __user pointer is not used. Update > __fd_install_received() to make writing to ufd optional via a NULL check. > However, for the fd_install_received_user() wrapper, ufd is NULL checked > so an -EFAULT can be returned to avoid changing the SCM_RIGHTS interface > behavior. Add new wrapper fd_install_received() for pidfd and seccomp > that does not use the ufd argument. For the new helper, the new fd needs > to be returned on success. Update the existing callers to handle it. > > Signed-off-by: Kees Cook > --- > fs/file.c | 22 ++++++++++++++-------- > include/linux/file.h | 7 +++++++ > net/compat.c | 2 +- > net/core/scm.c | 2 +- > 4 files changed, 23 insertions(+), 10 deletions(-) > > diff --git a/fs/file.c b/fs/file.c > index f2167d6feec6..de85a42defe2 100644 > --- a/fs/file.c > +++ b/fs/file.c > @@ -942,9 +942,10 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) > * @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. Writes the fd number to userspace. > + * checks and count updates. Optionally writes the fd number to userspace, if > + * @ufd is non-NULL. > * > - * Returns -ve on error. > + * Returns newly install fd or -ve on error. > */ > int __fd_install_received(struct file *file, int __user *ufd, unsigned int o_flags) > { > @@ -960,20 +961,25 @@ int __fd_install_received(struct file *file, int __user *ufd, unsigned int o_fla > if (new_fd < 0) > return new_fd; > > - error = put_user(new_fd, ufd); > - if (error) { > - put_unused_fd(new_fd); > - return error; > + if (ufd) { > + error = put_user(new_fd, ufd); > + if (error) { > + put_unused_fd(new_fd); > + return error; > + } > } > > - /* Bump the usage count and install the file. */ > + /* 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. > + */ > 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; > + return new_fd; > } > > static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) > diff --git a/include/linux/file.h b/include/linux/file.h > index fe18a1a0d555..e19974ed9322 100644 > --- a/include/linux/file.h > +++ b/include/linux/file.h > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include > > struct file; > > @@ -96,8 +97,14 @@ extern int __fd_install_received(struct file *file, int __user *ufd, > static inline int fd_install_received_user(struct file *file, int __user *ufd, > unsigned int o_flags) > { > + if (ufd == NULL) > + return -EFAULT; Isn't this *technically* a behvaiour change? Nonetheless, I think this is a much better approach than forcing everyone to do null checking, and avoids at least one error case where the kernel installs FDs for SCM_RIGHTS, and they're not actualy usable. > return __fd_install_received(file, ufd, o_flags); > } > +static inline int fd_install_received(struct file *file, unsigned int o_flags) > +{ > + return __fd_install_received(file, NULL, o_flags); > +} > > extern void flush_delayed_fput(void); > extern void __fput_sync(struct file *); > diff --git a/net/compat.c b/net/compat.c > index 94f288e8dac5..71494337cca7 100644 > --- a/net/compat.c > +++ b/net/compat.c > @@ -299,7 +299,7 @@ void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm) > > for (i = 0; i < fdmax; i++) { > err = fd_install_received_user(scm->fp->fp[i], cmsg_data + i, o_flags); > - if (err) > + if (err < 0) > break; > } > > diff --git a/net/core/scm.c b/net/core/scm.c > index df190f1fdd28..b9a0442ebd26 100644 > --- a/net/core/scm.c > +++ b/net/core/scm.c > @@ -307,7 +307,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) > > for (i = 0; i < fdmax; i++) { > err = fd_install_received_user(scm->fp->fp[i], cmsg_data + i, o_flags); > - if (err) > + if (err < 0) > break; > } > > -- > 2.25.1 > Reviewed-by: Sargun Dhillon