Received: by 2002:ab2:715a:0:b0:1fd:c064:50c with SMTP id l26csp52228lqm; Mon, 10 Jun 2024 12:22:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+XgIOKh4UDUirAh3slhy9Q3bRvpAUd9+AXhZLH8gTQ3NXiLdqQQHRsX/LmYRsCMNxeZ7jeIkTrC2HUdhYwSB5VvOTShrFpuLdsObggg== X-Google-Smtp-Source: AGHT+IGCnEgguUMJugx6p5xpu8eAH7rP7Nf26hjpjeQ8jah006IY6yM52xhHo/jguAtmHeWE5Ev2 X-Received: by 2002:a50:cd52:0:b0:57a:2eff:8ae with SMTP id 4fb4d7f45d1cf-57c5086b408mr7815176a12.1.1718047340686; Mon, 10 Jun 2024 12:22:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718047340; cv=pass; d=google.com; s=arc-20160816; b=Yoy3wNLl/2PKdWgWC9zspzQ/QE1oZNxzChzAjeUli0tC5gcUITgIfbVgpbPQg5ULYT S9LgOhaj9EuMJbSLpxEiUv7GxrEfpx2v7dnh9JeR9AMzRzYr5Y65XYIIB3DIlIj4LsTy G8L0cljuKmxRVR32UCqh3wj5cNs/AkYXe4mAGbhJcuIlJNXknAp8/KZ1hPbM6tl/yyAK Bi/Q2PI/o4xY2Bsy/kyoz1iTl1lZMMj0/7IRSK4NT4gVkIGXC33PeAijMDO7NMD8Y4R5 Z9UmqKoWexg6D/5vRhiR/KbXm9B6/9MJ5XP/astSWiwoEUn5M5hGcSyTwrZLaXm7O2nV 7UWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=8Fwxg+U8/L4kUngah1ecBeYb5cChpXVPmYuFhLTZliY=; fh=/OjFOSJ868ytMW6qzFEyIKB0fga+RIXQgrFX9SlzvPA=; b=Fi2xXvhAdt476fHwsT/YZDeb4TAxodxHkiAJT7LXRgSC3gM3dBHlZMw6vEgBPxmLTR eksGfDAQfcuJlMg1YGHHiKbgzYtfsFeM9RGvWfkfRl3vuImzRPDo0ITb5Hl9T5zrUmT/ +RMCtSfLc1CHG2Y7KE2eiRTR5q6a+JTQ1MUnQknDNX7UHZeO35sz6q+YQohA/x8HzBYo qnEJXy7lwkxgPNzIqQDO9VsTXCNVoUWtVeKcWwZgrmeLo4vv/zNSwQH8VDp5n05MQPsf xmb6XKAZ/g55oyi4uZH5ZpFnxxpCsu8eZGPoCsr9np53lzRV0xelBP0tTy3gmXr9zIHS 37Hg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OdSLRZxq; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-208789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208789-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57c748ba20fsi2311065a12.113.2024.06.10.12.22.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 12:22:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OdSLRZxq; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-208789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208789-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 65B741F22CCC for ; Mon, 10 Jun 2024 19:22:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2463B14E2FB; Mon, 10 Jun 2024 19:20:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OdSLRZxq" Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA77D14D2A4; Mon, 10 Jun 2024 19:20:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718047258; cv=none; b=EcQSuf31VElppyxPioMqDmLVWxl7/v9tpCcjtzZUttePbfP3HLSAIXFEASWPmfDCTXggtbsb+60ZVroOrMAdF5VKr0bf/5pZbcGuJGg+uaC7FVlRh1tlG0A689Mkee1oUv81a7NuzemJhfF14E4bxw2qmQd0HvI+KBagouuzVII= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718047258; c=relaxed/simple; bh=kt1bp0olkBGzyt6Nu7UC9ZUgmN10seHUl6pXobv+WAo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=gI8veqZopNcA1nWZTeKjzfGUWnATOFR45gwpu58DR3g4jXh0ymx2hdTXIhquAH1jPU5NRg81y1R+OY6o/OCPyJoR/1pi2yY3cVUnOPuar82Rwiz4T4CmxBXSs4J6KSFbW47FBR2kU5De0bJ5nwG7fYlaA/PBykh3S6DN4WYsxpc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OdSLRZxq; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-35f06861ae6so2729340f8f.2; Mon, 10 Jun 2024 12:20:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718047255; x=1718652055; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=8Fwxg+U8/L4kUngah1ecBeYb5cChpXVPmYuFhLTZliY=; b=OdSLRZxqHU2CTmGvdAUHUreIpsGOMoqqUpK4iEI3dIR2IIE3RAsymFMSkWp6gQJAvv 8eJ0Fam8LNh24sYdamtPNg7Pk77CNORwZzE3+1YvuJ8jCAoD+X9beynX9fGjWuw21K+5 gXnAVbeb6vQGA7sgLurqKPE9s/3GevpPCWKUy2+TCyhZs2RxVXpNLjbmQ9rnJJwBggao +aWlUrCSV/zKGBDYG0iOLnetrhvxK/NdagY2OHlYuzCF1DluJFrJu26DUlqCBrH5LyE7 v3dYPTkwOSRm5ZyxEtR1C1KV/961a87bGO6exYMFjc/FExPN2LnpfiR0zKruC92UUU2K +DWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718047255; x=1718652055; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8Fwxg+U8/L4kUngah1ecBeYb5cChpXVPmYuFhLTZliY=; b=B6LsrhR1159jOrhxd+9GuJ9O3HC9wYBpY8BAmrjkA8uNX/QZHpOaSV7987fuaQV4fy a9VMK9V+bvZtNQkMZvwblZp3jIV67X05+cwi7wK6i6rP/86ip1UtWpT5ncICdSA3hdnk zfNFW6vTZlk1eEdSIlt3Dwht9War7MiHMMh21sFvdgzFrw9mgt6CO7oOAacG7wrwWd9v hQBdlETWm+6660llOTfGO4jAfp0Aclb/RqPjGs9DgVApoxzGmZOT9e9I7CQmzhQPKHrP NmH0+4FgoEKN92/vsNZdLqnPyZIT8HuJOzCvEG63YWzMb5QxXmRCldbgK/QfKplf0RSi HlPQ== X-Forwarded-Encrypted: i=1; AJvYcCWcgomRAYvXI1EMhxWX55BQJT/B8gK98cocetPOrOOG54jwW6gDzAiR20HlgysNcpUA197tAlCxzF30MWy0pxSdXlx6ymwLkdiwpw5P0jIY3ymYDOFyXJIcdpxvN96g/AhMC2eizwB9MKe2f1a9 X-Gm-Message-State: AOJu0YwnBMGF3+YmOZvZwtI9uM2VDhIQv0H0wqNdhbOQjw6sfsptbg7r 07iRncfp7em43mzL/NUIke95XgS61ryX7ch5PgG29zpzNkzxPmIKIvKCCD9M X-Received: by 2002:adf:e9c5:0:b0:35f:9d8:9a48 with SMTP id ffacd0b85a97d-35f09d89bfemr5721228f8f.56.1718047254956; Mon, 10 Jun 2024 12:20:54 -0700 (PDT) Received: from [10.5.1.156] (188-22-218-164.adsl.highway.telekom.at. [188.22.218.164]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f25dc3f07sm2824329f8f.79.2024.06.10.12.20.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jun 2024 12:20:54 -0700 (PDT) Message-ID: <06f85db2-10b2-4921-b3d6-0061e9801e0c@gmail.com> Date: Mon, 10 Jun 2024 21:20:53 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] kunit/overflow: Adjust for __counted_by with DEFINE_RAW_FLEX() To: Kees Cook , "Gustavo A. R. Silva" Cc: Nathan Chancellor , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org References: <20240610182301.work.272-kees@kernel.org> Content-Language: en-US From: Christian Schrefl In-Reply-To: <20240610182301.work.272-kees@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 10.06.24 8:23 PM, Kees Cook wrote: > When a flexible array structure has a __counted_by annotation, its use > with DEFINE_RAW_FLEX() will result in the count being zero-initialized. > This is expected since one doesn't want to use RAW with a counted_by > struct. Adjust the tests to check for the condition and for compiler > support. > > Reported-by: Christian Schrefl > Closes: https://lore.kernel.org/all/0bfc6b38-8bc5-4971-b6fb-dc642a73fbfe@gmail.com/ > Suggested-by: Nathan Chancellor > Signed-off-by: Kees Cook > --- Thanks for the quick fix! Seems reasonable and the test passes now. Tested-by: Christian Schrefl Reviewed-by: Christian Schrefl > Cc: "Gustavo A. R. Silva" > Cc: linux-hardening@vger.kernel.org > --- > lib/overflow_kunit.c | 20 +++++++++++++++++--- > 1 file changed, 17 insertions(+), 3 deletions(-) > > diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c > index 4ef31b0bb74d..d305b0c054bb 100644 > --- a/lib/overflow_kunit.c > +++ b/lib/overflow_kunit.c > @@ -1178,14 +1178,28 @@ struct foo { > s16 array[] __counted_by(counter); > }; > > +struct bar { > + int a; > + u32 counter; > + s16 array[]; > +}; > + > static void DEFINE_FLEX_test(struct kunit *test) > { > - DEFINE_RAW_FLEX(struct foo, two, array, 2); > + /* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */ > + DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2); > +#if __has_attribute(__counted_by__) > + int expected_raw_size = sizeof(struct foo); > +#else > + int expected_raw_size = sizeof(struct foo) + 2 * sizeof(s16); > +#endif > + /* Without annotation, it will always be on-stack size. */ > + DEFINE_RAW_FLEX(struct bar, two, array, 2); > DEFINE_FLEX(struct foo, eight, array, counter, 8); > DEFINE_FLEX(struct foo, empty, array, counter, 0); > > - KUNIT_EXPECT_EQ(test, __struct_size(two), > - sizeof(struct foo) + sizeof(s16) + sizeof(s16)); > + KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), expected_raw_size); > + KUNIT_EXPECT_EQ(test, __struct_size(two), sizeof(struct bar) + 2 * sizeof(s16)); > KUNIT_EXPECT_EQ(test, __struct_size(eight), 24); > KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo)); > }