public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Xunlei Pang <xlpang@linux.alibaba.com>
To: "James E . J . Bottomley" <jejb@linux.ibm.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Hannes Reinecke <hare@suse.com>,
	Qingming Su <qingming.su@linux.alibaba.com>
Cc: linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: [PATCH] scsi: ses: Fix out-of-bound memory write
Date: Thu,  8 Jul 2021 15:51:41 +0800	[thread overview]
Message-ID: <20210708075141.103282-1-xlpang@linux.alibaba.com> (raw)

Our memory debug tool captured the following exception:

  BUG: memory corruption in ses_enclosure_data_process+0x24b/0x310 [ses]
  ses_enclosure_data_process+0x24b/0x310 [ses]
  ses_intf_add+0x444/0x542 [ses]
  class_interface_register+0x110/0x120
  ses_init+0x13/0x1000 [ses]
  do_one_initcall+0x41/0x1c0
  do_init_module+0x5c/0x260
  __do_sys_finit_module+0xb1/0x110
  do_syscall_64+0x2d/0x40
  entry_SYSCALL_64_after_hwframe+0x44/0xa9

The root cause is "desc_ptr[len] = '\0'" makes out-of-bound
memory write beyond "buf", so make it within the buffer size.

Reported-by: Qingming Su <qingming.su@linux.alibaba.com>
Signed-off-by: Xunlei Pang <xlpang@linux.alibaba.com>
---
 drivers/scsi/ses.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c
index c2afba2a5414..c1ac2e96d25d 100644
--- a/drivers/scsi/ses.c
+++ b/drivers/scsi/ses.c
@@ -544,11 +544,14 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 			char *name = NULL;
 			struct enclosure_component *ecomp;
 
+			if (desc_ptr + 4 >= buf + page7_len)
+				desc_ptr = NULL;
+
 			if (desc_ptr) {
-				if (desc_ptr >= buf + page7_len) {
+				len = (desc_ptr[2] << 8) + desc_ptr[3];
+				if (desc_ptr + 4 + len >= buf + page7_len) {
 					desc_ptr = NULL;
 				} else {
-					len = (desc_ptr[2] << 8) + desc_ptr[3];
 					desc_ptr += 4;
 					/* Add trailing zero - pushes into
 					 * reserved space */
-- 
2.20.1.7.g153144c


                 reply	other threads:[~2021-07-08  7:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210708075141.103282-1-xlpang@linux.alibaba.com \
    --to=xlpang@linux.alibaba.com \
    --cc=hare@suse.com \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=qingming.su@linux.alibaba.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox