提交 05bf1a77 编写于 作者: G Gustavo A. R. Silva 提交者: Jialin Zhang

net_sched: Use struct_size() and flex_array_size() helpers

mainline inclusion
from mainline-v5.16-rc1
commit 69508d43
category: bugfix
bugzilla: https://gitee.com/src-openeuler/kernel/issues/I6SFHJ
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=69508d43334e3b09c344f662272bcf24a5b508ed

--------------------------------

Make use of the struct_size() and flex_array_size() helpers instead of
an open-coded version, in order to avoid any potential type mistakes
or integer overflows that, in the worse scenario, could lead to heap
overflows.

Link: https://github.com/KSPP/linux/issues/160Signed-off-by: NGustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20210928193107.GA262595@embeddedorSigned-off-by: NJakub Kicinski <kuba@kernel.org>

Conflicts:
	net/sched/sch_api.c
Signed-off-by: NZhengchao Shao <shaozhengchao@huawei.com>
Reviewed-by: NLiu Jian <liujian56@huawei.com>
Signed-off-by: NJialin Zhang <zhangjialin11@huawei.com>
上级 b84c5aa1
...@@ -507,7 +507,8 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt, ...@@ -507,7 +507,8 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,
list_for_each_entry(stab, &qdisc_stab_list, list) { list_for_each_entry(stab, &qdisc_stab_list, list) {
if (memcmp(&stab->szopts, s, sizeof(*s))) if (memcmp(&stab->szopts, s, sizeof(*s)))
continue; continue;
if (tsize > 0 && memcmp(stab->data, tab, tsize * sizeof(u16))) if (tsize > 0 &&
memcmp(stab->data, tab, flex_array_size(stab, data, tsize)))
continue; continue;
stab->refcnt++; stab->refcnt++;
return stab; return stab;
...@@ -519,14 +520,14 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt, ...@@ -519,14 +520,14 @@ static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
stab = kmalloc(sizeof(*stab) + tsize * sizeof(u16), GFP_KERNEL); stab = kmalloc(struct_size(stab, data, tsize), GFP_KERNEL);
if (!stab) if (!stab)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
stab->refcnt = 1; stab->refcnt = 1;
stab->szopts = *s; stab->szopts = *s;
if (tsize > 0) if (tsize > 0)
memcpy(stab->data, tab, tsize * sizeof(u16)); memcpy(stab->data, tab, flex_array_size(stab, data, tsize));
list_add_tail(&stab->list, &qdisc_stab_list); list_add_tail(&stab->list, &qdisc_stab_list);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册