Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp771302ybl; Thu, 23 Jan 2020 07:27:48 -0800 (PST) X-Google-Smtp-Source: APXvYqxK04TCpY4TIf6PNGU+o60BHokTm7KQf6Yj3nVOUpSmp9fO3P9zxVzGf9L1emtbbE2L3vQD X-Received: by 2002:a9d:6c01:: with SMTP id f1mr11120115otq.133.1579793268665; Thu, 23 Jan 2020 07:27:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579793268; cv=none; d=google.com; s=arc-20160816; b=Mok3leEr/hNubhvIs76824RBi/zghrQFo0WIeosAN1Jd8H6qTqszqopa+JiIQlFauT ejb8VJIXw7bsjiKKs4ByGYQHZZy4UQASwGb7E3HTOwSrW3VO6HWc9w/mzG03SLcy2DoE gvVanIwGrGk5aQnX+SxfhfgNth3OlPg/F7JCJO4o18qD28UHJ4LOEj2njmTriYlpBpc2 gEEa+KbW1kPGS6ymP5fxrk1/RKbvz5B7hJqoKGhMaOZ5FMevzw3ComIVazi1XWgWBwBH nZBxearSlYywEByMMzWEw8dPpzxWsI0v3ywXL+spBk+qQujKhUbjCjPHhC17nFGcj0Bk I9qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=P5LBJaFDLztjq1pA7rdewN/YeJDD/aqWt25XqpUQugg=; b=iKmrb3EnlIYHdIQXcRPTvXzcrLoopWq2S30Mkdz0puyi5ZvYMY/ZlrOvPsWPeev52r 1aAM5F3qSTP3gYqg7mJR6TOQfDGR/tHN0j6NjaIJ1esISQDxhaAMKfE7NtMDTJxo951R 5DVqiVzZg54JDcEEB6cILDQ7nCz7PepKKbhxBE1+o/dPfgvm57G+jDJXOG234ecSBd5c ME7PvBFg2rY9bwtPdmrHBvKOQevSiPtbLUvvWZe5KD3Zd7f+qifqE+m0K9Wzw2xYRSG+ 7Wlf9IvXF/b4Y61I94TvYwZV4VFpEjcADL5XPTtZZjyNOg5Exy3Im71iHx0EdZ5uOFeK EqNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PhO4IWnS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k19si1292649otb.118.2020.01.23.07.27.36; Thu, 23 Jan 2020 07:27:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PhO4IWnS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729152AbgAWPZO (ORCPT + 99 others); Thu, 23 Jan 2020 10:25:14 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:36853 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729138AbgAWPZM (ORCPT ); Thu, 23 Jan 2020 10:25:12 -0500 Received: by mail-pf1-f193.google.com with SMTP id w2so1701155pfd.3 for ; Thu, 23 Jan 2020 07:25:12 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=P5LBJaFDLztjq1pA7rdewN/YeJDD/aqWt25XqpUQugg=; b=PhO4IWnS7UTGNIhjsCNuAQ+vXoBbxnfY+FxDg5skR81zbdnTTrSt0u+XLNYUy6JfR5 u1Z1SX8nflhnoYrAkWD7dAa2ImWhYnfxdG9acvMYHRvDCPlRxXwY8JCdfIk6kzayhhyS 6iCh4YUlv9F5YJ1hWAhw23Z8ixI8P88gcoLUk= 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=P5LBJaFDLztjq1pA7rdewN/YeJDD/aqWt25XqpUQugg=; b=h021w4D3WMiv3xqXvNsJwJegbv8Zl50Wf+po4UzAFoJCcVOUFbyVzn62QOxRz4obv8 N2VsMEK4Nu5tlDPbN2/bN0X+EIkf2NA7CTh0wcc4hyGmb/0DmSKvgRI/rtcpPA1KxHA0 eyEX8Kd3BH19M9zrZ3ePBxfBn07Cga7UoDsjsIfTNnv6vTDVn4ee1VCfF8ivFLkv5Lqk GvcqUIoNpEEpaDQyC563e8EA+eKNGOlJjYkF8ZTd9hMTtsFylTfQlCU9uWdKIbGL38wU 3wCS+dvDydeMFjog1g9DobtIJS4v7geUMVsX87EoEkz37pIjYDr5+vcR09DodgYYQEp1 MRkQ== X-Gm-Message-State: APjAAAUjl+sN7jLmwyTNH/RKITcWxYiuVCws7GexlluadLq1lPBtgIno UdAxPrdt9gL0BKMwyCL5ciT0QeqjJHYbIQ== X-Received: by 2002:a62:6805:: with SMTP id d5mr7923883pfc.125.1579793111792; Thu, 23 Jan 2020 07:25:11 -0800 (PST) Received: from kpsingh-kernel.localdomain ([2a00:79e1:abc:122:bd8d:3f7b:87f7:16d1]) by smtp.gmail.com with ESMTPSA id v5sm3108118pfn.122.2020.01.23.07.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 07:25:11 -0800 (PST) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Brendan Jackman , Florent Revest , Thomas Garnier , Alexei Starovoitov , Daniel Borkmann , James Morris , Kees Cook , Thomas Garnier , Michael Halcrow , Paul Turner , Brendan Gregg , Jann Horn , Matthew Garrett , Christian Brauner , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Florent Revest , Brendan Jackman , Martin KaFai Lau , Song Liu , Yonghong Song , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Nicolas Ferre , Stanislav Fomichev , Quentin Monnet , Andrey Ignatov , Joe Stringer Subject: [PATCH bpf-next v3 02/10] bpf: lsm: Add a skeleton and config options Date: Thu, 23 Jan 2020 07:24:32 -0800 Message-Id: <20200123152440.28956-3-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123152440.28956-1-kpsingh@chromium.org> References: <20200123152440.28956-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh The LSM can be enabled by CONFIG_SECURITY_BPF. Without CONFIG_SECURITY_BPF_ENFORCE, the LSM will run the attached eBPF programs but not enforce MAC policy based on the return value of the attached programs. The BPF LSM has two kinds of hooks: - Statically defined hooks defined at init with __lsm_ro_after_init which are attached to the security_hook_heads defined in security/security.c similar to other LSMs. - Mutable hooks that are attached to a separate security_hook_heads maintained by the BPF LSM (introduced in a subsequent patch). The mutable hooks are always executed after all the static hooks (irrespective of the position of "bpf" in the list of LSMs). Therefore, the newly introduced LSM_ORDER_LAST represents the behaviour of this LSM correctly Signed-off-by: KP Singh Reviewed-by: Brendan Jackman Reviewed-by: Florent Revest Reviewed-by: Thomas Garnier --- MAINTAINERS | 7 +++++++ include/linux/lsm_hooks.h | 1 + security/Kconfig | 11 ++++++----- security/Makefile | 2 ++ security/bpf/Kconfig | 22 ++++++++++++++++++++++ security/bpf/Makefile | 5 +++++ security/bpf/lsm.c | 26 ++++++++++++++++++++++++++ security/security.c | 5 +++++ 8 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 security/bpf/Kconfig create mode 100644 security/bpf/Makefile create mode 100644 security/bpf/lsm.c diff --git a/MAINTAINERS b/MAINTAINERS index 702382b89c37..e2b7f76a1a70 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3203,6 +3203,13 @@ S: Supported F: arch/x86/net/ X: arch/x86/net/bpf_jit_comp32.c +BPF SECURITY MODULE +M: KP Singh +L: linux-security-module@vger.kernel.org +L: bpf@vger.kernel.org +S: Maintained +F: security/bpf/ + BROADCOM B44 10/100 ETHERNET DRIVER M: Michael Chan L: netdev@vger.kernel.org diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 20d8cf194fb7..5f744fcb2275 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -2121,6 +2121,7 @@ extern void security_add_hooks(struct security_hook_list *hooks, int count, enum lsm_order { LSM_ORDER_FIRST = -1, /* This is only for capabilities. */ LSM_ORDER_MUTABLE = 0, + LSM_ORDER_LAST = 1, }; struct lsm_info { diff --git a/security/Kconfig b/security/Kconfig index 2a1a2d396228..6f1aab195e7d 100644 --- a/security/Kconfig +++ b/security/Kconfig @@ -236,6 +236,7 @@ source "security/tomoyo/Kconfig" source "security/apparmor/Kconfig" source "security/loadpin/Kconfig" source "security/yama/Kconfig" +source "security/bpf/Kconfig" source "security/safesetid/Kconfig" source "security/lockdown/Kconfig" @@ -277,11 +278,11 @@ endchoice config LSM string "Ordered list of enabled LSMs" - default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK - default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR - default "lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO - default "lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC - default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" + default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK + default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR + default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO + default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC + default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" help A comma-separated list of LSMs, in initialization order. Any LSMs left off this list will be ignored. This can be diff --git a/security/Makefile b/security/Makefile index be1dd9d2cb2f..50e6821dd7b7 100644 --- a/security/Makefile +++ b/security/Makefile @@ -12,6 +12,7 @@ subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown +subdir-$(CONFIG_SECURITY_BPF) += bpf # always enable default capabilities obj-y += commoncap.o @@ -29,6 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ +obj-$(CONFIG_SECURITY_BPF) += bpf/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists diff --git a/security/bpf/Kconfig b/security/bpf/Kconfig new file mode 100644 index 000000000000..a5f6c67ae526 --- /dev/null +++ b/security/bpf/Kconfig @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright 2019 Google LLC. + +config SECURITY_BPF + bool "BPF-based MAC and audit policy" + depends on SECURITY + depends on BPF_SYSCALL + help + This enables instrumentation of the security hooks with + eBPF programs. + + If you are unsure how to answer this question, answer N. + +config SECURITY_BPF_ENFORCE + bool "Deny operations based on the evaluation of the attached programs" + depends on SECURITY_BPF + help + eBPF programs attached to hooks can be used for both auditing and + enforcement. Enabling enforcement implies that the evaluation result + from the attached eBPF programs will allow or deny the operation + guarded by the security hook. diff --git a/security/bpf/Makefile b/security/bpf/Makefile new file mode 100644 index 000000000000..26a0ab6f99b7 --- /dev/null +++ b/security/bpf/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright 2019 Google LLC. + +obj-$(CONFIG_SECURITY_BPF) := lsm.o diff --git a/security/bpf/lsm.c b/security/bpf/lsm.c new file mode 100644 index 000000000000..dc9ac03c7aa0 --- /dev/null +++ b/security/bpf/lsm.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2019 Google LLC. + */ + +#include + +/* This is only for internal hooks, always statically shipped as part of the + * BPF LSM. Statically defined hooks are appended to the security_hook_heads + * which is common for LSMs and R/O after init. + */ +static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = {}; + +static int __init bpf_lsm_init(void) +{ + security_add_hooks(bpf_lsm_hooks, ARRAY_SIZE(bpf_lsm_hooks), "bpf"); + pr_info("LSM support for eBPF active\n"); + return 0; +} + +DEFINE_LSM(bpf) = { + .name = "bpf", + .init = bpf_lsm_init, + .order = LSM_ORDER_LAST, +}; diff --git a/security/security.c b/security/security.c index cd2d18d2d279..30a8aa700557 100644 --- a/security/security.c +++ b/security/security.c @@ -264,6 +264,11 @@ static void __init ordered_lsm_parse(const char *order, const char *origin) } } + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { + if (lsm->order == LSM_ORDER_LAST) + append_ordered_lsm(lsm, "last"); + } + /* Disable all LSMs not in the ordered list. */ for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { if (exists_ordered_lsm(lsm)) -- 2.20.1