From: Roberto Sassu <roberto.sassu@huaweicloud.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, song@kernel.org, yhs@fb.com,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
haoluo@google.com, jolsa@kernel.org, revest@chromium.org,
jackmanb@chromium.org, mykolal@fb.com, paul@paul-moore.com,
jmorris@namei.org, serge@hallyn.com, shuah@kernel.org
Cc: bpf@vger.kernel.org, linux-security-module@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Roberto Sassu <roberto.sassu@huawei.com>
Subject: [RFC][PATCH v2 5/7] selftests/bpf: Check if return values of LSM programs are allowed
Date: Wed, 7 Dec 2022 18:24:32 +0100 [thread overview]
Message-ID: <20221207172434.435893-6-roberto.sassu@huaweicloud.com> (raw)
In-Reply-To: <20221207172434.435893-1-roberto.sassu@huaweicloud.com>
From: Roberto Sassu <roberto.sassu@huawei.com>
Ensure that the eBPF verifier allows to load only LSM programs that return
an allowed value depending on the LSM hook they attach to.
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
.../testing/selftests/bpf/verifier/lsm_ret.c | 148 ++++++++++++++++++
1 file changed, 148 insertions(+)
create mode 100644 tools/testing/selftests/bpf/verifier/lsm_ret.c
diff --git a/tools/testing/selftests/bpf/verifier/lsm_ret.c b/tools/testing/selftests/bpf/verifier/lsm_ret.c
new file mode 100644
index 000000000000..c9c9cee8e406
--- /dev/null
+++ b/tools/testing/selftests/bpf/verifier/lsm_ret.c
@@ -0,0 +1,148 @@
+{
+ "lsm return value: positive not allowed, return -EPERM",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, -EPERM),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_permission",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: positive not allowed, return zero",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_permission",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: positive not allowed, return one",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_permission",
+ .expected_attach_type = BPF_LSM_MAC,
+ .errstr = "Invalid R0, cannot return 1",
+ .result = REJECT,
+},
+{
+ "lsm return value: zero/positive not allowed, return -EPERM",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, -EPERM),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_init_security",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: zero/positive not allowed, return zero",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_init_security",
+ .expected_attach_type = BPF_LSM_MAC,
+ .errstr = "Invalid R0, cannot return 0",
+ .result = REJECT,
+},
+{
+ "lsm return value: zero/positive not allowed, return one",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "inode_init_security",
+ .expected_attach_type = BPF_LSM_MAC,
+ .errstr = "Invalid R0, cannot return 1",
+ .result = REJECT,
+},
+{
+ "lsm return value: positive allowed, return one",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "getprocattr",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: positive allowed, return two",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 2),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "getprocattr",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: only one allowed, return one",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "audit_rule_match",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: only one allowed, return two",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 2),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "audit_rule_match",
+ .expected_attach_type = BPF_LSM_MAC,
+ .errstr = "Invalid R0, cannot return > 1",
+ .result = REJECT,
+},
+{
+ "lsm return value: negative not allowed, return -EPERM",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, -EPERM),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "ismaclabel",
+ .expected_attach_type = BPF_LSM_MAC,
+ .errstr = "Invalid R0, cannot return < 0",
+ .result = REJECT,
+},
+{
+ "lsm return value: negative not allowed, return zero",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "ismaclabel",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
+{
+ "lsm return value: negative not allowed, return one",
+ .insns = {
+ BPF_MOV64_IMM(BPF_REG_0, 1),
+ BPF_EXIT_INSN(),
+ },
+ .prog_type = BPF_PROG_TYPE_LSM,
+ .kfunc = "ismaclabel",
+ .expected_attach_type = BPF_LSM_MAC,
+ .result = ACCEPT,
+},
--
2.25.1
next prev parent reply other threads:[~2022-12-07 17:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-07 17:24 [RFC][PATCH v2 0/7] bpf-lsm: Check return values of security modules Roberto Sassu
2022-12-07 17:24 ` [RFC][PATCH v2 1/7] bpf: Remove superfluous btf_id_set_contains() declaration Roberto Sassu
2022-12-07 17:24 ` [RFC][PATCH v2 2/7] bpf: Mark ALU32 operations in bpf_reg_state structure Roberto Sassu
2022-12-11 2:28 ` Alexei Starovoitov
2022-12-12 12:44 ` Roberto Sassu
2022-12-12 17:04 ` Alexei Starovoitov
2022-12-12 18:10 ` Roberto Sassu
2022-12-07 17:24 ` [RFC][PATCH v2 3/7] lsm: Redefine LSM_HOOK() macro to add return value flags as argument Roberto Sassu
2022-12-07 17:24 ` [RFC][PATCH v2 4/7] bpf-lsm: Enforce return value limitations on security modules Roberto Sassu
2022-12-07 17:24 ` Roberto Sassu [this message]
2022-12-07 17:24 ` [RFC][PATCH v2 6/7] selftests/bpf: Prevent positive ret values in test_lsm and verify_pkcs7_sig Roberto Sassu
2022-12-07 17:24 ` [RFC][PATCH v2 7/7] selftests/bpf: Change return value in test_libbpf_get_fd_by_id_opts.c Roberto Sassu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221207172434.435893-6-roberto.sassu@huaweicloud.com \
--to=roberto.sassu@huaweicloud.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=jackmanb@chromium.org \
--cc=jmorris@namei.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=paul@paul-moore.com \
--cc=revest@chromium.org \
--cc=roberto.sassu@huawei.com \
--cc=sdf@google.com \
--cc=serge@hallyn.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).