Skip to content

Commit c842a4c

Browse files
jrfastabgregkh
authored andcommitted
bpf: sockmap, strparser, and tls are reusing qdisc_skb_cb and colliding
[ Upstream commit e0dc3b9 ] Strparser is reusing the qdisc_skb_cb struct to stash the skb message handling progress, e.g. offset and length of the skb. First this is poorly named and inherits a struct from qdisc that doesn't reflect the actual usage of cb[] at this layer. But, more importantly strparser is using the following to access its metadata. (struct _strp_msg *)((void *)skb->cb + offsetof(struct qdisc_skb_cb, data)) Where _strp_msg is defined as: struct _strp_msg { struct strp_msg strp; /* 0 8 */ int accum_len; /* 8 4 */ /* size: 12, cachelines: 1, members: 2 */ /* last cacheline: 12 bytes */ }; So we use 12 bytes of ->data[] in struct. However in BPF code running parser and verdict the user has read capabilities into the data[] array as well. Its not too problematic, but we should not be exposing internal state to BPF program. If its really needed then we can use the probe_read() APIs which allow reading kernel memory. And I don't believe cb[] layer poses any API breakage by moving this around because programs can't depend on cb[] across layers. In order to fix another issue with a ctx rewrite we need to stash a temp variable somewhere. To make this work cleanly this patch builds a cb struct for sk_skb types called sk_skb_cb struct. Then we can use this consistently in the strparser, sockmap space. Additionally we can start allowing ->cb[] write access after this. Fixes: 604326b ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Tested-by: Jussi Maki <joamaki@gmail.com> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Link: https://lore.kernel.org/bpf/20211103204736.248403-5-john.fastabend@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8b5c98a commit c842a4c

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎include/net/strparser.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,24 @@ struct strp_msg {
5454
int offset;
5555
};
5656

57+
struct _strp_msg {
58+
/* Internal cb structure. struct strp_msg must be first for passing
59+
* to upper layer.
60+
*/
61+
struct strp_msg strp;
62+
int accum_len;
63+
};
64+
65+
struct sk_skb_cb {
66+
#define SK_SKB_CB_PRIV_LEN 20
67+
unsigned char data[SK_SKB_CB_PRIV_LEN];
68+
struct _strp_msg strp;
69+
};
70+
5771
static inline struct strp_msg *strp_msg(struct sk_buff *skb)
5872
{
5973
return (struct strp_msg *)((void *)skb->cb +
60-
offsetof(struct qdisc_skb_cb, data));
74+
offsetof(struct sk_skb_cb, strp));
6175
}
6276

6377
/* Structure for an attached lower socket */

‎net/core/filter.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9493,6 +9493,27 @@ static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
94939493
*insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
94949494
si->src_reg, off);
94959495
break;
9496+
case offsetof(struct __sk_buff, cb[0]) ...
9497+
offsetofend(struct __sk_buff, cb[4]) - 1:
9498+
BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
9499+
BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9500+
offsetof(struct sk_skb_cb, data)) %
9501+
sizeof(__u64));
9502+
9503+
prog->cb_access = 1;
9504+
off = si->off;
9505+
off -= offsetof(struct __sk_buff, cb[0]);
9506+
off += offsetof(struct sk_buff, cb);
9507+
off += offsetof(struct sk_skb_cb, data);
9508+
if (type == BPF_WRITE)
9509+
*insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
9510+
si->src_reg, off);
9511+
else
9512+
*insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9513+
si->src_reg, off);
9514+
break;
9515+
9516+
94969517
default:
94979518
return bpf_convert_ctx_access(type, si, insn_buf, prog,
94989519
target_size);

‎net/strparser/strparser.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,10 @@
2727

2828
static struct workqueue_struct *strp_wq;
2929

30-
struct _strp_msg {
31-
/* Internal cb structure. struct strp_msg must be first for passing
32-
* to upper layer.
33-
*/
34-
struct strp_msg strp;
35-
int accum_len;
36-
};
37-
3830
static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
3931
{
4032
return (struct _strp_msg *)((void *)skb->cb +
41-
offsetof(struct qdisc_skb_cb, data));
33+
offsetof(struct sk_skb_cb, strp));
4234
}
4335

4436
/* Lower lock held */

0 commit comments

Comments
 (0)