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=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 796D2C2D0E2 for ; Thu, 24 Sep 2020 12:51:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D13521D24 for ; Thu, 24 Sep 2020 12:51:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="do2eV7gu" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727829AbgIXMoz (ORCPT ); Thu, 24 Sep 2020 08:44:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727820AbgIXMop (ORCPT ); Thu, 24 Sep 2020 08:44:45 -0400 Received: from mail-il1-x144.google.com (mail-il1-x144.google.com [IPv6:2607:f8b0:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06491C0613CE; Thu, 24 Sep 2020 05:44:45 -0700 (PDT) Received: by mail-il1-x144.google.com with SMTP id y9so3000622ilq.2; Thu, 24 Sep 2020 05:44:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=l5sZSI+cLE2joNnPykw3MJU/gzivLj0GX9Q9hGA5pVs=; b=do2eV7guKNVRVckBf3Qfgxpk/b3ON9tjlGe5E57+r1XmL7t/D5Kw5KdZ5uZbhjS1Ff QN0OB9vDaxjoBHoNISHtwjZkYjJhcOvuwq7phhDPiFl0X78WFGCOASDBa5khLnQZYNPm UiLhV9gs9CCVHZg9bYlt+/5X8Y6AEGDJNTCrkyszULDF1aC+PIjShWf3ODB/iyVwQMtu 24alqEDxI6BWef3lFhd6g7eQdorBv7AUCIEMY8KsgHMB6dhVngX/5p2+hVZ4MQE2Bmp2 QylQd+CTvWFDivAicsUJD9I76oXNjqgJzciXs+NXTr1SVYHKYHsW3d4YEtEbl8YvwW7k fFlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=l5sZSI+cLE2joNnPykw3MJU/gzivLj0GX9Q9hGA5pVs=; b=X9EAr+7RE38yDBxijH6tiB17c1UQOw6dIsytl6hodCNgmIFYhM4NOsR/ijP/w6ckFX Z4fd/euClphNVfFgcjd2NFaWzg0KTwuQJXhjSCqbKWIDLnE6vZS4JY3I6WmRkf8JvgIx BtPtLRuFEuUyrYG5bjdIsr+hiH+U3J7fOFq4gQJ2wTN3seQ8q0UUfJi4gSziud5aqsY1 w9ifcRoGCSVs5DQHAi2XuHxQpky8NfEWm2T541pG4u9qbWWQGO5PFhYgEQWLVr+1AjGD k/K43k0Il1CR+6cqX/SaDk9mLUlHDtg8Ykm9T/s8xXpSga/3PK3P7aJubRE60qPOctsW WRSA== X-Gm-Message-State: AOAM531w5nTO+52jWsdSNir927P7for/wz7Zz8TC1sepWhFMo71GyU3/ RTcM8em9HVKbasv2F+md97CjrReLIIUTqw== X-Google-Smtp-Source: ABdhPJxpBnD87qzZx6pccGeAQzN1T2pWR8/PeSQiL6zBbati5Zbhu9BAc67rqhD1Wk2L+0muJEHyqg== X-Received: by 2002:a05:6e02:f48:: with SMTP id y8mr3911683ilj.103.1600951484344; Thu, 24 Sep 2020 05:44:44 -0700 (PDT) Received: from localhost.localdomain (host-173-230-99-154.tnkngak.clients.pavlovmedia.com. [173.230.99.154]) by smtp.gmail.com with ESMTPSA id p5sm1575175ilg.32.2020.09.24.05.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 05:44:43 -0700 (PDT) From: YiFei Zhu To: containers@lists.linux-foundation.org Cc: YiFei Zhu , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Aleksa Sarai , Andrea Arcangeli , Andy Lutomirski , Dimitrios Skarlatos , Giuseppe Scrivano , Hubertus Franke , Jack Chen , Jann Horn , Josep Torrellas , Kees Cook , Tianyin Xu , Tobin Feldman-Fitzthum , Tycho Andersen , Valentin Rothberg , Will Drewry Subject: [PATCH v2 seccomp 4/6] seccomp/cache: Lookup syscall allowlist for fast path Date: Thu, 24 Sep 2020 07:44:19 -0500 Message-Id: <64052a5b81d5dacd63efb577c1d99e6f98e69702.1600951211.git.yifeifz2@illinois.edu> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: YiFei Zhu The fast (common) path for seccomp should be that the filter permits the syscall to pass through, and failing seccomp is expected to be an exceptional case; it is not expected for userspace to call a denylisted syscall over and over. This first finds the current allow bitmask by iterating through syscall_arches[] array and comparing it to the one in struct seccomp_data; this loop is expected to be unrolled. It then does a test_bit against the bitmask. If the bit is set, then there is no need to run the full filter; it returns SECCOMP_RET_ALLOW immediately. Co-developed-by: Dimitrios Skarlatos Signed-off-by: Dimitrios Skarlatos Signed-off-by: YiFei Zhu --- kernel/seccomp.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 20d33378a092..ac0266b6d18a 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -167,6 +167,12 @@ static inline void seccomp_cache_inherit(struct seccomp_filter *sfilter, const struct seccomp_filter *prev) { } + +static inline bool seccomp_cache_check(const struct seccomp_filter *sfilter, + const struct seccomp_data *sd) +{ + return false; +} #endif /* CONFIG_SECCOMP_CACHE_NR_ONLY */ /** @@ -321,6 +327,34 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) return 0; } +#ifdef CONFIG_SECCOMP_CACHE_NR_ONLY +/** + * seccomp_cache_check - lookup seccomp cache + * @sfilter: The seccomp filter + * @sd: The seccomp data to lookup the cache with + * + * Returns true if the seccomp_data is cached and allowed. + */ +static bool seccomp_cache_check(const struct seccomp_filter *sfilter, + const struct seccomp_data *sd) +{ + int syscall_nr = sd->nr; + int arch; + + if (unlikely(syscall_nr < 0 || syscall_nr >= NR_syscalls)) + return false; + + for (arch = 0; arch < ARRAY_SIZE(syscall_arches); arch++) { + if (likely(syscall_arches[arch] == sd->arch)) + return test_bit(syscall_nr, + sfilter->cache.syscall_ok[arch]); + } + + WARN_ON_ONCE(true); + return false; +} +#endif /* CONFIG_SECCOMP_CACHE_NR_ONLY */ + /** * seccomp_run_filters - evaluates all seccomp filters against @sd * @sd: optional seccomp data to be passed to filters @@ -343,6 +377,9 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd, if (WARN_ON(f == NULL)) return SECCOMP_RET_KILL_PROCESS; + if (seccomp_cache_check(f, sd)) + return SECCOMP_RET_ALLOW; + /* * All filters in the list are evaluated and the lowest BPF return * value always takes priority (ignoring the DATA). -- 2.28.0