diff options
author | Kees Cook <keescook@chromium.org> | 2018-05-09 01:29:52 -0400 |
---|---|---|
committer | Kees Cook <keescook@chromium.org> | 2018-06-05 15:16:51 -0400 |
commit | 2509b561f7c6599907c08cb364c86b8c45466e4f (patch) | |
tree | 4d9002abaf08e2ebcda9d313a2d825aeb198d102 | |
parent | 3b3b1a29eb89ba93f91213cbebb332a2ac31fa8b (diff) |
device: Use overflow helpers for devm_kmalloc()
Use the overflow helpers both in existing multiplication-using inlines as
well as the addition-overflow case in the core allocation routine.
Signed-off-by: Kees Cook <keescook@chromium.org>
-rw-r--r-- | drivers/base/devres.c | 7 | ||||
-rw-r--r-- | include/linux/device.h | 8 |
2 files changed, 12 insertions, 3 deletions
diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 95b67281cd2a..f98a097e73f2 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c | |||
@@ -84,9 +84,14 @@ static struct devres_group * node_to_group(struct devres_node *node) | |||
84 | static __always_inline struct devres * alloc_dr(dr_release_t release, | 84 | static __always_inline struct devres * alloc_dr(dr_release_t release, |
85 | size_t size, gfp_t gfp, int nid) | 85 | size_t size, gfp_t gfp, int nid) |
86 | { | 86 | { |
87 | size_t tot_size = sizeof(struct devres) + size; | 87 | size_t tot_size; |
88 | struct devres *dr; | 88 | struct devres *dr; |
89 | 89 | ||
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 | |||
90 | dr = kmalloc_node_track_caller(tot_size, gfp, nid); | 95 | dr = kmalloc_node_track_caller(tot_size, gfp, nid); |
91 | if (unlikely(!dr)) | 96 | if (unlikely(!dr)) |
92 | return NULL; | 97 | return NULL; |
diff --git a/include/linux/device.h b/include/linux/device.h index 477956990f5e..897efa647203 100644 --- a/include/linux/device.h +++ b/include/linux/device.h | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/ratelimit.h> | 25 | #include <linux/ratelimit.h> |
26 | #include <linux/uidgid.h> | 26 | #include <linux/uidgid.h> |
27 | #include <linux/gfp.h> | 27 | #include <linux/gfp.h> |
28 | #include <linux/overflow.h> | ||
28 | #include <asm/device.h> | 29 | #include <asm/device.h> |
29 | 30 | ||
30 | struct device; | 31 | struct device; |
@@ -668,9 +669,12 @@ static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) | |||
668 | static inline void *devm_kmalloc_array(struct device *dev, | 669 | static inline void *devm_kmalloc_array(struct device *dev, |
669 | size_t n, size_t size, gfp_t flags) | 670 | size_t n, size_t size, gfp_t flags) |
670 | { | 671 | { |
671 | if (size != 0 && n > SIZE_MAX / size) | 672 | size_t bytes; |
673 | |||
674 | if (unlikely(check_mul_overflow(n, size, &bytes))) | ||
672 | return NULL; | 675 | return NULL; |
673 | return devm_kmalloc(dev, n * size, flags); | 676 | |
677 | return devm_kmalloc(dev, bytes, flags); | ||
674 | } | 678 | } |
675 | static inline void *devm_kcalloc(struct device *dev, | 679 | static inline void *devm_kcalloc(struct device *dev, |
676 | size_t n, size_t size, gfp_t flags) | 680 | size_t n, size_t size, gfp_t flags) |