Received: by 10.223.185.116 with SMTP id b49csp1138802wrg; Wed, 14 Feb 2018 12:13:21 -0800 (PST) X-Google-Smtp-Source: AH8x225M0c+8wU5iE/YBgelnzmpcRiOSikm69XAOswQq5cGlSVnWisyFZ43AsyqZNa/Xd7U6ys/7 X-Received: by 2002:a17:902:5902:: with SMTP id o2-v6mr183796pli.79.1518639201364; Wed, 14 Feb 2018 12:13:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518639201; cv=none; d=google.com; s=arc-20160816; b=YlZytSXNJu472pDqUIzNTvtpwXd68BnrVUhxfxEsCEth2yrLx8fsrZioOxt/aEM0WR HWGcz1OJwRwAd1nkcbPYj2z3EKRiMZHw6wVgmcBE93h9xIfLJ5MQtdReIhegUZ67nNpA REFYXWRY+eQZX4clY3alf+U43YYIy6EIst9e+HJrIqAqRZ2NQm5km7fJzSpTng8Bnvke IOuzpJk8oDhlq90pnMJu82X9sb/wy9SfRf+EJhpcu2B3u4Jp1dPietaplv7toFE3wzsI 7D3sOOE6w2CptDTf/4DHQR89S/wcdp/Fq4Czu53ReisqjXm65hen15dDmjTLajJbigqI GcGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=Z2pU7HGIN5elFF5sBLj9vrV0QhrOAG6lcSSEkEP+iGQ=; b=MiNgvohV3asDShtYheuuoJB2FwnMn7S86gRxPt2z8bc24JSRLTDRcFJ5issHeO2ebY Q07Wu3m8WxhTO5ypjGPEpd8VnWyOSJfzZEVgyseQZOX63Fkax5Op/vH+smamrwcT9RZj nM2hai2kIDQoD90mxi1RrwXssQPQNoCI3G/4/6WnqoeofOJbRAH6Q0yE7DXylsiPAHcl 8Af0DhNx3BmGnIbP5G0XMf1Z2Qnwreor8ollzd78M0wlJuWJvi4/GBuLYt3GHU5cWzeY Z+XeQ/F1avnD6IWB85Rltsxb+nsbYEa44daS2fVqGFuXUG0TDAaXa05cOJd2O9M1BfiJ wwmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=jN/E4YbA; dkim=fail header.i=@chromium.org header.s=google header.b=FtfEjZeh; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x65si2170943pfa.74.2018.02.14.12.13.06; Wed, 14 Feb 2018 12:13:21 -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=@google.com header.s=20161025 header.b=jN/E4YbA; dkim=fail header.i=@chromium.org header.s=google header.b=FtfEjZeh; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162844AbeBNTWn (ORCPT + 99 others); Wed, 14 Feb 2018 14:22:43 -0500 Received: from mail-ua0-f195.google.com ([209.85.217.195]:46556 "EHLO mail-ua0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162787AbeBNTWl (ORCPT ); Wed, 14 Feb 2018 14:22:41 -0500 Received: by mail-ua0-f195.google.com with SMTP id y17so12611595uaa.13 for ; Wed, 14 Feb 2018 11:22:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=Z2pU7HGIN5elFF5sBLj9vrV0QhrOAG6lcSSEkEP+iGQ=; b=jN/E4YbAQc7BB26qfWSNFScSy4R5Uyg7SSr4W76EViRZ38+LC7NUL3ImK5tetELMwp Jk/wzNrdyQ2NrB90zL0CgviekUZs7lwn9LcsWsjHBmfNXEw52ahaISui1c+V+Ak1lSP7 wFDwJgCfowvZ8yqdGWWjb8p4XM02GscNyUvTWgaMMZYwcjylaaZ0ijX2aXTjpe0sX1df MyWmMGwKFbGsVVoVHlt5sHlr8l4dImSwLCifqszdf4WsMIE2V0Dmph/kljtyPc8vb280 TQbXp65/1dbf92yivPLQmOVthEwqGQbUACco5uW94yiM5zEgnGNfahHyKOXJEzjXVcwx zwNQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=Z2pU7HGIN5elFF5sBLj9vrV0QhrOAG6lcSSEkEP+iGQ=; b=FtfEjZehAiEad6Fd75P0NyvaI/dwC0Emnj3cQ5GoRz86pxd6OPDWQT2X3lkRtHkigq rJxuoyEW4dWURnMEpKY6my5WYK1uc4Z8C3YZAtT0FyXO83+qKfI7DX2hxOCdsbxMsKFY ygwbfTUjXTTqSqSmtDYdyfCh0AySoDtlSCROk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=Z2pU7HGIN5elFF5sBLj9vrV0QhrOAG6lcSSEkEP+iGQ=; b=ghdlO69rISJ4Hmv5Ag6VggY9FPimZVEcrFr2NsJge+L/IzttSy7IZWka9qrLCIoyPr qEq0QCPT1L1KS0ZT3gVKKPWf39b2g2tKB5nD37YOhh4Uufv/bL0DHFdd7s+UlZhPfZeQ 0AtT1khpUFRfZXS1P/yX/VTTS7EM9Pt56giVA052toSNaE5YOGZ7+2iRtY//TxyP5Nzp ubviWnBVuVBo/uxz3nt5/P1qGZZswrZp/MzC/UDHcQWJrnwBEOT8iWmdW39D/ZpO3bSk L+tTvL86H/Wtee+5LMfOqarij1wXTG4DUHyWs2oDnRKdssmVnVXTaUwmCO3Txk8OjbtV 9jUg== X-Gm-Message-State: APf1xPDoTShbLn0b/KoYkZr4/nIti/iHVe7t6R/KojdTsu6G94uQ7DjH qiQmlmMwiDAU65+xC0tLgu8eQL2h14fhz1tUHC/sXQ== X-Received: by 10.176.74.73 with SMTP id r9mr294522uae.164.1518636160093; Wed, 14 Feb 2018 11:22:40 -0800 (PST) MIME-Version: 1.0 Received: by 10.31.56.87 with HTTP; Wed, 14 Feb 2018 11:22:38 -0800 (PST) In-Reply-To: <20180214182618.14627-3-willy@infradead.org> References: <20180214182618.14627-1-willy@infradead.org> <20180214182618.14627-3-willy@infradead.org> From: Kees Cook Date: Wed, 14 Feb 2018 11:22:38 -0800 X-Google-Sender-Auth: xD-1pg1Xule7hFvixBJzy0-yDKM Message-ID: Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct To: Matthew Wilcox Cc: Andrew Morton , Matthew Wilcox , Linux-MM , LKML , Kernel Hardening , Julia Lawall , cocci@systeme.lip6.fr Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 14, 2018 at 10:26 AM, Matthew Wilcox wrote: > 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. Longer description, maybe? "Allocate a *b + c bytes of memory"? > + * @n: Number of elements. > + * @size: Size of each element (should be constant). > + * @c: Size of header (should be constant). If these should be constant, should we mark them as "const"? Or WARN if __builtin_constant_p() isn't true? > + * @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 renaming typo? Should this be "kvzalloc_struct()"? > + * 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) Nit: "(gfp) | __GFP_ZERO" just in case of insane usage. > + > +/** > + * 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) It might be nice to include another patch that replaces some of the existing/common uses of a*b+c with the new function... Otherwise, yes, please. We could build a coccinelle rule for additional replacements... -Kees -- Kees Cook Pixel Security