mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] greybus: operation: Fix NULL pointer dereference in gb_operation_message_alloc()
@ 2026-08-28 10:00 Yang Zi
  2026-08-28 12:21 ` Johan Hovold
  0 siblings, 1 reply; 2+ messages in thread
From: Yang Zi @ 2026-08-28 10:00 UTC (permalink / raw)
  To: johan; +Cc: elder, gregkh, greybus-dev, linux-kernel, 2959243019

gb_connection_recv() accepts a received message whose advertised size is
smaller than struct gb_operation_msg_hdr.  In particular, a header with a
size of zero passes the incomplete-message check and reaches
gb_operation_create_incoming().

This issue was found using a locally modified syzkaller.  The
analysis and fix were assisted by GPT-5.6.

The subtraction used to derive the request payload size then underflows.
When gb_operation_message_alloc() adds the header size, the result wraps
to zero, bypassing the maximum-buffer-size check.  kzalloc(0) returns
ZERO_SIZE_PTR and gb_operation_message_init() subsequently dereferences
it.

Reject advertised sizes smaller than the message header.  Also check the
payload size before adding the header size, so that the size calculation
cannot wrap and bypass the buffer-size limit.

Fixes: 87d208feb74f ("greybus: embed message buffer into message structure")
Assisted-by: Codex:gpt-5.6
Signed-off-by: Yang Zi <2959243019@qq.com>
---
v2:
- Add the syzkaller provenance and Assisted-by trailer.
- Regenerate the patch for git-send-email.
- Verify the received patch with git am and checkpatch.

 drivers/greybus/operation.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/greybus/operation.c b/drivers/greybus/operation.c
index 7e12ffb2dd60..c3d51176c373 100644
--- a/drivers/greybus/operation.c
+++ b/drivers/greybus/operation.c
@@ -364,14 +364,21 @@ gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
 {
 	struct gb_message *message;
 	struct gb_operation_msg_hdr *header;
-	size_t message_size = payload_size + sizeof(*header);
+	size_t message_size;
 
-	if (message_size > hd->buffer_size_max) {
+	/*
+	 * Reject a payload size that would make the total message size
+	 * overflow, before it wraps around and bypasses the maximum
+	 * buffer size check.
+	 */
+	if (payload_size > hd->buffer_size_max - sizeof(*header)) {
 		dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
-			 message_size, hd->buffer_size_max);
+			 payload_size, hd->buffer_size_max - sizeof(*header));
 		return NULL;
 	}
 
+	message_size = payload_size + sizeof(*header);
+
 	/* Allocate the message structure and buffer. */
 	message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
 	if (!message)
@@ -1047,6 +1054,11 @@ void gb_connection_recv(struct gb_connection *connection,
 	/* Use memcpy as data may be unaligned */
 	memcpy(&header, data, sizeof(header));
 	msg_size = le16_to_cpu(header.size);
+	if (msg_size < sizeof(header)) {
+		dev_err_ratelimited(dev, "%s: short message received (%zu < %zu)\n",
+				    connection->name, msg_size, sizeof(header));
+		return;
+	}
 	if (size < msg_size) {
 		dev_err_ratelimited(dev,
 				    "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
-- 
2.50.1


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

end of thread, other threads:[~2026-08-28 12:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-28 10:00 [PATCH v2] greybus: operation: Fix NULL pointer dereference in gb_operation_message_alloc() Yang Zi
2026-08-28 12:21 ` Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®