Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3008487imm; Sun, 3 Jun 2018 17:18:54 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKgdHZPlxvahMJOEc/vHRX/vTpBOam/QdrG+KYyV8PrdVVghLM7MnyriARbfL73TyAfRyYH X-Received: by 2002:a63:7b4e:: with SMTP id k14-v6mr15452643pgn.261.1528071534162; Sun, 03 Jun 2018 17:18:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528071534; cv=none; d=google.com; s=arc-20160816; b=qAq/3e9nsz+Svi+k1cddMb5jKvn2Mr2ccZSaYU5jdVhRQ7enR8Jtftz48X52dRO9Jl S64/ZfqzsBb5CSaUMG3tAAwwiFv6CQssRzzYAPQC/jhjusTF4+rbTybw9deVW07ChnXh wcc195Wz1LfFXge+96flCurW+FVh70wdIx88ZpfZg1Tua/5DUhMmTEvNXr7O+DXGOqm+ 1P5A9kT5xHtBk3dBPeDWVIocfbl7FJqjNhQAnL1lZACYliLdHVpZpU6ynY7rxZ0a8z1W udEDT5kfRrHC5/o2V6l0s3iQ5AJtV8EAyCvBXMDOK1QJ04mYCBipJHxYG/S1iD6p4+aS et0A== 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:arc-authentication-results; bh=7bHLkBm79kmWRUf/ZYhEKrQ4zQrx8xI/3LomMGFDWp0=; b=mT1wVpGDWLp5Oe17IrDmdthecc0hWMSeB4Z61ZFmcioNG63mO0BDeyimQImy1AZzQ5 0fayOY5WCdTjDtPJRJVchFUD+1/hzPAVL2virBOEev5qiRouJZ6hM7ixK1oR7YjZY+ME 4AVc3YODyVbS8bxL0sLQQta6DIKNYT85sXBp7VMpaIn+V92Bv80irlRM5pY6iJAZ5L7M 2rq6lnAQcgFIXYdFwTUr9leVtXuAxAF14S2TclpMLCacrB6oF3comsORDEvDinNvOzEa Rwbf23LgMOAu6xANwTad7ccuj8K5kMj2xgB0RFwfytk+Myv6YhqnfBXMxscAl3wyS2JT CifA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=vYQ5ueRx; 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 h126-v6si5329642pfg.126.2018.06.03.17.18.39; Sun, 03 Jun 2018 17:18:54 -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=vYQ5ueRx; 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 S1751603AbeFDASR (ORCPT + 99 others); Sun, 3 Jun 2018 20:18:17 -0400 Received: from mail-io0-f194.google.com ([209.85.223.194]:42652 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751444AbeFDASP (ORCPT ); Sun, 3 Jun 2018 20:18:15 -0400 Received: by mail-io0-f194.google.com with SMTP id r24-v6so1397917ioh.9 for ; Sun, 03 Jun 2018 17:18:15 -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=7bHLkBm79kmWRUf/ZYhEKrQ4zQrx8xI/3LomMGFDWp0=; b=vYQ5ueRxxnDTlk8EtNBytNm4QBLKZqwu3yMCw8Igp9EWjJcupmyGhk+/DhTqaOpN1u ix1LvYdXNWi0V4KKcfAmG5LA9kBa7wWvcE8GZnTcDIpQBi0r8UwR8BDYVG8X3NYJNAeW 4lWiPAwtw5KLBETlL79cmSWw5bbXktHJRwD19+4UK6zNjcz/meidbdlVQ6bI05q1v1cj M/xcVxau+p4pC4Iv9tOIR12zCoR0jlI0iFzNxTXrqmJo9F8H2SVrfr/9Lpr+gC3KhtVx os8YD2OyMQyQCFSBAkwy6lptDfyk4vzX1nnu9JfmmNBAtomQve3OY1G2ySyfq8zW0ias UvCw== 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=7bHLkBm79kmWRUf/ZYhEKrQ4zQrx8xI/3LomMGFDWp0=; b=oce0ZRGK4h++ghDfL8p/okH3oZGEWv6PEsfAe5hWmQFraO415q7sAdiyp8Esj405O7 nlxMAIqE3W48OsoYhIrZhRCaUTI+bGu084/QZynoOgXCj2GE+6FuEt+GJbKJDOKMy+bs BucqY7de5hu7nQBKMirmMNnFxCR2EjeK8c9cnOp6zj/NvLdvzEZSm6S1kdAuPJGLlfIE UCpK5jXcyw6nI6LTHre11123mnOkMrASwObMT+o+4ZhEfwsu9UjaUkFIvF9PlUZ7HgJo TGIWEiQ7orNxeW6ouP5HsnFUL7JeZKfSjmXDlYd2UgPIDQMBGhRiiaRxMg4M8Qxjg5XO f7Cw== X-Gm-Message-State: APt69E1B0luDIXVFV+ZeST1q/0VjQCEuIFj5n4+3Rlz6V8SRTWocB8aP elsE2WyEenFEW1QnAezAlzDwwg== X-Received: by 2002:a6b:15c6:: with SMTP id 189-v6mr18499241iov.141.1528071494808; Sun, 03 Jun 2018 17:18:14 -0700 (PDT) Received: from cisco ([8.24.24.129]) by smtp.gmail.com with ESMTPSA id e6-v6sm1726743iog.7.2018.06.03.17.18.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jun 2018 17:18:13 -0700 (PDT) Date: Sun, 3 Jun 2018 18:18:12 -0600 From: Tycho Andersen To: Jann Horn 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" Subject: Re: [PATCH v3 1/4] seccomp: add a return code to trap to userspace Message-ID: <20180604001812.GE15998@cisco> References: <20180531144949.24995-1-tycho@tycho.ws> <20180531144949.24995-2-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 Hi Jann, On Sun, Jun 03, 2018 at 08:41:01PM +0200, Jann Horn wrote: > On Sun, Jun 3, 2018 at 2:29 PM Tycho Andersen wrote: > > > > This patch introduces a means for syscalls matched in seccomp to notify > > some other task that a particular filter has been triggered. > > > > The motivation for this is primarily for use with containers. For example, > > if a container does an init_module(), we obviously don't want to load this > > untrusted code, which may be compiled for the wrong version of the kernel > > anyway. Instead, we could parse the module image, figure out which module > > the container is trying to load and load it on the host. > > > > As another example, containers cannot mknod(), since this checks > > capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or > > /dev/zero should be ok for containers to mknod, but we'd like to avoid hard > > coding some whitelist in the kernel. Another example is mount(), which has > > many security restrictions for good reason, but configuration or runtime > > knowledge could potentially be used to relax these restrictions. > > > > This patch adds functionality that is already possible via at least two > > other means that I know about, both of which involve ptrace(): first, one > > could ptrace attach, and then iterate through syscalls via PTRACE_SYSCALL. > > Unfortunately this is slow, so a faster version would be to install a > > filter that does SECCOMP_RET_TRACE, which triggers a PTRACE_EVENT_SECCOMP. > > Since ptrace allows only one tracer, if the container runtime is that > > tracer, users inside the container (or outside) trying to debug it will not > > be able to use ptrace, which is annoying. It also means that older > > distributions based on Upstart cannot boot inside containers using ptrace, > > since upstart itself uses ptrace to start services. > > > > The actual implementation of this is fairly small, although getting the > > synchronization right was/is slightly complex. > > > > Finally, it's worth noting that the classic seccomp TOCTOU of reading > > memory data from the task still applies here, but can be avoided with > > careful design of the userspace handler: if the userspace handler reads all > > of the task memory that is necessary before applying its security policy, > > the tracee's subsequent memory edits will not be read by the tracer. > [...] > > @@ -857,13 +1020,28 @@ static long seccomp_set_mode_filter(unsigned int flags, > > if (IS_ERR(prepared)) > > return PTR_ERR(prepared); > > > > + if (flags & SECCOMP_FILTER_FLAG_GET_LISTENER) { > > + listener = get_unused_fd_flags(O_RDWR); > > I think you want either 0 or O_CLOEXEC here? Do we? I suppose it makes sense to be able to set CLOEXEC, but I could imagine a case where a handler wanted to fork+exec to handle something. I'm happy to make the change, but it's not obvious to me that it's what we want by default. > > +out_put_fd: > > + if (flags & SECCOMP_FILTER_FLAG_GET_LISTENER) { > > + if (ret < 0) { > > + fput(listener_f); > > + put_unused_fd(listener); > > + } else { > > + fd_install(listener, listener_f); > > + ret = listener; > > + } > > + } > > out_free: > > seccomp_filter_free(prepared); > > return ret; > [...] > > +static __poll_t seccomp_notify_poll(struct file *file, > > + struct poll_table_struct *poll_tab) > > +{ > > + struct seccomp_filter *filter = file->private_data; > > + __poll_t ret = 0; > > + struct seccomp_knotif *cur; > > + > > + ret = mutex_lock_interruptible(&filter->notify_lock); > > + if (ret < 0) > > + return ret; > > + > > + list_for_each_entry(cur, &filter->notifications, list) { > > + if (cur->state == SECCOMP_NOTIFY_INIT) > > + ret |= EPOLLIN | EPOLLRDNORM; > > + if (cur->state == SECCOMP_NOTIFY_SENT) > > + ret |= EPOLLOUT | EPOLLWRNORM; > > + } > > + > > + mutex_unlock(&filter->notify_lock); > > + > > + return ret; > > +} > > I don't think f_op->poll handlers work like this. AFAIK you're > supposed to use something like poll_wait() to connect the caller to > something like a waitqueue head, so that as soon as the file becomes > ready for reading/writing, any waiting task is notified. See > eventfd_poll() in fs/eventfd.c for a simple example. AFAICS in the > current code, seccomp_notify_poll() only works if an event is pending > at the time seccomp_notify_poll() is called. Arg. I was trying to avoid adding yet another piece of synchronization, but perhaps it's not possible. Thanks for pointing this out. Tycho