From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADCD2C433DF for ; Tue, 16 Jun 2020 18:49:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81899208B3 for ; Tue, 16 Jun 2020 18:49:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="d1hgB/Jf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730209AbgFPStl (ORCPT ); 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-Google-Smtp-Source: ABdhPJy3iOEuaZxX1Ecv1DqNdAuYKJbtRDJF37S2pVwLCY+nnumK7HqELR1zqiOaHjj813LM+Z+ubA== 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