Skip to content

Commit 14a3d3e

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Remove MEM_UNINIT from skb/xdp MTU helpers
We can now undo parts of 4b3786a ("bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error") as discussed in [0]. Given the BPF helpers now have MEM_WRITE tag, the MEM_UNINIT can be cleared. The mtu_len is an input as well as output argument, meaning, the BPF program has to set it to something. It cannot be uninitialized. Therefore, allowing uninitialized memory and zeroing it on error would be odd. It was done as an interim step in 4b3786a as the desired behavior could not have been expressed before the introduction of MEM_WRITE tag. Fixes: 4b3786a ("bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/a86eb76d-f52f-dee4-e5d2-87e45de3e16f@iogearbox.net [0] Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20241021152809.33343-3-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 8ea6073 commit 14a3d3e

1 file changed

Lines changed: 15 additions & 27 deletions

File tree

net/core/filter.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6281,24 +6281,16 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
62816281
{
62826282
int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
62836283
struct net_device *dev = skb->dev;
6284-
int skb_len, dev_len;
6285-
int mtu = 0;
6284+
int mtu, dev_len, skb_len;
62866285

6287-
if (unlikely(flags & ~(BPF_MTU_CHK_SEGS))) {
6288-
ret = -EINVAL;
6289-
goto out;
6290-
}
6291-
6292-
if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) {
6293-
ret = -EINVAL;
6294-
goto out;
6295-
}
6286+
if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6287+
return -EINVAL;
6288+
if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6289+
return -EINVAL;
62966290

62976291
dev = __dev_via_ifindex(dev, ifindex);
6298-
if (unlikely(!dev)) {
6299-
ret = -ENODEV;
6300-
goto out;
6301-
}
6292+
if (unlikely(!dev))
6293+
return -ENODEV;
63026294

63036295
mtu = READ_ONCE(dev->mtu);
63046296
dev_len = mtu + dev->hard_header_len;
@@ -6333,19 +6325,15 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
63336325
struct net_device *dev = xdp->rxq->dev;
63346326
int xdp_len = xdp->data_end - xdp->data;
63356327
int ret = BPF_MTU_CHK_RET_SUCCESS;
6336-
int mtu = 0, dev_len;
6328+
int mtu, dev_len;
63376329

63386330
/* XDP variant doesn't support multi-buffer segment check (yet) */
6339-
if (unlikely(flags)) {
6340-
ret = -EINVAL;
6341-
goto out;
6342-
}
6331+
if (unlikely(flags))
6332+
return -EINVAL;
63436333

63446334
dev = __dev_via_ifindex(dev, ifindex);
6345-
if (unlikely(!dev)) {
6346-
ret = -ENODEV;
6347-
goto out;
6348-
}
6335+
if (unlikely(!dev))
6336+
return -ENODEV;
63496337

63506338
mtu = READ_ONCE(dev->mtu);
63516339
dev_len = mtu + dev->hard_header_len;
@@ -6357,7 +6345,7 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
63576345
xdp_len += len_diff; /* minus result pass check */
63586346
if (xdp_len > dev_len)
63596347
ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6360-
out:
6348+
63616349
*mtu_len = mtu;
63626350
return ret;
63636351
}
@@ -6368,7 +6356,7 @@ static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
63686356
.ret_type = RET_INTEGER,
63696357
.arg1_type = ARG_PTR_TO_CTX,
63706358
.arg2_type = ARG_ANYTHING,
6371-
.arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
6359+
.arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
63726360
.arg3_size = sizeof(u32),
63736361
.arg4_type = ARG_ANYTHING,
63746362
.arg5_type = ARG_ANYTHING,
@@ -6380,7 +6368,7 @@ static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
63806368
.ret_type = RET_INTEGER,
63816369
.arg1_type = ARG_PTR_TO_CTX,
63826370
.arg2_type = ARG_ANYTHING,
6383-
.arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
6371+
.arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
63846372
.arg3_size = sizeof(u32),
63856373
.arg4_type = ARG_ANYTHING,
63866374
.arg5_type = ARG_ANYTHING,

0 commit comments

Comments
 (0)