mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: George Kennedy <george.kennedy@oracle.com>
To: Joe Perches <joe@perches.com>,
	gregkh@linuxfoundation.org, jejb@linux.ibm.com,
	martin.petersen@oracle.com
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	dan.carpenter@oracle.com
Subject: Re: [PATCH] scsi: scsi_debug: fix return checks for kcalloc
Date: Thu, 4 Nov 2021 13:22:34 -0400	[thread overview]
Message-ID: <a7b4e31b-d548-5a29-9d98-fc6e916ac7fc@oracle.com> (raw)
In-Reply-To: <834e83a227f40c4654b97f2f0b045b4cbd326f16.camel@perches.com>

Thanks Joe,

On 11/4/2021 12:04 PM, Joe Perches wrote:
> On Wed, 2021-11-03 at 14:01 -0500, George Kennedy wrote:
>> Change return checks from kcalloc() to now check for NULL and
>> ZERO_SIZE_PTR using the ZERO_OR_NULL_PTR macro or the following
>> crash can occur if ZERO_SIZE_PTR indicator is returned.
>>
>> BUG: KASAN: null-ptr-deref in memcpy include/linux/fortify-string.h:191 [inline]
>> BUG: KASAN: null-ptr-deref in sg_copy_buffer+0x138/0x240 lib/scatterlist.c:974
>> Write of size 4 at addr 0000000000000010 by task syz-executor.1/22789
> []
>> diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
> []
>> @@ -3909,7 +3909,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,
>>   		return ret;
>>   	dnum = 2 * num;
>>   	arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
>> -	if (NULL == arr) {
>> +	if (ZERO_OR_NULL_PTR(arr)) {
>>   		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
>>   				INSUFF_RES_ASCQ);
>>   		return check_condition_result;
> This one isn't necessary as num is already tested for non-0 above
> this block.

The check for "num" preceding the above does this:

         if (0 == num)
                 return 0;       /* degenerate case, not an error */

Shouldn't I use that same size check and "return 0" if size == zero in 
the other cases?

>
>> @@ -4265,7 +4265,7 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
>>   		return ret;
>>   
>>   	arr = kcalloc(lb_size, vnum, GFP_ATOMIC);
>> -	if (!arr) {
>> +	if (ZERO_OR_NULL_PTR(arr)) {
>>   		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
>>   				INSUFF_RES_ASCQ);
>>   		return check_condition_result;
> Here it's probably clearer code to test vnum == 0 before the kcalloc
> and return check_condition_result;
>
>> @@ -4334,7 +4334,7 @@ static int resp_report_zones(struct scsi_cmnd *scp,
>>   			    max_zones);
>>   
>>   	arr = kcalloc(RZONES_DESC_HD, alloc_len, GFP_ATOMIC);
>> -	if (!arr) {
>> +	if (ZERO_OR_NULL_PTR(arr)) {
>>   		mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
>>   				INSUFF_RES_ASCQ);
>>   		return check_condition_result;
> And here test alloc_len == 0 before the kcalloc.

Using your suggested fix (with return 0 instead of return 
check_condition_result) the patch would look like this:

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index 40b473e..93913d2 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -4258,6 +4258,8 @@ static int resp_verify(struct scsi_cmnd *scp, 
struct sdebug_dev_info *devip)
                 mk_sense_invalid_opcode(scp);
                 return check_condition_result;
         }
+       if (0 == vnum)
+               return 0;       /* degenerate case, not an error */
         a_num = is_bytchk3 ? 1 : vnum;
         /* Treat following check like one for read (i.e. no write) 
access */
         ret = check_device_access_params(scp, lba, a_num, false);
@@ -4321,6 +4323,8 @@ static int resp_report_zones(struct scsi_cmnd *scp,
         }
         zs_lba = get_unaligned_be64(cmd + 2);
         alloc_len = get_unaligned_be32(cmd + 10);
+       if (0 == alloc_len)
+               return 0;       /* degenerate case, not an error */
         rep_opts = cmd[14] & 0x3f;
         partial = cmd[14] & 0x80;


Does the above look ok?

George

>
>


  reply	other threads:[~2021-11-04 17:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 19:01 George Kennedy
2021-11-04  6:25 ` Greg KH
2021-11-04 15:21   ` George Kennedy
2021-11-04 10:47 ` Dan Carpenter
2021-11-04 16:04 ` Joe Perches
2021-11-04 17:22   ` George Kennedy [this message]
2021-11-04 17:50 ` Douglas Gilbert

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=a7b4e31b-d548-5a29-9d98-fc6e916ac7fc@oracle.com \
    --to=george.kennedy@oracle.com \
    --cc=dan.carpenter@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jejb@linux.ibm.com \
    --cc=joe@perches.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®