All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tianchen Ding <dtcccc@linux.alibaba.com>
To: "James E.J. Bottomley" <jejb@linux.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] scsi: ses: Fix out-of-bound write at ses_enclosure_data_process()
Date: Fri, 15 Apr 2022 11:23:13 +0800	[thread overview]
Message-ID: <20220415032313.94991-1-dtcccc@linux.alibaba.com> (raw)

Our modified KFENCE reported a memory corruption:

[   52.584914] BUG: KFENCE: memory corruption in ses_enclosure_data_process+0x24b/0x310 [ses]

[   52.584917] Corrupted memory at 0xffff88982de06ff0 [ 0x00 . . . . . . . . . . . . . . . ] (in kfence-#1624698):
[   52.607212]  ses_enclosure_data_process+0x24b/0x310 [ses]
[   52.607215]  ses_intf_add+0x444/0x542 [ses]
[   52.621369]  class_interface_register+0x110/0x120
[   52.621373]  ses_init+0x13/0x1000 [ses]
[   52.621377]  do_one_initcall+0x41/0x1c0
[   52.621380]  do_init_module+0x5c/0x260
[   52.621382]  __do_sys_finit_module+0xb1/0x110
[   52.621386]  do_syscall_64+0x2d/0x40
[   52.621388]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

[   52.621393] kfence-#1624698 [0xffff88982de06fc0-0xffff88982de06fe0, size=33, cache=kmalloc-64] allocated by task 1033:
[   52.670344]  ses_enclosure_data_process+0x2ae/0x310 [ses]
[   52.670347]  ses_intf_add+0x444/0x542 [ses]
[   52.670353]  class_interface_register+0x110/0x120
[   52.688165]  ses_init+0x13/0x1000 [ses]
[   52.688169]  do_one_initcall+0x41/0x1c0
[   52.688172]  do_init_module+0x5c/0x260
[   52.688174]  __do_sys_finit_module+0xb1/0x110
[   52.688177]  do_syscall_64+0x2d/0x40
[   52.688179]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

This is because we check desc_ptr >= buf + page7_len first but then
write '\0' to desc_ptr[len+4], while this address may be out of bound.

Fixes: 21fab1d0595e ("[SCSI] ses: update enclosure data on hot add")
Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
---
 drivers/scsi/ses.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c
index 0a1734f34587..06b991e27c84 100644
--- a/drivers/scsi/ses.c
+++ b/drivers/scsi/ses.c
@@ -559,11 +559,11 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 			struct enclosure_component *ecomp;
 
 			if (desc_ptr) {
-				if (desc_ptr >= buf + page7_len) {
+				len = (desc_ptr[2] << 8) + desc_ptr[3];
+				desc_ptr += 4;
+				if (desc_ptr + 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 */
 					desc_ptr[len] = '\0';
-- 
2.33.0


             reply	other threads:[~2022-04-15  3:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-15  3:23 Tianchen Ding [this message]
2022-04-21  6:25 ` [RFC PATCH] scsi: ses: Fix out-of-bound write at ses_enclosure_data_process() Tianchen Ding

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=20220415032313.94991-1-dtcccc@linux.alibaba.com \
    --to=dtcccc@linux.alibaba.com \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.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 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.