Skip to contentSkip to conversation

bpf: Fix NULL pointer dereference in bpf_skb_fib_lookup()

PATCH Superseded Weiming Shi 3 participants 3 messages Apr 19 – Apr 20

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Fix NULL pointer dereference in bpf_skb_fib_lookup()

When tot_len is not provided by the user, bpf_skb_fib_lookup() resolves the FIB result's output device via dev_get_by_index_rcu() to check skb forwardability and fill in mtu_result. The returned pointer is dereferenced without a NULL check. If the device is concurrently unregistered, dev_get_by_index_rcu() returns NULL and is_skb_forwardable() crashes at dev->flags:

 KASAN: null-ptr-deref in range
  [0x00000000000000b0-0x00000000000000b7]
 Call Trace:
  is_skb_forwardable (include/linux/netdevice.h:4365)
  bpf_skb_fib_lookup (net/core/filter.c:6446)
  bpf_prog_test_run_skb (net/bpf/test_run.c)
  __sys_bpf (kernel/bpf/syscall.c)

Add the missing NULL check, returning -ENODEV to be consistent with how bpf_ipv4_fib_lookup() and bpf_ipv6_fib_lookup() handle the same condition.

Fixes e1850ea9bd9e ("bpf: bpf_fib_lookup return MTU value as output when looked up")
---
 net/core/filter.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/filter.c +2 -0
index 78b548158fb0..3e56b567bd18 100644
@@ -6450,6 +6450,8 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
 		 * against MTU of FIB lookup resulting net_device
 		 */
 		dev = dev_get_by_index_rcu(net, params->ifindex);
+		if (!dev)
+			return -ENODEV;
 		if (!is_skb_forwardable(dev, skb))
 			rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
 
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 31 lines · Weiming Shi

When tot_len is not provided by the user, bpf_skb_fib_lookup() resolves the FIB result's output device via dev_get_by_index_rcu() to check skb forwardability and fill in mtu_result. The returned pointer is dereferenced without a NULL check. If the device is concurrently unregistered, dev_get_by_index_rcu() returns NULL and is_skb_forwardable() crashes at dev->flags:

 KASAN: null-ptr-deref in range
  [0x00000000000000b0-0x00000000000000b7]
 Call Trace:
  is_skb_forwardable (include/linux/netdevice.h:4365)
  bpf_skb_fib_lookup (net/core/filter.c:6446)
  bpf_prog_test_run_skb (net/bpf/test_run.c)
  __sys_bpf (kernel/bpf/syscall.c)

Add the missing NULL check, returning -ENODEV to be consistent with how bpf_ipv4_fib_lookup() and bpf_ipv6_fib_lookup() handle the same condition.

Fixes e1850ea9bd9e ("bpf: bpf_fib_lookup return MTU value as output when looked up")
---
 net/core/filter.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/filter.c +0 -0
index 78b548158fb0..3e56b567bd18 100644

net/core/filter.c BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, · line 6454

Quoted 5 lines
net/core/filter.c +2 -0
@@ -6450,6 +6450,8 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
 		 * against MTU of FIB lookup resulting net_device
 		 */
 		dev = dev_get_by_index_rcu(net, params->ifindex);
+		if (!dev)
+			return -ENODEV;

The bug and its fix make sense to me. Given the race, it looks difficult to write a selftest for this. The condition might be worth an unlikely() as done in bpf_ipv{4,6}_fib_lookup() above.

Quoted 7 lines
net/core/filter.c +0 -0
 		if (!is_skb_forwardable(dev, skb))
 			rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
 
Signature · 1 line
2.43.0
#2 lore raw
In reply to Weiming Shi
Quoted 15 lines · Weiming Shi

When tot_len is not provided by the user, bpf_skb_fib_lookup() resolves the FIB result's output device via dev_get_by_index_rcu() to check skb forwardability and fill in mtu_result. The returned pointer is dereferenced without a NULL check. If the device is concurrently unregistered, dev_get_by_index_rcu() returns NULL and is_skb_forwardable() crashes at dev->flags:

  KASAN: null-ptr-deref in range
   [0x00000000000000b0-0x00000000000000b7]
  Call Trace:
   is_skb_forwardable (include/linux/netdevice.h:4365)
   bpf_skb_fib_lookup (net/core/filter.c:6446)
   bpf_prog_test_run_skb (net/bpf/test_run.c)
   __sys_bpf (kernel/bpf/syscall.c)
Quoted 5 lines

Add the missing NULL check, returning -ENODEV to be consistent with how bpf_ipv4_fib_lookup() and bpf_ipv6_fib_lookup() handle the same condition.

Fixes e1850ea9bd9e ("bpf: bpf_fib_lookup return MTU value as output when looked up")

Is it correct to blame  this commit?

I find such code block 'if (!is_skb_forwardable(dev, skb))' was introduced by 4f74fede40df

Quoted 19 lines
---
  net/core/filter.c | 2 ++
  1 file changed, 2 insertions(+)
net/core/filter.c +2 -0
index 78b548158fb0..3e56b567bd18 100644
@@ -6450,6 +6450,8 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
  		 * against MTU of FIB lookup resulting net_device
  		 */
  		dev = dev_get_by_index_rcu(net, params->ifindex);
+		if (!dev)
+			return -ENODEV;
  		if (!is_skb_forwardable(dev, skb))
  			rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;