Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

landlock: Fix warning from KUnit tests

get_id_range() expects a positive value as first argument but
get_random_u8() can return 0. Fix this by clamping it.

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
constants, but max() 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@maowtm.org>
Link: https://lore.kernel.org/r/73e28efc5b8cc394608b99d5bc2596ca917d7c4a.1750003733.git.m@maowtm.org
[mic: Minor cosmetic improvements]
Signed-off-by: Mickaël Salaün <mic@digikod.net>

authored by

Tingmao Wang and committed by
Mickaël Salaün
e0a69cf2 dae01387

+42 -27
+42 -27
security/landlock/id.c
··· 119 119 120 120 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 121 121 122 + static u8 get_random_u8_positive(void) 123 + { 124 + /* max() evaluates its arguments once. */ 125 + return max(1, get_random_u8()); 126 + } 127 + 122 128 static void test_range1_rand0(struct kunit *const test) 123 129 { 124 130 atomic64_t counter; ··· 133 127 init = get_random_u32(); 134 128 atomic64_set(&counter, init); 135 129 KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 0), init); 136 - KUNIT_EXPECT_EQ( 137 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 138 - init + 1); 130 + KUNIT_EXPECT_EQ(test, 131 + get_id_range(get_random_u8_positive(), &counter, 132 + get_random_u8()), 133 + init + 1); 139 134 } 140 135 141 136 static void test_range1_rand1(struct kunit *const test) ··· 147 140 init = get_random_u32(); 148 141 atomic64_set(&counter, init); 149 142 KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 1), init); 150 - KUNIT_EXPECT_EQ( 151 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 152 - init + 2); 143 + KUNIT_EXPECT_EQ(test, 144 + get_id_range(get_random_u8_positive(), &counter, 145 + get_random_u8()), 146 + init + 2); 153 147 } 154 148 155 149 static void test_range1_rand15(struct kunit *const test) ··· 161 153 init = get_random_u32(); 162 154 atomic64_set(&counter, init); 163 155 KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 15), init); 164 - KUNIT_EXPECT_EQ( 165 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 166 - init + 16); 156 + KUNIT_EXPECT_EQ(test, 157 + get_id_range(get_random_u8_positive(), &counter, 158 + get_random_u8()), 159 + init + 16); 167 160 } 168 161 169 162 static void test_range1_rand16(struct kunit *const test) ··· 175 166 init = get_random_u32(); 176 167 atomic64_set(&counter, init); 177 168 KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 16), init); 178 - KUNIT_EXPECT_EQ( 179 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 180 - init + 1); 169 + KUNIT_EXPECT_EQ(test, 170 + get_id_range(get_random_u8_positive(), &counter, 171 + get_random_u8()), 172 + init + 1); 181 173 } 182 174 183 175 static void test_range2_rand0(struct kunit *const test) ··· 189 179 init = get_random_u32(); 190 180 atomic64_set(&counter, init); 191 181 KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 0), init); 192 - KUNIT_EXPECT_EQ( 193 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 194 - init + 2); 182 + KUNIT_EXPECT_EQ(test, 183 + get_id_range(get_random_u8_positive(), &counter, 184 + get_random_u8()), 185 + init + 2); 195 186 } 196 187 197 188 static void test_range2_rand1(struct kunit *const test) ··· 203 192 init = get_random_u32(); 204 193 atomic64_set(&counter, init); 205 194 KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 1), init); 206 - KUNIT_EXPECT_EQ( 207 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 208 - init + 3); 195 + KUNIT_EXPECT_EQ(test, 196 + get_id_range(get_random_u8_positive(), &counter, 197 + get_random_u8()), 198 + init + 3); 209 199 } 210 200 211 201 static void test_range2_rand2(struct kunit *const test) ··· 217 205 init = get_random_u32(); 218 206 atomic64_set(&counter, init); 219 207 KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 2), init); 220 - KUNIT_EXPECT_EQ( 221 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 222 - init + 4); 208 + KUNIT_EXPECT_EQ(test, 209 + get_id_range(get_random_u8_positive(), &counter, 210 + get_random_u8()), 211 + init + 4); 223 212 } 224 213 225 214 static void test_range2_rand15(struct kunit *const test) ··· 231 218 init = get_random_u32(); 232 219 atomic64_set(&counter, init); 233 220 KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 15), init); 234 - KUNIT_EXPECT_EQ( 235 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 236 - init + 17); 221 + KUNIT_EXPECT_EQ(test, 222 + get_id_range(get_random_u8_positive(), &counter, 223 + get_random_u8()), 224 + init + 17); 237 225 } 238 226 239 227 static void test_range2_rand16(struct kunit *const test) ··· 245 231 init = get_random_u32(); 246 232 atomic64_set(&counter, init); 247 233 KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 16), init); 248 - KUNIT_EXPECT_EQ( 249 - test, get_id_range(get_random_u8(), &counter, get_random_u8()), 250 - init + 2); 234 + KUNIT_EXPECT_EQ(test, 235 + get_id_range(get_random_u8_positive(), &counter, 236 + get_random_u8()), 237 + init + 2); 251 238 } 252 239 253 240 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */