From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 341EF245012 for ; Fri, 17 Jul 2026 16:17:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=170.10.133.124 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784305066; cv=pass; b=t+Ajc2koEm0qsK1mSLpKyEooU3lNPFPUUcPjqvRtPqXb2FZ0f271txfc0pyj+pYIsmfdDIXdGM6dtLOsYIDnq1JQCZdvjBtH1UA+wVkfAZBJ4Jx6dPJUvps6xQuawo/FAnZntakCpmv5WTc4ueuHFKoz/W2Gk3HJT+hgWEFPWzI= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784305066; c=relaxed/simple; bh=hr2P2MH+R0IBzQgkI7o5io3mlFSLxg3GRQF7CqDV6i8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=h4Be9eYL06mGDENxgQnL3I4UJxPWHVu6tjG0txxPQBIq0Z7iP8ixp75+Yg5CzT1NWqFntbiwPdUYQUqVpX1htP/fUkoT7vfLQ62w/gVB22XPXSEAL5J5lSYP32mwhK0HneXHI4jGYU7/HWiqP/OUU/jOggrCdXmIJTbR5OC/7V8= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UZPgLDdh; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=df2qALlK; arc=pass smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UZPgLDdh"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="df2qALlK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784305064; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YXT11wOAqu0oxYNTZs56G73VylOJLbfiDeKc8PzTMns=; b=UZPgLDdhvAFGgJ79tbPNrpRjkPt4upYPbQqNE4B51AvsryyWgX5uuUKm30o9OuXKzKmMup 29WUYORtGHE+Rqa7/tDcsd7vP+MQMXfFSuBM6jX16N0JYIU2y57OD0Sm6TTo9MB1IVZMVZ W2ALzREikJKurvg55PNtreyENSUcJeE= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-168-wQJNiw_kP_6l6MTh9nVyzA-1; Fri, 17 Jul 2026 12:17:42 -0400 X-MC-Unique: wQJNiw_kP_6l6MTh9nVyzA-1 X-Mimecast-MFC-AGG-ID: wQJNiw_kP_6l6MTh9nVyzA_1784305062 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-495495ad5ddso5145075e9.3 for ; Fri, 17 Jul 2026 09:17:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1784305061; cv=none; d=google.com; s=arc-20260327; b=XIEieKY5zdcZr0R1TERC0DFBeRqY2aftiB9vQm9KulmnIcrrMAOeBXiy7OdsmlzvFH tR72j3GwpX924o6h3v4GaMXyOJm7/xFKKsCnvD7XnF3Wo4asz1CTjVUqXnX78x+VBSuU AulStOGeUVVLj/nXiKyJV6zt7ysOAntJaHsIpFLEy3P6qdt/VlwhdXlJ8WNysyyBS8W1 zyTAkNon0cxZtTRUnUxhM8Vg8KHonYZVlnz9iCdm1DRKTstL+RwyKq/i7VEBg5hRAVbN Zlk0zckXl64+Q3Hh5yylJ5ae/2Mq86n9kHfo8bxCaFSlF+945PBTCNnRKnDQyD9FNtrO RBXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=YXT11wOAqu0oxYNTZs56G73VylOJLbfiDeKc8PzTMns=; fh=I2BRePwpKUewynWvbdpkqF5fo+dqsvG+MjPHVJe+ahw=; b=Q+/7ApqkHBkJCAjVe/SWBHjnL2GwPkpBgWTG8d7VrWCL/ey+V/r7O0iurr0rlC1dYe j3XMd42sS1Le3L8K4XME6rcL01t26vg6JeR+OD76kuHQmnfVJvVQyukxlIbpk974hPvG 5Ctmr4sCMvH9fz67nVTkkZQHTa/j4wIs0d9cFrXDGZ6kgqZuEhVsP5PLJENQq5cR/wxO CS3QAX/DTWQeLf74iSKEGxZxa4lQw6amZ9KKajdunaJw5EWzlPIM89M1DYPK4tAZoqGY df7pI7TN48hjWPheC1JN8WDlEmX/x6yAM0Ytc9JiHJ6vp7YQqQtfenqy78iZ5k77hC0l 0rfQ==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1784305061; x=1784909861; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to:content-type; bh=YXT11wOAqu0oxYNTZs56G73VylOJLbfiDeKc8PzTMns=; b=df2qALlKaEd0W6MaN1qzAwAQQIZ9f+g0gIZu9qFOwzVMmzkR04U9qLLpUYT5maSNbn pNukuCdWskMNQ/5RcNVvoA69SGLNxKvaw1v8XvEpKmF+s2QnrZzMi3oWZ5H30QEu7/ZG Ei4wScg0C21DTxc9by/TWOZ1CM+KaWWgrQxBVPHe1ASsPLHOxRxVDypOOFHfgBSbML1o 7wTI04mPwrI8lc2Crc1Y3guSEUfBnUDTgck+eBbOOjl41YLKaHU2SkimcC3+XBqqR7i4 3V9WfSs6X83e25sJu0Bo7jqrBQFr+ak0Whdel3XvAZeN5Ls1A1G3VDR0W0dOS9voy9v3 3E6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784305061; x=1784909861; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YXT11wOAqu0oxYNTZs56G73VylOJLbfiDeKc8PzTMns=; b=OzMPcrdUztIXcyLWSlJ8+OW1Q59zNZX4Pq8jenHBK912SxsNWV1Ziycub2c6sLFnJw Gvsx/yCuAR9UgksshCKlAxCzC2LVGX75OEI0R6qD/iFXrhhzv8HS4fQ7h4/glQ6GoEW8 DojHqTHKJLY5KlQQPOmbs3d/XPBZkm33QsUgq0AxluuOFC/WkYtm1mHlEe8EoAO/8n6p GA56hQyymtVyvefo1AjmTsP7kjrgFCSXZp4+mQLQrHd7kPykLBKWR1a0YUQ9jdqOZg8W ILY7WiS9L/k3huOw9CgKc2/Lz1MHQbX8/VJq1S4o5UfmUHvvo5VxfvtUstlvn6bDmNrr b+hg== X-Gm-Message-State: AOJu0Yz1R+0LfsLDvVN5yOeKT4hKszV9PJBC2X0ev/6VabQbFv4PxNF/ rVgPTfg6uorteSfYBdUgi779zuKDkSTPC0dPOIKeGV7RUjAF/7LSwvl9VgL1tALWQ9Z7g7SdBux UgBngliYPcDl+zyo0jlRzzLQM5Oj4zvGae1SuZPWVj5o/C/ske6hp2+H+wNhoo+1Ci5Odh74QaW 1vNDL0b9v0LpLZbOccOMi2+01Ai+SHbrjAAGafTzzE X-Gm-Gg: AfdE7cnLP8s3Ep9aCUUirTSgJ8cVrXdqNpfkmLAXYW9HYa64WsBF5PbGjpw2sTtu/yq b4+QfOET4el1/Na8K/R+DzUpd57Dic4wZN63A+J7zHybrKwPJNGXMRkqWcyoc+2Iobq8L/Le8rQ PAfa/IV0y0QJ1SDEJAE0l5kE4OcosEHR7wsvXLFZx282mTpZwss96/pat+qD7WTKhmzdvrYCAQP C9+VKDYqNZJIpc= X-Received: by 2002:a05:600c:1f8e:b0:493:f639:9b2f with SMTP id 5b1f17b1804b1-4954a513f80mr40741145e9.39.1784305061421; Fri, 17 Jul 2026 09:17:41 -0700 (PDT) X-Received: by 2002:a05:600c:1f8e:b0:493:f639:9b2f with SMTP id 5b1f17b1804b1-4954a513f80mr40740785e9.39.1784305061013; Fri, 17 Jul 2026 09:17:41 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260714151818.538869-1-jtaubepe@redhat.com> <202607150906.33F01B40@keescook> In-Reply-To: <202607150906.33F01B40@keescook> From: Jesse Taube Date: Fri, 17 Jul 2026 12:17:30 -0400 X-Gm-Features: AUfX_myHiwbSFnZA7pykG4S2FdrSIoimOaEtOBf23z7uaiU33r0-xSVZLcdcRTY Message-ID: Subject: Re: [RFC PATCH] overflow: Add DECLARE_SIZED_FLEX() helper family To: Kees Cook Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "Gustavo A. R. Silva" , Przemek Kitszel , Christoph Hellwig , John Meneghini , Chris Leech , Jesse Taube Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jul 15, 2026 at 12:12=E2=80=AFPM Kees Cook wrote: > > On Tue, Jul 14, 2026 at 11:18:18AM -0400, Jesse Taube wrote: > > Add new DECLARE_SIZED_FLEX() helper to set the default size of a > > flexible-array member. The code is identical to the declaration in > > __DEFINE_FLEX() which has also been changed to use DECLARE_SIZED_FLEX()= . > > > > Add DECLARE_COUNTED_FLEX_ARRAY() helper which is a variant of > > DECLARE_FLEX_ARRAY() with a counted-by attribute. > > > > Also add default sized variants of > > DECLARE_FLEX_ARRAY(), DECLARE_SIZED_FLEX(), and > > DECLARE_COUNTED_FLEX_ARRAY(), DECLARE_COUNTED_SIZED_FLEX(). > > Where do you want to use these new helpers? > > > Signed-off-by: Jesse Taube > > --- > > include/linux/overflow.h | 58 +++++++++++++++++++++++++++++++++++++--- > > 1 file changed, 54 insertions(+), 4 deletions(-) > > > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > > index a8cb6319b4fb..a3384cae49e5 100644 > > --- a/include/linux/overflow.h > > +++ b/include/linux/overflow.h > > @@ -464,6 +464,59 @@ static __always_inline size_t __must_check size_su= b(size_t minuend, size_t subtr > > */ > > #define struct_offset(p, member) (offsetof(typeof(*(p)), member)) > > > > +/** > > + * __DECLARE_SIZED_FLEX() - helper macro for DECLARE_SIZED_FLEX() fami= ly. > > + * Allows for easily declaring a structure with a trailing flexible ar= ray member > > + * to have a specific size. > > + * > > + * @obj: object to be given a specific size > > + * @name: Name of the array member. > > + * @count: Number of elements in the array; must be compile-time const= . > > + */ > > +#define __DECLARE_SIZED_FLEX(obj, name, count) \ > > + _Static_assert(__builtin_constant_p(count), \ > > + "default sized flex array members require compile-= time const count"); \ > > + union { \ > > + u8 bytes[struct_size_t(obj, name, count)]; \ > > + obj; \ > > + } > > + > > +/** > > + * DECLARE_COUNTED_FLEX_ARRAY() - Declare a counted flexible array > > + * > > + * @type: Type name. > > + * @name: Name of the array member. > > + * @counter: Name of the __counted_by member. > > + */ > > +#define DECLARE_COUNTED_FLEX_ARRAY(type, name, counter)\ > > + struct { \ > > + size_t counter; \ > > + type name[] __counted_by(counter); \ > > + } > > + > > +/** > > + * DECLARE_SIZED_FLEX() - Declare a structure with a trailing flexible= array > > + * member with a default size. > > + * > > + * @type: Type name. > > + * @name: Name of the array member. > > + * @count: Number of elements in the array; must be compile-time const= . > > + */ > > +#define DECLARE_SIZED_FLEX(type, name, count) \ > > + __DECLARE_SIZED_FLEX(type name[], name, count) > > + > > +/** > > + * DECLARE_COUNTED_SIZED_FLEX() - Declare a structure with a trailing = flexible > > + * array member counted by count, with a default size. > > + * > > + * @type: Type name. > > + * @name: Name of the array member. > > + * @counter: Name of the __counted_by member. > > + * @count: Number of elements in the array; must be compile-time const= . > > + */ > > +#define DECLARE_COUNTED_SIZED_FLEX(type, name, counter, count) \ > > + __DECLARE_SIZED_FLEX(DECLARE_COUNTED_FLEX_ARRAY(type, name, count= er), name, count) > > I ask about where these will be used because one of the things I want to > keep tied together is the "counter" and "count", which usually means the > declaration should be combined with an initializer in some way so that > the counter gets assigned, as DEFINE_FLEX() ultimately does. > > I'm worried that extracting the internal construction of __DEFINE_FLEX > means we may run the risk of increasing the risk of losing that tie. We can add a comment to make sure to set count, or use DEFINE_FLEX to allocate it. Im not sure if there is a way for this to be a compile time error though. > So, I'd love to understand how you want to use this, as that would help > my understanding and potentially shape the design so we can keep counter > and count strongly associated. Basically, I want to use `DECLARE_COUNTED_SIZED_FLEX` for a version of this patch that uses `__counted_by` https://lore.kernel.org/linux-scsi/97526d45-ec7d-48a0-bdc6-659f75839f53@emb= eddedor.com/ I have checked that it does always set the `counter` variable correctly. As for `DECLARE_COUNTED_FLEX_ARRAY` there is already `DECLARE_FLEX_ARRAY` a= nd the counted variant is already used in a few places. A good example is `struct max77759_maxq_response` which subsiqently gets allocated with `DEFINE_FLEX` https://elixir.bootlin.com/linux/v7.2-rc1/source/include/linux/mfd/max77759= .h#L253 I assume you are ok with adding `__DECLARE_SIZED_FLEX` and `DECLARE_SIZED_FLEX` as they are usefull in cases where keeping structure offsets is important, similar to TRAILING_OVERLAP. An example of where `__DECLARE_SIZED_FLEX` could be used = is: https://lore.kernel.org/all/20260714185621.610105-1-jtaubepe@redhat.com/ Thanks, Jesse Taube > > > + > > /** > > * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. > > * Enables caller macro to pass arbitrary trailing expressions > > @@ -477,10 +530,7 @@ static __always_inline size_t __must_check size_su= b(size_t minuend, size_t subtr > > #define __DEFINE_FLEX(type, name, member, count, trailer...) = \ > > _Static_assert(__builtin_constant_p(count), = \ > > "onstack flex array members require compile-time c= onst count"); \ > > - union { = \ > > - u8 bytes[struct_size_t(type, member, count)]; = \ > > - type obj; = \ > > - } name##_u trailer; = \ > > + __DECLARE_SIZED_FLEX(type obj, member, count) name##_u trailer; = \ > > type *name =3D (type *)&name##_u > > -Kees > > -- > Kees Cook >