Skip to content

Commit e0a69cf

Browse files
micromaomaol0kod
authored andcommitted
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: d9d2a68 ("landlock: Add unique ID generator") Signed-off-by: Tingmao Wang <[email protected]> Link: https://lore.kernel.org/r/73e28efc5b8cc394608b99d5bc2596ca917d7c4a.1750003733.git.m@maowtm.org [mic: Minor cosmetic improvements] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent dae0138 commit e0a69cf

File tree

1 file changed

+42
-27
lines changed
  • security/landlock

1 file changed

+42
-27
lines changed

security/landlock/id.c

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,
119119

120120
#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
121121

122+
static u8 get_random_u8_positive(void)
123+
{
124+
/* max() evaluates its arguments once. */
125+
return max(1, get_random_u8());
126+
}
127+
122128
static void test_range1_rand0(struct kunit *const test)
123129
{
124130
atomic64_t counter;
@@ -127,9 +133,10 @@ static void test_range1_rand0(struct kunit *const test)
127133
init = get_random_u32();
128134
atomic64_set(&counter, init);
129135
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 0), init);
130-
KUNIT_EXPECT_EQ(
131-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
132-
init + 1);
136+
KUNIT_EXPECT_EQ(test,
137+
get_id_range(get_random_u8_positive(), &counter,
138+
get_random_u8()),
139+
init + 1);
133140
}
134141

135142
static void test_range1_rand1(struct kunit *const test)
@@ -140,9 +147,10 @@ static void test_range1_rand1(struct kunit *const test)
140147
init = get_random_u32();
141148
atomic64_set(&counter, init);
142149
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 1), init);
143-
KUNIT_EXPECT_EQ(
144-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
145-
init + 2);
150+
KUNIT_EXPECT_EQ(test,
151+
get_id_range(get_random_u8_positive(), &counter,
152+
get_random_u8()),
153+
init + 2);
146154
}
147155

148156
static void test_range1_rand15(struct kunit *const test)
@@ -153,9 +161,10 @@ static void test_range1_rand15(struct kunit *const test)
153161
init = get_random_u32();
154162
atomic64_set(&counter, init);
155163
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 15), init);
156-
KUNIT_EXPECT_EQ(
157-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
158-
init + 16);
164+
KUNIT_EXPECT_EQ(test,
165+
get_id_range(get_random_u8_positive(), &counter,
166+
get_random_u8()),
167+
init + 16);
159168
}
160169

161170
static void test_range1_rand16(struct kunit *const test)
@@ -166,9 +175,10 @@ static void test_range1_rand16(struct kunit *const test)
166175
init = get_random_u32();
167176
atomic64_set(&counter, init);
168177
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 16), init);
169-
KUNIT_EXPECT_EQ(
170-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
171-
init + 1);
178+
KUNIT_EXPECT_EQ(test,
179+
get_id_range(get_random_u8_positive(), &counter,
180+
get_random_u8()),
181+
init + 1);
172182
}
173183

174184
static void test_range2_rand0(struct kunit *const test)
@@ -179,9 +189,10 @@ static void test_range2_rand0(struct kunit *const test)
179189
init = get_random_u32();
180190
atomic64_set(&counter, init);
181191
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 0), init);
182-
KUNIT_EXPECT_EQ(
183-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
184-
init + 2);
192+
KUNIT_EXPECT_EQ(test,
193+
get_id_range(get_random_u8_positive(), &counter,
194+
get_random_u8()),
195+
init + 2);
185196
}
186197

187198
static void test_range2_rand1(struct kunit *const test)
@@ -192,9 +203,10 @@ static void test_range2_rand1(struct kunit *const test)
192203
init = get_random_u32();
193204
atomic64_set(&counter, init);
194205
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 1), init);
195-
KUNIT_EXPECT_EQ(
196-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
197-
init + 3);
206+
KUNIT_EXPECT_EQ(test,
207+
get_id_range(get_random_u8_positive(), &counter,
208+
get_random_u8()),
209+
init + 3);
198210
}
199211

200212
static void test_range2_rand2(struct kunit *const test)
@@ -205,9 +217,10 @@ static void test_range2_rand2(struct kunit *const test)
205217
init = get_random_u32();
206218
atomic64_set(&counter, init);
207219
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 2), init);
208-
KUNIT_EXPECT_EQ(
209-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
210-
init + 4);
220+
KUNIT_EXPECT_EQ(test,
221+
get_id_range(get_random_u8_positive(), &counter,
222+
get_random_u8()),
223+
init + 4);
211224
}
212225

213226
static void test_range2_rand15(struct kunit *const test)
@@ -218,9 +231,10 @@ static void test_range2_rand15(struct kunit *const test)
218231
init = get_random_u32();
219232
atomic64_set(&counter, init);
220233
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 15), init);
221-
KUNIT_EXPECT_EQ(
222-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
223-
init + 17);
234+
KUNIT_EXPECT_EQ(test,
235+
get_id_range(get_random_u8_positive(), &counter,
236+
get_random_u8()),
237+
init + 17);
224238
}
225239

226240
static void test_range2_rand16(struct kunit *const test)
@@ -231,9 +245,10 @@ static void test_range2_rand16(struct kunit *const test)
231245
init = get_random_u32();
232246
atomic64_set(&counter, init);
233247
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 16), init);
234-
KUNIT_EXPECT_EQ(
235-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
236-
init + 2);
248+
KUNIT_EXPECT_EQ(test,
249+
get_id_range(get_random_u8_positive(), &counter,
250+
get_random_u8()),
251+
init + 2);
237252
}
238253

239254
#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */

0 commit comments

Comments
 (0)