From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2C08C433DF for ; Tue, 9 Jun 2020 01:17:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C18C820774 for ; Tue, 9 Jun 2020 01:17:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591665444; bh=LcyC8kXR/tZ9xNdJt1SFm1gMfQldVueWxhQb2Y+X8wk=; h=Date:From:To:Cc:Subject:List-ID:From; b=fNLZjuy4f5zAtuG0gfqorD1u1J82nOQXTm6U8OEelCRVkYhJHO5IemQTiYucrHtS7 EHUFrxKms0nUCt0D1Atm78TIU11fORdMM+usUCLD5t7vKW8ycqbrWhOxryD4TSIqZB kmUueW27jNmGsWdGNd1U7lOptFdaIH0ahC5KDg1c= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727004AbgFIBRY (ORCPT ); Mon, 8 Jun 2020 21:17:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:46882 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726938AbgFIBRX (ORCPT ); Mon, 8 Jun 2020 21:17:23 -0400 Received: from embeddedor (unknown [189.207.59.248]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 84B422076C; Tue, 9 Jun 2020 01:17:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591665442; bh=LcyC8kXR/tZ9xNdJt1SFm1gMfQldVueWxhQb2Y+X8wk=; h=Date:From:To:Cc:Subject:From; b=oYoqgqo1MokxNoYNhfkSCgy1gWVgYdfuN8Fh5lWjb/TvfGVQah4nJwPzllCvgtg5g QBl8qcfiXcYDGyR8fbWWfsedekksNm0RbU8l9NwJiKE+2gh8zmOqh93cQCqc0Qi9d8 Tr2koovN4j4NZvVaNsXZZh9SP9r8sunxcEgk28gE= Date: Mon, 8 Jun 2020 20:22:33 -0500 From: "Gustavo A. R. Silva" To: Kees Cook Cc: linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH v2] overflow.h: Add flex_array_size() helper Message-ID: <20200609012233.GA3371@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add flex_array_size() helper for the calculation of the size, in bytes, of a flexible array member contained within an enclosing structure. Example of usage: struct something { size_t count; struct foo items[]; }; struct something *instance; instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL); instance->count = count; memcpy(instance->items, source, flex_array_size(instance, items, instance->count)); The helper returns SIZE_MAX on overflow instead of wrapping around. (Additionally replace parameter n with count in struct_size() for unification). Signed-off-by: Gustavo A. R. Silva --- Changes in v2: - Add further information to the helper documentation. - Use same code style as struct_size() for consistency. include/linux/overflow.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 659045046468f..d2329a914304c 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -304,16 +304,33 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c) * 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. + * @count: Number of elements in the array. * * Calculates size of memory needed for structure @p followed by an - * array of @n @member elements. + * array of @count @member elements. * * Return: number of bytes needed or SIZE_MAX on overflow. */ -#define struct_size(p, member, n) \ - __ab_c_size(n, \ +#define struct_size(p, member, count) \ + __ab_c_size(count, \ sizeof(*(p)->member) + __must_be_array((p)->member),\ sizeof(*(p))) +/** + * flex_array_size() - Calculate size, in bytes, of a flexible array member + * within an enclosing structure. Read on for more details. + * + * @p: Pointer to the structure. + * @member: Name of the flexible array member. + * @count: Number of elements in the array. + * + * Calculates size, in bytes, of a flexible array @member of @count elements + * within structure @p. + * + * Return: number of bytes needed or SIZE_MAX on overflow. + */ +#define flex_array_size(p, member, count) \ + array_size(count, \ + sizeof(*(p)->member) + __must_be_array((p)->member)) + #endif /* __LINUX_OVERFLOW_H */ -- 2.27.0