From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
stable@vger.kernel.org, Kees Cook <keescook@chromium.org>,
Matthew Wilcox <mawilcox@microsoft.com>
Subject: [PATCH 4.4 31/32] overflow.h: Add allocation size calculation helpers
Date: Mon, 26 Apr 2021 09:29:29 +0200 [thread overview]
Message-ID: <20210426072817.603584888@linuxfoundation.org> (raw)
In-Reply-To: <20210426072816.574319312@linuxfoundation.org>
From: Kees Cook <keescook@chromium.org>
commit 610b15c50e86eb1e4b77274fabcaea29ac72d6a8 upstream.
In preparation for replacing unchecked overflows for memory allocations,
this creates helpers for the 3 most common calculations:
array_size(a, b): 2-dimensional array
array3_size(a, b, c): 3-dimensional array
struct_size(ptr, member, n): struct followed by n-many trailing members
Each of these return SIZE_MAX on overflow instead of wrapping around.
(Additionally renames a variable named "array_size" to avoid future
collision.)
Co-developed-by: Matthew Wilcox <mawilcox@microsoft.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/md/dm-table.c | 10 +++---
include/linux/overflow.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 5 deletions(-)
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -516,14 +516,14 @@ static int adjoin(struct dm_table *table
* On the other hand, dm-switch needs to process bulk data using messages and
* excessive use of GFP_NOIO could cause trouble.
*/
-static char **realloc_argv(unsigned *array_size, char **old_argv)
+static char **realloc_argv(unsigned *size, char **old_argv)
{
char **argv;
unsigned new_size;
gfp_t gfp;
- if (*array_size) {
- new_size = *array_size * 2;
+ if (*size) {
+ new_size = *size * 2;
gfp = GFP_KERNEL;
} else {
new_size = 8;
@@ -531,8 +531,8 @@ static char **realloc_argv(unsigned *arr
}
argv = kmalloc(new_size * sizeof(*argv), gfp);
if (argv) {
- memcpy(argv, old_argv, *array_size * sizeof(*argv));
- *array_size = new_size;
+ memcpy(argv, old_argv, *size * sizeof(*argv));
+ *size = new_size;
}
kfree(old_argv);
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -202,4 +202,77 @@
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
+/**
+ * array_size() - Calculate size of 2-dimensional array.
+ *
+ * @a: dimension one
+ * @b: dimension two
+ *
+ * Calculates size of 2-dimensional array: @a * @b.
+ *
+ * Returns: number of bytes needed to represent the array or SIZE_MAX on
+ * overflow.
+ */
+static inline __must_check size_t array_size(size_t a, size_t b)
+{
+ size_t bytes;
+
+ if (check_mul_overflow(a, b, &bytes))
+ return SIZE_MAX;
+
+ return bytes;
+}
+
+/**
+ * array3_size() - Calculate size of 3-dimensional array.
+ *
+ * @a: dimension one
+ * @b: dimension two
+ * @c: dimension three
+ *
+ * Calculates size of 3-dimensional array: @a * @b * @c.
+ *
+ * Returns: number of bytes needed to represent the array or SIZE_MAX on
+ * overflow.
+ */
+static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
+{
+ size_t bytes;
+
+ if (check_mul_overflow(a, b, &bytes))
+ return SIZE_MAX;
+ if (check_mul_overflow(bytes, c, &bytes))
+ return SIZE_MAX;
+
+ return bytes;
+}
+
+static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
+{
+ size_t bytes;
+
+ if (check_mul_overflow(n, size, &bytes))
+ return SIZE_MAX;
+ if (check_add_overflow(bytes, c, &bytes))
+ return SIZE_MAX;
+
+ return bytes;
+}
+
+/**
+ * struct_size() - Calculate size of structure with trailing array.
+ * @p: Pointer to the structure.
+ * @member: Name of the array member.
+ * @n: Number of elements in the array.
+ *
+ * Calculates size of memory needed for structure @p followed by an
+ * array of @n @member elements.
+ *
+ * Return: number of bytes needed or SIZE_MAX on overflow.
+ */
+#define struct_size(p, member, n) \
+ __ab_c_size(n, \
+ sizeof(*(p)->member) + __must_be_array((p)->member),\
+ sizeof(*(p)))
+
#endif /* __LINUX_OVERFLOW_H */
next prev parent reply other threads:[~2021-04-26 7:31 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-26 7:28 [PATCH 4.4 00/32] 4.4.268-rc1 review Greg Kroah-Hartman
2021-04-26 7:28 ` [PATCH 4.4 01/32] net/sctp: fix race condition in sctp_destroy_sock Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 02/32] Input: nspire-keypad - enable interrupts only when opened Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 03/32] dmaengine: dw: Make it dependent to HAS_IOMEM Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 04/32] ARM: dts: Fix moving mmc devices with aliases for omap4 & 5 Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 05/32] arc: kernel: Return -EFAULT if copy_to_user() fails Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 06/32] neighbour: Disregard DEAD dst in neigh_update Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 07/32] ARM: keystone: fix integer overflow warning Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 08/32] ASoC: fsl_esai: Fix TDM slot setup for I2S mode Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 09/32] net: ieee802154: stop dump llsec keys for monitors Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 10/32] net: ieee802154: stop dump llsec devs " Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 11/32] net: ieee802154: forbid monitor for add llsec dev Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 12/32] net: ieee802154: stop dump llsec devkeys for monitors Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 13/32] net: ieee802154: forbid monitor for add llsec devkey Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 14/32] net: ieee802154: stop dump llsec seclevels for monitors Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 15/32] net: ieee802154: forbid monitor for add llsec seclevel Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 16/32] pcnet32: Use pci_resource_len to validate PCI resource Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 17/32] Input: i8042 - fix Pegatron C15B ID entry Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 18/32] scsi: libsas: Reset num_scatter if libata marks qc as NODATA Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 19/32] net: davicom: Fix regulator not turned off on failed probe Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 20/32] i40e: fix the panic when running bpf in xdpdrv mode Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 21/32] ARM: 9071/1: uprobes: Dont hook on thumb instructions Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 22/32] net: hso: fix null-ptr-deref during tty device unregistration Greg Kroah-Hartman
2021-04-26 7:36 ` Johan Hovold
2021-04-26 7:58 ` Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 23/32] ext4: correct error label in ext4_rename() Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 24/32] ARM: dts: Fix swapped mmc order for omap3 Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 25/32] s390/entry: save the caller of psw_idle Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 26/32] xen-netback: Check for hotplug-status existence before watching Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 27/32] cavium/liquidio: Fix duplicate argument Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 28/32] ia64: fix discontig.c section mismatches Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 29/32] ia64: tools: remove duplicate definition of ia64_mf() on ia64 Greg Kroah-Hartman
2021-04-26 7:29 ` [PATCH 4.4 30/32] compiler.h: enable builtin overflow checkers and add fallback code Greg Kroah-Hartman
2021-04-26 7:29 ` Greg Kroah-Hartman [this message]
2021-04-26 7:29 ` [PATCH 4.4 32/32] x86/crash: Fix crash_setup_memmap_entries() out-of-bounds access Greg Kroah-Hartman
2021-04-26 9:05 ` [PATCH 4.4 00/32] 4.4.268-rc1 review Pavel Machek
2021-04-26 13:04 ` Jon Hunter
2021-04-26 18:32 ` Guenter Roeck
2021-04-26 23:48 ` Shuah Khan
2021-04-27 7:48 ` Naresh Kamboju
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210426072817.603584888@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mawilcox@microsoft.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).