Skip to content

Commit 2857676

Browse files
committed
Merge tag 'overflow-v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull overflow updates from Kees Cook: "This adds the new overflow checking helpers and adds them to the 2-factor argument allocators. And this adds the saturating size helpers and does a treewide replacement for the struct_size() usage. Additionally this adds the overflow testing modules to make sure everything works. I'm still working on the treewide replacements for allocators with "simple" multiplied arguments: *alloc(a * b, ...) -> *alloc_array(a, b, ...) and *zalloc(a * b, ...) -> *calloc(a, b, ...) as well as the more complex cases, but that's separable from this portion of the series. I expect to have the rest sent before -rc1 closes; there are a lot of messy cases to clean up. Summary: - Introduce arithmetic overflow test helper functions (Rasmus) - Use overflow helpers in 2-factor allocators (Kees, Rasmus) - Introduce overflow test module (Rasmus, Kees) - Introduce saturating size helper functions (Matthew, Kees) - Treewide use of struct_size() for allocators (Kees)" * tag 'overflow-v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: treewide: Use struct_size() for devm_kmalloc() and friends treewide: Use struct_size() for vmalloc()-family treewide: Use struct_size() for kmalloc()-family device: Use overflow helpers for devm_kmalloc() mm: Use overflow helpers in kvmalloc() mm: Use overflow helpers in kmalloc_array*() test_overflow: Add memory allocation overflow tests overflow.h: Add allocation size calculation helpers test_overflow: Report test failures test_overflow: macrofy some more, do more tests for free lib: add runtime test of check_*_overflow functions compiler.h: enable builtin overflow checkers and add fallback code
2 parents 5eb6eed + 0ed2dd0 commit 2857676

File tree

99 files changed

+916
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+916
-205
lines changed

crypto/af_alg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ int af_alg_alloc_tsgl(struct sock *sk)
500500
sg = sgl->sg;
501501

502502
if (!sg || sgl->cur >= MAX_SGL_ENTS) {
503-
sgl = sock_kmalloc(sk, sizeof(*sgl) +
504-
sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
503+
sgl = sock_kmalloc(sk,
504+
struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
505505
GFP_KERNEL);
506506
if (!sgl)
507507
return -ENOMEM;

drivers/base/devres.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ static struct devres_group * node_to_group(struct devres_node *node)
8484
static __always_inline struct devres * alloc_dr(dr_release_t release,
8585
size_t size, gfp_t gfp, int nid)
8686
{
87-
size_t tot_size = sizeof(struct devres) + size;
87+
size_t tot_size;
8888
struct devres *dr;
8989

90+
/* We must catch any near-SIZE_MAX cases that could overflow. */
91+
if (unlikely(check_add_overflow(sizeof(struct devres), size,
92+
&tot_size)))
93+
return NULL;
94+
9095
dr = kmalloc_node_track_caller(tot_size, gfp, nid);
9196
if (unlikely(!dr))
9297
return NULL;

drivers/clk/bcm/clk-bcm2835-aux.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ static int bcm2835_aux_clk_probe(struct platform_device *pdev)
4040
if (IS_ERR(reg))
4141
return PTR_ERR(reg);
4242

43-
onecell = devm_kmalloc(dev, sizeof(*onecell) + sizeof(*onecell->hws) *
44-
BCM2835_AUX_CLOCK_COUNT, GFP_KERNEL);
43+
onecell = devm_kmalloc(dev,
44+
struct_size(onecell, hws,
45+
BCM2835_AUX_CLOCK_COUNT),
46+
GFP_KERNEL);
4547
if (!onecell)
4648
return -ENOMEM;
4749
onecell->num = BCM2835_AUX_CLOCK_COUNT;

drivers/clk/bcm/clk-bcm2835.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,8 +2147,8 @@ static int bcm2835_clk_probe(struct platform_device *pdev)
21472147
size_t i;
21482148
int ret;
21492149

2150-
cprman = devm_kzalloc(dev, sizeof(*cprman) +
2151-
sizeof(*cprman->onecell.hws) * asize,
2150+
cprman = devm_kzalloc(dev,
2151+
struct_size(cprman, onecell.hws, asize),
21522152
GFP_KERNEL);
21532153
if (!cprman)
21542154
return -ENOMEM;

drivers/clk/bcm/clk-iproc-asiu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ void __init iproc_asiu_setup(struct device_node *node,
197197
if (WARN_ON(!asiu))
198198
return;
199199

200-
asiu->clk_data = kzalloc(sizeof(*asiu->clk_data->hws) * num_clks +
201-
sizeof(*asiu->clk_data), GFP_KERNEL);
200+
asiu->clk_data = kzalloc(struct_size(asiu->clk_data, hws, num_clks),
201+
GFP_KERNEL);
202202
if (WARN_ON(!asiu->clk_data))
203203
goto err_clks;
204204
asiu->clk_data->num = num_clks;

drivers/clk/bcm/clk-iproc-pll.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,7 @@ void iproc_pll_clk_setup(struct device_node *node,
744744
if (WARN_ON(!pll))
745745
return;
746746

747-
clk_data = kzalloc(sizeof(*clk_data->hws) * num_clks +
748-
sizeof(*clk_data), GFP_KERNEL);
747+
clk_data = kzalloc(struct_size(clk_data, hws, num_clks), GFP_KERNEL);
749748
if (WARN_ON(!clk_data))
750749
goto err_clk_data;
751750
clk_data->num = num_clks;

drivers/clk/berlin/bg2.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,7 @@ static void __init berlin2_clock_setup(struct device_node *np)
509509
u8 avpll_flags = 0;
510510
int n, ret;
511511

512-
clk_data = kzalloc(sizeof(*clk_data) +
513-
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
512+
clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
514513
if (!clk_data)
515514
return;
516515
clk_data->num = MAX_CLKS;

drivers/clk/berlin/bg2q.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ static void __init berlin2q_clock_setup(struct device_node *np)
295295
struct clk_hw **hws;
296296
int n, ret;
297297

298-
clk_data = kzalloc(sizeof(*clk_data) +
299-
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
298+
clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
300299
if (!clk_data)
301300
return;
302301
clk_data->num = MAX_CLKS;

drivers/clk/clk-asm9260.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ static void __init asm9260_acc_init(struct device_node *np)
273273
int n;
274274
u32 accuracy = 0;
275275

276-
clk_data = kzalloc(sizeof(*clk_data) +
277-
sizeof(*clk_data->hws) * MAX_CLKS, GFP_KERNEL);
276+
clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
278277
if (!clk_data)
279278
return;
280279
clk_data->num = MAX_CLKS;

drivers/clk/clk-aspeed.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ static void __init aspeed_cc_init(struct device_node *np)
627627
if (!scu_base)
628628
return;
629629

630-
aspeed_clk_data = kzalloc(sizeof(*aspeed_clk_data) +
631-
sizeof(*aspeed_clk_data->hws) * ASPEED_NUM_CLKS,
632-
GFP_KERNEL);
630+
aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
631+
ASPEED_NUM_CLKS),
632+
GFP_KERNEL);
633633
if (!aspeed_clk_data)
634634
return;
635635

0 commit comments

Comments
 (0)