Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp25055imm; Thu, 27 Sep 2018 15:16:17 -0700 (PDT) X-Google-Smtp-Source: ACcGV62mbTePjny49/a4zlHryRNFffADD3D9EUMsIQUO4H+jQcwG4Om2xr96gANrlJLrQssKETAL X-Received: by 2002:a63:e5e:: with SMTP id 30-v6mr12282860pgo.320.1538086577158; Thu, 27 Sep 2018 15:16:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538086577; cv=none; d=google.com; s=arc-20160816; b=RIoYpAIW+e0Pw0RzaNs5uDg388KMBBpmpXVkbZb0tuSYqv8AZwsL5oCI9Q6GXDlWTM QRVMApJ4Gh9r0npmzhRrYtLBe41kfVcFaM4fZqaNO/xl5VQWv4fr9jF/KL2GXbfMhRQk +i7MjKI2Yf1ExXjOE5LrAVxFgFMev18w7ptKaQ60YC3rOGtatWTRsGQVd7AE+vd/MZOi 80sv1ODpjsESO7CKR2VnRv+V8Ei3NTnxNAgWHl3v6zGGxr5eSKYJHX3UDmfAEo4qsmzv eBoeGaOpTnm5njepnBn6Iq+UkRzMkLNkG62aBDctYeLMp0XB04gx9RMhweEVSL+Lvz2w osEw== 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=vMIkSPx8+z9ANIY70WuvrO206CHBD5Bnwx9SIRCbNv4=; b=l9MGGUpbCMnqQH7BGTbhvCF7B74j1dqkUWQkYon0iKQs6Z+iFkbhz+jpvqjwOlZqd8 RgRKqyDlHXpXhRvlcjPiXru8BnkOuPfnXVcoZqtWEOEVaqW8Jh19+5nkOzPN9ObiLPSq DTKdwog7M1kehUxkpHpEl+oHhpg9wVeIxtD3LFQNI1xM5L00GreDD149nJiMFG0jLc+T n2NzQiSD6musa3UgZDArGmCQ6oXhJig1uMDRj9T1QQdgRIwvQu/D26y8ri3ZrdMIZ75b JnvsrdXvTIhlW2dZx0hMhmfjgRdGCkrPauzRKS+kBeWgznOtX0M2EXPLPqOHTLF17Q8B MukQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=NrvPDGY2; 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 u10-v6si3309177pfd.297.2018.09.27.15.16.01; Thu, 27 Sep 2018 15:16:17 -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=NrvPDGY2; 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 S1727407AbeI1EgN (ORCPT + 99 others); Fri, 28 Sep 2018 00:36:13 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:34071 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725957AbeI1EgM (ORCPT ); Fri, 28 Sep 2018 00:36:12 -0400 Received: by mail-oi1-f194.google.com with SMTP id 13-v6so3649314ois.1 for ; Thu, 27 Sep 2018 15:15:44 -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=vMIkSPx8+z9ANIY70WuvrO206CHBD5Bnwx9SIRCbNv4=; b=NrvPDGY22+P/4p2J/A1pYNO+njsNGG7mf546ERUhhasdBLwm5xtSbijCFeZQ/zQ3rP hCPuWn5QpZs/SBkDe2jrRI0nX8z1BKjJ8YK+4sT8NN5LjWMlnuJly7w5PyFd0B/IQe4L TWSNFSgmxl+GtkGjKSlIltwI+dmxYM/0Pm0S6HFMKPhs+okQz/A2lyz9o9+11uPZvsoK n3BRJD08YoHjuCcC1bURJKOshCjHK/hAofLpQQR5St4o15jbMjDVT/2m9WNc0MgHdfP3 9XJqac1g2PqoISjFiwHQUOtazyPwnW9s1w3W7OIP+x2dlXZS32bTJDDqrhPukptiptSl p9Cg== 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=vMIkSPx8+z9ANIY70WuvrO206CHBD5Bnwx9SIRCbNv4=; b=hAbjebnM2c6Zar4Z4ItQFPxNxhS1kfpZpB3OT3I/SUxQqnee/VlAQG/lt1VD22AB+R oMRzqg3HXxMUiMSHxNsfCTtavV9k3PNUnABG5pOxGu/Qxo9A/7dVALDy+/CNTqkYfw+a RxNeF35w8HaP6huUHFR6zsP8KuEX7wkKarnMJcFaGPTTbCydjw9XSA2Hgx9CQ7Nn/sHc XwgrFiWEYX5FndCvrhjGE4VIOlX9kStpn/Nv677rdGxL9eFjJISPPNIJZybWZOxkXUDh TWr7L73oo+XlE3ETEaRF5edNY8d60gigtaVTqH+PO58uIpZSE8WdPFaHFMnzG2b2noaQ zDyg== X-Gm-Message-State: ABuFfoghAFPDdc3BZLUi0wW6MKjecu+HXKXE4QPgIso5emRVdOTbfrHQ 2mYjsIzOtAv5VTxLXHjHY0ZE0A== X-Received: by 2002:aca:c786:: with SMTP id x128-v6mr4412805oif.43.1538086544294; Thu, 27 Sep 2018 15:15:44 -0700 (PDT) Received: from cisco.cisco.com ([128.107.241.175]) by smtp.gmail.com with ESMTPSA id h22-v6sm1765985otd.21.2018.09.27.15.15.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 27 Sep 2018 15:15:43 -0700 (PDT) Date: Thu, 27 Sep 2018 16:15:40 -0600 From: Tycho Andersen To: Kees Cook Cc: LKML , Linux Containers , Linux API , Andy Lutomirski , Oleg Nesterov , "Eric W . Biederman" , "Serge E . Hallyn" , Christian Brauner , Tyler Hicks , Akihiro Suda , Jann Horn , "linux-fsdevel@vger.kernel.org" Subject: Re: [PATCH v7 5/6] seccomp: add a way to pass FDs via a notification fd Message-ID: <20180927221540.GE15491@cisco.cisco.com> References: <20180927151119.9989-1-tycho@tycho.ws> <20180927151119.9989-6-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 03:09:06PM -0700, Kees Cook wrote: > On Thu, Sep 27, 2018 at 8:11 AM, Tycho Andersen wrote: > > This patch adds a way to insert FDs into the tracee's process (also > > close/overwrite fds for the tracee). This functionality is necessary to > > mock things like socketpair() or dup2() or similar, but since it depends on > > external (vfs) patches, I've left it as a separate patch as before so the > > core functionality can still be merged while we argue about this. Except > > this time it doesn't add any ugliness to the API :) > > > > v7: new in v7 > > > > Signed-off-by: Tycho Andersen > > 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 > > --- > > .../userspace-api/seccomp_filter.rst | 16 +++ > > include/uapi/linux/seccomp.h | 9 ++ > > kernel/seccomp.c | 54 ++++++++ > > tools/testing/selftests/seccomp/seccomp_bpf.c | 126 ++++++++++++++++++ > > 4 files changed, 205 insertions(+) > > > > diff --git a/Documentation/userspace-api/seccomp_filter.rst b/Documentation/userspace-api/seccomp_filter.rst > > index d2e61f1c0a0b..383a8dbae304 100644 > > --- a/Documentation/userspace-api/seccomp_filter.rst > > +++ b/Documentation/userspace-api/seccomp_filter.rst > > @@ -237,6 +237,13 @@ The interface for a seccomp notification fd consists of two structures: > > __s64 val; > > }; > > > > + struct seccomp_notif_put_fd { > > + __u64 id; > > + __s32 fd; > > + __u32 fd_flags; > > + __s32 to_replace; > > + }; > > + > > Users can read via ``ioctl(SECCOMP_NOTIF_RECV)`` (or ``poll()``) on a seccomp > > notification fd to receive a ``struct seccomp_notif``, which contains five > > members: the input length of the structure, a unique-per-filter ``id``, the > > @@ -256,6 +263,15 @@ mentioned above in this document: all arguments being read from the tracee's > > memory should be read into the tracer's memory before any policy decisions are > > made. This allows for an atomic decision on syscall arguments. > > > > +Userspace can also insert (or overwrite) file descriptors of the tracee using > > +``ioctl(SECCOMP_NOTIF_PUT_FD)``. The ``id`` member is the request/pid to insert > > +the fd into. The ``fd`` is the fd in the listener's table to send or ``-1`` if > > +an fd should be closed instead. The ``to_replace`` fd is the fd in the tracee's > > +table that should be overwritten, or -1 if a new fd is installed. ``fd_flags`` > > +should be the flags that the fd in the tracee's table is opened with (e.g. > > +``O_CLOEXEC`` or similar). The return value from this ioctl is the fd number > > +that was installed. > > + > > Sysctls > > ======= > > > > diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h > > index d4ccb32fe089..91d77f041fbb 100644 > > --- a/include/uapi/linux/seccomp.h > > +++ b/include/uapi/linux/seccomp.h > > @@ -77,6 +77,13 @@ struct seccomp_notif_resp { > > __s64 val; > > }; > > > > +struct seccomp_notif_put_fd { > > + __u64 id; > > + __s32 fd; > > + __u32 fd_flags; > > + __s32 to_replace; > > +}; > > + > > #define SECCOMP_IOC_MAGIC 0xF7 > > > > /* Flags for seccomp notification fd ioctl. */ > > @@ -86,5 +93,7 @@ struct seccomp_notif_resp { > > struct seccomp_notif_resp) > > #define SECCOMP_NOTIF_ID_VALID _IOR(SECCOMP_IOC_MAGIC, 2, \ > > __u64) > > +#define SECCOMP_NOTIF_PUT_FD _IOR(SECCOMP_IOC_MAGIC, 3, \ > > + struct seccomp_notif_put_fd) > > > > #endif /* _UAPI_LINUX_SECCOMP_H */ > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > > index 17685803a2af..07a05ad59731 100644 > > --- a/kernel/seccomp.c > > +++ b/kernel/seccomp.c > > @@ -41,6 +41,8 @@ > > #include > > #include > > #include > > +#include > > +#include > > > > enum notify_state { > > SECCOMP_NOTIFY_INIT, > > @@ -1684,6 +1686,56 @@ static long seccomp_notify_id_valid(struct seccomp_filter *filter, > > return ret; > > } > > > > +static long seccomp_notify_put_fd(struct seccomp_filter *filter, > > + unsigned long arg) > > +{ > > + struct seccomp_notif_put_fd req; > > + void __user *buf = (void __user *)arg; > > + struct seccomp_knotif *knotif = NULL; > > + long ret; > > + > > + if (copy_from_user(&req, buf, sizeof(req))) > > + return -EFAULT; > > + > > + if (req.fd < 0 && req.to_replace < 0) > > + return -EINVAL; > > + > > + ret = mutex_lock_interruptible(&filter->notify_lock); > > + if (ret < 0) > > + return ret; > > + > > + ret = -ENOENT; > > + list_for_each_entry(knotif, &filter->notif->notifications, list) { > > + struct file *file = NULL; > > + > > + if (knotif->id != req.id) > > + continue; > > + > > + if (req.fd >= 0) > > + file = fget(req.fd); > > Shouldn't we test for !file here? Yes. Derp. Tycho