Received: by 10.223.185.116 with SMTP id b49csp1028652wrg; Wed, 14 Feb 2018 10:28:13 -0800 (PST) X-Google-Smtp-Source: AH8x227hkX0rSw0cBN+ukxmzkYqxfYMx98JaidaUoU/bYc1WyRAhKcC6RKeTkzE/KTPVTWVfNIhi X-Received: by 10.99.95.81 with SMTP id t78mr94608pgb.380.1518632893049; Wed, 14 Feb 2018 10:28:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518632893; cv=none; d=google.com; s=arc-20160816; b=u7B2y15BUgVgHv+/9vRRLS/shdXdZn8PDGwBUNZapvjS1dPbZ4GBHrc/+xvIa9rbhA yK1vPu5OBUIVZyssaVHLV9r1DORJ3H73XXbfVhcwtheBjZ0ViQjeGem6jTcgI8ioXcjF +MIMCfPDtqYvC7rmGetxgjCjBjQcQtZuoI3vQEakgB4pdYfKqgnddwHUKaVkf6QCh0M2 IhtlTc+s7M3zCmLDhNXxLEVHAC6KdA8h4zDhqIzHKPxoHwhnuEhBvwU0ApXIzI0I1UVG PxHBCE0qSUBerw1REeWEYnh95ieIVGuYrNFkD6KgO8WBGCuzF1CYNIQQdDRLtig9KAHV LF8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=SbcDeWioldL6lk81YWqtgNQgQdycQJFstVySef00ZII=; b=EYxOEqBWMR9aO5rVuMosc/YD5ytQXp5O5SMQtl5B4h4comosBHQ0zCsnnyzMewyvMV A0at/oz0dOmLr+450PTnjADP8zJ8V6WBmU9EemG8CRcJTDyp9Kp4vU+C5U6nqKQJcw0I XKa5jdzadOfaKUq/SRVTcBYQZcj32ukkbSzjcKKEiW0gfeJcpqOejo4sq0RaUA6oZfAn jBk7Tp+hfWpEnnv5Nl5Ofns4CaTA39MpXx+gR0j/+nPN891+XH6r7Au0ZeKEL9PRYOlg RxgPJaaP6YczwCec4Js8BpK3phh/1zRJCl2MZfGPA4v/eeFzLzRDlCAv/Qv3Nx0DrFrd NupQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Tk58S/qd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p1si5517983pfp.91.2018.02.14.10.27.58; Wed, 14 Feb 2018 10:28:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Tk58S/qd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162054AbeBNS01 (ORCPT + 99 others); Wed, 14 Feb 2018 13:26:27 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:58556 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161948AbeBNS0Z (ORCPT ); Wed, 14 Feb 2018 13:26:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=SbcDeWioldL6lk81YWqtgNQgQdycQJFstVySef00ZII=; b=Tk58S/qdtd8KYws5J9M93yVmH ZscpHlb4WvC3ySj1dk3Y+kMC40tHExWVk/3UWTv6qJx7/xXLyfi3qbkEhirW9q3hojVWCT59yqLSe XaayxI/7U9THVeUB4QRezSSAF76Tpb7967hW1Y5vYReoqDj88FUG+WYGIMCy0tDHM507XgYJaPG+/ RUOoQDQ/7BLAbmt3w/cMxV13RLd4IbMPN7AxfyZXsuTX7N/PDaMkv6h6LmVOouTlNk7cLYTeqphnZ kxSgby3LzpL1Z97lM19M+V+H7yKcgCsYskAsMpV39NK223dxC7MFqHoyIBrj3XvMVC57E2rITV7Dt DpFNCcXAg==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1em1l9-0003tB-Oe; Wed, 14 Feb 2018 18:26:23 +0000 From: Matthew Wilcox To: Andrew Morton Cc: Matthew Wilcox , linux-mm@kvack.org, Kees Cook , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct Date: Wed, 14 Feb 2018 10:26:18 -0800 Message-Id: <20180214182618.14627-3-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180214182618.14627-1-willy@infradead.org> References: <20180214182618.14627-1-willy@infradead.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox We have kvmalloc_array in order to safely allocate an array with a number of elements specified by userspace (avoiding arithmetic overflow leading to a buffer overrun). But it's fairly common to have a header in front of that array (eg specifying the length of the array), so we need a helper function for that situation. kvmalloc_ab_c() is the workhorse that does the calculation, but in spite of our best efforts to name the arguments, it's really hard to remember which order to put the arguments in. kvzalloc_struct() eliminates that effort; you tell it about the struct you're allocating, and it puts the arguments in the right order for you (and checks that the arguments you've given are at least plausible). For comparison between the three schemes: sev = kvzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL); sev = kvzalloc_ab_c(elems, sizeof(struct v4l2_kevent), sizeof(*sev), GFP_KERNEL); sev = kvzalloc_struct(sev, events, elems, GFP_KERNEL); Signed-off-by: Matthew Wilcox --- include/linux/mm.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 81bd7f0be286..ddf929c5aaee 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -557,6 +557,57 @@ static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) return kvmalloc(n * size, flags); } +/** + * kvmalloc_ab_c() - Allocate memory. + * @n: Number of elements. + * @size: Size of each element (should be constant). + * @c: Size of header (should be constant). + * @gfp: Memory allocation flags. + * + * Use this function to allocate @n * @size + @c bytes of memory. This + * function is safe to use when @n is controlled from userspace; it will + * return %NULL if the required amount of memory cannot be allocated. + * Use kvfree() to free the allocated memory. + * + * The kvzalloc_hdr_arr() function is easier to use as it has typechecking + * and you do not need to remember which of the arguments should be constants. + * + * Context: Process context. May sleep; the @gfp flags should be based on + * %GFP_KERNEL. + * Return: A pointer to the allocated memory or %NULL. + */ +static inline __must_check +void *kvmalloc_ab_c(size_t n, size_t size, size_t c, gfp_t gfp) +{ + if (size != 0 && n > (SIZE_MAX - c) / size) + return NULL; + + return kvmalloc(n * size + c, gfp); +} +#define kvzalloc_ab_c(a, b, c, gfp) kvmalloc_ab_c(a, b, c, gfp | __GFP_ZERO) + +/** + * kvzalloc_struct() - Allocate and zero-fill a structure containing a + * variable length array. + * @p: Pointer to the structure. + * @member: Name of the array member. + * @n: Number of elements in the array. + * @gfp: Memory allocation flags. + * + * Allocate (and zero-fill) enough memory for a structure with an array + * of @n elements. This function is safe to use when @n is specified by + * userspace as the arithmetic will not overflow. + * Use kvfree() to free the allocated memory. + * + * Context: Process context. May sleep; the @gfp flags should be based on + * %GFP_KERNEL. + * Return: Zero-filled memory or a NULL pointer. + */ +#define kvzalloc_struct(p, member, n, gfp) \ + (typeof(p))kvzalloc_ab_c(n, \ + sizeof(*(p)->member) + __must_be_array((p)->member), \ + offsetof(typeof(*(p)), member), gfp) + extern void kvfree(const void *addr); static inline atomic_t *compound_mapcount_ptr(struct page *page) -- 2.15.1