public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] greybus: Fix null pointer dereference in gb_operation_response_send()
@ 2024-10-27  7:53 Suraj Sonawane
  2024-10-27 13:57 ` Peter Seiderer
  2024-10-27 14:00 ` Alex Elder
  0 siblings, 2 replies; 5+ messages in thread
From: Suraj Sonawane @ 2024-10-27  7:53 UTC (permalink / raw)
  To: johan, elder, gregkh; +Cc: greybus-dev, linux-kernel, Suraj Sonawane

Fix an issue detected by the Smatch static tool:
drivers/greybus/operation.c:852 gb_operation_response_send() error:
we previously assumed 'operation->response' could be null (see line 829)

The issue occurs because 'operation->response' may be null if the
response allocation fails at line 829. However, the code tries to
access 'operation->response->header' at line 852 without checking if
it was successfully allocated. This can cause a crash if 'response'
is null.

To fix this, add a check to ensure 'operation->response' is not null
before accessing its header. If the response is null, log an error
message and return -ENOMEM to stop further processing, preventing
any crashes or undefined behavior.

Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
---
 drivers/greybus/operation.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/greybus/operation.c b/drivers/greybus/operation.c
index 8459e9bc0..521899fbc 100644
--- a/drivers/greybus/operation.c
+++ b/drivers/greybus/operation.c
@@ -849,7 +849,13 @@ static int gb_operation_response_send(struct gb_operation *operation,
 		goto err_put;
 
 	/* Fill in the response header and send it */
-	operation->response->header->result = gb_operation_errno_map(errno);
+	if (operation->response) {
+		operation->response->header->result = gb_operation_errno_map(errno);
+	} else {
+		dev_err(&connection->hd->dev, "failed to allocate response\n");
+		ret = -ENOMEM;
+		goto err_put_active;
+	}
 
 	ret = gb_message_send(operation->response, GFP_KERNEL);
 	if (ret)
-- 
2.34.1


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

end of thread, other threads:[~2024-10-28 12:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-27  7:53 [PATCH] greybus: Fix null pointer dereference in gb_operation_response_send() Suraj Sonawane
2024-10-27 13:57 ` Peter Seiderer
2024-10-28 12:39   ` Suraj Sonawane
2024-10-27 14:00 ` Alex Elder
2024-10-28 12:43   ` Suraj Sonawane

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