Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2669948ybt; Tue, 16 Jun 2020 11:51:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx00i0Ul8FqDUHvaGorpMGvtzHtIworzY8sn58rXpOKTgBqerXByNddncD0ptMULvANyKky X-Received: by 2002:a17:906:e0c:: with SMTP id l12mr3955455eji.435.1592333514399; Tue, 16 Jun 2020 11:51:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592333514; cv=none; d=google.com; s=arc-20160816; b=zIyxrIupX06bCdBZB4I7XCkp040USk8EC8XTG7UPkZBbdyGY66drZEUmH2aE1f/Ok5 3zIoFvFsg5XrnAYpcD4+0X1uRcoo9zukdW8GAevV5L9fWBy3ycGGk4QmCC+9VkQg2abY NopSsDv+TWpQPpY59OX9TVm+yDH9/96bTslIoHSeJKUEfKyoYAakwp/8y/14TvMJZWdL UCBlMFCMAhR5q98AsmmB1QrCiNi03ZZhPgIAfiGZ18FPPTqPgjuoJsJRNOw2zcw5mnIb WPD+FbUtsmZggM5xCcmU+G3SG4ywk4H2tCt8PTmRJYflrDZ7RVf/+53svJGk5StGC+0R vDlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=b8hRDF1XXrhu33JiDGyXvZcU8whbvzEGz0i9JpZbnTw=; b=Qq4E2OmhfUaeUQQNvjVLNfpAVqupr6N3/iM6wS0AD7g+F8d9arkS8SMI4Kk/XMCvSI q1ud8MgLtKQuJzs/tPjMoh5CDMyo/EsAOXa5g/TJmSSNgQfBhNF4QFXx83qkkqsrYEff zeLX4kAxqmvgPmS8MP5HB/IWZLYG68FbD3WEaA/Me3kMBze2VNHMsEXI4iN736V0t6xF hQnsF84z9vHtfxMB1frdTLX8b1w0+b7OwSN1yGItxJP1Q+/4WlFYf/oZHeede6iV2OD9 hbZ2sGmzAZXV+DTRPxXxhhKl7MWhrHIuPw+xeiwkmXTuNsoIGgkFdt7AYnK8VE0THOh/ 3LyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="d1hgB/Jf"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si10484814edr.432.2020.06.16.11.51.32; Tue, 16 Jun 2020 11:51:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="d1hgB/Jf"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730196AbgFPStl (ORCPT + 99 others); Tue, 16 Jun 2020 14:49:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729452AbgFPStk (ORCPT ); Tue, 16 Jun 2020 14:49:40 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05CD5C06174E for ; Tue, 16 Jun 2020 11:49:39 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id b5so9916501pfp.9 for ; Tue, 16 Jun 2020 11:49:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=b8hRDF1XXrhu33JiDGyXvZcU8whbvzEGz0i9JpZbnTw=; b=d1hgB/JfsbnTPAahOwwwk5u0Ves0Pd6Np/WEpvfWvNicPrEwEbwW6uhgYBPbHqFmUd pqtLB52Wexz5oocb976Fc/Pe1WmQYH1IBjio5pDJErc1ZsgHAMAfaKk84Tw0uy9PAFsv yHqRw5O7ESY5H28zG5bwR+MlYz9OKbGTkbgzw= 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; bh=b8hRDF1XXrhu33JiDGyXvZcU8whbvzEGz0i9JpZbnTw=; b=GjgQx/wqypJqd+m8MynqF7arpNcRlkCa2m7dXeExutns2pb4PvX3HAC2g0y9Wxc+Cg VQg+d1rx92FwsIDRaCJ2uTrRCqYMnlriTJoL+8US3g/D5MBSt7GoN8NK+CMGbRKtiYWB klvE7kFiD7/VQaYlX8ZBYoI7RdvqrEGcYG3Pk+66nTU2cRDmn+0IlnL/AXgBtSwIlZMI j4SBVmLRx4avrzQVc7bnbCORn/xXYwlk5U6Dy0jp14OYuwSZc0y8z8YN5Sxw4q9VJrPd TUaXiiIdabycTbI2RHreM1pgK6VCIDgdqIVNVrpTOxJ2wp6yWY333eNdob69Rl5aj/0k N2Yw== X-Gm-Message-State: AOAM530qcHsrFmV2tzAPG3R6+i/wMQpEXqJbU3chGFwYyXdN6hQMYi+D xXs9h0oyuLP9BOYOfmD5419M6w== X-Received: by 2002:a62:4ec7:: with SMTP id c190mr3210241pfb.55.1592333378455; Tue, 16 Jun 2020 11:49:38 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y7sm18313185pfq.43.2020.06.16.11.49.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 11:49:37 -0700 (PDT) Date: Tue, 16 Jun 2020 11:49:36 -0700 From: Kees Cook To: Jann Horn Cc: kernel list , Christian Brauner , Sargun Dhillon , Tycho Andersen , "zhujianwei (C)" , Dave Hansen , Matthew Wilcox , Andy Lutomirski , Will Drewry , Shuah Khan , Matt Denton , Chris Palmer , Jeffrey Vander Stoep , Aleksa Sarai , Hehuazhen , the arch/x86 maintainers , Linux Containers , linux-security-module , Linux API Subject: Re: [PATCH 4/8] seccomp: Implement constant action bitmaps Message-ID: <202006161145.1E6B616CBE@keescook> References: <20200616074934.1600036-1-keescook@chromium.org> <20200616074934.1600036-5-keescook@chromium.org> <202006160757.99FD9B785@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 16, 2020 at 08:36:28PM +0200, Jann Horn wrote: > On Tue, Jun 16, 2020 at 5:49 PM Kees Cook wrote: > > On Tue, Jun 16, 2020 at 02:14:47PM +0200, Jann Horn wrote: > > > Wouldn't it be simpler to use a function that can run a subset of > > > seccomp cBPF and bails out on anything that indicates that a syscall's > > > handling is complex or on instructions it doesn't understand? For > > > syscalls that have a fixed policy, a typical seccomp filter doesn't > > > even use any of the BPF_ALU ops, the scratch space, or the X register; > > > it just uses something like the following set of operations, which is > > > easy to emulate without much code: > > > > > > BPF_LD | BPF_W | BPF_ABS > > > BPF_JMP | BPF_JEQ | BPF_K > > > BPF_JMP | BPF_JGE | BPF_K > > > BPF_JMP | BPF_JGT | BPF_K > > > BPF_JMP | BPF_JA > > > BPF_RET | BPF_K > > > > Initially, I started down this path. It needed a bit of plumbing into > > BPF to better control the lifetime of the cBPF "saved original filter" > > (normally used by CHECKPOINT_RESTORE uses) > > I don't think you need that? When a filter is added, you can compute > the results of the added individual filter, and then merge the state. That's what I thought too, but unfortunately not (unless I missed something) -- the seccomp verifier is run as a callback from the BPF internals, so seccomp only see what the user sends (which is unverified) and the final eBPF filter. There isn't state I can attach during the callback, so I opted to just do the same thing as CHECKPOINT_RESTORE, but to then explicitly free the cBPF after bitmap generation. > > and then I needed to keep > > making exceptions (same list you have: ALU, X register, scratch, etc) > > in the name of avoiding too much complexity in the emulator. I decided > > I'd rather reuse the existing infrastructure to actually execute the > > filter (no cBPF copy needed to be saved, no separate code, and full > > instruction coverage). > > If you really think that this bit of emulation is so bad, you could > also make a copy of the BPF filter in which you replace all load > instructions from syscall arguments with "return NON_CONSTANT_RESULT", > and then run that through the normal BPF infrastructure. > > > > Something like (completely untested): > [...] > > I didn't actually finish going down the emulator path (I stopped right > > around the time I verified that libseccomp does use BPF_ALU -- though > > only BPF_AND), so I didn't actually evaluate the filter contents for other > > filter builders (i.e. Chrome). > > > > But, if BPF_ALU | BPF_AND were added to your code above, it would cover > > everything libseccomp generates (which covers a lot of the seccomp > > filters, e.g. systemd, docker). I just felt funny about an "incomplete" > > emulator. > > > > Though now you've got me looking. It seems this is the core > > of Chrome's BPF instruction generation: > > https://github.com/chromium/chromium/blob/master/sandbox/linux/bpf_dsl/policy_compiler.cc > > It also uses ALU|AND, but adds JMP|JSET. > > > > So... that's only 2 more instructions to cover what I think are likely > > the two largest seccomp instruction generators. > > > > > That way, you won't need any of this complicated architecture-specific stuff. > > > > There are two arch-specific needs, and using a cBPF-subset emulator > > just gets rid of the local TLB flush. The other part is distinguishing > > the archs. Neither requirement is onerous (TLB flush usually just > > needs little more than an extern, arch is already documented in the > > per-arch syscall_get_arch()). > > But it's also somewhat layer-breaking and reliant on very specific > assumptions. Normal kernel code doesn't mess around with page table > magic, outside of very specific low-level things. And your method > would break if the fixed-value members were not all packed together at > the start of the structure. Right -- that was lucky. I suspect the emulation route will win out here. > And from a hardening perspective: The more code we add that fiddles > around with PTEs directly, rather than going through higher-level > abstractions, the higher the chance that something gets horribly > screwed up. For example, this bit from your patch looks *really* > suspect: > > + preempt_disable(); > + set_pte_at(&init_mm, vaddr, ptep, > pte_mkold(*(READ_ONCE(ptep)))); > + local_flush_tlb_kernel_range(vaddr, vaddr + PAGE_SIZE); > + preempt_enable(); > > First off, that set_pte_at() is just a memory write; I don't see why > you put it inside a preempt_disable() region. > But more importantly, sticking a local TLB flush inside a > preempt_disable() region with nothing else in there looks really > shady. How is that supposed to work? If we migrate from CPU0 to CPU1 > directly before this region, and then from CPU1 back to CPU0 directly > afterwards, the local TLB flush will have no effect. Yeah, true, that's another good reason not to do this. -- Kees Cook