linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: core: Correctly handle ReportSize being zero
@ 2020-08-29 11:26 Marc Zyngier
  2020-09-01  8:14 ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Zyngier @ 2020-08-29 11:26 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel, stable, kernel-team

It appears that a ReportSize value of zero is legal, even if a bit
non-sensical. Most of the HID code seems to handle that gracefully,
except when computing the total size in bytes. When fed as input to
memset, this leads to some funky outcomes.

Detect the corner case and correctly compute the size.

Cc: stable@vger.kernel.org
Signed-off-by: Marc Zyngier <maz@kernel.org>
---
 drivers/hid/hid-core.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 359616e3efbb..d2ecc9c45255 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1597,6 +1597,17 @@ static void hid_output_field(const struct hid_device *hid,
 	}
 }
 
+/*
+ * Compute the size of a report.
+ */
+static size_t hid_compute_report_size(struct hid_report *report)
+{
+	if (report->size)
+		return ((report->size - 1) >> 3) + 1;
+
+	return 0;
+}
+
 /*
  * Create a report. 'data' has to be allocated using
  * hid_alloc_report_buf() so that it has proper size.
@@ -1609,7 +1620,7 @@ void hid_output_report(struct hid_report *report, __u8 *data)
 	if (report->id > 0)
 		*data++ = report->id;
 
-	memset(data, 0, ((report->size - 1) >> 3) + 1);
+	memset(data, 0, hid_compute_report_size(report));
 	for (n = 0; n < report->maxfield; n++)
 		hid_output_field(report->device, report->field[n], data);
 }
@@ -1739,7 +1750,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
 		csize--;
 	}
 
-	rsize = ((report->size - 1) >> 3) + 1;
+	rsize = hid_compute_report_size(report);
 
 	if (report_enum->numbered && rsize >= HID_MAX_BUFFER_SIZE)
 		rsize = HID_MAX_BUFFER_SIZE - 1;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] HID: core: Correctly handle ReportSize being zero
  2020-08-29 11:26 [PATCH] HID: core: Correctly handle ReportSize being zero Marc Zyngier
@ 2020-09-01  8:14 ` Jiri Kosina
  2020-09-01  9:34   ` Benjamin Tissoires
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Kosina @ 2020-09-01  8:14 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Benjamin Tissoires, linux-input, linux-kernel, stable, kernel-team

On Sat, 29 Aug 2020, Marc Zyngier wrote:

> It appears that a ReportSize value of zero is legal, even if a bit
> non-sensical. Most of the HID code seems to handle that gracefully,
> except when computing the total size in bytes. When fed as input to
> memset, this leads to some funky outcomes.
> 
> Detect the corner case and correctly compute the size.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Marc Zyngier <maz@kernel.org>

Thanks Marc; Benjamin will be pushing this patch through his regression 
testing machinery, and if all is good, I'll push it for 5.9-rc still.

-- 
Jiri Kosina
SUSE Labs


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] HID: core: Correctly handle ReportSize being zero
  2020-09-01  8:14 ` Jiri Kosina
@ 2020-09-01  9:34   ` Benjamin Tissoires
  0 siblings, 0 replies; 3+ messages in thread
From: Benjamin Tissoires @ 2020-09-01  9:34 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Marc Zyngier, open list:HID CORE LAYER, lkml, 3.8+, kernel-team

On Tue, Sep 1, 2020 at 10:14 AM Jiri Kosina <jikos@kernel.org> wrote:
>
> On Sat, 29 Aug 2020, Marc Zyngier wrote:
>
> > It appears that a ReportSize value of zero is legal, even if a bit
> > non-sensical. Most of the HID code seems to handle that gracefully,
> > except when computing the total size in bytes. When fed as input to
> > memset, this leads to some funky outcomes.
> >
> > Detect the corner case and correctly compute the size.
> >
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Marc Zyngier <maz@kernel.org>
>
> Thanks Marc; Benjamin will be pushing this patch through his regression
> testing machinery, and if all is good, I'll push it for 5.9-rc still.

Test results were good. I have now pushed this patch to for-5.9/upstream-fixes

Cheers,
Benjamin

>
> --
> Jiri Kosina
> SUSE Labs
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-01  9:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-29 11:26 [PATCH] HID: core: Correctly handle ReportSize being zero Marc Zyngier
2020-09-01  8:14 ` Jiri Kosina
2020-09-01  9:34   ` Benjamin Tissoires

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).