From patchwork Thu Aug 3 03:19:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9878095 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 0C43560360 for ; Thu, 3 Aug 2017 03:20:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F19CF2882E for ; Thu, 3 Aug 2017 03:20:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E54CC28831; Thu, 3 Aug 2017 03:20:28 +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=ham 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 746FE2882E for ; Thu, 3 Aug 2017 03:20:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752050AbdHCDU0 (ORCPT ); Wed, 2 Aug 2017 23:20:26 -0400 Received: from mail-pf0-f174.google.com ([209.85.192.174]:35346 "EHLO mail-pf0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752040AbdHCDTb (ORCPT ); Wed, 2 Aug 2017 23:19:31 -0400 Received: by mail-pf0-f174.google.com with SMTP id t86so966356pfe.2 for ; Wed, 02 Aug 2017 20:19:31 -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=a7tcfq6nWB6pIont181OjIiMOtstWb6KKxdwtBq8w1o=; b=cGV0swaG1tf870ON4cfuuqPtyjNkult4v9BhtCzDfZ4hUcoVKCjxQrr4H03Njveoy1 ++z/+Ye6KJrTCJiV7jGLWknrZv7SR1JFcNb+53RcKKyQtoTIr3kUywXvEP623z7cg/J0 VM64huD4zaiI6Tk3QuMMlHH1GDUqXhxhZ+Vn0= 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=a7tcfq6nWB6pIont181OjIiMOtstWb6KKxdwtBq8w1o=; b=BuK1AgiwkWN9kvgo9H4RJA5/zRz8ldoCYbPsk5Tc+zZ5PE7GOdrr7xUbkpQQH5Rm+u rKGPFGPC9TGTMkDHm5cTC2pe5ZoL3ftjYQzTMZsBHchXC4pdIqT2rLMGSnxtbxbiMNkW OzI6qlur9G8kFXnpLoHr1dbVeTHtd9ZozdI8gXyde13wXFZ0vPxR62mCeFO6q4pfCmHA u8vx7F9ZB7YHA9BfLE1nOs4ibgJ97rtbG9Yh8/K9pjZ9aQ5XzhBIL56j/c+1kminDj0M SuK1ot7danCYv6TX68eregwRGPzwlPgm1ihHdAdDoR2VG4Ip94qWAjepfynMGOflaaYr MYhA== X-Gm-Message-State: AIVw111q0wMcOfwK9PQtWPwfIP3lrSaA6gOeneaAXBMFN7msSik4XpEd scdW5xDU3GxiDFU8 X-Received: by 10.98.213.196 with SMTP id d187mr249921pfg.146.1501730371029; Wed, 02 Aug 2017 20:19:31 -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 a125sm53034143pgc.37.2017.08.02.20.19.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Aug 2017 20:19:28 -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 2/4] seccomp: Add SECCOMP_FILTER_FLAG_KILL_PROCESS Date: Wed, 2 Aug 2017 20:19:11 -0700 Message-Id: <1501730353-46840-3-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501730353-46840-1-git-send-email-keescook@chromium.org> References: <1501730353-46840-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 Right now, SECCOMP_RET_KILL kills the current thread. There have been a few requests for RET_KILL to kill the entire process (the thread group), but since seccomp's u32 return values are ABI, and ordered by lowest value, with RET_KILL as 0, there isn't a trivial way to provide an even smaller value that would mean the more restrictive action of killing the thread group. Instead, create a filter flag that indicates that a RET_KILL from this filter must kill the process rather than the thread. This can be set (and not cleared) via the new SECCOMP_FILTER_FLAG_KILL_PROCESS flag. Pros: - the logic for the filter action is contained in the filter. - userspace can detect support for the feature since earlier kernels will reject the new flag. Cons: - depends on adding an assignment to the seccomp_run_filters() loop (previous patch). Alternatives to this approach with pros/cons: - Use a new test during seccomp_run_filters() that treats the RET_DATA mask of a RET_KILL action as special. If a new bit is set in the data, then treat the return value as -1 (lower than 0). Pros: - the logic for the filter action is contained in the filter. Cons: - added complexity to time-sensitive seccomp_run_filters() loop. - there isn't a trivial way for userspace to detect if the kernel supports the feature (earlier kernels will silently ignore the RET_DATA and only kill the thread). - Have SECCOMP_FILTER_FLAG_KILL_PROCESS attach to the seccomp struct rather than the filter. Pros: - no change needed to seccomp_run_filters() loop. Cons: - the change in behavior technically originates external to the filter, which allows for later filters to "enhance" a previously applied filter's RET_KILL to kill the entire process, which may be unexpected. Signed-off-by: Kees Cook --- include/linux/seccomp.h | 3 ++- include/uapi/linux/seccomp.h | 3 ++- kernel/seccomp.c | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h index ecc296c137cd..59d001ba655c 100644 --- a/include/linux/seccomp.h +++ b/include/linux/seccomp.h @@ -3,7 +3,8 @@ #include -#define SECCOMP_FILTER_FLAG_MASK (SECCOMP_FILTER_FLAG_TSYNC) +#define SECCOMP_FILTER_FLAG_MASK (SECCOMP_FILTER_FLAG_TSYNC | \ + SECCOMP_FILTER_FLAG_KILL_PROCESS) #ifdef CONFIG_SECCOMP diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h index 0f238a43ff1e..4b75d8c297b6 100644 --- a/include/uapi/linux/seccomp.h +++ b/include/uapi/linux/seccomp.h @@ -15,7 +15,8 @@ #define SECCOMP_SET_MODE_FILTER 1 /* Valid flags for SECCOMP_SET_MODE_FILTER */ -#define SECCOMP_FILTER_FLAG_TSYNC 1 +#define SECCOMP_FILTER_FLAG_TSYNC 1 +#define SECCOMP_FILTER_FLAG_KILL_PROCESS 2 /* * All BPF programs must return a 32-bit value. diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 8bdcf01379e4..931eb9cbd093 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -44,6 +44,7 @@ * is only needed for handling filters shared across tasks. * @prev: points to a previously installed, or inherited, filter * @prog: the BPF program to evaluate + * @kill_process: if true, RET_KILL will kill process rather than thread. * * seccomp_filter objects are organized in a tree linked via the @prev * pointer. For any task, it appears to be a singly-linked list starting @@ -59,6 +60,7 @@ struct seccomp_filter { refcount_t usage; struct seccomp_filter *prev; struct bpf_prog *prog; + bool kill_process; }; /* Limit any path through the tree to 256KB worth of instructions. */ @@ -448,6 +450,10 @@ static long seccomp_attach_filter(unsigned int flags, return ret; } + /* Set process-killing flag, if present. */ + if (flags & SECCOMP_FILTER_FLAG_KILL_PROCESS) + filter->kill_process = true; + /* * If there is an existing filter, make it the prev and don't drop its * task reference. @@ -658,7 +664,11 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd, seccomp_init_siginfo(&info, this_syscall, data); do_coredump(&info); } - do_exit(SIGSYS); + /* Kill entire thread group if requested (or went haywire). */ + if (!match || match->kill_process) + do_group_exit(SIGSYS); + else + do_exit(SIGSYS); } unreachable();