Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2562824ybt; Tue, 16 Jun 2020 09:06:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyffm4ShH568TbZY0yV5XtdYVqCkQiI3e9l8+EkfrBN1i0NZpBqQ12khDRlu0AIT6eaDFnX X-Received: by 2002:a17:906:c952:: with SMTP id fw18mr3378953ejb.505.1592323590805; Tue, 16 Jun 2020 09:06:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592323590; cv=none; d=google.com; s=arc-20160816; b=JPhWpn2LPyNeq1lRQqhNrIH9yfGkmOgOtvOU6O2krU8+TD6bR9vRBS5HRekjWOi9gH oBpASCt5uLfvNmXnPkAuBGaO6n5txePywgM21pAzx89aKfNZ33cx7h6KzBTaNTTkdONv 9y3kgHFg21AyCTHSEmK+65N7K5xj3FruW0BmAJtipynK5UaAD11lVlMOnu+jL4BRWdt1 5/zUAFn55vWJah02Akzi0qxbaP/J7IzHR77vLgsEswZmz2mbX6t+1j12PZMrEpFUXmrz vP0l1WLs6nWda6tJfAGF38+0etsnqZdR9VRTfGv5iStMmJBgPDLPHlRJfqTK59KzbiBB 7pvg== 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=C2Y4BhWefKmP9iji9eylvkFo4/M9jmgkj2BXKFGkoJE=; b=Ax/gwvk5e0QFVcf/zx9CWnn3RviuzFaVBK7mLoGyhkdN2+q2lTgXmD/wdTGO0lDydZ 60kUcDStjOzhkASjcB8TT1JSEo4TlclK7JMw2YftWXKh7/nSd++1raI1jOkOO7g/7+FG aiF93v+VXWSo2JrNIvVzTnMmILZYZd5XkPcCL75Z9bmP4ZgDkdyIPbx64D5gHYc0FdLA dBHNU6SP7swDkK+6NMG2WemlgE+VQDWaMDYhIZiYa98tU13qlXL0Aab/SBFZzEQO5iup rTjoloFRWa24ibhTNBnL3UIVLHiTl6JfFgdedSjTKhf65VxSPozJSpeVGt3D5JKZl3KQ efFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=aJ8p70Y5; 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 j1si11071611edy.413.2020.06.16.09.06.07; Tue, 16 Jun 2020 09:06:30 -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=aJ8p70Y5; 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 S1733145AbgFPQBw (ORCPT + 99 others); Tue, 16 Jun 2020 12:01:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733172AbgFPQBt (ORCPT ); Tue, 16 Jun 2020 12:01:49 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E598C061755 for ; Tue, 16 Jun 2020 09:01:49 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id s10so9435755pgm.0 for ; Tue, 16 Jun 2020 09:01:49 -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=C2Y4BhWefKmP9iji9eylvkFo4/M9jmgkj2BXKFGkoJE=; b=aJ8p70Y5LnowTS4d6Nvq5WyEu93YaLiozLfkw0uDW6dYUF3xQNCbIJvKarTK5Ed9iD m7i0zsBqFI1pIpAyIluoSmBT0Risms7oI2Pp32JEbx7n0H5pqbwQtOkP1/KHECdDu/FT ltBtg48/YKabACQ+Epr94E4U9IQIdRDp/x9Fs= 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=C2Y4BhWefKmP9iji9eylvkFo4/M9jmgkj2BXKFGkoJE=; b=Tj5B0sOyvW7yiSc/4SvjOK9qZdQaxIUwW5MWDEuTmUijbgjlHy2ib2QhE/J5BvWHZw Pix4kuNJ6+I0gNTuecBWoD/1mVuTDmleF0SRFBgIPQAZqygEcXFWUOQZW7P0tCcvBBz7 z0j8cmQb5Xedim1IoC9Etifn6T5XDoCHdl06pKh9bKGJTevl39OzKbbxjY/uIQBf80gA Bl8QhgM8x0YYkO3bVIn8wxO/+jSo/6gMmaY4IhA9fCt9f/1ZAaOfv14tlsqD3E6BNCP+ SMNhp0wgfb9MuMaTE/l7qMjW+RqV3KAMLmlVCrVsaOiI8wWdp+UKXXLP4z/2S9FkNTjE ccwQ== X-Gm-Message-State: AOAM530JIZKnxe1+Ipxy+6wxLyEFWWspoCWbjwPlnkXnFXRCxIRhrlFT iiPA47z7pz6iAuJ9A7Cv+XYW8w== X-Received: by 2002:a63:5307:: with SMTP id h7mr2625624pgb.28.1592323309057; Tue, 16 Jun 2020 09:01:49 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id iq19sm2942471pjb.48.2020.06.16.09.01.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 09:01:48 -0700 (PDT) Date: Tue, 16 Jun 2020 09:01:46 -0700 From: Kees Cook To: Dave Hansen Cc: linux-kernel@vger.kernel.org, Christian Brauner , Sargun Dhillon , Tycho Andersen , Jann Horn , "zhujianwei (C)" , Dave Hansen , Matthew Wilcox , Andy Lutomirski , Will Drewry , Shuah Khan , Matt Denton , Chris Palmer , Jeffrey Vander Stoep , Aleksa Sarai , Hehuazhen , x86@kernel.org, Linux Containers , linux-security-module@vger.kernel.org, linux-api@vger.kernel.org Subject: Re: [PATCH 4/8] seccomp: Implement constant action bitmaps Message-ID: <202006160851.E8F9928AAB@keescook> References: <20200616074934.1600036-1-keescook@chromium.org> <20200616074934.1600036-5-keescook@chromium.org> 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 07:40:17AM -0700, Dave Hansen wrote: > On 6/16/20 12:49 AM, Kees Cook wrote: > > + /* Mark the second page as untouched (i.e. "old") */ > > + 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(); > > If you can, I'd wrap that nugget up in a helper. I'd also suggest being > very explicit in a comment about what it is trying to do: ensure no TLB > entries exist so that a future access will always set the Accessed bit. Yeah, good idea! > > > + /* Make sure the PTE agrees that it is untouched. */ > > + if (WARN_ON_ONCE(sd_touched(ptep))) > > + return; > > + /* Read a portion of struct seccomp_data from the second page. */ > > + check = sd->instruction_pointer; > > + /* First, verify the contents are zero from vzalloc(). */ > > + if (WARN_ON_ONCE(check)) > > + return; > > + /* Now make sure the ACCESSED bit has been set after the read. */ > > + if (!sd_touched(ptep)) { > > + /* > > + * If autodetection fails, fall back to standard beahavior by > > + * clearing the entire "allow" bitmap. > > + */ > > + pr_warn_once("seccomp: cannot build automatic syscall filters\n"); > > + bitmap_zero(bitmaps->allow, NR_syscalls); > > + return; > > + } > > I can't find any big holes with this. It's the kind of code that makes > me nervous, but mostly because it's pretty different that anything else > we have in the kernel. > > It's also clear to me here that you probably have a slightly different > expectation of what the PTE accessed flag means versus the hardware > guys. What you are looking for it to mean is roughly: "a retired > instruction touched this page". > > The hardware guys would probably say it's closer to "a TLB entry was > established for this page." Remember that TLB entries can be > established speculatively or from things like prefetchers. While I > don't know of anything microarchitectural today which would trip this > mechanism, it's entirely possible that something in the future might. > Accessing close to the page boundary is the exact kind of place folks > might want to optimize. Yeah, and to that end, going the cBPF emulator route removes this kind of "weird" behavior. > > *But*, at least it would err in the direction of being conservative. It > would say "somebody touched the page!" more often than it should, but > never _less_ often than it should. Right -- I made sure to design the bitmaps and the direction of the checking to fail towards running the filter instead of bypassing it. > One thing about the implementation (which is roughly): > > // Touch the data: > check = sd->instruction_pointer; > // Examine the PTE mapping that data: > if (!sd_touched(ptep)) { > // something > } > > There aren't any barriers in there, which could lead to the sd_touched() > check being ordered before the data touch. I think a rmb() will > suffice. You could even do it inside sd_touched(). Ah yeah, I had convinced myself that READ_ONCE() gained me that coverage, but I guess that's not actually true here. > Was there a reason you chose to export a ranged TLB flush? I probably > would have just used the single-page flush_tlb_one_kernel() for this > purpose if I were working in arch-specific code. No particular reason -- it just seemed easiest to make available given the interfaces. I could do the single-page version instead, if this way of doing things survives review. ;) Thanks for looking at it! -- Kees Cook