Skip to content

Commit 5edeb09

Browse files
jsitnickiKernel Patches Daemon
authored andcommitted
bpf: stub out skb metadata dynptr read/write ops when CONFIG_NET=n
Kernel Test Robot reported a compiler warning - a null pointer may be passed to memmove in __bpf_dynptr_{read,write} when building without networking support. The warning is correct from a static analysis standpoint, but not actually reachable. Without CONFIG_NET, creating dynptrs to skb metadata is impossible since the constructor kfunc is missing. Fix this the same way as for skb and xdp data dynptrs. Add wrappers for loading and storing bytes to skb metadata, and stub them out to return an error when CONFIG_NET=n. Fixes: 6877cd3 ("bpf: Enable read/write access to skb metadata through a dynptr") Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent c640618 commit 5edeb09

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

include/linux/filter.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,20 @@ void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
17791779
void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
17801780
void *buf, unsigned long len, bool flush);
17811781
void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset);
1782+
1783+
static inline int __bpf_skb_meta_load_bytes(struct sk_buff *skb,
1784+
u32 offset, void *to, u32 len)
1785+
{
1786+
memmove(to, bpf_skb_meta_pointer(skb, offset), len);
1787+
return 0;
1788+
}
1789+
1790+
static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
1791+
const void *from, u32 len)
1792+
{
1793+
memmove(bpf_skb_meta_pointer(skb, offset), from, len);
1794+
return 0;
1795+
}
17821796
#else /* CONFIG_NET */
17831797
static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
17841798
void *to, u32 len)
@@ -1818,6 +1832,18 @@ static inline void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
18181832
{
18191833
return NULL;
18201834
}
1835+
1836+
static inline int __bpf_skb_meta_load_bytes(struct sk_buff *skb, u32 offset,
1837+
void *to, u32 len)
1838+
{
1839+
return -EOPNOTSUPP;
1840+
}
1841+
1842+
static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
1843+
const void *from, u32 len)
1844+
{
1845+
return -EOPNOTSUPP;
1846+
}
18211847
#endif /* CONFIG_NET */
18221848

18231849
#endif /* __LINUX_FILTER_H__ */

kernel/bpf/helpers.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,8 +1778,7 @@ static int __bpf_dynptr_read(void *dst, u32 len, const struct bpf_dynptr_kern *s
17781778
case BPF_DYNPTR_TYPE_XDP:
17791779
return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len);
17801780
case BPF_DYNPTR_TYPE_SKB_META:
1781-
memmove(dst, bpf_skb_meta_pointer(src->data, src->offset + offset), len);
1782-
return 0;
1781+
return __bpf_skb_meta_load_bytes(src->data, src->offset + offset, dst, len);
17831782
default:
17841783
WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
17851784
return -EFAULT;
@@ -1839,8 +1838,7 @@ int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u32 offset, void *src,
18391838
case BPF_DYNPTR_TYPE_SKB_META:
18401839
if (flags)
18411840
return -EINVAL;
1842-
memmove(bpf_skb_meta_pointer(dst->data, dst->offset + offset), src, len);
1843-
return 0;
1841+
return __bpf_skb_meta_store_bytes(dst->data, dst->offset + offset, src, len);
18441842
default:
18451843
WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
18461844
return -EFAULT;

0 commit comments

Comments
 (0)