net: prevent user from passing illegal stab size
Subsystems (3)
PATCH 1/1 net: prevent user from passing illegal stab size
We observed below report when playing with netlink sock:
UBSAN: shift-out-of-bounds in net/sched/sch_api.c:580:10 shift exponent 249 is too large for 32-bit type CPU: 0 PID: 685 Comm: a.out Not tainted Call Trace: dump_stack_lvl+0x8d/0xcf ubsan_epilogue+0xa/0x4e __ubsan_handle_shift_out_of_bounds+0x161/0x182 __qdisc_calculate_pkt_len+0xf0/0x190 __dev_queue_xmit+0x2ed/0x15b0
it seems like kernel won't check the stab size_log passing from user, and will use the insane value later to calculate pkt_len.
This patch just add a check on the size_log to avoid insane calculation.
--- include/uapi/linux/pkt_sched.h | 1 + net/sched/sch_api.c | 3 +++ 2 files changed, 4 insertions(+)
include/uapi/linux/pkt_sched.h +1 -0
@@ -98,6 +98,7 @@ struct tc_ratespec { };#define TC_RTAB_SIZE 1024 +#define TC_LOG_MAX 30struct tc_sizespec {unsigned char cell_log;
net/sched/sch_api.c +3 -0
@@ -513,6 +513,9 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,return stab; } +if (s->size_log >TC_LOG_MAX ) +return ERR_PTR (-EINVAL ); + stab =kmalloc (sizeof (*stab) + tsize *sizeof (u16),GFP_KERNEL );if (!stab)return ERR_PTR (-ENOMEM );
Signature · 1 line
1.8.3.1
Jakub Kicinski
maintainerQuoted 30 lines · On Thu, 23 Sep 2021 17:08:13 +0800 王贇 wrote:
We observed below report when playing with netlink sock:
UBSAN: shift-out-of-bounds in net/sched/sch_api.c:580:10 shift exponent 249 is too large for 32-bit type CPU: 0 PID: 685 Comm: a.out Not tainted Call Trace: dump_stack_lvl+0x8d/0xcf ubsan_epilogue+0xa/0x4e __ubsan_handle_shift_out_of_bounds+0x161/0x182 __qdisc_calculate_pkt_len+0xf0/0x190 __dev_queue_xmit+0x2ed/0x15b0it seems like kernel won't check the stab
size_logpassing from user, and will use the insane value later to calculatepkt_len.This patch just add a check on the
size_logto avoid insane calculation.Reported-by Abaci <[email protected]>Signed-off-by Michael Wang <[email protected]>--- include/uapi/linux/pkt_sched.h | 1 + net/sched/sch_api.c | 3 +++ 2 files changed, 4 insertions(+)include/uapi/linux/pkt_sched.h +0
-0
include/uapi/linux/pkt_sched.h struct tc_ratespec { · line 101
Quoted 4 lines
include/uapi/linux/pkt_sched.h +1
-0@@ -98,6 +98,7 @@ struct tc_ratespec { };#define TC_RTAB_SIZE 1024 +#define TC_LOG_MAX 30
Adding a uAPI define is not necessary.
net/sched/sch_api.c static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt, · line 517
Quoted 12 lines
include/uapi/linux/pkt_sched.h +0
-0struct tc_sizespec {unsigned char cell_log;net/sched/sch_api.c +2
-0@@ -513,6 +513,9 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,return stab; } +if (s->size_log >TC_LOG_MAX ) +return ERR_PTR (-EINVAL );
Looks sane, please add an extack message.
Why not cover cell_log as well while at it?
Quoted 3 lines
net/sched/sch_api.c +0
-0stab =kmalloc (sizeof (*stab) + tsize *sizeof (u16),GFP_KERNEL );if (!stab)return ERR_PTR (-ENOMEM );
Quoted 33 lines · Jakub Kicinski
Quoted 32 lines · On Thu, 23 Sep 2021 17:08:13 +0800 王贇 wrote:
We observed below report when playing with netlink sock:
UBSAN: shift-out-of-bounds in net/sched/sch_api.c:580:10 shift exponent 249 is too large for 32-bit type CPU: 0 PID: 685 Comm: a.out Not tainted Call Trace: dump_stack_lvl+0x8d/0xcf ubsan_epilogue+0xa/0x4e __ubsan_handle_shift_out_of_bounds+0x161/0x182 __qdisc_calculate_pkt_len+0xf0/0x190 __dev_queue_xmit+0x2ed/0x15b0it seems like kernel won't check the stab
size_logpassing from user, and will use the insane value later to calculatepkt_len.This patch just add a check on the
size_logto avoid insane calculation.Reported-by Abaci <[email protected]>Signed-off-by Michael Wang <[email protected]>--- include/uapi/linux/pkt_sched.h | 1 + net/sched/sch_api.c | 3 +++ 2 files changed, 4 insertions(+)include/uapi/linux/pkt_sched.h +0
-0@@ -98,6 +98,7 @@ struct tc_ratespec { };
Quoted 4 lines
include/uapi/linux/pkt_sched.h struct tc_ratespec { · line 101
Quoted 2 lines
include/uapi/linux/pkt_sched.h +1
-0#define TC_RTAB_SIZE 1024 +#define TC_LOG_MAX 30Adding a uAPI define is not necessary.
Yes, I'll move this to somewhere else.
Quoted 11 lines
Quoted 11 lines
include/uapi/linux/pkt_sched.h +0
-0struct tc_sizespec {unsigned char cell_log;net/sched/sch_api.c +0
-0@@ -513,6 +513,9 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,return stab; }
Quoted 6 lines
net/sched/sch_api.c static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt, · line 517
Quoted 2 lines
net/sched/sch_api.c +2
-0+if (s->size_log >TC_LOG_MAX ) +return ERR_PTR (-EINVAL );Looks sane, please add an extack message.
Why not cover
cell_logas well while at it?
You're right, will add message and check this one too in v2 :-)
Regards,
Michael Wang
Quoted 4 lines
Quoted 4 lines
net/sched/sch_api.c +0
-0stab =kmalloc (sizeof (*stab) + tsize *sizeof (u16),GFP_KERNEL );if (!stab)return ERR_PTR (-ENOMEM );
PATCH v2 1/1 net: prevent user from passing illegal stab size
In reply to 王贇We observed below report when playing with netlink sock:
UBSAN: shift-out-of-bounds in net/sched/sch_api.c:580:10 shift exponent 249 is too large for 32-bit type CPU: 0 PID: 685 Comm: a.out Not tainted Call Trace: dump_stack_lvl+0x8d/0xcf ubsan_epilogue+0xa/0x4e __ubsan_handle_shift_out_of_bounds+0x161/0x182 __qdisc_calculate_pkt_len+0xf0/0x190 __dev_queue_xmit+0x2ed/0x15b0
it seems like kernel won't check the stab log value passing from user, and will use the insane value later to calculate pkt_len.
This patch just add a check on the size/cell_log to avoid insane calculation.
--- include/net/pkt_sched.h | 1 + net/sched/sch_api.c | 6 ++++++ 2 files changed, 7 insertions(+)
include/net/pkt_sched.h +1 -0
@@ -11,6 +11,7 @@#include <uapi/linux/pkt_sched.h> #define DEFAULT_TX_QUEUE_LEN 1000 +#define STAB_SIZE_LOG_MAX 30struct qdisc_walker {int stop;
net/sched/sch_api.c +6 -0
@@ -513,6 +513,12 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,return stab; } +if (s->size_log >STAB_SIZE_LOG_MAX || + s->cell_log >STAB_SIZE_LOG_MAX ) { +NL_SET_ERR_MSG (extack,"Invalid logarithmic size of size table" ); +return ERR_PTR (-EINVAL ); + } + stab =kmalloc (sizeof (*stab) + tsize *sizeof (u16),GFP_KERNEL );if (!stab)return ERR_PTR (-ENOMEM );
Signature · 1 line
1.8.3.1