From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 0B5243382E1 for ; Fri, 21 Aug 2026 13:50:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787320223; cv=none; b=iJTEoomqYLjUQsqeQyXrlYur2OwjeCV+gH3uflwbpVcd7uxOmKB0Oeh1HS7uuPnmGZWLacMOP5dvkxC0aXYBPDf/hkLBFixHI3yLVzIJZ7dnRSNFJzQBFR9wKgGTcohW6U8ggW6yl3TRgeQax2IrA1o89mMAjmpDUjwAvoK7Q8g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787320223; c=relaxed/simple; bh=YIE/n3/7/5T1bcjR6AsdGCclXRfzoqcmYgqcJu2FMsA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Nw988H/9G6vadA1orc8aUf9n3Ssge0GyeRPRgvNs0oeE/tLto2VQL0qxWN76z8PYtLgZaI4fG8xCU/WyLct+vlQZjgeK/ZHubTUAUb9oizDeZMCY1MD0yVxB6jc5OaidUZ7KV+BozmIkz6jKHsxu5Cb2LIC0UTGhOHY9fNuEgKs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PVc6G3/A; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PVc6G3/A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA44C1F000E9; Fri, 21 Aug 2026 13:50:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787320221; bh=luuTjYAaQzZdKrmBN4Z6KkllMK6sR0OQrihtv2xaHyU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=PVc6G3/AWbgh7fykZnA3PHlonwSjBoZ8uzHXp35qdJA15/vH4kuiMhMGq0ePg2Uam W4ul4gh0chx5p81jfSx/Us+OTIIwV9ia4nO1aJq+gDwc9TzpeuaG6VQh66b54Jpvlq on8XJzwgQR4PG6JOSshfeXIBgI6mRbVEmsse3UsaNKDqorIkbJl+oOs4zZeTJzhT3i +hhyBc3voDN+iy9nR7ApkWBJnNYb7uC1ei9kB2nyPOJ7xjj8PKOkhCVR7NacLKwvSY w2kyBWmHxJz6AIj7Icv+H52X+DCQSJta14haxZFbKEW80pWPoNi27QT9ARtvQYlWQ1 Lxn0YrrCnmLfg== Date: Fri, 21 Aug 2026 15:50:17 +0200 From: Benjamin Tissoires To: Jiri Kosina Cc: =?utf-8?B?SsO8cmdlbiBHcm/Dnw==?= , lkml , michael.bommarito@gmail.com, longli@microsoft.com, decui@microsoft.com, wei.liu@kernel.org, haiyangz@microsoft.com, kys@microsoft.com Subject: Re: [PATCH v2] HID: hyperv: make pointer arithmetics understandable for FORTIFY_SOURCE Message-ID: References: <9b35a830-7497-4d1f-b292-5a7594289931@suse.com> <6r2on01s-03n0-5r0n-0s21-n7q5r86o71np@xreary.bet> <53b147e8-cc10-4d49-9d19-ae52288123a3@suse.com> <776pn42o-p481-6p04-n03p-291305n30569@xreary.bet> <6s739024-9652-16r2-270q-q2026q5157q3@xreary.bet> <662f5ab3-8be8-4df6-a92c-a402bdd512df@suse.com> <746q4npo-1s59-4824-q633-9qr343no4369@xreary.bet> <17q6s829-78pr-nrq1-qrr7-9qo9n750pq63@xreary.bet> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <17q6s829-78pr-nrq1-qrr7-9qo9n750pq63@xreary.bet> On Aug 21 2026, Jiri Kosina wrote: > On Fri, 21 Aug 2026, Benjamin Tissoires wrote: > > > > - report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength; > > > + report = (u8 *)info + offsetof(struct synthhid_device_info, hid_descriptor) + info->hid_descriptor.bLength; > > > > Isn't that overcomplicated?: > > > > Above we have: > > struct synthhid_device_info { > > struct synthhid_msg_hdr header; > > struct hv_input_dev_info hid_dev_info; > > struct hid_descriptor hid_descriptor; > > }; > > ... > > struct synthhid_device_info *info; > > ... > > info = kunit_kzalloc(test, sizeof(*info) + 4, GFP_KERNEL); > > > > so info is 0 allocated with the sizeof(struct synthhid_device_info) plus > > 4 for the report (immediately after). > > > > with the bLength being set a couple of lines above, we basically have: > > + report = (u8 *)info + offsetof(struct synthhid_device_info, hid_descriptor) + sizeof(struct hid_descriptor); > > > > So pointer address + offset of the last field + size of the last field. > > > > Isn't that equivalent to: > > report = (u8 *)info + sizeof(*info); > > > > or even: `(u8 *)(info + 1)`? > > I considered that one as well, but then I decided to keep what the > original author intended as perhaps it's more self-explanatory what we're > actually doing. But the argument that whenever a new field is added after > hid_descriptor, this will silently break, is convicing enough :) Yep, that's a killer argument :) Acked-by: Benjamin Tissoires Cheers, Benjamin > > Thanks. v2 below. > > Juergen, could you please re-Ack your Tested-by: on this? For some reason, > my allyesconfig with gcc-15 doesn't trigger it. > > > > > From: Jiri Kosina > Subject: [PATCH] HID: hyperv: make pointer arithmetics understandable for FORTIFY_SOURCE > > Commit 83df7b5fa6735b5084ecd2 ("HID: hyperv: add KUnit coverage for device info > bounds") introduced this piece of code > > report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength; > memset(report, 0x42, 4); > > to populate the report descriptor, making use of the fact that the report > &info->hid_descriptor points to a struct hid_descriptor (which is a fixed-size struct). > GCC's FORTIFY_SOURCE infer the object size from that specific struct field rather than the > outer dynamically allocated info buffer. As a result, writing past sizeof(struct hid_descriptor) > triggers the __write_overflow_field warning. > > Calculate the pointer offset using info directly, so the compiler evaluates the > memory bounds against the allocated flexible layout of struct > synthhid_device_info instead of the nested struct. > > Fixes: 83df7b5fa6735b5084ecd2 ("HID: hyperv: add KUnit coverage for device info bounds") > Reported-by: Jürgen Groß > Signed-off-by: Jiri Kosina > --- > drivers/hid/hid-hyperv.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/hid/hid-hyperv.c b/drivers/hid/hid-hyperv.c > index 6579bd19da13..cfc061dbdd24 100644 > --- a/drivers/hid/hid-hyperv.c > +++ b/drivers/hid/hid-hyperv.c > @@ -687,7 +687,7 @@ static void mousevsc_device_info_valid_descriptor(struct kunit *test) > > info->hid_descriptor.bLength = sizeof(struct hid_descriptor); > info->hid_descriptor.rpt_desc.wDescriptorLength = cpu_to_le16(4); > - report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength; > + report = (u8 *)(info + 1); > memset(report, 0x42, 4); > > mousevsc_on_receive_device_info(input_dev, info, sizeof(*info) + 4); > @@ -713,7 +713,7 @@ static void mousevsc_device_info_report_desc_oob(struct kunit *test) > > info->hid_descriptor.bLength = sizeof(struct hid_descriptor); > info->hid_descriptor.rpt_desc.wDescriptorLength = cpu_to_le16(64); > - report = ((u8 *)&info->hid_descriptor) + info->hid_descriptor.bLength; > + report = (u8 *)(info + 1); > memset(report, 0x42, 8); > > mousevsc_on_receive_device_info(input_dev, info, sizeof(*info) + 8); > > -- > Jiri Kosina > SUSE Labs > >