Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2816059ybb; Fri, 27 Mar 2020 12:35:51 -0700 (PDT) X-Received: by 2002:a4a:a88a:: with SMTP id q10mr856735oom.17.1585337511560; Fri, 27 Mar 2020 12:31:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsw3Mde7KILBP7qXDtaEvrdmTEVBs4/DgZ2Exiy0tm+ivfWYFRwFxTcqNK8ckd6glTWe8vL X-Received: by 2002:a4a:a88a:: with SMTP id q10mr856642oom.17.1585337510670; Fri, 27 Mar 2020 12:31:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585337510; cv=none; d=google.com; s=arc-20160816; b=rRjZq5LXJyXrLFMhU9lX7yph9dxUU+Bhyq2Fpzp6QdoWv2id2M5krVNRAI5aT3kg1v /xIpsOmwTcl9c7j4wNEWwonOX1C3kxtzrmz46VnTgSpxQUn35uUocqQvQ8plrJMKffl+ srm9mFULE1X24aOvQj+T1sbwrAO8HMg/v9q0BW7tBC8CWvpz5EvyKOgYSg2UptIpTvGE Xjz1l3EfaQEiuAIstRRGJh75ZwJAuprLdDbeEJUznsE27w51vewMx3sVUqd91O2V1Ggv rP7RVTpiO2pLlCcNnUxJG6O6ARQk/bOaOh+seFkt0eQ7Jg3M1k5j00gMFCUQtpO5gwwe B9mw== 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=fi0wlo+PTlR63f+1WnfYaQN4JRPMx/qWTtudseh8Beg=; b=CSzUACJP7ZU5WclcEzlFeZMsdOYKCQVpCafpEkpUPmaRK/9ZjoqI94g7LLzZSsJEgU y4s9xKUSp3gG+DOM726bzkAcV/5aMTkEKr5fSVRXUs/z5XO0gltaPMXx4zljPwtEMocu BVk0BPI4xDsb8KjbHMvFpE+qPhqc3V3EwiDh03jjgrLWc/EbrlgLZCH+MT5u6evCIxmS Z/cr/LGYk6ZHwZuUlZ2hbQYI4w9Ehuxb6nCk6ASQWHVmGJBBsNNRbHcx9tSkMB1/42C7 SLIjhpjBq4qaqg+i4O6DxPTCd/Use8I08UjAk9O3OJE3tNWbOjbSDFOycFMl/+h5oEd6 eylA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NlM0vxBe; 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 t125si2545606oie.167.2020.03.27.12.31.24; Fri, 27 Mar 2020 12:31:50 -0700 (PDT) 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=NlM0vxBe; 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 S1727835AbgC0T3G (ORCPT + 99 others); Fri, 27 Mar 2020 15:29:06 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34051 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727772AbgC0T3C (ORCPT ); Fri, 27 Mar 2020 15:29:02 -0400 Received: by mail-wr1-f65.google.com with SMTP id 65so12894209wrl.1 for ; Fri, 27 Mar 2020 12:28:59 -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 :mime-version:content-transfer-encoding; bh=fi0wlo+PTlR63f+1WnfYaQN4JRPMx/qWTtudseh8Beg=; b=NlM0vxBeUU64PoXr1EWEKkuqWfLl1QZL+7Nqunf9O9OOSYVK2ViZhd7Zc3ATS9LRkZ dHGzw9f1nKFfYkpDWaPK9oFMsAjOZK/KQQkslRDo2VTcbSRUmJRzECd5CiTBMDPZd3GJ /qYAdUMHlH8cWuaIYGOUU/nQZehvUYNtCSZVg= 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=fi0wlo+PTlR63f+1WnfYaQN4JRPMx/qWTtudseh8Beg=; b=aq84iW52d6APOCnol1L/BalKe9FZCqGRztd0mzQj/U40dIWagZTa57yCFYfUerf+va rx8+aDcDcZqm710hQjbMV5Yp9FJvAuVyKyUqhZFv2uxGdSZs0xh1v6ySJ/04UYpEW5oR 6KDz1Z0GZSYNPm37In/QormJPNpMmpvBYU3wx4ZmzwLBQhw+XvDBnnlM4xb0uAOdphmL JuAT1z6rtJoeK1bhoUIAJuolDOYaWF+8kVE7OfOE0Bv4Tk1RPVxkllP5wOHpWAAjW7Gz 9gKOZ9NtmwOWOLwDwUWqhzIMptwX/XW9zM6ZLiw8oMIm5LkHs4SJkq+mAjdtKCk1Dudq PMEw== X-Gm-Message-State: ANhLgQ1Nar02IgRHdWLauH+H/cKrSRSNnYQJ0ghb4JAvbXFfDcCFO3vJ +/gMwxORZebJX+Mry/duYaqOIeuDqhA= X-Received: by 2002:a5d:4cc2:: with SMTP id c2mr1050394wrt.398.1585337338621; Fri, 27 Mar 2020 12:28:58 -0700 (PDT) Received: from kpsingh-kernel.localdomain (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id h132sm9828537wmf.18.2020.03.27.12.28.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 12:28:58 -0700 (PDT) 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 , Yonghong Song , Andrii Nakryiko , James Morris , Alexei Starovoitov , Daniel Borkmann , James Morris , Kees Cook , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman , Greg Kroah-Hartman Subject: [PATCH bpf-next v8 1/8] bpf: Introduce BPF_PROG_TYPE_LSM Date: Fri, 27 Mar 2020 20:28:47 +0100 Message-Id: <20200327192854.31150-2-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200327192854.31150-1-kpsingh@chromium.org> References: <20200327192854.31150-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 Introduce types and configs for bpf programs that can be attached to LSM hooks. The programs can be enabled by the config option CONFIG_BPF_LSM. Signed-off-by: KP Singh Reviewed-by: Brendan Jackman Reviewed-by: Florent Revest Reviewed-by: Thomas Garnier Acked-by: Yonghong Song Acked-by: Andrii Nakryiko Acked-by: James Morris --- MAINTAINERS | 1 + include/linux/bpf.h | 3 +++ include/linux/bpf_types.h | 4 ++++ include/uapi/linux/bpf.h | 2 ++ init/Kconfig | 12 ++++++++++++ kernel/bpf/Makefile | 1 + kernel/bpf/bpf_lsm.c | 17 +++++++++++++++++ kernel/trace/bpf_trace.c | 12 ++++++------ tools/include/uapi/linux/bpf.h | 2 ++ tools/lib/bpf/libbpf_probes.c | 1 + 10 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bpf_lsm.c diff --git a/MAINTAINERS b/MAINTAINERS index 5dbee41045bc..3197fe9256b2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3147,6 +3147,7 @@ R: Martin KaFai Lau R: Song Liu R: Yonghong Song R: Andrii Nakryiko +R: KP Singh L: netdev@vger.kernel.org L: bpf@vger.kernel.org T: git git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git diff --git a/include/linux/bpf.h b/include/linux/bpf.h index bdb981c204fa..af81ec7b783c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1513,6 +1513,9 @@ extern const struct bpf_func_proto bpf_tcp_sock_proto; extern const struct bpf_func_proto bpf_jiffies64_proto; extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto; +const struct bpf_func_proto *bpf_tracing_func_proto( + enum bpf_func_id func_id, const struct bpf_prog *prog); + /* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once(void); u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h index c81d4ece79a4..ba0c2d56f8a3 100644 --- a/include/linux/bpf_types.h +++ b/include/linux/bpf_types.h @@ -70,6 +70,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_STRUCT_OPS, bpf_struct_ops, void *, void *) BPF_PROG_TYPE(BPF_PROG_TYPE_EXT, bpf_extension, void *, void *) +#ifdef CONFIG_BPF_LSM +BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm, + void *, void *) +#endif /* CONFIG_BPF_LSM */ #endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 5d01c5c7e598..9f2673c58788 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -181,6 +181,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_TRACING, BPF_PROG_TYPE_STRUCT_OPS, BPF_PROG_TYPE_EXT, + BPF_PROG_TYPE_LSM, }; enum bpf_attach_type { @@ -211,6 +212,7 @@ enum bpf_attach_type { BPF_TRACE_FENTRY, BPF_TRACE_FEXIT, BPF_MODIFY_RETURN, + BPF_LSM_MAC, __MAX_BPF_ATTACH_TYPE }; diff --git a/init/Kconfig b/init/Kconfig index 20a6ac33761c..deae572d1927 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1616,6 +1616,18 @@ config KALLSYMS_BASE_RELATIVE # end of the "standard kernel features (expert users)" menu # syscall, maps, verifier + +config BPF_LSM + bool "LSM Instrumentation with BPF" + depends on BPF_SYSCALL + depends on SECURITY + depends on BPF_JIT + help + Enables instrumentation of the security hooks with eBPF programs for + implementing dynamic MAC and Audit Policies. + + If you are unsure how to answer this question, answer N. + config BPF_SYSCALL bool "Enable bpf() system call" select BPF diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 046ce5d98033..f2d7be596966 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -29,4 +29,5 @@ obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o endif ifeq ($(CONFIG_BPF_JIT),y) obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o +obj-${CONFIG_BPF_LSM} += bpf_lsm.o endif diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c new file mode 100644 index 000000000000..82875039ca90 --- /dev/null +++ b/kernel/bpf/bpf_lsm.c @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (C) 2020 Google LLC. + */ + +#include +#include +#include + +const struct bpf_prog_ops lsm_prog_ops = { +}; + +const struct bpf_verifier_ops lsm_verifier_ops = { + .get_func_proto = bpf_tracing_func_proto, + .is_valid_access = btf_ctx_access, +}; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index e619eedb5919..37ffceab608f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -779,8 +779,8 @@ static const struct bpf_func_proto bpf_send_signal_thread_proto = { .arg1_type = ARG_ANYTHING, }; -static const struct bpf_func_proto * -tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) +const struct bpf_func_proto * +bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { switch (func_id) { case BPF_FUNC_map_lookup_elem: @@ -865,7 +865,7 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_override_return_proto; #endif default: - return tracing_func_proto(func_id, prog); + return bpf_tracing_func_proto(func_id, prog); } } @@ -975,7 +975,7 @@ tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_stack: return &bpf_get_stack_proto_tp; default: - return tracing_func_proto(func_id, prog); + return bpf_tracing_func_proto(func_id, prog); } } @@ -1082,7 +1082,7 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_read_branch_records: return &bpf_read_branch_records_proto; default: - return tracing_func_proto(func_id, prog); + return bpf_tracing_func_proto(func_id, prog); } } @@ -1210,7 +1210,7 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_stack: return &bpf_get_stack_proto_raw_tp; default: - return tracing_func_proto(func_id, prog); + return bpf_tracing_func_proto(func_id, prog); } } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 5d01c5c7e598..9f2673c58788 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -181,6 +181,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_TRACING, BPF_PROG_TYPE_STRUCT_OPS, BPF_PROG_TYPE_EXT, + BPF_PROG_TYPE_LSM, }; enum bpf_attach_type { @@ -211,6 +212,7 @@ enum bpf_attach_type { BPF_TRACE_FENTRY, BPF_TRACE_FEXIT, BPF_MODIFY_RETURN, + BPF_LSM_MAC, __MAX_BPF_ATTACH_TYPE }; diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index b782ebef6ac9..2c92059c0c90 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -108,6 +108,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, case BPF_PROG_TYPE_TRACING: case BPF_PROG_TYPE_STRUCT_OPS: case BPF_PROG_TYPE_EXT: + case BPF_PROG_TYPE_LSM: default: break; } -- 2.20.1