Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3096396pxu; Sat, 10 Oct 2020 19:49:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4OepLTmqB7lm9NzpBgZ1I2jO9Z14m7yOCS8fOylk0TGovN9j4GLHdh5G4xA5g9Xxw0J8V X-Received: by 2002:a17:906:f988:: with SMTP id li8mr9306799ejb.93.1602384567447; Sat, 10 Oct 2020 19:49:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602384567; cv=none; d=google.com; s=arc-20160816; b=JNHbvrOD92FM17KVRYJCaqD0savg9qEBHvJsFnczVjr+P5U8l5Zi3eHhXHx/djrS3P Xj5Oct/nWpbdrLAqnu+Hdeq+znQfSX+rv/y2FR25Dm79a3iG3TIgXONyklRw4oFR+luO BrVYD3bE9+XHgc5HNK0dTu6KFnrpPbz/wukraa0HO0p4AIZX5+MhFyu8Ri4xcUMARFuX rRwX5wEVdp9/YV4aPduh6hgqyTlolN/TgpYZnIyG0rvsQVbTTE4Haiu0R01+PH7xBoBD L4BNY5OP3bkUZGBRjzeHDqIzz975cZW0/1kW2jy9t6gfl6W8yPBx1I5ZQeQycQ3TN+wv Rs7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tY2ViAT8BuOfi5xuMFyOWzlw2bSYg16jBN39DY5VAdQ=; b=j/hU6g/StoZbRRjsD4HgjHNv0X2Mg+K0UP+GBaFX8qZoSMmsYmX0f/H/diiEvdkKQn oqBxsfiY6Fz2jD5GTIjYJswuzsj9XfMakdodgLr/n1Y1NFtHTEtinm4Ubd6rb4myWz0w 6r4a7EzksHzvnsQ+jOHAgTRMaCUd4802hUSepkOjRoXHyC7WmpWnlbwilSOGhZ6v0XoH O/2X13AI2GhbEA9E6P53WUoaHxV6q3LGaKmvLEE9TniqSEBJliUhcMEkOhLsD0pPrKhg cbEiHoC+FuBzv402vzni+9FOg9n135R+4hj4lYfudyUWYKrFst0P99L/Ey3dEhQrOUrc H4cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nFnOOMc8; 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 b18si9679702ejz.84.2020.10.10.19.48.53; Sat, 10 Oct 2020 19:49:27 -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=nFnOOMc8; 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 S2390052AbgJIWrK (ORCPT + 99 others); Fri, 9 Oct 2020 18:47:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388159AbgJIWrJ (ORCPT ); Fri, 9 Oct 2020 18:47:09 -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 71A6DC0613D2 for ; Fri, 9 Oct 2020 15:47:09 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id r10so8399056pgb.10 for ; Fri, 09 Oct 2020 15:47:09 -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=tY2ViAT8BuOfi5xuMFyOWzlw2bSYg16jBN39DY5VAdQ=; b=nFnOOMc8hwTlNtEo9+8Ej+MSHepQT8JXKR5+tljjPBINdllBge3d5tGOIDJSNx+Wkv GapnYtqRtJDA/6J+Yg3OlY0A/eo58LHl9EgDXJRaKOt6UVva3ErneneIJdZ6wcZYGRRP uQ9FxJut9HVsX/++OQYjAZO0lvCIBrRZ5MbVU= 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=tY2ViAT8BuOfi5xuMFyOWzlw2bSYg16jBN39DY5VAdQ=; b=TYsIf9jXsRmxrk25QyKKtIP1wrI7uSFTaBqABGisCCntlz4nfiez5/fXVDqzkCCR8R w2fC+MOTb2trRvbhwTOgfNIG6qdgpiexowYLVjS7f+XCFrsuE3IgOb0aWUJs7i4ljxbT e7Y0y6x5i7QJlgvosPJxjj74vSZzM/I1UcirdYXhRcOfZjcZ2K+R1taHJI3QiCPKZU1n U95RGPwWx3zcnD1HJQcfyPp/Fh+1DW+mm1KgItOSMMYSskClR+dHel9rAdJE+jCdZsCM NfjveOchdK0RrUNe/6rh5pgii0zD6JBCdTZd9ECi7I3XhLn/PbzwtPkz8/v+wjvpjuLN PVsg== X-Gm-Message-State: AOAM533+Fluup006t9vg1ZM9wzQD1Kuqz8Aaiq2GHPXYEq458RUjPjWh swKReLgnZ5CB7d0VnO7SXyEV2A== X-Received: by 2002:a17:90a:c087:: with SMTP id o7mr6916984pjs.155.1602283628884; Fri, 09 Oct 2020 15:47:08 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id i24sm11570377pfd.15.2020.10.09.15.47.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Oct 2020 15:47:08 -0700 (PDT) Date: Fri, 9 Oct 2020 15:47:07 -0700 From: Kees Cook To: Jann Horn Cc: YiFei Zhu , Linux Containers , YiFei Zhu , bpf , kernel list , Aleksa Sarai , Andrea Arcangeli , Andy Lutomirski , David Laight , Dimitrios Skarlatos , Giuseppe Scrivano , Hubertus Franke , Jack Chen , Josep Torrellas , Tianyin Xu , Tobin Feldman-Fitzthum , Tycho Andersen , Valentin Rothberg , Will Drewry Subject: Re: [PATCH v4 seccomp 2/5] seccomp/cache: Add "emulator" to check if filter is constant allow Message-ID: <202010091545.962A2F5@keescook> References: <1a40458d081ce0d5423eb0282210055496e28774.1602263422.git.yifeifz2@illinois.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 09, 2020 at 11:30:18PM +0200, Jann Horn wrote: > On Fri, Oct 9, 2020 at 7:15 PM YiFei Zhu wrote: > > > > From: YiFei Zhu > > > > SECCOMP_CACHE will only operate on syscalls that do not access > > any syscall arguments or instruction pointer. To facilitate > > this we need a static analyser to know whether a filter will > > return allow regardless of syscall arguments for a given > > architecture number / syscall number pair. This is implemented > > here with a pseudo-emulator, and stored in a per-filter bitmap. > > > > In order to build this bitmap at filter attach time, each filter is > > emulated for every syscall (under each possible architecture), and > > checked for any accesses of struct seccomp_data that are not the "arch" > > nor "nr" (syscall) members. If only "arch" and "nr" are examined, and > > the program returns allow, then we can be sure that the filter must > > return allow independent from syscall arguments. > > > > Nearly all seccomp filters are built from these cBPF instructions: > > > > 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_JSET | BPF_K > > BPF_JMP | BPF_JA > > BPF_RET | BPF_K > > BPF_ALU | BPF_AND | BPF_K > > > > Each of these instructions are emulated. Any weirdness or loading > > from a syscall argument will cause the emulator to bail. > > > > The emulation is also halted if it reaches a return. In that case, > > if it returns an SECCOMP_RET_ALLOW, the syscall is marked as good. > > > > Emulator structure and comments are from Kees [1] and Jann [2]. > > > > Emulation is done at attach time. If a filter depends on more > > filters, and if the dependee does not guarantee to allow the > > syscall, then we skip the emulation of this syscall. > > > > [1] https://lore.kernel.org/lkml/20200923232923.3142503-5-keescook@chromium.org/ > > [2] https://lore.kernel.org/lkml/CAG48ez1p=dR_2ikKq=xVxkoGg0fYpTBpkhJSv1w-6BG=76PAvw@mail.gmail.com/ > [...] > > @@ -682,6 +693,150 @@ seccomp_prepare_user_filter(const char __user *user_filter) > > return filter; > > } > > > > +#ifdef SECCOMP_ARCH_NATIVE > > +/** > > + * seccomp_is_const_allow - check if filter is constant allow with given data > > + * @fprog: The BPF programs > > + * @sd: The seccomp data to check against, only syscall number are arch > > + * number are considered constant. > > nit: s/syscall number are arch number/syscall number and arch number/ > > > + */ > > +static bool seccomp_is_const_allow(struct sock_fprog_kern *fprog, > > + struct seccomp_data *sd) > > +{ > > + unsigned int insns; > > + unsigned int reg_value = 0; > > + unsigned int pc; > > + bool op_res; > > + > > + if (WARN_ON_ONCE(!fprog)) > > + return false; > > + > > + insns = bpf_classic_proglen(fprog); > > bpf_classic_proglen() is defined as: > > #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0])) > > so this is wrong - what you want is the number of instructions in the > program, what you actually have is the size of the program in bytes. > Please instead check for `pc < fprog->len` in the loop condition. Oh yes, good catch. I had this wrong in my v1. -- Kees Cook