linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region
       [not found] <20210818060533.3569517-1-keescook@chromium.org>
@ 2021-08-18  6:04 ` Kees Cook
  2021-08-20 13:01   ` Jiri Kosina
  2021-08-18  6:05 ` [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event Kees Cook
  1 sibling, 1 reply; 9+ messages in thread
From: Kees Cook @ 2021-08-18  6:04 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, Jiri Kosina, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, netdev, dri-devel, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

In preparation for FORTIFY_SOURCE performing compile-time and run-time
field bounds checking for memcpy(), memmove(), and memset(), avoid
intentionally writing across neighboring fields.

Use struct_group() in struct cp2112_string_report around members report,
length, type, and string, so they can be referenced together. This will
allow memcpy() and sizeof() to more easily reason about sizes, improve
readability, and avoid future warnings about writing beyond the end of
report.

"pahole" shows no size nor member offset changes to struct
cp2112_string_report.  "objdump -d" shows no meaningful object
code changes (i.e. only source line number induced differences.)

Cc: Jiri Kosina <jikos@kernel.org>
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cc: linux-input@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/hid/hid-cp2112.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
index 477baa30889c..ece147d1a278 100644
--- a/drivers/hid/hid-cp2112.c
+++ b/drivers/hid/hid-cp2112.c
@@ -129,10 +129,12 @@ struct cp2112_xfer_status_report {
 
 struct cp2112_string_report {
 	u8 dummy;		/* force .string to be aligned */
-	u8 report;		/* CP2112_*_STRING */
-	u8 length;		/* length in bytes of everyting after .report */
-	u8 type;		/* USB_DT_STRING */
-	wchar_t string[30];	/* UTF16_LITTLE_ENDIAN string */
+	struct_group_attr(contents, __packed,
+		u8 report;		/* CP2112_*_STRING */
+		u8 length;		/* length in bytes of everything after .report */
+		u8 type;		/* USB_DT_STRING */
+		wchar_t string[30];	/* UTF16_LITTLE_ENDIAN string */
+	);
 } __packed;
 
 /* Number of times to request transfer status before giving up waiting for a
@@ -986,8 +988,8 @@ static ssize_t pstr_show(struct device *kdev,
 	u8 length;
 	int ret;
 
-	ret = cp2112_hid_get(hdev, attr->report, &report.report,
-			     sizeof(report) - 1, HID_FEATURE_REPORT);
+	ret = cp2112_hid_get(hdev, attr->report, (u8 *)&report.contents,
+			     sizeof(report.contents), HID_FEATURE_REPORT);
 	if (ret < 3) {
 		hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
 			ret);
-- 
2.30.2


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

* [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
       [not found] <20210818060533.3569517-1-keescook@chromium.org>
  2021-08-18  6:04 ` [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region Kees Cook
@ 2021-08-18  6:05 ` Kees Cook
  2021-08-20 13:02   ` Jiri Kosina
  1 sibling, 1 reply; 9+ messages in thread
From: Kees Cook @ 2021-08-18  6:05 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, Stefan Achatz, Jiri Kosina, Benjamin Tissoires,
	linux-input, Gustavo A. R. Silva, Greg Kroah-Hartman,
	Andrew Morton, linux-wireless, netdev, dri-devel, linux-staging,
	linux-block, linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

In preparation for FORTIFY_SOURCE performing compile-time and run-time
field bounds checking for memset(), avoid intentionally writing across
neighboring fields.

Add struct_group() to mark region of struct kone_mouse_event that should
be initialized to zero.

Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cc: linux-input@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/hid/hid-roccat-kone.c |  2 +-
 drivers/hid/hid-roccat-kone.h | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-roccat-kone.c b/drivers/hid/hid-roccat-kone.c
index 1ca64481145e..ea17abc7ad52 100644
--- a/drivers/hid/hid-roccat-kone.c
+++ b/drivers/hid/hid-roccat-kone.c
@@ -857,7 +857,7 @@ static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
 		memcpy(&kone->last_mouse_event, event,
 				sizeof(struct kone_mouse_event));
 	else
-		memset(&event->tilt, 0, 5);
+		memset(&event->wipe, 0, sizeof(event->wipe));
 
 	kone_keep_values_up_to_date(kone, event);
 
diff --git a/drivers/hid/hid-roccat-kone.h b/drivers/hid/hid-roccat-kone.h
index 4a1a9cb76b08..65c800e3addc 100644
--- a/drivers/hid/hid-roccat-kone.h
+++ b/drivers/hid/hid-roccat-kone.h
@@ -152,11 +152,13 @@ struct kone_mouse_event {
 	uint16_t x;
 	uint16_t y;
 	uint8_t wheel; /* up = 1, down = -1 */
-	uint8_t tilt; /* right = 1, left = -1 */
-	uint8_t unknown;
-	uint8_t event;
-	uint8_t value; /* press = 0, release = 1 */
-	uint8_t macro_key; /* 0 to 8 */
+	struct_group(wipe,
+		uint8_t tilt; /* right = 1, left = -1 */
+		uint8_t unknown;
+		uint8_t event;
+		uint8_t value; /* press = 0, release = 1 */
+		uint8_t macro_key; /* 0 to 8 */
+	);
 } __attribute__ ((__packed__));
 
 enum kone_mouse_events {
-- 
2.30.2


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

* Re: [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region
  2021-08-18  6:04 ` [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region Kees Cook
@ 2021-08-20 13:01   ` Jiri Kosina
  2021-08-20 15:48     ` Kees Cook
  0 siblings, 1 reply; 9+ messages in thread
From: Jiri Kosina @ 2021-08-20 13:01 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, netdev, dri-devel, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Tue, 17 Aug 2021, Kees Cook wrote:

> In preparation for FORTIFY_SOURCE performing compile-time and run-time
> field bounds checking for memcpy(), memmove(), and memset(), avoid
> intentionally writing across neighboring fields.
> 
> Use struct_group() in struct cp2112_string_report around members report,
> length, type, and string, so they can be referenced together. This will
> allow memcpy() and sizeof() to more easily reason about sizes, improve
> readability, and avoid future warnings about writing beyond the end of
> report.
> 
> "pahole" shows no size nor member offset changes to struct
> cp2112_string_report.  "objdump -d" shows no meaningful object
> code changes (i.e. only source line number induced differences.)
> 
> Cc: Jiri Kosina <jikos@kernel.org>
> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> Cc: linux-input@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>

Applied, thanks.

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
  2021-08-18  6:05 ` [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event Kees Cook
@ 2021-08-20 13:02   ` Jiri Kosina
       [not found]     ` <CAJr-aD=6-g7VRw2Hw0dhs+RrtA=Tago5r6Dukfw_gGPB0YYKOQ@mail.gmail.com>
  0 siblings, 1 reply; 9+ messages in thread
From: Jiri Kosina @ 2021-08-20 13:02 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, Stefan Achatz, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, netdev, dri-devel, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Tue, 17 Aug 2021, Kees Cook wrote:

> In preparation for FORTIFY_SOURCE performing compile-time and run-time
> field bounds checking for memset(), avoid intentionally writing across
> neighboring fields.
> 
> Add struct_group() to mark region of struct kone_mouse_event that should
> be initialized to zero.
> 
> Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
> Cc: Jiri Kosina <jikos@kernel.org>
> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> Cc: linux-input@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>

Applied, thank you Kees.

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
       [not found]     ` <CAJr-aD=6-g7VRw2Hw0dhs+RrtA=Tago5r6Dukfw_gGPB0YYKOQ@mail.gmail.com>
@ 2021-08-20 15:27       ` Jiri Kosina
  2021-08-20 15:49         ` Kees Cook
  2021-08-20 15:57         ` Kees Cook
  0 siblings, 2 replies; 9+ messages in thread
From: Jiri Kosina @ 2021-08-20 15:27 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, Stefan Achatz, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, Network Development,
	Maling list - DRI developers, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Fri, 20 Aug 2021, Kees Cook wrote:

> > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > field bounds checking for memset(), avoid intentionally writing across
> > > neighboring fields.
> > >
> > > Add struct_group() to mark region of struct kone_mouse_event that should
> > > be initialized to zero.
> > >
> > > Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
> > > Cc: Jiri Kosina <jikos@kernel.org>
> > > Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > > Cc: linux-input@vger.kernel.org
> > > Signed-off-by: Kees Cook <keescook@chromium.org>
> >
> > Applied, thank you Kees.
> >
> 
> Eek! No, this will break the build: struct_group() is not yet in the tree.
> I can carry this with an Ack, etc.

I was pretty sure I saw struct_group() already in linux-next, but that was 
apparently a vacation-induced brainfart, sorry. Dropping.

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region
  2021-08-20 13:01   ` Jiri Kosina
@ 2021-08-20 15:48     ` Kees Cook
  0 siblings, 0 replies; 9+ messages in thread
From: Kees Cook @ 2021-08-20 15:48 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: linux-kernel, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, netdev, dri-devel, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Fri, Aug 20, 2021 at 03:01:43PM +0200, Jiri Kosina wrote:
> On Tue, 17 Aug 2021, Kees Cook wrote:
> 
> > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > field bounds checking for memcpy(), memmove(), and memset(), avoid
> > intentionally writing across neighboring fields.
> > 
> > Use struct_group() in struct cp2112_string_report around members report,
> > length, type, and string, so they can be referenced together. This will
> > allow memcpy() and sizeof() to more easily reason about sizes, improve
> > readability, and avoid future warnings about writing beyond the end of
> > report.
> > 
> > "pahole" shows no size nor member offset changes to struct
> > cp2112_string_report.  "objdump -d" shows no meaningful object
> > code changes (i.e. only source line number induced differences.)
> > 
> > Cc: Jiri Kosina <jikos@kernel.org>
> > Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > Cc: linux-input@vger.kernel.org
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> 
> Applied, thanks.

I'm not sure if my other HTML email got through, but please don't apply
these to separate trees -- struct_group() is introduced as part of this
series.

-- 
Kees Cook

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

* Re: [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
  2021-08-20 15:27       ` Jiri Kosina
@ 2021-08-20 15:49         ` Kees Cook
  2021-08-20 15:57         ` Kees Cook
  1 sibling, 0 replies; 9+ messages in thread
From: Kees Cook @ 2021-08-20 15:49 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: LKML, Stefan Achatz, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, Network Development,
	Maling list - DRI developers, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Fri, Aug 20, 2021 at 05:27:35PM +0200, Jiri Kosina wrote:
> On Fri, 20 Aug 2021, Kees Cook wrote:
> 
> > > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > > field bounds checking for memset(), avoid intentionally writing across
> > > > neighboring fields.
> > > >
> > > > Add struct_group() to mark region of struct kone_mouse_event that should
> > > > be initialized to zero.
> > > >
> > > > Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
> > > > Cc: Jiri Kosina <jikos@kernel.org>
> > > > Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > > > Cc: linux-input@vger.kernel.org
> > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > >
> > > Applied, thank you Kees.
> > >
> > 
> > Eek! No, this will break the build: struct_group() is not yet in the tree.
> > I can carry this with an Ack, etc.
> 
> I was pretty sure I saw struct_group() already in linux-next, but that was 
> apparently a vacation-induced brainfart, sorry. Dropping.

Cool, no worries. Sorry for the confusion!

-- 
Kees Cook

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

* Re: [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
  2021-08-20 15:27       ` Jiri Kosina
  2021-08-20 15:49         ` Kees Cook
@ 2021-08-20 15:57         ` Kees Cook
  2021-08-20 16:11           ` Jiri Kosina
  1 sibling, 1 reply; 9+ messages in thread
From: Kees Cook @ 2021-08-20 15:57 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: LKML, Stefan Achatz, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, Network Development,
	Maling list - DRI developers, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Fri, Aug 20, 2021 at 05:27:35PM +0200, Jiri Kosina wrote:
> On Fri, 20 Aug 2021, Kees Cook wrote:
> 
> > > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > > field bounds checking for memset(), avoid intentionally writing across
> > > > neighboring fields.
> > > >
> > > > Add struct_group() to mark region of struct kone_mouse_event that should
> > > > be initialized to zero.
> > > >
> > > > Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
> > > > Cc: Jiri Kosina <jikos@kernel.org>
> > > > Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > > > Cc: linux-input@vger.kernel.org
> > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > >
> > > Applied, thank you Kees.
> > >
> > 
> > Eek! No, this will break the build: struct_group() is not yet in the tree.
> > I can carry this with an Ack, etc.
> 
> I was pretty sure I saw struct_group() already in linux-next, but that was 
> apparently a vacation-induced brainfart, sorry. Dropping.

Oh, for these two patches, can I add your Acked-by while I carry them?

Thanks!

-- 
Kees Cook

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

* Re: [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event
  2021-08-20 15:57         ` Kees Cook
@ 2021-08-20 16:11           ` Jiri Kosina
  0 siblings, 0 replies; 9+ messages in thread
From: Jiri Kosina @ 2021-08-20 16:11 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, Stefan Achatz, Benjamin Tissoires, linux-input,
	Gustavo A. R. Silva, Greg Kroah-Hartman, Andrew Morton,
	linux-wireless, Network Development,
	Maling list - DRI developers, linux-staging, linux-block,
	linux-kbuild, clang-built-linux, Rasmus Villemoes,
	linux-hardening

On Fri, 20 Aug 2021, Kees Cook wrote:

> > > > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > > > field bounds checking for memset(), avoid intentionally writing across
> > > > > neighboring fields.
> > > > >
> > > > > Add struct_group() to mark region of struct kone_mouse_event that should
> > > > > be initialized to zero.
> > > > >
> > > > > Cc: Stefan Achatz <erazor_de@users.sourceforge.net>
> > > > > Cc: Jiri Kosina <jikos@kernel.org>
> > > > > Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > > > > Cc: linux-input@vger.kernel.org
> > > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > >
> > > > Applied, thank you Kees.
> > > >
> > > 
> > > Eek! No, this will break the build: struct_group() is not yet in the tree.
> > > I can carry this with an Ack, etc.
> > 
> > I was pretty sure I saw struct_group() already in linux-next, but that was 
> > apparently a vacation-induced brainfart, sorry. Dropping.
> 
> Oh, for these two patches, can I add your Acked-by while I carry them?

Yes, thanks, and sorry for the noise.

-- 
Jiri Kosina
SUSE Labs


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

end of thread, other threads:[~2021-08-20 16:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20210818060533.3569517-1-keescook@chromium.org>
2021-08-18  6:04 ` [PATCH v2 22/63] HID: cp2112: Use struct_group() for memcpy() region Kees Cook
2021-08-20 13:01   ` Jiri Kosina
2021-08-20 15:48     ` Kees Cook
2021-08-18  6:05 ` [PATCH v2 55/63] HID: roccat: Use struct_group() to zero kone_mouse_event Kees Cook
2021-08-20 13:02   ` Jiri Kosina
     [not found]     ` <CAJr-aD=6-g7VRw2Hw0dhs+RrtA=Tago5r6Dukfw_gGPB0YYKOQ@mail.gmail.com>
2021-08-20 15:27       ` Jiri Kosina
2021-08-20 15:49         ` Kees Cook
2021-08-20 15:57         ` Kees Cook
2021-08-20 16:11           ` Jiri Kosina

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).