Received: by 10.223.185.116 with SMTP id b49csp1139189wrg; Wed, 14 Feb 2018 12:13:45 -0800 (PST) X-Google-Smtp-Source: AH8x227jxpsVEHrWn0cqdWg+8MljkGYgakLpf9NeRLYz1veZu+A1ClCo1SKN0ZEPWzGJUOF7rSYk X-Received: by 10.101.96.47 with SMTP id p15mr169037pgu.390.1518639225203; Wed, 14 Feb 2018 12:13:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518639225; cv=none; d=google.com; s=arc-20160816; b=IRT0jkuHf/qMg9TXKFPuDghGV3gJllOwCPAMR8XJ9nNYrm9sO5Gt/5D8flrmPLiqPo a/VHkoDUH8cTpy+oYhmAwq+QdQugTXcsk33Nu3Hdx5FjuNTjnM3I5Y0UOjhxhLXi2AbV Z/u6o7oxNO6RXjkK71dDF1bZug84K+pE0fPKUt04JDJssy6sCljo9YB1uB01k3O2iKH/ 50OC0mb5GwA4QRj4F79qF+/r09S9EG45yLfHx3cpDBqZmYU28yjWNvyRGJu/c+5bo3vZ KajZO9+KHmGu7wGcHx9qIYvrp+YMZfoVL80YE36DMB8JnY20MbvdsjBrtaGfLpQmlZPh 3Jmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=MN1F2np1bJafkAF/Bd3V73K6ITijlYNY3Y/66BI8LzQ=; b=F1HiO9OrbyKUJEeGY65qREsVwC06k1b+IBzpsp+AvkFq+OMLl8bS+YLbpDv79WKhJ+ UTHXiSJDSEJespHoLi4Uxvf3doaf+5O/kik/b0nC+NRKEyNoh4P1gOw3QOIT9xvARYY8 crDXEAij7MQ+b4sIlJR9ZKDVX461o3FJSUfmdXoDiCpnpMT3ccGq8FU+RRApFXNYmIcO rW0oN7JEwZDnV3O1xOlmACMHpeGAWnDpTh+IRBWM/8zJfJ8sSj29WWwI0xlotODhBk8z k2W/1o0eIij/xQy89u1NX4ObVRRAXed/5x1cj09zjIBEZ02bMR6Mbps8i2mWDagdylVg mjVg== ARC-Authentication-Results: i=1; mx.google.com; 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 97-v6si3612338plc.406.2018.02.14.12.13.30; Wed, 14 Feb 2018 12:13:45 -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; 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 S1162920AbeBNT1u (ORCPT + 99 others); Wed, 14 Feb 2018 14:27:50 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:19861 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162787AbeBNT1s (ORCPT ); Wed, 14 Feb 2018 14:27:48 -0500 X-IronPort-AV: E=Sophos;i="5.46,513,1511823600"; d="scan'208";a="254816015" Received: from abo-3-110-68.mrs.modulonet.fr (HELO [192.168.0.15]) ([85.68.110.3]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Feb 2018 20:27:46 +0100 Date: Wed, 14 Feb 2018 20:27:46 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Kees Cook cc: Matthew Wilcox , Andrew Morton , Matthew Wilcox , Linux-MM , LKML , Kernel Hardening , cocci@systeme.lip6.fr Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct In-Reply-To: Message-ID: References: <20180214182618.14627-1-willy@infradead.org> <20180214182618.14627-3-willy@infradead.org> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 14 Feb 2018, Kees Cook wrote: > 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... Thanks for the suggestion. I will look into it. julia