net/sched: cls_u32: validate offshift to prevent shift-out-of-bounds
Subsystems (3)
PATCH 1/1 net/sched: cls_u32: validate offshift to prevent shift-out-of-bounds
u32_change() copies the user-provided tc_u32_sel.offshift (unsigned char, 0-255) into the kernel knode object without bounds validation. When a packet later hits u32_classify() with TC_U32_VAROFFSET set, it evaluates ntohs(offmask & *data) >> offshift where the left operand is a 16-bit value promoted to a 32-bit int. Any offshift >= 32 is undefined behavior per C11 §6.5.7, triggerable by an unprivileged user via user/network namespaces.
UBSAN: shift-out-of-bounds in net/sched/cls_u32.c:236:43
shift exponent 32 is too large for 32-bit type int
Fix this by rejecting offshift >= 16 during filter creation in u32_change().
--- net/sched/cls_u32.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/sched/cls_u32.c +7 -0
@@ -1107,6 +1107,13 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, goto erridr; } +if (s->flags &TC_U32_VAROFFSET && s->offshift >=16 ) { +NL_SET_ERR_MSG_MOD (extack, +"offshift must be less than 16" ); + err = -EINVAL ; + goto erridr; + } + n =kzalloc_flex (*n, sel.keys, s->nkeys);if (n ==NULL ) { err = -ENOBUFS ;
Signature · 1 line
2.53.0
Jamal Hadi Salim
maintainerQuoted 27 lines · Cen Zhang (Microsoft)
u32_change()copies the user-provided tc_u32_sel.offshift (unsigned char, 0-255) into the kernel knode object without bounds validation. When a packet later hitsu32_classify()withTC_U32_VAROFFSETset, it evaluatesntohs(offmask & *data) >> offshiftwhere the left operand is a 16-bit value promoted to a 32-bit int. Any offshift >= 32 is undefined behavior per C11 §6.5.7, triggerable by an unprivileged user via user/network namespaces.UBSAN: shift-out-of-bounds in net/sched/cls_u32.c:236:43shift exponent 32 is too large for 32-bit type int
Fix this by rejecting offshift >= 16 during filter creation in
u32_change().Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by [email protected]Signed-off-by Cen Zhang (Microsoft) <[email protected]>--- net/sched/cls_u32.c | 7 +++++++ 1 file changed, 7 insertions(+)net/sched/cls_u32.c +0
-0
net/sched/cls_u32.c static int u32_change(struct net *net, struct sk_buff *in_skb, · line 1110
Quoted 4 lines
net/sched/cls_u32.c +1
-0@@ -1107,6 +1107,13 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, goto erridr; } +if (s->flags &TC_U32_VAROFFSET && s->offshift >=16 ) {
Remove this VAROFFSET flag test.
It is possible that someone could make a call from user space without this flag and use skip_sw and it ends up in a driver which doesnt check for this boundary.
Otherwise both Victor and myself tested with POC and were able to verify the bug and the fix.
Please add Victor's tested by and my acked-by when you resubmit.
cheers,
jamal
Quoted 12 lines
net/sched/cls_u32.c +6
-1+NL_SET_ERR_MSG_MOD (extack, +"offshift must be less than 16" ); + err = -EINVAL ; + goto erridr; + } + n =kzalloc_flex (*n, sel.keys, s->nkeys);if (n ==NULL ) { err = -ENOBUFS ; --2.53.0