linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member
@ 2023-05-16 23:14 Gustavo A. R. Silva
  2023-05-16 23:14 ` [PATCH 1/2][next] " Gustavo A. R. Silva
  2023-05-16 23:14 ` [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper Gustavo A. R. Silva
  0 siblings, 2 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2023-05-16 23:14 UTC (permalink / raw)
  To: Stanimir Varbanov, Vikash Garodia, Andy Gross, Bjorn Andersson,
	Konrad Dybcio, Mauro Carvalho Chehab, linux-media, linux-arm-msm,
	linux-kernel, Gustavo A. R. Silva, linux-hardening

Hi!

This series aims to replace one-element arrays with flexible-array
members in struct hfi_session_set_buffers_pkt.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
routines on memcpy() and help us make progress towards globally
enabling -fstrict-flex-arrays=3 [1].

Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/160
Link: https://github.com/KSPP/linux/issues/292
Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]

Gustavo A. R. Silva (2):
  media: venus: hfi_cmds: Replace one-element array with flexible-array
    member
  media: venus: hfi_cmds: Use struct_size() helper

 drivers/media/platform/qcom/venus/hfi_cmds.c | 16 ++++++++--------
 drivers/media/platform/qcom/venus/hfi_cmds.h |  2 +-
 2 files changed, 9 insertions(+), 9 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member
  2023-05-16 23:14 [PATCH 0/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member Gustavo A. R. Silva
@ 2023-05-16 23:14 ` Gustavo A. R. Silva
  2023-05-17 19:11   ` Kees Cook
  2023-05-25 11:47   ` Vikash Garodia
  2023-05-16 23:14 ` [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper Gustavo A. R. Silva
  1 sibling, 2 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2023-05-16 23:14 UTC (permalink / raw)
  To: Stanimir Varbanov, Vikash Garodia, Andy Gross, Bjorn Andersson,
	Konrad Dybcio, Mauro Carvalho Chehab, linux-media, linux-arm-msm,
	linux-kernel, Gustavo A. R. Silva, linux-hardening

One-element arrays are deprecated, and we are replacing them with flexible
array members instead. So, replace one-element arrays with flexible-array
members in struct hfi_session_set_buffers_pkt, and refactor the rest of
the code, accordingly.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
routines on memcpy() and help us make progress towards globally
enabling -fstrict-flex-arrays=3 [1].

This results in no differences in binary output.

Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/292
Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/media/platform/qcom/venus/hfi_cmds.c | 12 ++++++------
 drivers/media/platform/qcom/venus/hfi_cmds.h |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
index bc3f8ff05840..21d1b3c90dc0 100644
--- a/drivers/media/platform/qcom/venus/hfi_cmds.c
+++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
@@ -200,8 +200,8 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
 		struct hfi_buffer_info *bi;
 
 		pkt->extradata_size = bd->extradata_size;
-		pkt->shdr.hdr.size = sizeof(*pkt) - sizeof(u32) +
-			(bd->num_buffers * sizeof(*bi));
+		pkt->shdr.hdr.size = sizeof(*pkt) +
+			bd->num_buffers * sizeof(*bi);
 		bi = (struct hfi_buffer_info *)pkt->buffer_info;
 		for (i = 0; i < pkt->num_buffers; i++) {
 			bi->buffer_addr = bd->device_addr;
@@ -210,7 +210,7 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
 	} else {
 		pkt->extradata_size = 0;
 		pkt->shdr.hdr.size = sizeof(*pkt) +
-			((bd->num_buffers - 1) * sizeof(u32));
+			bd->num_buffers * sizeof(u32);
 		for (i = 0; i < pkt->num_buffers; i++)
 			pkt->buffer_info[i] = bd->device_addr;
 	}
@@ -243,8 +243,8 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
 			bi->extradata_addr = bd->extradata_addr;
 		}
 		pkt->shdr.hdr.size =
-				sizeof(struct hfi_session_set_buffers_pkt) -
-				sizeof(u32) + (bd->num_buffers * sizeof(*bi));
+				sizeof(struct hfi_session_set_buffers_pkt) +
+				bd->num_buffers * sizeof(*bi);
 	} else {
 		for (i = 0; i < pkt->num_buffers; i++)
 			pkt->buffer_info[i] = bd->device_addr;
@@ -252,7 +252,7 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
 		pkt->extradata_size = 0;
 		pkt->shdr.hdr.size =
 				sizeof(struct hfi_session_set_buffers_pkt) +
-				((bd->num_buffers - 1) * sizeof(u32));
+				bd->num_buffers * sizeof(u32);
 	}
 
 	pkt->response_req = bd->response_required;
diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.h b/drivers/media/platform/qcom/venus/hfi_cmds.h
index 99bc0b6db67c..ba74d03eb9cd 100644
--- a/drivers/media/platform/qcom/venus/hfi_cmds.h
+++ b/drivers/media/platform/qcom/venus/hfi_cmds.h
@@ -117,7 +117,7 @@ struct hfi_session_set_buffers_pkt {
 	u32 extradata_size;
 	u32 min_buffer_size;
 	u32 num_buffers;
-	u32 buffer_info[1];
+	u32 buffer_info[];
 };
 
 struct hfi_session_get_sequence_header_pkt {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper
  2023-05-16 23:14 [PATCH 0/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member Gustavo A. R. Silva
  2023-05-16 23:14 ` [PATCH 1/2][next] " Gustavo A. R. Silva
@ 2023-05-16 23:14 ` Gustavo A. R. Silva
  2023-05-17 19:12   ` Kees Cook
  2023-05-25 12:20   ` Vikash Garodia
  1 sibling, 2 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2023-05-16 23:14 UTC (permalink / raw)
  To: Stanimir Varbanov, Vikash Garodia, Andy Gross, Bjorn Andersson,
	Konrad Dybcio, Mauro Carvalho Chehab, linux-media, linux-arm-msm,
	linux-kernel, Gustavo A. R. Silva, linux-hardening

Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to
contain.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
index 21d1b3c90dc0..3f74d518ad08 100644
--- a/drivers/media/platform/qcom/venus/hfi_cmds.c
+++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
@@ -209,8 +209,8 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
 		}
 	} else {
 		pkt->extradata_size = 0;
-		pkt->shdr.hdr.size = sizeof(*pkt) +
-			bd->num_buffers * sizeof(u32);
+		pkt->shdr.hdr.size = struct_size(pkt, buffer_info,
+						 bd->num_buffers);
 		for (i = 0; i < pkt->num_buffers; i++)
 			pkt->buffer_info[i] = bd->device_addr;
 	}
@@ -251,8 +251,8 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
 
 		pkt->extradata_size = 0;
 		pkt->shdr.hdr.size =
