Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1667929imm; Sat, 2 Jun 2018 06:14:40 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI39ao867iX/uqfWUGioZ6FY82RRkc3dNG0quNCJI4HvrO+JrncyJZifNobZsvW3jnjx501 X-Received: by 2002:a17:902:3281:: with SMTP id z1-v6mr14696468plb.226.1527945280471; Sat, 02 Jun 2018 06:14:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527945280; cv=none; d=google.com; s=arc-20160816; b=qCC9I5xf7Z9vfv+YTXRF2UfgTt8Kdv3NQrrs8CBOUoW7qq0/D+E8hx2BwJd/mmduP5 VznifsznHqorqJY/UxkGDo08/cyKImfNfbBoiIpd5m3gMkjMNxsUECNPeSo5PetO7ZrO L4q1vz4KRo1ZDL/qEeiNa57YpY6bt9owhVylPmwXLhi2quxTrJtwxrcLiewNQNa+SEVK jE4fa7S86hIii81d9K/W7wO3GwgUhgaQlUyp+nYoprv6QfKQpzTYt0dGdeamxN6zO69D Ja1D4z9FD0yieuTVAYtJ65KTDKmWCokReqvZGZ435V1AQsYzt25juRKSb7nG1qn5W4LW zQWQ== 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 :arc-authentication-results; bh=D4KwFFOBzjP84AJwzzNpMcSX9IlUr4kQ6CkycldMAUk=; b=YvzHrUUa8ssPlOyFUb2F13Pw+s7LL7YtpRkDum/TjdNTUqSKRvAanag4X3oUcGLmv/ TNSu1sWwfGH5YUMEHCYBIJQ29SOlok6NjTPAatz3f9hIhyCT3pXSxcDof6h5YatTUKtt rLt56ERqShUx6s2P7EK/SHdyNyik3OW7Id1hYKU/b/pUDEwfiPEweIr8zgJ9U3s58QD0 RGOZwQ0+cGHDFbE6nsRpygEmXcN1/vdsw6Jd0EdP5eMHCAvOtXtBOhYZJnhKTNc0DHC4 OkemfB40ct9MDtHwZpm3TI6fInf5PsWxAXPGHKmP1LGkuIDIUj5AnKOMMPQQb1BdA6rp j5nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TaWw/dxJ; 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 38-v6si42969161plc.446.2018.06.02.06.14.25; Sat, 02 Jun 2018 06:14:40 -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=TaWw/dxJ; 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 S1751553AbeFBNNx (ORCPT + 99 others); Sat, 2 Jun 2018 09:13:53 -0400 Received: from mail-ot0-f195.google.com ([74.125.82.195]:44682 "EHLO mail-ot0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750793AbeFBNNv (ORCPT ); Sat, 2 Jun 2018 09:13:51 -0400 Received: by mail-ot0-f195.google.com with SMTP id w13-v6so5011200ote.11 for ; Sat, 02 Jun 2018 06:13:51 -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=D4KwFFOBzjP84AJwzzNpMcSX9IlUr4kQ6CkycldMAUk=; b=TaWw/dxJsDGKqPoh1CjT0GG6b+m83f9ymF+hmMUhrmSgB+ge43QyPNQ0U4OMnpDmAf Duy7hl7NsGUmILlJDkY9H4GFvUPFs1x+zysopgI5R8HdUIieMJJWdsJH1wiwhApLBLsk 2T9gopZSf7BrJ0DDYCJNtULJ38FXQKdEbyjOoC0wr1wqjb8AaVcwbiBbftLO7l0Vo638 7aY7V/dnmAYSKOwIZoNxA4zPovA7i/WLKrgt+/j7VRJp6CAA58jN9cT2O9dhdM9RzloI VQf8UqIEp0U8M46v9B2qNuLQHHSx3kdhQVjxrLoWcd4KhS8/Weq7LtUt+t6/83g7gNIb gPLQ== 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=D4KwFFOBzjP84AJwzzNpMcSX9IlUr4kQ6CkycldMAUk=; b=Zzqlj43a/dZLFMBXYQnTKAkWGiEB1QvNzjLIZ1Trdm5qvAjJ+G7z6gSpK7iYlHmfV8 OaYNq+gsKas4gi0K9q6tSZdm2DbE3YdSDN928ydF7+qergvfiaiJvcfXiuJMjFiUvyOz s2+VdLzvh42THUk8grv/ZWbcNxoGNKH9kDNTgaO16GN1eVE5zjkj8wJCx+xqb9uXPxvW DcC3SvSfrwNZlUi5ZHuluvEw7UXgp7QN/90vRJELNcNDmR2UK3E8bK8hOf5m3iop6gNW X4MhH4HnNyZn4ohfm6oXC+ZigROxro8O/bLvxhqCBkwc0GU09ikjAeY9FNLGjEjQe/d3 7CPg== X-Gm-Message-State: ALKqPweuCadODZHRjP7GUo0Eogwe+jfX/O3LYERk9qsfdl0xfXnBa6JI KMkQFhAoJFk+RnhBjHrY97buSmtEOOQKxbateAEgXA== X-Received: by 2002:a9d:1192:: with SMTP id v18-v6mr9400995otf.254.1527945230745; Sat, 02 Jun 2018 06:13:50 -0700 (PDT) MIME-Version: 1.0 References: <20180531144949.24995-1-tycho@tycho.ws> <20180531144949.24995-5-tycho@tycho.ws> In-Reply-To: <20180531144949.24995-5-tycho@tycho.ws> From: Jann Horn Date: Sat, 2 Jun 2018 15:13:39 +0200 Message-ID: Subject: Re: [PATCH v3 4/4] seccomp: add support for passing fds via USER_NOTIF To: tycho@tycho.ws Cc: kernel list , containers@lists.linux-foundation.org, Kees Cook , Andy Lutomirski , Oleg Nesterov , "Eric W. Biederman" , "Serge E. Hallyn" , christian.brauner@ubuntu.com, Tyler Hicks , suda.akihiro@lab.ntt.co.jp, "Tobin C. Harding" 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 Sat, Jun 2, 2018 at 2:58 PM Tycho Andersen wrote: > The idea here is that the userspace handler should be able to pass an fd > back to the trapped task, for example so it can be returned from socket(). > > I've proposed one API here, but I'm open to other options. In particular, > this only lets you return an fd from a syscall, which may not be enough in > all cases. For example, if an fd is written to an output parameter instead > of returned, the current API can't handle this. Another case is that > netlink takes as input fds sometimes (IFLA_NET_NS_FD, e.g.). If netlink > ever decides to install an fd and output it, we wouldn't be able to handle > this either. > > Still, the vast majority of interesting cases are covered by this API, so > perhaps it is Enough. > > I've left it as a separate commit for two reasons: > * It illustrates the way in which we would grow struct seccomp_notif and > struct seccomp_notif_resp without using netlink > * It shows just how little code is needed to accomplish this :) [...] > + fd = get_unused_fd_flags(n.flags); Here, you're using n.flags in a context where it will be tested against O_CLOEXEC to determine whether the new fd should be close-on-exec. [...] > + /* > + * This is a little hokey: we need a real fget() (i.e. not > + * __fget_light(), which is what fdget does), but we also need > + * the flags from strcut fd. So, we get it, put it, and get it > + * again for real. > + */ > + fd = fdget(resp.fd); > + knotif->flags = fd.flags; > + fdput(fd); > + > + knotif->file = fget(resp.fd); > + if (!knotif->file) { > + ret = -EBADF; > + goto out; > + } But here fd.flags contains the low 2 bits of the return value of __fget_light, which are either 0 or FDPUT_FPUT (encoded as 1). This flag states whether fdget() took a reference on the file, which is mostly equivalent to "is the current process multithreaded?". (This is the reason why fdget returns flags and fget doesn't - the flag from fdget is to decide whether you'll need an fput(), which is unconditional for fget().) Apart from this issue, I think that in general, it's probably not a good idea to copy the close-on-exec flag from the fd in the supervising process - the supervising process might want all the fds it is working with to be O_CLOEXEC independent of whether the supervised process wants an O_CLOEXEC fd. It might make sense to add a field for this to struct seccomp_notif_resp instead.