Skip to content

Commit ebafc1e

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
selftests/bpf: Add three test cases for bits_iter
Add more test cases for bits iterator: (1) huge word test Verify the multiplication overflow of nr_bits in bits_iter. Without the overflow check, when nr_words is 67108865, nr_bits becomes 64, causing bpf_probe_read_kernel_common() to corrupt the stack. (2) max word test Verify correct handling of maximum nr_words value (511). (3) bad word test Verify early termination of bits iteration when bits iterator initialization fails. Also rename bits_nomem to bits_too_big to better reflect its purpose. Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e133938 commit ebafc1e

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

tools/testing/selftests/bpf/progs/verifier_bits_iter.c

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign,
1515
int *bpf_iter_bits_next(struct bpf_iter_bits *it) __ksym __weak;
1616
void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym __weak;
1717

18+
u64 bits_array[511] = {};
19+
1820
SEC("iter.s/cgroup")
1921
__description("bits iter without destroy")
2022
__failure __msg("Unreleased reference")
@@ -110,16 +112,16 @@ int bit_index(void)
110112
}
111113

112114
SEC("syscall")
113-
__description("bits nomem")
115+
__description("bits too big")
114116
__success __retval(0)
115-
int bits_nomem(void)
117+
int bits_too_big(void)
116118
{
117119
u64 data[4];
118120
int nr = 0;
119121
int *bit;
120122

121123
__builtin_memset(&data, 0xff, sizeof(data));
122-
bpf_for_each(bits, bit, &data[0], 513) /* Be greater than 512 */
124+
bpf_for_each(bits, bit, &data[0], 512) /* Be greater than 511 */
123125
nr++;
124126
return nr;
125127
}
@@ -151,3 +153,56 @@ int zero_words(void)
151153
nr++;
152154
return nr;
153155
}
156+
157+
SEC("syscall")
158+
__description("huge words")
159+
__success __retval(0)
160+
int huge_words(void)
161+
{
162+
u64 data[8] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1};
163+
int nr = 0;
164+
int *bit;
165+
166+
bpf_for_each(bits, bit, &data[0], 67108865)
167+
nr++;
168+
return nr;
169+
}
170+
171+
SEC("syscall")
172+
__description("max words")
173+
__success __retval(4)
174+
int max_words(void)
175+
{
176+
volatile int nr = 0;
177+
int *bit;
178+
179+
bits_array[0] = (1ULL << 63) | 1U;
180+
bits_array[510] = (1ULL << 33) | (1ULL << 32);
181+
182+
bpf_for_each(bits, bit, bits_array, 511) {
183+
if (nr == 0 && *bit != 0)
184+
break;
185+
if (nr == 2 && *bit != 32672)
186+
break;
187+
nr++;
188+
}
189+
return nr;
190+
}
191+
192+
SEC("syscall")
193+
__description("bad words")
194+
__success __retval(0)
195+
int bad_words(void)
196+
{
197+
void *bad_addr = (void *)(3UL << 30);
198+
int nr = 0;
199+
int *bit;
200+
201+
bpf_for_each(bits, bit, bad_addr, 1)
202+
nr++;
203+
204+
bpf_for_each(bits, bit, bad_addr, 4)
205+
nr++;
206+
207+
return nr;
208+
}

0 commit comments

Comments
 (0)