Skip to contentSkip to conversation

net: bpf: increase the chance that the semi-random bpf is run

PATCH New Daniel Borkmann 1 participant 1 message May 16, 2013 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: bpf: increase the chance that the semi-random bpf is run

The kernel filter code expects us to have a return instruction and that won't be changed in the kernel code. So increase the chance that we can also get past the filter precheck and run this fuzzed filter code.

---
Just a minor addon to the previous patch.

 net/bpf.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/bpf.c +12 -2
index c628ac0..88f4697 100644
@@ -110,10 +110,20 @@ static const uint16_t bpf_misc_vars[] = {
 #define bpf_rand(type) \
 	(bpf_##type##_vars[rand() % ARRAY_SIZE(bpf_##type##_vars)])
 
-static uint16_t gen_bpf_code(void)
+static uint16_t gen_bpf_code(bool last_instr)
 {
 	uint16_t ret = bpf_rand(class);
 
+	if (last_instr) {
+		/* The kernel filter precheck code already tests if
+		 * there's a return instruction as the last one, so
+		 * increase the chance to be accepted and that we
+		 * actually run the generated fuzz filter code.
+		 */
+		if (rand() % 2 == 0)
+			ret = BPF_RET;
+	}
+
 	switch (ret) {
 	case BPF_LD:
 	case BPF_LDX:
@@ -168,7 +178,7 @@ void gen_bpf(unsigned long *addr, unsigned long *addrlen)
 	for (i = 0; i < bpf->len; i++) {
 		memset(&bpf->filter[i], 0, sizeof(bpf->filter[i]));
 
-		bpf->filter[i].code = gen_bpf_code();
+		bpf->filter[i].code = gen_bpf_code(i == bpf->len - 1);
 
 		/* Fill out jump offsets if jmp instruction */
 		if (BPF_CLASS(bpf->filter[i].code) == BPF_JMP) {
Signature · 1 line
1.7.11.7