Skip to contentSkip to conversation

staging: r8188eu: Use flexible-array for one length array member

PATCH In review Deepak R Varma 4 participants 7 messages Oct 28, 2022 – Nov 3, 2022 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: r8188eu: Use flexible-array for one length array member

Flexible-array member should be used instead of one or zero member to meet the need for having a dynamically sized trailing elements in a structure. Refer to links [1] and [2] for detailed guidance on this suggestion.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Issue identified using coccicheck.

---
 drivers/staging/r8188eu/include/odm.h         | 2 +-
 drivers/staging/r8188eu/include/wlan_bssdef.h | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/include/odm.h +1 -1
index 89b01dd614ba..e2a9de5b9323 100644
@@ -166,7 +166,7 @@ struct odm_ra_info {
 
 struct ijk_matrix_regs_set {
 	bool	bIQKDone;
-	s32	Value[1][IQK_Matrix_REG_NUM];
+	s32	Value[][IQK_Matrix_REG_NUM];
 };
 
 struct odm_rf_cal {
drivers/staging/r8188eu/include/wlan_bssdef.h +3 -3
index 831c465df500..33177de194eb 100644
@@ -179,7 +179,7 @@ struct ndis_802_11_status_ind {
 
 struct ndis_802_11_auth_evt {
 	struct ndis_802_11_status_ind       Status;
-	struct ndis_802_11_auth_req  Request[1];
+	struct ndis_802_11_auth_req  Request[];
 };
 
 struct ndis_802_11_test {
@@ -291,7 +291,7 @@ struct pmkid_candidate {
 struct ndis_802_11_pmkid_list {
 	u32 Version;       /*  Version of the structure */
 	u32 NumCandidates; /*  No. of pmkid candidates */
-	struct pmkid_candidate CandidateList[1];
+	struct pmkid_candidate CandidateList[];
 };
 
 struct ndis_802_11_auth_encrypt {
@@ -304,7 +304,7 @@ struct ndis_802_11_cap {
 	u32  Version;
 	u32  NoOfPMKIDs;
 	u32  NoOfAuthEncryptPairsSupported;
-	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[1];
+	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[];
 };
 
 u8 key_2char2num(u8 hch, u8 lch);

--
2.34.1

#1 lore raw

Hi Deepak R,

Deepak R Varma <[email protected]> says:

Quoted 21 lines

Flexible-array member should be used instead of one or zero member to meet the need for having a dynamically sized trailing elements in a structure. Refer to links [1] and [2] for detailed guidance on this suggestion.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Issue identified using coccicheck.

---
  drivers/staging/r8188eu/include/odm.h         | 2 +-
  drivers/staging/r8188eu/include/wlan_bssdef.h | 6 +++---
  2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/include/odm.h +0 -0
index 89b01dd614ba..e2a9de5b9323 100644

drivers/staging/r8188eu/include/odm.h struct odm_ra_info { · line 171

Quoted 7 lines
drivers/staging/r8188eu/include/odm.h +1 -1
@@ -166,7 +166,7 @@ struct odm_ra_info {
 
  struct ijk_matrix_regs_set {
  	bool	bIQKDone;
-	s32	Value[1][IQK_Matrix_REG_NUM];
+	s32	Value[][IQK_Matrix_REG_NUM];
  };
 

you are changing the actual size of the struct. Wondering if you have tested this patch somehow

drivers/staging/r8188eu/include/wlan_bssdef.h struct ndis_802_11_status_ind { · line 184

Quoted 13 lines
drivers/staging/r8188eu/include/odm.h +0 -0
  struct odm_rf_cal {
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -1
index 831c465df500..33177de194eb 100644
@@ -179,7 +179,7 @@ struct ndis_802_11_status_ind {
 
  struct ndis_802_11_auth_evt {
  	struct ndis_802_11_status_ind       Status;
-	struct ndis_802_11_auth_req  Request[1];
+	struct ndis_802_11_auth_req  Request[];
  };
 

this structure seems to be unused. Better to remove it instead of maintaining the old code

drivers/staging/r8188eu/include/wlan_bssdef.h struct pmkid_candidate { · line 295

Quoted 8 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -1
  struct ndis_802_11_test {
@@ -291,7 +291,7 @@ struct pmkid_candidate {
  struct ndis_802_11_pmkid_list {
  	u32 Version;       /*  Version of the structure */
  	u32 NumCandidates; /*  No. of pmkid candidates */
-	struct pmkid_candidate CandidateList[1];
+	struct pmkid_candidate CandidateList[];
  };

this one as well

Quoted 12 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -2
  struct ndis_802_11_auth_encrypt {
@@ -304,7 +304,7 @@ struct ndis_802_11_cap {
  	u32  Version;
  	u32  NoOfPMKIDs;
  	u32  NoOfAuthEncryptPairsSupported;
-	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[1];
+	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[];
  };
 
  u8 key_2char2num(u8 hch, u8 lch);
--
Quoted 2 lines

2.34.1

and this one as well

Quoted 2 lines

With regards,
Pavel Skripkin

#2 lore raw
Quoted 25 lines · Pavel Skripkin

Hi Deepak R,

Deepak R Varma <[email protected]> says:

Quoted 22 lines

Flexible-array member should be used instead of one or zero member to meet the need for having a dynamically sized trailing elements in a structure. Refer to links [1] and [2] for detailed guidance on this suggestion.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Issue identified using coccicheck.

---
  drivers/staging/r8188eu/include/odm.h         | 2 +-
  drivers/staging/r8188eu/include/wlan_bssdef.h | 6 +++---
  2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/include/odm.h +0 -0
index 89b01dd614ba..e2a9de5b9323 100644
@@ -166,7 +166,7 @@ struct odm_ra_info {
 
Quoted 9 lines

drivers/staging/r8188eu/include/odm.h struct odm_ra_info { · line 171

Quoted 6 lines
drivers/staging/r8188eu/include/odm.h +1 -1
  struct ijk_matrix_regs_set {
  	bool	bIQKDone;
-	s32	Value[1][IQK_Matrix_REG_NUM];
+	s32	Value[][IQK_Matrix_REG_NUM];
  };
 

you are changing the actual size of the struct. Wondering if you have tested this patch somehow

Hello Pavel,
Thank you for reviewing the patch. I build the module post making the changes an ensured that the build is successful. However, I am not sure how to check the changes I am proposing. Could you please direct me to some information on how to test patches? Is there some documentation generic/driver-specific that I can refer to?

Quoted 8 lines
Quoted 8 lines
drivers/staging/r8188eu/include/odm.h +0 -0
  struct odm_rf_cal {
drivers/staging/r8188eu/include/wlan_bssdef.h +0 -0
index 831c465df500..33177de194eb 100644
@@ -179,7 +179,7 @@ struct ndis_802_11_status_ind {
 
Quoted 9 lines

drivers/staging/r8188eu/include/wlan_bssdef.h struct ndis_802_11_status_ind { · line 184

Quoted 6 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -1
  struct ndis_802_11_auth_evt {
  	struct ndis_802_11_status_ind       Status;
-	struct ndis_802_11_auth_req  Request[1];
+	struct ndis_802_11_auth_req  Request[];
  };
 

this structure seems to be unused. Better to remove it instead of maintaining the old code

Sounds good. I agree and will do as suggested.

Quoted 11 lines

drivers/staging/r8188eu/include/wlan_bssdef.h struct pmkid_candidate { · line 295

Quoted 9 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -1
  struct ndis_802_11_test {
@@ -291,7 +291,7 @@ struct pmkid_candidate {
  struct ndis_802_11_pmkid_list {
  	u32 Version;       /*  Version of the structure */
  	u32 NumCandidates; /*  No. of pmkid candidates */
-	struct pmkid_candidate CandidateList[1];
+	struct pmkid_candidate CandidateList[];
  };

this one as well

Yes, same here.

Quoted 11 lines
Quoted 11 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +1 -1
  struct ndis_802_11_auth_encrypt {
@@ -304,7 +304,7 @@ struct ndis_802_11_cap {
  	u32  Version;
  	u32  NoOfPMKIDs;
  	u32  NoOfAuthEncryptPairsSupported;
-	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[1];
+	struct ndis_802_11_auth_encrypt AuthenticationEncryptionSupported[];
  };
 
Quoted 6 lines
Quoted 4 lines
drivers/staging/r8188eu/include/wlan_bssdef.h +0 -1
  u8 key_2char2num(u8 hch, u8 lch);
--

2.34.1

and this one as well

Yes, will do.

Thank you again for your feedback and suggestions.

./drv

Quoted 11 lines
Quoted 3 lines

With regards,
Pavel Skripkin

#3 lore raw
Quoted 36 lines · Deepak R Varma
Quoted 25 lines · Pavel Skripkin

Hi Deepak R,

Deepak R Varma <[email protected]> says:

Quoted 22 lines

Flexible-array member should be used instead of one or zero member to meet the need for having a dynamically sized trailing elements in a structure. Refer to links [1] and [2] for detailed guidance on this suggestion.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Issue identified using coccicheck.

---
  drivers/staging/r8188eu/include/odm.h         | 2 +-
  drivers/staging/r8188eu/include/wlan_bssdef.h | 6 +++---
  2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/include/odm.h +0 -0
index 89b01dd614ba..e2a9de5b9323 100644
@@ -166,7 +166,7 @@ struct odm_ra_info {
 
Quoted 9 lines

drivers/staging/r8188eu/include/odm.h struct odm_ra_info { · line 171

Quoted 6 lines
drivers/staging/r8188eu/include/odm.h +1 -1
  struct ijk_matrix_regs_set {
  	bool	bIQKDone;
-	s32	Value[1][IQK_Matrix_REG_NUM];
+	s32	Value[][IQK_Matrix_REG_NUM];
  };
 

you are changing the actual size of the struct. Wondering if you have tested this patch somehow

Quoted 6 lines

Hello Pavel,
Thank you for reviewing the patch. I build the module post making the changes an ensured that the build is successful. However, I am not sure how to check the changes I am proposing. Could you please direct me to some information on how to test patches? Is there some documentation generic/driver-specific that I can refer to?

You just have to look at every place where it is used and especially look at where it is allocated. It is only used in one place:

	struct ijk_matrix_regs_set IQKMatrixRegSetting;

But that is in the middle of a struct and generally it doesn't make sense to have a flex array in the middle of a struct. So investigating further, we see that it really is a one element array:

Do a grep:

	pDM_Odm->RFCalibrateInfo.IQKMatrixRegSetting[ChannelMappedIndex].Value[0][1]);

The first element is always zero. So this patch introduces memory corruption.

The code is messy and should be cleaned up, of course.

regards,
dan carpenter

#4 lore raw
Quoted 53 lines · Dan Carpenter
Quoted 36 lines · Deepak R Varma
Quoted 25 lines · Pavel Skripkin

Hi Deepak R,

Deepak R Varma <[email protected]> says:

Quoted 22 lines

Flexible-array member should be used instead of one or zero member to meet the need for having a dynamically sized trailing elements in a structure. Refer to links [1] and [2] for detailed guidance on this suggestion.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Issue identified using coccicheck.

---
  drivers/staging/r8188eu/include/odm.h         | 2 +-
  drivers/staging/r8188eu/include/wlan_bssdef.h | 6 +++---
  2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/include/odm.h +0 -0
index 89b01dd614ba..e2a9de5b9323 100644
@@ -166,7 +166,7 @@ struct odm_ra_info {
 
Quoted 9 lines

drivers/staging/r8188eu/include/odm.h struct odm_ra_info { · line 171

Quoted 6 lines
drivers/staging/r8188eu/include/odm.h +1 -1
  struct ijk_matrix_regs_set {
  	bool	bIQKDone;
-	s32	Value[1][IQK_Matrix_REG_NUM];
+	s32	Value[][IQK_Matrix_REG_NUM];
  };
 

you are changing the actual size of the struct. Wondering if you have tested this patch somehow

Quoted 6 lines

Hello Pavel,
Thank you for reviewing the patch. I build the module post making the changes an ensured that the build is successful. However, I am not sure how to check the changes I am proposing. Could you please direct me to some information on how to test patches? Is there some documentation generic/driver-specific that I can refer to?

You just have to look at every place where it is used and especially look at where it is allocated. It is only used in one place:

	struct ijk_matrix_regs_set IQKMatrixRegSetting;

But that is in the middle of a struct and generally it doesn't make sense to have a flex array in the middle of a struct. So investigating further, we see that it really is a one element array:

Quoted 8 lines

Do a grep:

	pDM_Odm->RFCalibrateInfo.IQKMatrixRegSetting[ChannelMappedIndex].Value[0][1]);

The first element is always zero. So this patch introduces memory corruption.

The code is messy and should be cleaned up, of course.

Hello Pavel and Dan,
I looked at the surround code and places where the proposed patch will have an impact. As you rightly pointed out this array can't be treated as a flexible array since it fits right in the middle of another struct.

I am wondering if ijk_matrix_regs_set (or IQKMatrixRegSetting) is a one element array, why is it declared as a two dimensional array? Can this be a simple one dimensional array instead? Is this the cleanup you are referring to when you say "code is messy and should be cleaned up"?

Thank you,
./drv

Quoted 4 lines

regards,
dan carpenter

#5 lore raw
Quoted 4 lines · Deepak R Varma

I am wondering if ijk_matrix_regs_set (or IQKMatrixRegSetting) is a one element array, why is it declared as a two dimensional array? Can this be a simple one dimensional array instead? Is this the cleanup you are referring to when you say "code is messy and should be cleaned up"?

Yes. You have interpreted that correctly.

regards,
dan carpenter

#6 lore raw
Quoted 7 lines · Dan Carpenter
Quoted 4 lines · Deepak R Varma

I am wondering if ijk_matrix_regs_set (or IQKMatrixRegSetting) is a one element array, why is it declared as a two dimensional array? Can this be a simple one dimensional array instead? Is this the cleanup you are referring to when you say "code is messy and should be cleaned up"?

Yes. You have interpreted that correctly.

Thank you for the confirmation. This will be an interesting correction. I will review and submit a patch shortly.

Thank you,
./drv

Quoted 5 lines

regards,
dan carpenter