Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1357120rwi; Wed, 26 Oct 2022 14:20:41 -0700 (PDT) X-Google-Smtp-Source: AMsMyM53npf3ovJgLJxTuJYLvwoxdV0iEK97Ct/HhC3QjB/NQFAG06nQc1kLNETHhQ05/lBufk7C X-Received: by 2002:a17:903:1245:b0:178:9234:3768 with SMTP id u5-20020a170903124500b0017892343768mr4952712plh.146.1666819240993; Wed, 26 Oct 2022 14:20:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666819240; cv=none; d=google.com; s=arc-20160816; b=cDArdo2IBS1y7MjOkv1IT53zOyRsLkC4ja3D7Iewmq/FSueO6S2izgzqK7yPiAqHu0 vl/um9czoHO9rKxAzteucq6M2vWmVOvgeuhOQ/3071uddBuDbLBG//oJLYheJEYoQN2/ CBpcJeBW69BF5HSLPU/va4RlzxhsCEU8pgPFMP+hHZ6Yx0msgwdrs4euA+8MrhX1l6bH gvpvvGI4yC3f2ClH5TkbxOghWTp0pX0vxNb1zPn5kMvs7QmjFXQMgjP/jo469I67F0n0 z0LkLV2eVOBlSGum0zXh+sjSBAZQ3T/598ZwNnI0guF1HM1KfAV8j9Xk6uscYcdd0Mhr y2sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=E0kxEl2S2a68MlFLjbJ42CBU2Unm12GzaCEH0lHQWuY=; b=fsYCm4K3Xp+13G5QQ3HoE7goF9n/9U6dTTOl7zauDK+Yd7IvN31Go1ToKUZcJEvcVU 8BJ+ogfvADbquvbqifzgJuPOQ/ACPAoGfU3oc7jIj+n+23pmwAenjBmhLyjen7Yzqlh3 s34FtUf7L8XmxL8oee3/vmhXyb3QKJVMh7bUcJrvA85znKDFHaqmQoZlr0wKhygCvgYl LIxcvuQCSZnl/lQ1E97CtXoCVh01Zvol6lgDTl0puZzL9MYZYQ2yzPQn2Gx1m9RCL0Q9 ufMAfT0xkhzABsmQeeI9+4SmyxmmHTSwmyl2F9K1/Xjo9HRZDh1s/Y2tW+yxihDCPTBZ sYkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=obeJhxI+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a631753000000b0046afc3948c4si7130249pgx.707.2022.10.26.14.20.24; Wed, 26 Oct 2022 14:20:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=obeJhxI+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234430AbiJZUdx (ORCPT + 99 others); Wed, 26 Oct 2022 16:33:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234111AbiJZUdu (ORCPT ); Wed, 26 Oct 2022 16:33:50 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD55E7E338 for ; Wed, 26 Oct 2022 13:33:49 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id f9so11701083pgj.2 for ; Wed, 26 Oct 2022 13:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=E0kxEl2S2a68MlFLjbJ42CBU2Unm12GzaCEH0lHQWuY=; b=obeJhxI+ENacAUlLPp+v61BhonjTS9sjU5Bul5it9+TdjHX1b4Op8qbkgN5uiAd9Gn Rd++IsKXJUdHvO+QOHUTGWHEmxI4CX4zFjED0OsH9PsV0jOV3C/F27o4oDQHKnHqCHbR ik1V1MssxtQ/uj/MararQ0+xsoC+XV6V9lfSw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=E0kxEl2S2a68MlFLjbJ42CBU2Unm12GzaCEH0lHQWuY=; b=Uj/XuJyHHUPeSivkHXUnq0inOmP2ris4stsx6c+PlUDT9ztafoED8AbgllMsmveeAr AiIsgfByphj70sUih73EnacKFAEiMWOSsXJ3LSCrrEaAaVxfRjlP/aU0k5tmNmA118Rb uFAuSbjyOMC5FMclb8c9M+RwBJSEETrdYMOkXqraB+qE22sE2EF3afesJ8k92uQL7GfJ zvsA8NKxrrcSElrz7HmN9JmF0s4+IVk8rAo+sgkKfizQtETqrrn1BxJKgJ7Br6ij6aD1 55fv7zwHqazFMUyDq4s6yAgnp2h+BkdRerS3eOtigXDd9d9oEFZRPSESpXlpCztMBmuM iG2Q== X-Gm-Message-State: ACrzQf0KTNSZ4/mJyBXfwfrfhch5jP7+X/cjXQR2ubFxCHbigN3bIOWW Pz22oV0sJjY2fOYMpFPW6f/OOA== X-Received: by 2002:a05:6a00:2307:b0:565:9079:b165 with SMTP id h7-20020a056a00230700b005659079b165mr46196368pfh.53.1666816429195; Wed, 26 Oct 2022 13:33:49 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s27-20020aa78bdb000000b0056bc30e618dsm3377335pfd.38.2022.10.26.13.33.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Oct 2022 13:33:48 -0700 (PDT) Date: Wed, 26 Oct 2022 13:33:47 -0700 From: Kees Cook To: Keith Packard Cc: "Darrick J . Wong" , "Gustavo A. R. Silva" , Francis Laniel , Daniel Axtens , Dan Williams , Vincenzo Frascino , Guenter Roeck , Daniel Vetter , Tadeusz Struk , Zorro Lang , Geert Uytterhoeven , Andy Shevchenko , Paolo Abeni , linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2 1/2] Introduce flexible array struct helpers Message-ID: <202210260904.41D0BE00A@keescook> References: <20221024171848.never.522-kees@kernel.org> <20221024172058.534477-1-keescook@chromium.org> <87k04pf4tk.fsf@keithp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87k04pf4tk.fsf@keithp.com> X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 24, 2022 at 11:35:03AM -0700, Keith Packard wrote: > Kees Cook writes: > > > + * struct flex_array_struct_example { > > + * ... // arbitrary members > > + * bounded_flex_array( > > + * u16, part_count, // count of elements stored in "parts" below. > > + * u32, parts // flexible array with elements of type u32. > > + * ); > > + * ); > > > + * struct flex_array_struct_example { > > + * ... // position-sensitive members > > + * // count of elements stored in "parts" below. > > + * DECLARE_FAS_COUNT(u16, part_count); > > + * .. // position-sensitive members > > + * // flexible array with elements of type u32. > > + * DECLARE_FAS_ARRAY(u32, parts); > > + * }; > > I'm sure there's a good reason, but these two macros appear to be doing > similar things and yet have very different naming conventions. Maybe: > > FAS_DECLARE_COUNT(type, name) > FAS_DECLARE_ARRAY(type, name) > FAS_DECLARE(size_type, size_name, array_type, array_name) Well, the custom has been for individual things, call it "DECLARE_*", and for groups, we went with lower-case macros (e.g. struct_group()). > > > +/* For use with flexible array structure helpers, in */ > > +#define __DECLARE_FAS_COUNT(TYPE, NAME) \ > > + union { \ > > + TYPE __flex_array_elements_count; \ > > + TYPE NAME; \ > > + } > > How often could that second "public" member be 'const'? That would catch > places which accidentally assign to this field. > > For code which does want to write to this field, is it mostly trimming > data from the end, or does it actually smash in arbitrary values? For > the former case, would it be helpful to have a test to make sure the > assigned size isn't larger than the real size (yeah, that would probably > take an extra field holding the real size), or larger than the current size? I don't think this'll work within arbitrary struct declarations, but it would be nice. :) -- Kees Cook