All of lore.kernel.org
 help / color / mirror / Atom feed
From: Haowen Bai <baihaowen@meizu.com>
To: <linux@roeck-us.net>, <heikki.krogerus@linux.intel.com>,
	<gregkh@linuxfoundation.org>
Cc: <linux-usb@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	Haowen Bai <baihaowen@meizu.com>
Subject: [PATCH] usb: typec: tcpm: testing array offset 'port->logbuffer_head' before use
Date: Fri, 25 Mar 2022 12:34:09 +0800	[thread overview]
Message-ID: <1648182849-9209-1-git-send-email-baihaowen@meizu.com> (raw)

Fix possible indexing array of bound for 
port->logbuffer[port->logbuffer_head], where port->logbuffer_head boundary
 check happens later. so we do it before.

Signed-off-by: Haowen Bai <baihaowen@meizu.com>
---
 drivers/usb/typec/tcpm/tcpm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 5fce795..541e9e4 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -591,6 +591,14 @@ static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
 	unsigned long rem_nsec;
 
 	mutex_lock(&port->logbuffer_lock);
+
+	if (port->logbuffer_head < 0 ||
+	    port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
+		dev_warn(port->dev,
+			 "Bad log buffer index %d\n", port->logbuffer_head);
+		goto abort;
+	}
+
 	if (!port->logbuffer[port->logbuffer_head]) {
 		port->logbuffer[port->logbuffer_head] =
 				kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
@@ -607,13 +615,6 @@ static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
 		strcpy(tmpbuffer, "overflow");
 	}
 
-	if (port->logbuffer_head < 0 ||
-	    port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
-		dev_warn(port->dev,
-			 "Bad log buffer index %d\n", port->logbuffer_head);
-		goto abort;
-	}
-
 	if (!port->logbuffer[port->logbuffer_head]) {
 		dev_warn(port->dev,
 			 "Log buffer index %d is NULL\n", port->logbuffer_head);
-- 
2.7.4


             reply	other threads:[~2022-03-25  4:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-25  4:34 Haowen Bai [this message]
2022-03-25  4:45 ` [PATCH] usb: typec: tcpm: testing array offset 'port->logbuffer_head' before use Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1648182849-9209-1-git-send-email-baihaowen@meizu.com \
    --to=baihaowen@meizu.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.