From: Roberto Sassu <roberto.sassu@huawei.com>
To: <zohar@linux.ibm.com>, <shuah@kernel.org>, <ast@kernel.org>,
<daniel@iogearbox.net>, <andrii@kernel.org>, <yhs@fb.com>,
<kpsingh@kernel.org>, <revest@chromium.org>,
<gregkh@linuxfoundation.org>
Cc: <linux-integrity@vger.kernel.org>,
<linux-security-module@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <bpf@vger.kernel.org>,
<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
Roberto Sassu <roberto.sassu@huawei.com>
Subject: [PATCH v3 9/9] selftests/bpf: Check that bpf_kernel_read_file() denies reading IMA policy
Date: Wed, 2 Mar 2022 12:14:04 +0100 [thread overview]
Message-ID: <20220302111404.193900-10-roberto.sassu@huawei.com> (raw)
In-Reply-To: <20220302111404.193900-1-roberto.sassu@huawei.com>
Check that bpf_kernel_read_file() denies the reading of an IMA policy, by
ensuring that ima_setup.sh exits with an error.
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
.../selftests/bpf/prog_tests/test_ima.c | 17 +++++++++++++++++
tools/testing/selftests/bpf/progs/ima.c | 18 ++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/test_ima.c b/tools/testing/selftests/bpf/prog_tests/test_ima.c
index b13a141c4220..b13feceb38f1 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_ima.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_ima.c
@@ -59,6 +59,7 @@ static void test_init(struct ima__bss *bss)
bss->use_ima_file_hash = false;
bss->enable_bprm_creds_for_exec = false;
bss->enable_kernel_read_file = false;
+ bss->test_deny = false;
}
void test_test_ima(void)
@@ -200,6 +201,22 @@ void test_test_ima(void)
ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
ASSERT_NEQ(ima_hash_from_bpf[1], 0, "ima_hash");
+ /*
+ * Test #6
+ * - Goal: ensure that the kernel_read_file hook denies an operation
+ * - Expected result: 0 samples
+ */
+ test_init(skel->bss);
+ skel->bss->enable_kernel_read_file = true;
+ skel->bss->test_deny = true;
+ err = _run_measured_process(measured_dir, &skel->bss->monitored_pid,
+ "load-policy");
+ if (CHECK(!err, "run_measured_process #6", "err = %d\n", err))
+ goto close_clean;
+
+ err = ring_buffer__consume(ringbuf);
+ ASSERT_EQ(err, 0, "num_samples_or_err");
+
close_clean:
snprintf(cmd, sizeof(cmd), "./ima_setup.sh cleanup %s", measured_dir);
err = system(cmd);
diff --git a/tools/testing/selftests/bpf/progs/ima.c b/tools/testing/selftests/bpf/progs/ima.c
index e3ce943c5c3d..e16a2c208481 100644
--- a/tools/testing/selftests/bpf/progs/ima.c
+++ b/tools/testing/selftests/bpf/progs/ima.c
@@ -21,6 +21,7 @@ char _license[] SEC("license") = "GPL";
bool use_ima_file_hash;
bool enable_bprm_creds_for_exec;
bool enable_kernel_read_file;
+bool test_deny;
static void ima_test_common(struct file *file)
{
@@ -51,6 +52,17 @@ static void ima_test_common(struct file *file)
return;
}
+static int ima_test_deny(void)
+{
+ u32 pid;
+
+ pid = bpf_get_current_pid_tgid() >> 32;
+ if (pid == monitored_pid && test_deny)
+ return -EPERM;
+
+ return 0;
+}
+
SEC("lsm.s/bprm_committed_creds")
void BPF_PROG(bprm_committed_creds, struct linux_binprm *bprm)
{
@@ -71,6 +83,8 @@ SEC("lsm.s/kernel_read_file")
int BPF_PROG(kernel_read_file, struct file *file, enum kernel_read_file_id id,
bool contents)
{
+ int ret;
+
if (!enable_kernel_read_file)
return 0;
@@ -80,6 +94,10 @@ int BPF_PROG(kernel_read_file, struct file *file, enum kernel_read_file_id id,
if (id != READING_POLICY)
return 0;
+ ret = ima_test_deny();
+ if (ret < 0)
+ return ret;
+
ima_test_common(file);
return 0;
}
--
2.32.0
next prev parent reply other threads:[~2022-03-02 11:18 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-02 11:13 [PATCH v3 0/9] bpf-lsm: Extend interoperability with IMA Roberto Sassu
2022-03-02 11:13 ` [PATCH v3 1/9] ima: Fix documentation-related warnings in ima_main.c Roberto Sassu
2022-03-06 19:24 ` Mimi Zohar
2022-03-02 11:13 ` [PATCH v3 2/9] ima: Always return a file measurement in ima_file_hash() Roberto Sassu
2022-03-06 19:31 ` Mimi Zohar
2022-03-02 11:13 ` [PATCH v3 3/9] bpf-lsm: Introduce new helper bpf_ima_file_hash() Roberto Sassu
2022-03-02 11:13 ` [PATCH v3 4/9] selftests/bpf: Move sample generation code to ima_test_common() Roberto Sassu
2022-03-02 11:14 ` [PATCH v3 5/9] selftests/bpf: Add test for bpf_ima_file_hash() Roberto Sassu
2022-03-02 11:14 ` [PATCH v3 6/9] selftests/bpf: Check if the digest is refreshed after a file write Roberto Sassu
2022-03-02 11:14 ` [PATCH v3 7/9] bpf-lsm: Make bpf_lsm_kernel_read_file() as sleepable Roberto Sassu
2022-03-02 11:14 ` [PATCH v3 8/9] selftests/bpf: Add test for bpf_lsm_kernel_read_file() Roberto Sassu
2022-03-02 11:14 ` Roberto Sassu [this message]
2022-03-02 22:20 ` [PATCH v3 0/9] bpf-lsm: Extend interoperability with IMA Alexei Starovoitov
2022-03-03 10:07 ` Roberto Sassu
2022-03-03 16:05 ` Mimi Zohar
2022-03-03 16:17 ` KP Singh
2022-03-03 16:29 ` Mimi Zohar
2022-03-03 18:14 ` KP Singh
2022-03-03 19:13 ` Mimi Zohar
2022-03-03 22:39 ` Alexei Starovoitov
2022-03-07 2:56 ` Mimi Zohar
2022-03-07 13:17 ` KP Singh
2022-03-07 20:44 ` Mimi Zohar
2022-03-06 19:23 ` Mimi Zohar
2022-03-07 10:31 ` Roberto Sassu
2022-03-11 3:20 ` patchwork-bot+netdevbpf
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=20220302111404.193900-10-roberto.sassu@huawei.com \
--to=roberto.sassu@huawei.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=gregkh@linuxfoundation.org \
--cc=kpsingh@kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=revest@chromium.org \
--cc=shuah@kernel.org \
--cc=yhs@fb.com \
--cc=zohar@linux.ibm.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).