Skip to contentSkip to conversation

scsi: backport fix for NULL deref in scsi_queue_rq to 5.10.y

PATCH In review Khemissi Mohammed el Amine 2 participants 4 messages Feb 23 – Feb 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: backport fix for NULL deref in scsi_queue_rq to 5.10.y

This backports upstream commits 35fe6fa57b99 and 6ca9818d1624 to 5.10 LTS.

The original fix prevents a NULL pointer dereference in scsi_queue_rq() when a BSG ioctl is issued with a zero-length request and a NULL cmnd pointer. Without this fix, a local user with access to /dev/bsg/* can trigger a kernel panic.

The crash occurs in scsi_command_size() when it dereferences a NULL cmnd pointer. This was confirmed on kernel 5.10.0+ with a 100% reproducible exploit.

CVE-2021-47552

---
 drivers/scsi/scsi_lib.c    | 6 ++++++
 include/scsi/scsi_common.h | 2 ++
 2 files changed, 8 insertions(+)
drivers/scsi/scsi_lib.c +6 -0
index 03c6d0620..4e86bfd3e 100644
@@ -1174,6 +1174,12 @@ static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
 {
 	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
 
+	/* Check for NULL command pointer */
+	if (!cmd->cmnd) {
+		scsi_req(req)->result = DID_NO_CONNECT << 16;
+		return BLK_STS_IOERR;
+	}
+
 	/*
 	 * Passthrough requests may transfer data, in which case they must
 	 * a bio attached to them.  Or they might contain a SCSI command
include/scsi/scsi_common.h +2 -0
index 5b567b43e..1d9dcadb3 100644
@@ -21,6 +21,8 @@ extern const unsigned char scsi_command_size_tbl[8];
 static inline unsigned
 scsi_command_size(const unsigned char *cmnd)
 {
+	if (!cmnd)
+		return 0;
 	return (cmnd[0] == VARIABLE_LENGTH_CMD) ?
 		scsi_varlen_cdb_length(cmnd) : COMMAND_SIZE(cmnd[0]);
 }
Signature · 1 line
2.51.0
#1 lore raw

Hi Khemessi,

Quoted 2 lines · Khemissi Mohammed el Amine

This backports upstream commits 35fe6fa57b99 and 6ca9818d1624 to 5.10 LTS.

Note: the upstream commit is 6ca9818d1624e136a76ae8faedb6b6c95ca66903 the first one(35fe6fa57b99) is just a stable backport to 6.3.y branch, so saying backport upstream commits A and B when A and B are same might mislead a bit.

Quoted 11 lines

The original fix prevents a NULL pointer dereference in scsi_queue_rq() when a BSG ioctl is issued with a zero-length request and a NULL cmnd pointer. Without this fix, a local user with access to /dev/bsg/* can trigger a kernel panic.

The crash occurs in scsi_command_size() when it dereferences a NULL cmnd pointer. This was confirmed on kernel 5.10.0+ with a 100% reproducible exploit.

CVE-2021-47552

1. This is not upstream commit message, alwyas use cherry-pick -x and document [ Upstream commit 6ca9818d1624e136a76ae8faedb6b6c95ca66903 ] in the first line of commit message, don't alter the upstream commit message, append the commit message with SOB at the bottom:

something like this: https://lore.kernel.org/stable/[email protected]/T/#u

2. Documentation can be found here:
https://www.kernel.org/doc/html/v6.18/process/stable-kernel-rules.html
^^ Check Option 1/2/3

3. Also, neither of the commits you listed do the same thing that I can see in the below diff.

4. Also the CVE listed above doesn't seem to be related to the commits you listed above, can you please double check ?

Thanks,
Harshit

Quoted 36 lines
---
  drivers/scsi/scsi_lib.c    | 6 ++++++
  include/scsi/scsi_common.h | 2 ++
  2 files changed, 8 insertions(+)
drivers/scsi/scsi_lib.c +6 -0
index 03c6d0620..4e86bfd3e 100644
@@ -1174,6 +1174,12 @@ static blk_status_t scsi_setup_scsi_cmnd(struct scsi_device *sdev,
  {
  	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
 
+	/* Check for NULL command pointer */
+	if (!cmd->cmnd) {
+		scsi_req(req)->result = DID_NO_CONNECT << 16;
+		return BLK_STS_IOERR;
+	}
+
  	/*
  	 * Passthrough requests may transfer data, in which case they must
  	 * a bio attached to them.  Or they might contain a SCSI command
include/scsi/scsi_common.h +2 -0
index 5b567b43e..1d9dcadb3 100644
@@ -21,6 +21,8 @@ extern const unsigned char scsi_command_size_tbl[8];
  static inline unsigned
  scsi_command_size(const unsigned char *cmnd)
  {
+	if (!cmnd)
+		return 0;
  	return (cmnd[0] == VARIABLE_LENGTH_CMD) ?
  		scsi_varlen_cdb_length(cmnd) : COMMAND_SIZE(cmnd[0]);
  }
#2 lore raw

Hi Harshit,

Thank you again for your patience and detailed feedback.

I want to be fully transparent: this is my first time submitting a kernel patch, and I made several mistakes I now understand:

- CVE-2021-47552 is unrelated to this bug (blk-mq race, not NULL dereference)
- The commits I referenced do not fix this specific issue
- This is an original fix, not a backport

I also have a practical problem: I am not yet familiar with git send-email, and when I paste the patch manually, the whitespace gets corrupted.

Could you kindly advise me on the correct way to submit this patch as a beginner? I want to do this properly.

The bug itself is real and 100% reproducible. I just want to contribute it correctly.

Thank you,
Amine

#3 lore raw

Hi Khemissi.

Quoted 4 lines · Amine Khemissi

Hi Harshit,

Thank you again for your patience and detailed feedback.

No problem.

Quoted 3 lines

I want to be fully transparent: this is my first time submitting a kernel patch, and I made several mistakes I now understand:

Sure let me try helping, might not be perfect.

1. You are sending it to long-term-stable kernel (5.10.y)

So stable kernels, long-term-stable and upstream release. You can read more about them in [1]

2. Generally you worn against the upstream branch: (master from: https://github.com/torvalds/linux/tree/master or linux-next [2]

Once the patch gets merged upstream [1] , then you can backport it to stable. So the first question I would ask is if this issue is reproducible on upstream kernel(7.0-rc1) ? If it does, then try fixing it there. ./scripts/get_maintainers.pl will suggest you whom to email your patch. For submitting a patch there is some good documentation in [3] and [4].

Quoted 4 lines

- CVE-2021-47552 is unrelated to this bug (blk-mq race, not NULL dereference)
- The commits I referenced do not fix this specific issue
- This is an original fix, not a backport

Relating it with 2021 CVE might confuse the situation and in general, a CVE is associated with a fix, so when you reference it directly it might give an impression that you are trying to backport the CVE fix to stable kernels.

Quoted 4 lines

I also have a practical problem: I am not yet familiar with git send-email, and when I paste the patch manually, the whitespace gets corrupted.

References [3] and [4] will suggest ways to configure your git send-email.

Quoted 3 lines

Could you kindly advise me on the correct way to submit this patch as a beginner? I want to do this properly.

I would also suggest running ./scripts/checkpatch.pl on your patch.

Quoted 3 lines

The bug itself is real and 100% reproducible. I just want to contribute it correctly.

If this is a new issue, please use upstream torvalds/master branch.

Couple of situations that might arise:

1. It is somehow fixed in 7.0-rc1 and you couldn't reproduce it there, in such cases, try finding the commit that solves the problem, and then submit a backport to affected LTS/stable releases using [5], see the options mentioned. Also remember that while submitting a backport to stable/lts branch, always ensure all newer stable/lts branches have the fix. (say: you want the fix to be backported to 5.10.y, you must ensure it is already fixed in the 5.15.y, 6.1.y, 6.6.y, 6.12.y, 6.18.y, 6.19.y)

2. If you have to patch upstream, one way to correctly write a patch is add a Fixes: tag(that would indicate which commit introduced the problem), also if it affects stable kernels(CC: stable vger as documented in Option 1 of [5])

Let me know if you have more questions, also you can ask questions like this on [email protected] .

Thanks,
Harshit

Quoted 2 lines

Thank you,
Amine

[1] https://www.kernel.org/category/releases.html

[2] https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git

[3] https://docs.kernel.org/process/submitting-patches.html

[4] https://nickdesaulniers.github.io/blog/2017/05/16/submitting-your-first-patch-to-the-linux-kernel-and-responding-to-feedback/

[5] https://www.kernel.org/doc/html/v6.18/process/stable-kernel-rules.html