Skip to content

Commit 1fbc2e0

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: make sure to clear unused fields in tunnel/xfrm state fetch
Since the remaining bits are not filled in struct bpf_tunnel_key resp. struct bpf_xfrm_state and originate from uninitialized stack space, we should make sure to clear them before handing control back to the program. Also add a padding element to struct bpf_xfrm_state for future use similar as we have in struct bpf_tunnel_key and clear it as well. struct bpf_xfrm_state { __u32 reqid; /* 0 4 */ __u32 spi; /* 4 4 */ __u16 family; /* 8 2 */ /* XXX 2 bytes hole, try to pack */ union { __u32 remote_ipv4; /* 4 */ __u32 remote_ipv6[4]; /* 16 */ }; /* 12 16 */ /* size: 28, cachelines: 1, members: 4 */ /* sum members: 26, holes: 1, sum holes: 2 */ /* last cacheline: 28 bytes */ }; Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent cb20b08 commit 1fbc2e0

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ struct bpf_tunnel_key {
22682268
};
22692269
__u8 tunnel_tos;
22702270
__u8 tunnel_ttl;
2271-
__u16 tunnel_ext;
2271+
__u16 tunnel_ext; /* Padding, future use. */
22722272
__u32 tunnel_label;
22732273
};
22742274

@@ -2279,6 +2279,7 @@ struct bpf_xfrm_state {
22792279
__u32 reqid;
22802280
__u32 spi; /* Stored in network byte order */
22812281
__u16 family;
2282+
__u16 ext; /* Padding, future use. */
22822283
union {
22832284
__u32 remote_ipv4; /* Stored in network byte order */
22842285
__u32 remote_ipv6[4]; /* Stored in network byte order */

‎net/core/filter.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,13 +3445,16 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
34453445
to->tunnel_id = be64_to_cpu(info->key.tun_id);
34463446
to->tunnel_tos = info->key.tos;
34473447
to->tunnel_ttl = info->key.ttl;
3448+
to->tunnel_ext = 0;
34483449

34493450
if (flags & BPF_F_TUNINFO_IPV6) {
34503451
memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
34513452
sizeof(to->remote_ipv6));
34523453
to->tunnel_label = be32_to_cpu(info->key.label);
34533454
} else {
34543455
to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3456+
memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3457+
to->tunnel_label = 0;
34553458
}
34563459

34573460
if (unlikely(size != sizeof(struct bpf_tunnel_key)))
@@ -4047,11 +4050,14 @@ BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
40474050
to->reqid = x->props.reqid;
40484051
to->spi = x->id.spi;
40494052
to->family = x->props.family;
4053+
to->ext = 0;
4054+
40504055
if (to->family == AF_INET6) {
40514056
memcpy(to->remote_ipv6, x->props.saddr.a6,
40524057
sizeof(to->remote_ipv6));
40534058
} else {
40544059
to->remote_ipv4 = x->props.saddr.a4;
4060+
memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
40554061
}
40564062

40574063
return 0;

0 commit comments

Comments
 (0)