Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3767008pxu; Sun, 11 Oct 2020 23:53:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVbEW+6VxvxcyBoRjNa0mgJFoG586yzCqi+g2w2W5RIcUiGNVaw6RfHvHbYeXNwnkOhko3 X-Received: by 2002:a17:906:93ef:: with SMTP id yl15mr8176557ejb.529.1602485602332; Sun, 11 Oct 2020 23:53:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602485602; cv=none; d=google.com; s=arc-20160816; b=k7GBkLEUHMQdxkvMqpuKomJ10yHrLoPc+KW9ckmXJGEecgit2cwLMB8V8J2pP8mZmE OFtnvCEGbWzTR3tohn9nrxsorwuU9WDJ3x39gUkuv6gWlX90YnJ1DNaK2JFA0gm6xwmF zRDBBPBpZBLZQuBBuiabhfNDnAq6BTrwQhjB/156qjSfjI88RB1o2YKSTv/yGTDElNz+ zfSlzVoEbojFM7H1DQwhsZJyn3u9ABKPmXqGQlJeWo03AXNRcqDe8+HkSCNTCFjtEeq6 PUvQ/K0fswrOwg8hoa4oqVTtiiTAIyv0pUhYyGbDfjK7Sr5ScU68r4b+Pv+a3eQY1Swh CP/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=0oV/+pj7Fnm5nZaRHqpOkjT78UAULW7d63AGoeM3lX8=; b=SYHl40Ay5xcbJ2RVVYfmQ2iCZlIyosEqpVeW1LyJXeeDQvx3zH44VUQ7FArd3jm1pY ftQqN1PvZsdnamzn/lwvp149IohjX2Tj1TIdln2QbKEdwgls/na9KZjPsavrZaYoy8M2 zZpTs4h98ji0+mnzpTTYfWBSpF06DXgi2oM0XhZlhvmcknYSI40LniDxEDBeIaryLUj3 XnRRyNN8BMvPATKZu26jBlioZk4lvODDV795Ia3fRodBzDTCtVZtNg06oKnXA3JudyAO Z2NtzrnoMR+eMtRdMs6hXMXSKZOvmFbu0PqiPk5PWYllHRWK01N/mKOGyuqztH207Cyo uwVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TeTWSCEs; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x18si11771540edl.321.2020.10.11.23.52.59; Sun, 11 Oct 2020 23:53:22 -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=@google.com header.s=20161025 header.b=TeTWSCEs; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726547AbgJLGqy (ORCPT + 99 others); Mon, 12 Oct 2020 02:46:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726123AbgJLGqn (ORCPT ); Mon, 12 Oct 2020 02:46:43 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05E23C0613CE for ; Sun, 11 Oct 2020 23:46:43 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id cq12so15756408edb.2 for ; Sun, 11 Oct 2020 23:46:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0oV/+pj7Fnm5nZaRHqpOkjT78UAULW7d63AGoeM3lX8=; b=TeTWSCEsBYqyE/x1ZaeOkH0jWJEzqZ7ade7Tm1kFTgH31HzCcN0hNCHh62tJU51HXo oT1/7SjvVBjHHxKmo/6ioy411q0Lew2Z791SIXZMxKBXgleoTXwKqXOyNNnLsGMHo9d0 +cTV7LNmhJPh1s7mUi2rqebsCXVI4yUVkSL/drzsPeODDm3sQVnnb5dbXYyhw3hhx/wm iS3jYetCXUTdyqg4zYSARhPFq28CXyB+zKkODklGDX6GJO4KkCIAFinOTcE+cmXsNi6Q 4vs8+0YDkbjqMfOplYJWskfn3CRqo227oSBgHHSaMH6yj8m+JXb+qAeT40KP8PiAX0mW HpOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0oV/+pj7Fnm5nZaRHqpOkjT78UAULW7d63AGoeM3lX8=; b=ZKcOgigJnKmUKc24eDjfSyCIdSfWWJy55iFAgD0kitv+Dhv5Sw5gXqC3qjC2oeXH9n jNgX8rZM5uBlg2nriSXtpmhpELp177BkgCTh/v7iiPCa9MYHUW+IxH/8/Y5vwN1vwJ/b 2w+LOgIZeknAvAto034R32EN8GvSQtCV/QS55LURv5XeqYLqz3pyqhNnUl3uCVVMBXwX CiZTg+f2FXbn/X7B0c0cni2aUnr22VEov2eB1jKUo2+IkoDInPqVQ3vqRGLQwzKq5PBL e3CXmHOAyci06H4UY1H9ruFVeZMYS3jL4NYe77XvxbuE1e/dmlxyBFHV6YBhEurKx5pt 5WPA== X-Gm-Message-State: AOAM532vylWJsrD/J7N30TglYKwWAaosxgCOLrF9eJWLxrBfvZBv/C2n 9z6qiA/oWKEQrLFcKA1xQRNq1bYS27E43kgnPgjYkQ== X-Received: by 2002:aa7:d349:: with SMTP id m9mr12421814edr.51.1602485201449; Sun, 11 Oct 2020 23:46:41 -0700 (PDT) MIME-Version: 1.0 References: <71c7be2db5ee08905f41c3be5c1ad6e2601ce88f.1602431034.git.yifeifz2@illinois.edu> In-Reply-To: <71c7be2db5ee08905f41c3be5c1ad6e2601ce88f.1602431034.git.yifeifz2@illinois.edu> From: Jann Horn Date: Mon, 12 Oct 2020 08:46:15 +0200 Message-ID: Subject: Re: [PATCH v5 seccomp 2/5] seccomp/cache: Add "emulator" to check if filter is constant allow To: YiFei Zhu Cc: 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 , Kees Cook , Tianyin Xu , Tobin Feldman-Fitzthum , Tycho Andersen , Valentin Rothberg , Will Drewry Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Oct 11, 2020 at 5:48 PM YiFei Zhu wrote: > 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/ > > Suggested-by: Jann Horn > Co-developed-by: Kees Cook > Signed-off-by: Kees Cook > Signed-off-by: YiFei Zhu Reviewed-by: Jann Horn