[PATCH] landlock/id: Fix WARNING from kunit tests
Mickaël Salaün
mic at digikod.net
Tue Jun 17 18:55:26 UTC 2025
On Sun, Jun 15, 2025 at 05:09:36PM +0100, Tingmao Wang wrote:
> Looks like get_random_u8 returned 0 here. Fix this by clamping it.
Goot catch, thanks!
>
> (Validated by running the test in a for loop for 1000 times. Note that
> MAX is wrong as it is only supposed to be used for unchanging variables,
> but the lowercase version is good here.)
>
> [..] ok 9 test_range2_rand1
> [..] ok 10 test_range2_rand2
> [..] ok 11 test_range2_rand15
> [..] ------------[ cut here ]------------
> [..] WARNING: CPU: 6 PID: 104 at security/landlock/id.c:99 test_range2_rand16 (security/landlock/id.c:99 (discriminator 1) security/landlock/id.c:234 (discriminator 1))
> [..] Modules linked in:
> [..] CPU: 6 UID: 0 PID: 104 Comm: kunit_try_catch Tainted: G N 6.16.0-rc1-dev-00001-g314a2f98b65f #1 PREEMPT(undef)
> [..] Tainted: [N]=TEST
> [..] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
> [..] RIP: 0010:test_range2_rand16 (security/landlock/id.c:99 (discriminator 1) security/landlock/id.c:234 (discriminator 1))
> [..] Code: 49 c7 c0 10 70 30 82 4c 89 ff 48 c7 c6 a0 63 1e 83 49 c7 45 a0 e0 63 1e 83 e8 3f 95 17 00 e9 1f ff ff ff 0f 0b e9 df fd ff ff <0f> 0b ba 01 00 00 00 e9 68 fe ff ff 49 89 45 a8 49 8d 4d a0 45 31
> ..
> [..] RSP: 0000:ffff888104eb7c78 EFLAGS: 00010246
> [..] RAX: 0000000000000000 RBX: 000000000870822c RCX: 0000000000000000
> ^^^^^^^^^^^^^^^^
> [..]
> [..] Call Trace:
> [..]
> [..] ---[ end trace 0000000000000000 ]---
> [..] ok 12 test_range2_rand16
> [..] # landlock_id: pass:12 fail:0 skip:0 total:12
> [..] # Totals: pass:12 fail:0 skip:0 total:12
> [..] ok 1 landlock_id
>
> Fixes: d9d2a68ed44b ("landlock: Add unique ID generator")
> Signed-off-by: Tingmao Wang <m at maowtm.org>
> ---
> security/landlock/id.c | 69 +++++++++++++++++++++++++-----------------
> 1 file changed, 42 insertions(+), 27 deletions(-)
>
> diff --git a/security/landlock/id.c b/security/landlock/id.c
> index 56f7cc0fc744..b02a7da2f15d 100644
> --- a/security/landlock/id.c
> +++ b/security/landlock/id.c
> @@ -119,6 +119,12 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,
>
> #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
>
> +static u8 get_random_u8_positive(void)
> +{
> + /* max evaluates its arguments once */
> + return max(1, get_random_u8());
> +}
> +
> static void test_range1_rand0(struct kunit *const test)
> {
> atomic64_t counter;
> @@ -127,9 +133,10 @@ static void test_range1_rand0(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 0), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 1);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 1);
> }
>
> static void test_range1_rand1(struct kunit *const test)
> @@ -140,9 +147,10 @@ static void test_range1_rand1(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 1), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 2);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 2);
> }
>
> static void test_range1_rand15(struct kunit *const test)
> @@ -153,9 +161,10 @@ static void test_range1_rand15(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 15), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 16);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 16);
> }
>
> static void test_range1_rand16(struct kunit *const test)
> @@ -166,9 +175,10 @@ static void test_range1_rand16(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 16), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 1);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 1);
> }
>
> static void test_range2_rand0(struct kunit *const test)
> @@ -179,9 +189,10 @@ static void test_range2_rand0(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 0), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 2);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 2);
> }
>
> static void test_range2_rand1(struct kunit *const test)
> @@ -192,9 +203,10 @@ static void test_range2_rand1(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 1), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 3);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 3);
> }
>
> static void test_range2_rand2(struct kunit *const test)
> @@ -205,9 +217,10 @@ static void test_range2_rand2(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 2), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 4);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 4);
> }
>
> static void test_range2_rand15(struct kunit *const test)
> @@ -218,9 +231,10 @@ static void test_range2_rand15(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 15), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 17);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 17);
> }
>
> static void test_range2_rand16(struct kunit *const test)
> @@ -231,9 +245,10 @@ static void test_range2_rand16(struct kunit *const test)
> init = get_random_u32();
> atomic64_set(&counter, init);
> KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 16), init);
> - KUNIT_EXPECT_EQ(
> - test, get_id_range(get_random_u8(), &counter, get_random_u8()),
> - init + 2);
> + KUNIT_EXPECT_EQ(test,
> + get_id_range(get_random_u8_positive(), &counter,
> + get_random_u8()),
> + init + 2);
> }
>
> #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */
>
> base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
> --
> 2.49.0
>
>
More information about the Linux-security-module-archive
mailing list