From patchwork Tue Aug 8 01:59:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9886433 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 B30E7603F9 for ; Tue, 8 Aug 2017 01:59:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A2EF82874F for ; Tue, 8 Aug 2017 01:59:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 97B0328750; Tue, 8 Aug 2017 01:59:55 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,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 66D9D28755 for ; Tue, 8 Aug 2017 01:59:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751967AbdHHB7O (ORCPT ); Mon, 7 Aug 2017 21:59:14 -0400 Received: from mail-pg0-f42.google.com ([74.125.83.42]:33531 "EHLO mail-pg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751917AbdHHB7M (ORCPT ); Mon, 7 Aug 2017 21:59:12 -0400 Received: by mail-pg0-f42.google.com with SMTP id u5so8578839pgn.0 for ; Mon, 07 Aug 2017 18:59:12 -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=l0bn7Os3nt2B+H5Ps+SizXfZkqKRXPwjjQaExa7ZkFaI6STFjvQjxsBujx1aBi4lli mdaLvLQPiHxkvTkGctpiYXmITt11TtaFEdWDVUi0CPv5OKbF+rRbLVyPL3uUT+yYw4Sq UFNQbqgV63rGumFyS0n4CB3kPf+Pp2wkh/F2I= 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=JI5rXFZza8AbI9pXKn7Z5dsOBi5fXwWBEikiozc3PuNu3H63T+HQHwl3Jz6DuqLq+o /Rn2noBBLBtApWfFqfHkbdwDwcaoEksRHNLKwmRJgk9cj/6bqXXkZfyW2C3GLe/CcPPA nT0awGQ+p9FQap0wWnjb6hAN5E3nWOTVHuTB16+Pep8Od3Nsz14FsivZRMDhdSjP2O7t BROGl5V2jDLMfJC6qZTWhf2J4D1MixRhuMp0JJjVvb82dB5nUATlyGzi3sjTrPENuhlO Xm+fgFlgoH917HbAglst6KySlYbutaA1m7ZM2wkpk1//eePON2AYfnY3ttvPCoka/tZ6 HGjA== X-Gm-Message-State: AHYfb5jgkWdsWzoHg5Osg4ydC+E8nr047oJ8q/wEjhisLG8G4ZPbkAy+ ZNIaU//1gUNGg2AA X-Received: by 10.84.179.3 with SMTP id a3mr2761617plc.480.1502157552216; Mon, 07 Aug 2017 18:59:12 -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 l66sm203545pfb.107.2017.08.07.18.59.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Aug 2017 18:59:10 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Fabricio Voznika , Tyler Hicks , Andy Lutomirski , Will Drewry , Shuah Khan , linux-kselftest@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v2 1/4] seccomp: Provide matching filter for introspection Date: Mon, 7 Aug 2017 18:59:05 -0700 Message-Id: <1502157548-111843-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1502157548-111843-1-git-send-email-keescook@chromium.org> References: <1502157548-111843-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: