Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2335328imm; Thu, 27 Sep 2018 11:04:53 -0700 (PDT) X-Google-Smtp-Source: ACcGV6035IsMo45i+IrsvAjkECkYadd5H4uc1iR4q7s6U74Eq5IAzkSguzZBy4nh/J+1/Rlax/tM X-Received: by 2002:a62:215b:: with SMTP id h88-v6mr7737455pfh.233.1538071493700; Thu, 27 Sep 2018 11:04:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538071493; cv=none; d=google.com; s=arc-20160816; b=b9EYaRqchqs9Li99T/6lzoo8A/cR3vEju1YHm/LXC6tdk1Bi+QMywBwq0JvMf3Gwdk jUTAOMoWHHjJqPO7VqMfJ1JPoksa/QIn0TPGK8O+nYPTkym/OfPKt15v8hocB9Xc2WHy jvVI5bNA3HsDGQcgyp+WDjNvLvg0N26SkIeix5PY6TWVwRvjrTmO43pm2PbRa9xWPNP7 Gj+FVzSPWB44eDNB9Q/B1xq490wp0+eRqadKIJvj+EaxvuPpQLG135kHN/lhYux3IPEH O0dRtc3A2QSMnktgz3P504geeKwYy7B35W531mLpSzGg2/pvzFJrRHlihb/Mc2kUzWup A6lg== 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=PVeVP6wrlR/Kkx+zEt2gMYHwcYZRXu1qGypYVgaZHnE=; b=A5PtAYjFhu+KCex64YjCPuGMc9cV5xXUNj0uHnNft11Mdtqtk+dZ/Vfa8XOsypzB46 2gUqnGZjoVKasylLGlsGQ0bMlw4JE2GW5VaPtujJGMFi21llSbyvROlaa4rOUGcKJtgX /l6mlpaHHhPtUXsRgwYD6CdSsZo2KhAikeNcXCDFk2VUAFi84Cfu1r+SBKQ8iFdy/WUX Q5uwgz97QRmMGYFTDFdAT110U4sCJ6CbgEQgcF4/1t5f3eOtIFjtbhhzy7EIUbSLkldT tp90WVRpGyD/v7s7q1BaA5wAEPiHyu32FME6kuvOju/MnUKpdoC3SYB7PxYCK3nefWa5 nT4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b="z6BUNv5/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 62-v6si2603140ple.450.2018.09.27.11.04.27; Thu, 27 Sep 2018 11:04:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b="z6BUNv5/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728213AbeI1AXr (ORCPT + 99 others); Thu, 27 Sep 2018 20:23:47 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:46072 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727612AbeI1AXr (ORCPT ); Thu, 27 Sep 2018 20:23:47 -0400 Received: by mail-pf1-f194.google.com with SMTP id a23-v6so2427765pfi.12 for ; Thu, 27 Sep 2018 11:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho-ws.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=PVeVP6wrlR/Kkx+zEt2gMYHwcYZRXu1qGypYVgaZHnE=; b=z6BUNv5//GXBiVsZbbiGiU9oUlaneXWUku1rbe0LuupaZwaTIhXL7dZu7bxA/vvEzm cc7Zw/oirGZjQltOqoTleNt2NdZ38UmaX6KUqYbD6jwTCbQlY+DMITgzCRue+Jdfi0cX ++xTcxORF1XwqWwSQJt7EMEeGaISpIlygpbfl9edQQ7TnaAD+fPycx29k0NDMScj0Eau Wj8nQguhPF/BVK/EWjTJVVied2SCijcGrMvLPy4KCt80yd8p1SRK7itdu+FAS329GXO2 /YM923Dq6EvwT9WfDrJG83dZL2YkE8COuf7sbk6cO3gBzs4Cmhnu6G0NozJiUhxb3Aee HjNg== 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=PVeVP6wrlR/Kkx+zEt2gMYHwcYZRXu1qGypYVgaZHnE=; b=BoeZslOrSm5IXxzzqn+azm5bOM09yMRrwU1rg28WKeuUgFtOK+8hKbtufXKN+c4xlG LVKwseX4vzqqhgvKhjUR6VnI32Xr0O8KQH1JTFOKzAe7A00jyJW6WeDeUHpQNcisop2r qEeR1hIIcvynen9B6efnJ72zQgkQW4aihmCopg+iORdi55E28BnYgFtOTjvBTLLp6nG1 etWsm43IDVyXUxMjRYVGLq5wvj/X+m9On6sAH2xidiRdLAKtKtJSt1WBmCSLo9nml2jf 1I8xMMK20B2jOXZCVXlSzA4kL5b+vuHxB8DyFviGtkiHWAOH13H/NftL5K22akAetrbQ XRwg== X-Gm-Message-State: ABuFfogs9l8ZlbHHyP2Yf1KP0qDAMMynGWssuhI5zn+jQuDsMsI7Bawy hQVlbeFJvJMOamm5MHL/RpCdPA== X-Received: by 2002:a63:ac11:: with SMTP id v17-v6mr11379710pge.196.1538071459590; Thu, 27 Sep 2018 11:04:19 -0700 (PDT) Received: from cisco.cisco.com ([128.107.241.165]) by smtp.gmail.com with ESMTPSA id b76-v6sm6171562pfj.16.2018.09.27.11.04.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 27 Sep 2018 11:04:18 -0700 (PDT) Date: Thu, 27 Sep 2018 12:04:15 -0600 From: Tycho Andersen To: Jann Horn Cc: Kees Cook , kernel list , containers@lists.linux-foundation.org, Linux API , Andy Lutomirski , Oleg Nesterov , "Eric W. Biederman" , "Serge E. Hallyn" , Christian Brauner , Tyler Hicks , suda.akihiro@lab.ntt.co.jp, linux-fsdevel@vger.kernel.org, Al Viro Subject: Re: [PATCH v7 4/6] files: add a replace_fd_files() function Message-ID: <20180927180415.GA15491@cisco.cisco.com> References: <20180927151119.9989-1-tycho@tycho.ws> <20180927151119.9989-5-tycho@tycho.ws> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Thu, Sep 27, 2018 at 06:49:02PM +0200, Jann Horn wrote: > On Thu, Sep 27, 2018 at 5:11 PM Tycho Andersen wrote: > > Similar to fd_install/__fd_install, we want to be able to replace an fd of > > an arbitrary struct files_struct, not just current's. We'll use this in the > > next patch to implement the seccomp ioctl that allows inserting fds into a > > stopped process' context. > [...] > > diff --git a/fs/file.c b/fs/file.c > > index 7ffd6e9d103d..3b3c5aadaadb 100644 > > --- a/fs/file.c > > +++ b/fs/file.c > > @@ -850,24 +850,32 @@ __releases(&files->file_lock) > > } > > > > int replace_fd(unsigned fd, struct file *file, unsigned flags) > > +{ > > + return replace_fd_task(current, fd, file, flags); > > +} > > + > > +/* > > + * Same warning as __alloc_fd()/__fd_install() here. > > + */ > > +int replace_fd_task(struct task_struct *task, unsigned fd, > > + struct file *file, unsigned flags) > > { > > int err; > > - struct files_struct *files = current->files; > > Why did you remove this? You could just do s/current/task/ instead, right? No reason, probably just flailing around trying to figure out what exactly I wanted. I'll make the change, thanks. > > if (!file) > > - return __close_fd(files, fd); > > + return __close_fd(task->files, fd); > > > > - if (fd >= rlimit(RLIMIT_NOFILE)) > > + if (fd >= task_rlimit(task, RLIMIT_NOFILE)) > > return -EBADF; > > > > - spin_lock(&files->file_lock); > > - err = expand_files(files, fd); > > + spin_lock(&task->files->file_lock); > > + err = expand_files(task->files, fd); > > if (unlikely(err < 0)) > > goto out_unlock; > > - return do_dup2(files, file, fd, flags); > > + return do_dup2(task->files, file, fd, flags); > > > > out_unlock: > > - spin_unlock(&files->file_lock); > > + spin_unlock(&task->files->file_lock); > > return err; > > } > > > > diff --git a/include/linux/file.h b/include/linux/file.h > > index 6b2fb032416c..f94277fee038 100644 > > --- a/include/linux/file.h > > +++ b/include/linux/file.h > > @@ -11,6 +11,7 @@ > > #include > > > > struct file; > > +struct task_struct; > > > > extern void fput(struct file *); > > > > @@ -79,6 +80,13 @@ static inline void fdput_pos(struct fd f) > > > > extern int f_dupfd(unsigned int from, struct file *file, unsigned flags); > > extern int replace_fd(unsigned fd, struct file *file, unsigned flags); > > +/* > > + * Warning! This is only safe if you know the owner of the files_struct is > > + * stopped outside syscall context. It's a very bad idea to use this unless you > > + * have similar guarantees in your code. > > + */ > > +extern int replace_fd_task(struct task_struct *task, unsigned fd, > > + struct file *file, unsigned flags); > > I think Linux kernel coding style is normally to have comments on the > implementations of functions, not in the headers? Maybe replace the > warning above the implemenation of replace_fd_task() with this > comment. Will do. Cheers, Tycho