Skip to content

Commit 4b3786a

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
For all non-tracing helpers which formerly had ARG_PTR_TO_{LONG,INT} as input arguments, zero the value for the case of an error as otherwise it could leak memory. For tracing, it is not needed given CAP_PERFMON can already read all kernel memory anyway hence bpf_get_func_arg() and bpf_get_func_ret() is skipped in here. Also, the MTU helpers mtu_len pointer value is being written but also read. Technically, the MEM_UNINIT should not be there in order to always force init. Removing MEM_UNINIT needs more verifier rework though: MEM_UNINIT right now implies two things actually: i) write into memory, ii) memory does not have to be initialized. If we lift MEM_UNINIT, it then becomes: i) read into memory, ii) memory must be initialized. This means that for bpf_*_check_mtu() we're readding the issue we're trying to fix, that is, it would then be able to write back into things like .rodata BPF maps. Follow-up work will rework the MEM_UNINIT semantics such that the intent can be better expressed. For now just clear the *mtu_len on error path which can be lifted later again. Fixes: 8a67f2d ("bpf: expose bpf_strtol and bpf_strtoul to all program types") Fixes: d7a4cb9 ("bpf: Introduce bpf_strtol and bpf_strtoul helpers") Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 18752d7 commit 4b3786a

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

kernel/bpf/helpers.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
523523
long long _res;
524524
int err;
525525

526+
*res = 0;
526527
err = __bpf_strtoll(buf, buf_len, flags, &_res);
527528
if (err < 0)
528529
return err;
@@ -548,6 +549,7 @@ BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
548549
bool is_negative;
549550
int err;
550551

552+
*res = 0;
551553
err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
552554
if (err < 0)
553555
return err;

kernel/bpf/syscall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5934,6 +5934,7 @@ static const struct bpf_func_proto bpf_sys_close_proto = {
59345934

59355935
BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
59365936
{
5937+
*res = 0;
59375938
if (flags)
59385939
return -EINVAL;
59395940

net/core/filter.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6262,20 +6262,25 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
62626262
int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
62636263
struct net_device *dev = skb->dev;
62646264
int skb_len, dev_len;
6265-
int mtu;
6265+
int mtu = 0;
62666266

6267-
if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6268-
return -EINVAL;
6267+
if (unlikely(flags & ~(BPF_MTU_CHK_SEGS))) {
6268+
ret = -EINVAL;
6269+
goto out;
6270+
}
62696271

6270-
if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6271-
return -EINVAL;
6272+
if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len))) {
6273+
ret = -EINVAL;
6274+
goto out;
6275+
}
62726276

62736277
dev = __dev_via_ifindex(dev, ifindex);
6274-
if (unlikely(!dev))
6275-
return -ENODEV;
6278+
if (unlikely(!dev)) {
6279+
ret = -ENODEV;
6280+
goto out;
6281+
}
62766282

62776283
mtu = READ_ONCE(dev->mtu);
6278-
62796284
dev_len = mtu + dev->hard_header_len;
62806285

62816286
/* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
@@ -6293,15 +6298,12 @@ BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
62936298
*/
62946299
if (skb_is_gso(skb)) {
62956300
ret = BPF_MTU_CHK_RET_SUCCESS;
6296-
62976301
if (flags & BPF_MTU_CHK_SEGS &&
62986302
!skb_gso_validate_network_len(skb, mtu))
62996303
ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
63006304
}
63016305
out:
6302-
/* BPF verifier guarantees valid pointer */
63036306
*mtu_len = mtu;
6304-
63056307
return ret;
63066308
}
63076309

@@ -6311,19 +6313,21 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
63116313
struct net_device *dev = xdp->rxq->dev;
63126314
int xdp_len = xdp->data_end - xdp->data;
63136315
int ret = BPF_MTU_CHK_RET_SUCCESS;
6314-
int mtu, dev_len;
6316+
int mtu = 0, dev_len;
63156317

63166318
/* XDP variant doesn't support multi-buffer segment check (yet) */
6317-
if (unlikely(flags))
6318-
return -EINVAL;
6319+
if (unlikely(flags)) {
6320+
ret = -EINVAL;
6321+
goto out;
6322+
}
63196323

63206324
dev = __dev_via_ifindex(dev, ifindex);
6321-
if (unlikely(!dev))
6322-
return -ENODEV;
6325+
if (unlikely(!dev)) {
6326+
ret = -ENODEV;
6327+
goto out;
6328+
}
63236329

63246330
mtu = READ_ONCE(dev->mtu);
6325-
6326-
/* Add L2-header as dev MTU is L3 size */
63276331
dev_len = mtu + dev->hard_header_len;
63286332

63296333
/* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
@@ -6333,10 +6337,8 @@ BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
63336337
xdp_len += len_diff; /* minus result pass check */
63346338
if (xdp_len > dev_len)
63356339
ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6336-
6337-
/* BPF verifier guarantees valid pointer */
6340+
out:
63386341
*mtu_len = mtu;
6339-
63406342
return ret;
63416343
}
63426344

0 commit comments

Comments
 (0)