Skip to content

Commit

Permalink
selftests/bpf: Fix IMA test
Browse files Browse the repository at this point in the history
[ Upstream commit 12fabae ]

Commit 62622da ("ima: return IMA digest value only when IMA_COLLECTED
flag is set") caused bpf_ima_inode_hash() to refuse to give non-fresh
digests. IMA test #3 assumed the old behavior, that bpf_ima_inode_hash()
still returned also non-fresh digests.

Correct the test by accepting both cases. If the samples returned are 1,
assume that the commit above is applied and that the returned digest is
fresh. If the samples returned are 2, assume that the commit above is not
applied, and check both the non-fresh and fresh digest.

Fixes: 62622da ("ima: return IMA digest value only when IMA_COLLECTED flag is set")
Reported-by: David Vernet <void@manifault.com>
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Matt Bobrowski <mattbobrowski@google.com>
Link: https://lore.kernel.org/bpf/20230308103713.1681200-1-roberto.sassu@huaweicloud.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
robertosassu authored and gregkh committed May 11, 2023
1 parent 5252c71 commit 2157b81
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions tools/testing/selftests/bpf/prog_tests/test_ima.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void test_test_ima(void)
u64 bin_true_sample;
char cmd[256];

int err, duration = 0;
int err, duration = 0, fresh_digest_idx = 0;
struct ima *skel = NULL;

skel = ima__open_and_load();
Expand Down Expand Up @@ -129,7 +129,15 @@ void test_test_ima(void)
/*
* Test #3
* - Goal: confirm that bpf_ima_inode_hash() returns a non-fresh digest
* - Expected result: 2 samples (/bin/true: non-fresh, fresh)
* - Expected result:
* 1 sample (/bin/true: fresh) if commit 62622dab0a28 applied
* 2 samples (/bin/true: non-fresh, fresh) if commit 62622dab0a28 is
* not applied
*
* If commit 62622dab0a28 ("ima: return IMA digest value only when
* IMA_COLLECTED flag is set") is applied, bpf_ima_inode_hash() refuses
* to give a non-fresh digest, hence the correct result is 1 instead of
* 2.
*/
test_init(skel->bss);

Expand All @@ -144,13 +152,18 @@ void test_test_ima(void)
goto close_clean;

err = ring_buffer__consume(ringbuf);
ASSERT_EQ(err, 2, "num_samples_or_err");
ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
ASSERT_NEQ(ima_hash_from_bpf[1], 0, "ima_hash");
ASSERT_EQ(ima_hash_from_bpf[0], bin_true_sample, "sample_equal_or_err");
ASSERT_GE(err, 1, "num_samples_or_err");
if (err == 2) {
ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
ASSERT_EQ(ima_hash_from_bpf[0], bin_true_sample,
"sample_equal_or_err");
fresh_digest_idx = 1;
}

ASSERT_NEQ(ima_hash_from_bpf[fresh_digest_idx], 0, "ima_hash");
/* IMA refreshed the digest. */
ASSERT_NEQ(ima_hash_from_bpf[1], bin_true_sample,
"sample_different_or_err");
ASSERT_NEQ(ima_hash_from_bpf[fresh_digest_idx], bin_true_sample,
"sample_equal_or_err");

/*
* Test #4
Expand Down

0 comments on commit 2157b81

Please sign in to comment.