Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp259190pxk; Thu, 24 Sep 2020 05:09:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2l8fdnlqDARIjP5jKdM4COWYJSYigkuKzXGdZvJZgh4o+eIK7raHtJB5ed2TJrbdpmcMt X-Received: by 2002:a05:6402:326:: with SMTP id q6mr705090edw.216.1600949395990; Thu, 24 Sep 2020 05:09:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600949395; cv=none; d=google.com; s=arc-20160816; b=gh259fclOd8EoJYbPVGOiFOe8pr2126Bz1UcNK7igKOdZ5bL3w+sWjdFfj2+6xekp9 Lrc5lOdJckfiwRPGvHiKyzhAxENG1d7iywDQH40IQOCisRy+Zm75DSmlniMMCtIz5o5U mkwXvzUH7DVVUxk8EMuo/6BEKDDjR9sCubkq/fobo2WlJ5Ecc/fhBLi4OnBsvWurW+ab AJ4D7QrlU1l/9mk3txP9Q9fTD/1OQLzQIDnd0GQ/LFfdNXyYz3jstlHzRNweP2CnB7gC L5z8KY3egUGeItBJZOg8LlhCZucOdLJ4HkVdkwg0BB98X5cpXWA0Bat153oNs+Ru2koD Hz0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SYKa1nrEaZaYwZ8wYDMo+wGBRjI8YKJTKmH+OljJTGg=; b=aC1PZjx9x7xJg3dB+tS8ieYY2sQMtPzvyDyir/o3nArz8P+bXiwDwYFUmzQ/Ub/fTh b2wLeid97KLThrvlOeIhnMO5kcq+T090v1l+u/uKwKknK2Wflu8Gl3utfpfPXM24v1f9 +mw3t19GVyDjxN2nF1RuqZaFrmirXv9+YgAm2ghm4lH8OYWMw9e6aod7XAxMlMlhaYxt 0kDr0VAhbEYew67QL/3l33V+Y14np4WSsB9sT6EfKnRJP6DMGgTDUjI8LehFfvz3NQFy sWzXcOa/Z5IX5yL7Khe4B/5QyjDBu63+O5exvfqIgM/QQliXNsSJHTbDSYYOj+05/t08 akVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=r9uakENV; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i24si1997268edr.73.2020.09.24.05.09.31; Thu, 24 Sep 2020 05:09:55 -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=@gmail.com header.s=20161025 header.b=r9uakENV; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbgIXMG6 (ORCPT + 99 others); Thu, 24 Sep 2020 08:06:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726406AbgIXMGz (ORCPT ); Thu, 24 Sep 2020 08:06:55 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90F7CC0613CE; Thu, 24 Sep 2020 05:06:55 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id r9so3039584ioa.2; Thu, 24 Sep 2020 05:06:55 -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=SYKa1nrEaZaYwZ8wYDMo+wGBRjI8YKJTKmH+OljJTGg=; b=r9uakENVZx3FshPV+JWxKdMA/UFifGO75CEByTaA1MQ7piwbKpqOgJyuN5h4qIsH6L AR1TMSH0M8ESEauKZ3CoROg4wA1sf1sd65zjzUrwfDJ7He2gW2hVB4rAejjqRkA6LYLp w6VY7ZlVsvh62gvHCCFXKqbaaStiEFRe833jE4vIoCWo4m5BfG5KaYv/tYf0w5Fx/DIy +aLTs8HcBLgpgOrsxQf06H85GyUWAmColJpWEtJWGGBmAAbn/O30q8qTicZuL1wOdzAu 6wEPp35ZnzzZuUrTP0Jc3oyGY5+G6nVAndZI1pdHL9xdP33v5cLnEOw4CghoeO7zZhq2 4AIQ== 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=SYKa1nrEaZaYwZ8wYDMo+wGBRjI8YKJTKmH+OljJTGg=; b=A1vqiw8KZKFaSac3kikFaToUtf3FV7LDFoxarDAwW6u0reo2ZOPr8x1i1Mri4j1TKg TOCPDq/wi+CiY9iVb4ysR1kWZ4jEZNgMf5aIF1tYFBXHxQEKA1L6IR7p/6t0SWgtgvro NMWLrA6HofWU/6k+7Q0TIv1VqSx17pyf7rL/FL5lQVZqnMrZsD/eDyn29a/pSTlgecRq ODtTqoNFNwyOIux2Rkj//G7u7Zx3yXpwDbABwwAzMs1rdoLDXQmPJ9H2gKfgP0vNGgXg IiHbKdP94Pqcu5oqDxiDW0aukB8124o137UNuOWf5V78tQZLW4oLNG9UPSoBtRvJpOWj MwfA== X-Gm-Message-State: AOAM533AGCckIWM1wNCixrpzMktF6JUugB9qkIMibs0O8NLfzTOTkNer LtrgtxTkU9Iviwab0kfrMU8= X-Received: by 2002:a6b:be44:: with SMTP id o65mr3036720iof.53.1600949214929; Thu, 24 Sep 2020 05:06:54 -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 a23sm1259435ioc.54.2020.09.24.05.06.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 05:06:54 -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 seccomp 4/6] seccomp/cache: Lookup syscall allowlist for fast path Date: Thu, 24 Sep 2020 07:06:44 -0500 Message-Id: <6c754a336aef916e3a69af8b8b47834317f4dc60.1600946701.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: linux-kernel@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 7c286d66f983..5b1bd8329e9c 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