From patchwork Wed Aug 9 19:01:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9891705 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0926A602D7 for ; Wed, 9 Aug 2017 19:03:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF43E2899E for ; Wed, 9 Aug 2017 19:03:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E428F289B8; Wed, 9 Aug 2017 19:03:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5EA712899E for ; Wed, 9 Aug 2017 19:03:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751940AbdHITCo (ORCPT ); Wed, 9 Aug 2017 15:02:44 -0400 Received: from mail-pf0-f181.google.com ([209.85.192.181]:34495 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752050AbdHITCG (ORCPT ); Wed, 9 Aug 2017 15:02:06 -0400 Received: by mail-pf0-f181.google.com with SMTP id o86so31435787pfj.1 for ; Wed, 09 Aug 2017 12:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=53Z+d3Th5dvKZalSDtSCkRQWXn8pM7SBpZgTe6zF0+Q=; b=RqJPFGjUJe7rAfc0lbr/NZwiO10OKeZfez15OwF8zCNqqXnDOlIZRycX1+UNeQIANV eiocuxeRqkmMhBhDKo29qWPmdl15FdwFMw1P+4/ZQElMJsS1L5o/r4SzFNhQDZ3F4gxg LSb1tNnfcFrNWkJjfG9jRaDY6COY2IxulQH5k= 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; bh=53Z+d3Th5dvKZalSDtSCkRQWXn8pM7SBpZgTe6zF0+Q=; b=EbRs3Mvjhkde86NuCySUHu9idjEirzm8lkQth8vNRbR1EaJ+0aksuksgbLUCkvd/J4 MfuLc8wawGRjv3QQdDZ29j/CF7kAxZ5NzZazz0dt7iqj/DgetCLUrzq5pQRmJvOe8KpK UJiblFZK8+aPrcCUADN4fu8C218To6TF6C+LruKcwmZNvccA+t+FSjvDCg8/tAAMtc8c i4I5UYylzp5f8wvDZ3oCTEcr/4mEkO0HxFMCHhZAYy8ZTLeQmsNbGea+7bzKdzvjQn4t T7pJSfam59V6jPiV9i7Lc2jmW4Uu5Xpz7CA4DyjBgC0fWlO+yS6P7amU+N/7nk5UBwHg iMlw== X-Gm-Message-State: AHYfb5jXHDiVitORGUnegiEMvp0Hd+7jybJo2mkuCrVMTAL7xbJHrkHH hwm694Zo+QQ/FvHO X-Received: by 10.99.37.6 with SMTP id l6mr8488613pgl.407.1502305325604; Wed, 09 Aug 2017 12:02:05 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id p68sm9598633pfj.48.2017.08.09.12.02.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Aug 2017 12:02:00 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Tyler Hicks , Fabricio Voznika , Andy Lutomirski , Will Drewry , Tycho Andersen , Shuah Khan , linux-kselftest@vger.kernel.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v3 1/4] seccomp: Provide matching filter for introspection Date: Wed, 9 Aug 2017 12:01:54 -0700 Message-Id: <1502305317-85052-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1502305317-85052-1-git-send-email-keescook@chromium.org> References: <1502305317-85052-1-git-send-email-keescook@chromium.org> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Both the upcoming logging improvements and changes to RET_KILL will need to know which filter a given seccomp return value originated from. In order to delay logic processing of result until after the seccomp loop, this adds a single pointer assignment on matches. This will allow both log and RET_KILL logic to work off the filter rather than doing more expensive tests inside the time-critical run_filters loop. Running tight cycles of getpid() with filters attached shows no measurable difference in speed. Suggested-by: Tyler Hicks Signed-off-by: Kees Cook Reviewed-by: Tyler Hicks --- kernel/seccomp.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 98b59b5db90b..1f3347fc2605 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -171,10 +171,14 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) /** * seccomp_run_filters - evaluates all seccomp filters against @sd * @sd: optional seccomp data to be passed to filters + * @match: stores struct seccomp_filter that resulted in the return value, + * unless filter returned SECCOMP_RET_ALLOW, in which case it will + * be unchanged. * * Returns valid seccomp BPF response codes. */ -static u32 seccomp_run_filters(const struct seccomp_data *sd) +static u32 seccomp_run_filters(const struct seccomp_data *sd, + struct seccomp_filter **match) { struct seccomp_data sd_local; u32 ret = SECCOMP_RET_ALLOW; @@ -198,8 +202,10 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd) for (; f; f = f->prev) { u32 cur_ret = BPF_PROG_RUN(f->prog, sd); - if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) + if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) { ret = cur_ret; + *match = f; + } } return ret; } @@ -566,6 +572,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, const bool recheck_after_trace) { u32 filter_ret, action; + struct seccomp_filter *match = NULL; int data; /* @@ -574,7 +581,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, */ rmb(); - filter_ret = seccomp_run_filters(sd); + filter_ret = seccomp_run_filters(sd, &match); data = filter_ret & SECCOMP_RET_DATA; action = filter_ret & SECCOMP_RET_ACTION; @@ -638,6 +645,11 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, return 0; case SECCOMP_RET_ALLOW: + /* + * Note that the "match" filter will always be NULL for + * this action since SECCOMP_RET_ALLOW is the starting + * state in seccomp_run_filters(). + */ return 0; case SECCOMP_RET_KILL: