Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp221634imm; Thu, 27 Sep 2018 19:47:30 -0700 (PDT) X-Google-Smtp-Source: ACcGV603yfYjscSvy4oeRQmVSwujq638QP30z149GdrilUH9p7FuGOc1nGyiqaW1aZbjemgYk1Ew X-Received: by 2002:a62:670a:: with SMTP id b10-v6mr14446314pfc.243.1538102849925; Thu, 27 Sep 2018 19:47:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538102849; cv=none; d=google.com; s=arc-20160816; b=v2vqZF8iF6IukT01p7L4/3VONJdNLY72gfMkfW72isbh6ElxktGyNGBDRuTBV02HWY 05eTTJcbCm40f+2UOE2jRH0GSeQZcy+yBhzB4s8N/LaPpEbbkg350eqxsrAc1y1AE0uh aGevXFcoDmErZgFx5ynPih3gGh22VqyaYDxxKz4qp4WXejHGbdifPjDxUoHP6Ih6YrMf kX1KRlf3gaaN6d6smQ18FXA5kvONmontkyvcmvjNKMXJ8K2heroDVYOLAWOet42TzEPc xOdgEqcVwkSKVbUecLWTUBVT7mpbYzFsCd9MSxYF5S9pajWiRbG4B5fir3ahARuQnhby Im7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=PW7NN9PdF8Bhzhl1yD2glQ+K46HI7lE2i7BL23tLIK4=; b=S4pWTc2UNckz7rzeumoqRkfhpMz97OM40OsijVxaNZk6E+hVsSjIWn9DS0xRNPkY+s ngeMkdHNZfYILXLuNKMjobyjinGFEr58o3+1aL+0FEcLloTI+zaE8czn/VKlZi+ErxwU nLlP3Z4MQguhiQdeLM0EASfVFrqRigYCDu2urmdaqYAFrvQWMW48XevCkyEmlKH0yZJU A7irVeT2OUVsJu7A9NIr0O60Z8HPXQcCkA4hAMtV4H+lPWm5kjRxABBRvfsdD7CO84Ws pGpzIBrFAjFEdEdtEgXu/fsy8i9sweM9ByPS+XvMdJOcZtSP35HrtDFOmiz2+d10nRPy yWGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vI13q0x0; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h9-v6si3876538plk.461.2018.09.27.19.47.00; Thu, 27 Sep 2018 19:47:29 -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=@google.com header.s=20161025 header.b=vI13q0x0; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727389AbeI1JIW (ORCPT + 99 others); Fri, 28 Sep 2018 05:08:22 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:33252 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726121AbeI1JIW (ORCPT ); Fri, 28 Sep 2018 05:08:22 -0400 Received: by mail-oi1-f195.google.com with SMTP id a203-v6so4085549oib.0 for ; Thu, 27 Sep 2018 19:46:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PW7NN9PdF8Bhzhl1yD2glQ+K46HI7lE2i7BL23tLIK4=; b=vI13q0x0tFwJClbfYoHmAx1MyNazWl4+uDj2nQKLXoCP3NQZA9dk60vOyYJljUOd77 jWZ5EHiHANywgZl0shlo4Yr0Xw3V/BeeLRypIXn0+lWvferg79yeHUwpSsxyIdSk2rwR vlUDnm9Jh2rbO4vZj/KhiTyiI4K4ULabUsWyTzixLYZ0FISb6YMFcnVhCiTLDBFflz5U qQz5Lvkf/LH0mNOmtjCWtgadoFOgvR7HnVZuRfcj3waaPkxsRucTNZ1BCJeNuI795IOH Tt1vH59AW34dyEWBVXYON0U4P1OMCQaxsgGE5n+VrXEM7duDTTQRQA9lTTKuFa1uEHVJ Kljw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PW7NN9PdF8Bhzhl1yD2glQ+K46HI7lE2i7BL23tLIK4=; b=Fsw9C+QFXvK5bUQlf0UHaO2UGR3/04AJq4caOoR/iEt5jyoZ+wm1i5H+lkkORqulUS sRbHbzqd+CH6/MIzsIAtn72QaOqydmPZpijy2sFLXxnvmPNAcY1Vb2Lh3bQKDtnXeX2P 1u6nTmhhpKbY/Bi+aoYfgvVBr6m9z0eZtYWMq3ZI3rMDm9gVBqHjiRnSsBmGY4OIB8ZB ZdK/+mRrXJxb902yajuEymHD+IDo9cbjBiYc6ODaM/gZyl2j6GdBaIXe1jjXS7XyjiHw y3NTzJ8xfkXX3QtAMzQL/sV/umvHt58lYCdajGraPKe49Q/tYWnXdLNdyrWqgFINeUrf tZrA== X-Gm-Message-State: ABuFfoiTjuUDcfBQUuySS21Z4fcXyAyYt11ls4Bna0fLPR/QWUSEfmTJ N4ZBDQVyOn23Iagmu/HwV51l3Swm5ChBp0h2lW7k/Q== X-Received: by 2002:aca:b844:: with SMTP id i65-v6mr4383782oif.177.1538102813990; Thu, 27 Sep 2018 19:46:53 -0700 (PDT) MIME-Version: 1.0 References: <20180927151119.9989-1-tycho@tycho.ws> <20180927151119.9989-5-tycho@tycho.ws> In-Reply-To: From: Jann Horn Date: Fri, 28 Sep 2018 04:46:26 +0200 Message-ID: Subject: Re: [PATCH v7 4/6] files: add a replace_fd_files() function To: Kees Cook Cc: Tycho Andersen , 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 Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 28, 2018 at 4:20 AM Kees Cook wrote: > On Thu, Sep 27, 2018 at 2:59 PM, Kees Cook wrote: > > On Thu, Sep 27, 2018 at 8:11 AM, 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. > >> > >> v7: new in v7 > >> > >> Signed-off-by: Tycho Andersen > >> CC: Alexander Viro > >> CC: Kees Cook > >> CC: Andy Lutomirski > >> CC: Oleg Nesterov > >> CC: Eric W. Biederman > >> CC: "Serge E. Hallyn" > >> CC: Christian Brauner > >> CC: Tyler Hicks > >> CC: Akihiro Suda > >> --- > >> fs/file.c | 22 +++++++++++++++------- > >> include/linux/file.h | 8 ++++++++ > >> 2 files changed, 23 insertions(+), 7 deletions(-) > >> > >> 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; > > > > Same feedback as Jann: on a purely "smaller diff" note, this could > > just be s/current/task/ here and all the other s/files/task->files/ > > would go away... > > > >> > >> 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); > > > > Perhaps call this __replace_fd() to indicate the "please don't use > > this unless you're very sure"ness of it? > > > >> extern void set_close_on_exec(unsigned int fd, int flag); > >> extern bool get_close_on_exec(unsigned int fd); > >> extern int get_unused_fd_flags(unsigned flags); > >> -- > >> 2.17.1 > >> > > > > If I can get an Ack from Al, that would be very nice. :) > > In out-of-band feedback from Al, he's pointed out a much cleaner > approach: do the work on the "current" side. i.e. current is stopped > in __seccomp_filter in the case SECCOMP_RET_USER_NOTIFY. Instead of > having the ioctl-handing process doing the work, have it done on the > other side. This may cause some additional complexity on the ioctl > return path, but it solves both this problem and the "ptrace attach" > issue: have the work delayed until "current" gets caught by seccomp. Can you elaborate on this? Are you saying you want to, for every file descriptor that should be transferred, put a reference to the file into the kernel's seccomp notification data structure, wake up the task that's waiting for a reply, let the task install an fd, send back a response on whether installing the FD worked, and then return that response back to the container manager process? That sounds like a pretty complicated dance that I'd prefer to avoid.