lkml.org 
[lkml]   [2022]   [Dec]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[RFC][PATCH v2 5/7] selftests/bpf: Check if return values of LSM programs are allowed
Date
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
\
 
 \ /
  Last update: 2022-12-07 18:27    [W:0.088 / U:0.356 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site