lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Wed, 22 Jun 2022 15:26:38 -0700
From:   Kees Cook <keescook@...omium.org>
To:     "Gustavo A. R. Silva" <gustavoars@...nel.org>
Cc:     linux-kernel@...r.kernel.org,
        Kashyap Desai <kashyap.desai@...adcom.com>,
        Sumit Saxena <sumit.saxena@...adcom.com>,
        Shivasharan S <shivasharan.srikanteshwara@...adcom.com>,
        "James E.J. Bottomley" <jejb@...ux.ibm.com>,
        "Martin K. Petersen" <martin.petersen@...cle.com>,
        megaraidlinux.pdl@...adcom.com, linux-scsi@...r.kernel.org,
        linux-hardening@...r.kernel.org
Subject: Re: [PATCH v2 3/4][next] scsi: megaraid_sas: Replace one-element
 array with flexible-array member in MR_DRV_RAID_MAP

On Wed, Aug 04, 2021 at 11:20:04PM -0500, Gustavo A. R. Silva wrote:
> Replace one-element array with a flexible-array member in struct
> MR_DRV_RAID_MAP and use the flex_array_size() helper.
> 
> This helps with the ongoing efforts to globally enable -Warray-bounds
> and get us closer to being able to tighten the FORTIFY_SOURCE routines
> on memcpy().
> 
> Link: https://en.wikipedia.org/wiki/Flexible_array_member
> Link: https://www.kernel.org/doc/html/v5.10/process/deprecated.html#zero-length-and-one-element-arrays
> Link: https://github.com/KSPP/linux/issues/79
> Link: https://github.com/KSPP/linux/issues/109
> Signed-off-by: Gustavo A. R. Silva <gustavoars@...nel.org>

I'd really like to see this fixed. :) I'm running into this 1-element
array problem now with UBSAN_BOUNDS:

[   10.011173] UBSAN: array-index-out-of-bounds in /build/linux-WLUive/linux-5.15.0/drivers/scsi/megaraid/megaraid_sas_fp.c:103:32
[   10.087824] index 1 is out of range for type 'MR_LD_SPAN_MAP [1]'

and I'm not the only one:

https://bugzilla.kernel.org/show_bug.cgi?id=215943

> ---
> Changes in v2:
>  - None.
> 
>  drivers/scsi/megaraid/megaraid_sas_fp.c     | 6 +++---
>  drivers/scsi/megaraid/megaraid_sas_fusion.h | 2 +-
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/scsi/megaraid/megaraid_sas_fp.c b/drivers/scsi/megaraid/megaraid_sas_fp.c
> index da1cad1ee123..9cb36ef96c2c 100644
> --- a/drivers/scsi/megaraid/megaraid_sas_fp.c
> +++ b/drivers/scsi/megaraid/megaraid_sas_fp.c
> @@ -229,8 +229,8 @@ static int MR_PopulateDrvRaidMap(struct megasas_instance *instance, u64 map_id)
>  					le32_to_cpu(desc_table->raid_map_desc_offset));
>  				memcpy(pDrvRaidMap->ldSpanMap,
>  				       fw_map_dyn->ld_span_map,
> -				       sizeof(struct MR_LD_SPAN_MAP) *
> -				       le32_to_cpu(desc_table->raid_map_desc_elements));
> +				       flex_array_size(pDrvRaidMap, ldSpanMap,
> +				       le32_to_cpu(desc_table->raid_map_desc_elements)));
>  			break;
>  			default:
>  				dev_dbg(&instance->pdev->dev, "wrong number of desctableElements %d\n",
> @@ -254,7 +254,7 @@ static int MR_PopulateDrvRaidMap(struct megasas_instance *instance, u64 map_id)
>  			pDrvRaidMap->ldTgtIdToLd[i] =
>  				(u16)fw_map_ext->ldTgtIdToLd[i];
>  		memcpy(pDrvRaidMap->ldSpanMap, fw_map_ext->ldSpanMap,
> -		       sizeof(struct MR_LD_SPAN_MAP) * ld_count);
> +		       flex_array_size(pDrvRaidMap, ldSpanMap, ld_count));
>  		memcpy(pDrvRaidMap->arMapInfo, fw_map_ext->arMapInfo,
>  		       sizeof(struct MR_ARRAY_INFO) * MAX_API_ARRAYS_EXT);
>  		memcpy(pDrvRaidMap->devHndlInfo, fw_map_ext->devHndlInfo,
> diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.h b/drivers/scsi/megaraid/megaraid_sas_fusion.h
> index 9adb8b30f422..5fe2f7a6eebe 100644
> --- a/drivers/scsi/megaraid/megaraid_sas_fusion.h
> +++ b/drivers/scsi/megaraid/megaraid_sas_fusion.h
> @@ -1182,7 +1182,7 @@ struct MR_DRV_RAID_MAP {
>  		devHndlInfo[MAX_RAIDMAP_PHYSICAL_DEVICES_DYN];
>  	u16 ldTgtIdToLd[MAX_LOGICAL_DRIVES_DYN];
>  	struct MR_ARRAY_INFO arMapInfo[MAX_API_ARRAYS_DYN];
> -	struct MR_LD_SPAN_MAP      ldSpanMap[1];
> +	struct MR_LD_SPAN_MAP      ldSpanMap[];
>  
>  };
>  

I think this patch is incomplete, and the wrapping struct needs to be
adjusted too:

@@ -1193,7 +1193,7 @@ struct MR_DRV_RAID_MAP {
 struct MR_DRV_RAID_MAP_ALL {
 
        struct MR_DRV_RAID_MAP raidMap;
-       struct MR_LD_SPAN_MAP ldSpanMap[MAX_LOGICAL_DRIVES_DYN - 1];
+       struct MR_LD_SPAN_MAP ldSpanMap[MAX_LOGICAL_DRIVES_DYN];
 } __packed;
 
With that added, I get zero changes to the executable code.

I assume the others need adjustment too.

-- 
Kees Cook

Powered by blists - more mailing lists