-				sizeof(struct hfi_session_set_buffers_pkt) +
-				bd->num_buffers * sizeof(u32);
+			struct_size((struct hfi_session_set_buffers_pkt *)0,
+				    buffer_info, bd->num_buffers);
 	}
 
 	pkt->response_req = bd->response_required;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member
  2023-05-16 23:14 ` [PATCH 1/2][next] " Gustavo A. R. Silva
@ 2023-05-17 19:11   ` Kees Cook
  2023-05-25 11:47   ` Vikash Garodia
  1 sibling, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-05-17 19:11 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Stanimir Varbanov, Vikash Garodia, Andy Gross, Bjorn Andersson,
	Konrad Dybcio, Mauro Carvalho Chehab, linux-media, linux-arm-msm,
	linux-kernel, linux-hardening

On Tue, May 16, 2023 at 05:14:27PM -0600, Gustavo A. R. Silva wrote:
> One-element arrays are deprecated, and we are replacing them with flexible
> array members instead. So, replace one-element arrays with flexible-array
> members in struct hfi_session_set_buffers_pkt, and refactor the rest of
> the code, accordingly.
> 
> This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
> routines on memcpy() and help us make progress towards globally
> enabling -fstrict-flex-arrays=3 [1].
> 
> This results in no differences in binary output.
> 
> Link: https://github.com/KSPP/linux/issues/79
> Link: https://github.com/KSPP/linux/issues/292
> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper
  2023-05-16 23:14 ` [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper Gustavo A. R. Silva
@ 2023-05-17 19:12   ` Kees Cook
  2023-05-25 12:20   ` Vikash Garodia
  1 sibling, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-05-17 19:12 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Stanimir Varbanov, Vikash Garodia, Andy Gross, Bjorn Andersson,
	Konrad Dybcio, Mauro Carvalho Chehab, linux-media, linux-arm-msm,
	linux-kernel, linux-hardening

On Tue, May 16, 2023 at 05:14:49PM -0600, Gustavo A. R. Silva wrote:
> Prefer struct_size() over open-coded versions of idiom:
> 
> sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> 
> where count is the max number of items the flexible array is supposed to
> contain.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member
  2023-05-16 23:14 ` [PATCH 1/2][next] " Gustavo A. R. Silva
  2023-05-17 19:11   ` Kees Cook
@ 2023-05-25 11:47   ` Vikash Garodia
  1 sibling, 0 replies; 7+ messages in thread
From: Vikash Garodia @ 2023-05-25 11:47 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Stanimir Varbanov, Andy Gross,
	Bjorn Andersson, Konrad Dybcio, Mauro Carvalho Chehab,
	linux-media, linux-arm-msm, linux-kernel, linux-hardening



On 5/17/2023 4:44 AM, Gustavo A. R. Silva wrote:
> One-element arrays are deprecated, and we are replacing them with flexible
> array members instead. So, replace one-element arrays with flexible-array
> members in struct hfi_session_set_buffers_pkt, and refactor the rest of
> the code, accordingly.
> 
> This helps with the ongoing efforts to tighten the FORTIFY_SOURCE
> routines on memcpy() and help us make progress towards globally
> enabling -fstrict-flex-arrays=3 [1].
> 
> This results in no differences in binary output.
> 
> Link: https://github.com/KSPP/linux/issues/79
> Link: https://github.com/KSPP/linux/issues/292
> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

The patch looks good. As stated in previous patch, lets combine this into a
single series.

> ---
>  drivers/media/platform/qcom/venus/hfi_cmds.c | 12 ++++++------
>  drivers/media/platform/qcom/venus/hfi_cmds.h |  2 +-
>  2 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
> index bc3f8ff05840..21d1b3c90dc0 100644
> --- a/drivers/media/platform/qcom/venus/hfi_cmds.c
> +++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
> @@ -200,8 +200,8 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
>  		struct hfi_buffer_info *bi;
>  
>  		pkt->extradata_size = bd->extradata_size;
> -		pkt->shdr.hdr.size = sizeof(*pkt) - sizeof(u32) +
> -			(bd->num_buffers * sizeof(*bi));
> +		pkt->shdr.hdr.size = sizeof(*pkt) +
> +			bd->num_buffers * sizeof(*bi);
>  		bi = (struct hfi_buffer_info *)pkt->buffer_info;
>  		for (i = 0; i < pkt->num_buffers; i++) {
>  			bi->buffer_addr = bd->device_addr;
> @@ -210,7 +210,7 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
>  	} else {
>  		pkt->extradata_size = 0;
>  		pkt->shdr.hdr.size = sizeof(*pkt) +
> -			((bd->num_buffers - 1) * sizeof(u32));
> +			bd->num_buffers * sizeof(u32);
>  		for (i = 0; i < pkt->num_buffers; i++)
>  			pkt->buffer_info[i] = bd->device_addr;
>  	}
> @@ -243,8 +243,8 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
>  			bi->extradata_addr = bd->extradata_addr;
>  		}
>  		pkt->shdr.hdr.size =
> -				sizeof(struct hfi_session_set_buffers_pkt) -
> -				sizeof(u32) + (bd->num_buffers * sizeof(*bi));
> +				sizeof(struct hfi_session_set_buffers_pkt) +
> +				bd->num_buffers * sizeof(*bi);
>  	} else {
>  		for (i = 0; i < pkt->num_buffers; i++)
>  			pkt->buffer_info[i] = bd->device_addr;
> @@ -252,7 +252,7 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
>  		pkt->extradata_size = 0;
>  		pkt->shdr.hdr.size =
>  				sizeof(struct hfi_session_set_buffers_pkt) +
> -				((bd->num_buffers - 1) * sizeof(u32));
> +				bd->num_buffers * sizeof(u32);
>  	}
>  
>  	pkt->response_req = bd->response_required;
> diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.h b/drivers/media/platform/qcom/venus/hfi_cmds.h
> index 99bc0b6db67c..ba74d03eb9cd 100644
> --- a/drivers/media/platform/qcom/venus/hfi_cmds.h
> +++ b/drivers/media/platform/qcom/venus/hfi_cmds.h
> @@ -117,7 +117,7 @@ struct hfi_session_set_buffers_pkt {
>  	u32 extradata_size;
>  	u32 min_buffer_size;
>  	u32 num_buffers;
> -	u32 buffer_info[1];
> +	u32 buffer_info[];
>  };
>  
>  struct hfi_session_get_sequence_header_pkt {

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper
  2023-05-16 23:14 ` [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper Gustavo A. R. Silva
  2023-05-17 19:12   ` Kees Cook
@ 2023-05-25 12:20   ` Vikash Garodia
  1 sibling, 0 replies; 7+ messages in thread
From: Vikash Garodia @ 2023-05-25 12:20 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Stanimir Varbanov, Andy Gross,
	Bjorn Andersson, Konrad Dybcio, Mauro Carvalho Chehab,
	linux-media, linux-arm-msm, linux-kernel, linux-hardening


On 5/17/2023 4:44 AM, Gustavo A. R. Silva wrote:
> Prefer struct_size() over open-coded versions of idiom:
> 
> sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> 
> where count is the max number of items the flexible array is supposed to
> contain.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

The patch looks good. As stated in previous patch, lets combine this into a
single series.

> ---
>  drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
> index 21d1b3c90dc0..3f74d518ad08 100644
> --- a/drivers/media/platform/qcom/venus/hfi_cmds.c
> +++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
> @@ -209,8 +209,8 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
>  		}
>  	} else {
>  		pkt->extradata_size = 0;
> -		pkt->shdr.hdr.size = sizeof(*pkt) +
> -			bd->num_buffers * sizeof(u32);
> +		pkt->shdr.hdr.size = struct_size(pkt, buffer_info,
> +						 bd->num_buffers);
>  		for (i = 0; i < pkt->num_buffers; i++)
>  			pkt->buffer_info[i] = bd->device_addr;
>  	}
> @@ -251,8 +251,8 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
>  
>  		pkt->extradata_size = 0;
>  		pkt->shdr.hdr.size =
> -				sizeof(struct hfi_session_set_buffers_pkt) +
> -				bd->num_buffers * sizeof(u32);
> +			struct_size((struct hfi_session_set_buffers_pkt *)0,
> +				    buffer_info, bd->num_buffers);
>  	}
>  
>  	pkt->response_req = bd->response_required;

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-05-25 12:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-16 23:14 [PATCH 0/2][next] media: venus: hfi_cmds: Replace one-element array with flexible-array member Gustavo A. R. Silva
2023-05-16 23:14 ` [PATCH 1/2][next] " Gustavo A. R. Silva
2023-05-17 19:11   ` Kees Cook
2023-05-25 11:47   ` Vikash Garodia
2023-05-16 23:14 ` [PATCH 2/2][next] media: venus: hfi_cmds: Use struct_size() helper Gustavo A. R. Silva
2023-05-17 19:12   ` Kees Cook
2023-05-25 12:20   ` Vikash Garodia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).