public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
@ 2024-06-19 19:42 Javier Carrasco
  2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Javier Carrasco @ 2024-06-19 19:42 UTC (permalink / raw)
  To: Johan Hovold, Greg Kroah-Hartman, Kees Cook, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-usb, linux-kernel, linux-hardening, llvm, Javier Carrasco

The size is assigned before the first reference to the flexible array
(see pkt_add()), which allows for a straightforward annotation without
further modifications.

Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
Changes in v2:
- Use struct_size to calculate the size of pkt.
- Link to v1: https://lore.kernel.org/r/20240619-garmin_gps_counted_by-v1-1-d8d816f085d9@gmail.com

---
Javier Carrasco (2):
      USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
      USB: serial: garmin_gps: use struct_size to allocate pkt

 drivers/usb/serial/garmin_gps.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
---
base-commit: 0c52056d9f77508cb6d4d68d3fc91c6c08ec71af
change-id: 20240619-garmin_gps_counted_by-376545960353

Best regards,
-- 
Javier Carrasco <javier.carrasco.cruz@gmail.com>


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

* [PATCH v2 1/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
  2024-06-19 19:42 [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Javier Carrasco
@ 2024-06-19 19:42 ` Javier Carrasco
  2024-06-19 19:56   ` Gustavo A. R. Silva
  2024-06-19 20:34   ` Kees Cook
  2024-06-19 19:42 ` [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt Javier Carrasco
  2024-07-05 12:04 ` [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Johan Hovold
  2 siblings, 2 replies; 8+ messages in thread
From: Javier Carrasco @ 2024-06-19 19:42 UTC (permalink / raw)
  To: Johan Hovold, Greg Kroah-Hartman, Kees Cook, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-usb, linux-kernel, linux-hardening, llvm, Javier Carrasco

Use the __counted_by compiler attribute for the data[] flexible array
member to improve the results of array bound sanitizers.

Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
 drivers/usb/serial/garmin_gps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
index 670e942fdaaa..57df6ad183ff 100644
--- a/drivers/usb/serial/garmin_gps.c
+++ b/drivers/usb/serial/garmin_gps.c
@@ -104,7 +104,7 @@ struct garmin_packet {
 	int               seq;
 	/* the real size of the data array, always > 0 */
 	int               size;
-	__u8              data[];
+	__u8              data[] __counted_by(size);
 };
 
 /* structure used to keep the current state of the driver */

-- 
2.40.1


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

* [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt
  2024-06-19 19:42 [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Javier Carrasco
  2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
@ 2024-06-19 19:42 ` Javier Carrasco
  2024-06-19 19:55   ` Gustavo A. R. Silva
  2024-06-19 20:33   ` Kees Cook
  2024-07-05 12:04 ` [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Johan Hovold
  2 siblings, 2 replies; 8+ messages in thread
From: Javier Carrasco @ 2024-06-19 19:42 UTC (permalink / raw)
  To: Johan Hovold, Greg Kroah-Hartman, Kees Cook, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-usb, linux-kernel, linux-hardening, llvm, Javier Carrasco

Use the struct_size macro to calculate the size of the pkt, which
includes a trailing flexible array.

Suggested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
 drivers/usb/serial/garmin_gps.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
index 57df6ad183ff..6d6ec7eed87c 100644
--- a/drivers/usb/serial/garmin_gps.c
+++ b/drivers/usb/serial/garmin_gps.c
@@ -267,8 +267,7 @@ static int pkt_add(struct garmin_data *garmin_data_p,
 
 	/* process only packets containing data ... */
 	if (data_length) {
-		pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
-								GFP_ATOMIC);
+		pkt = kmalloc(struct_size(pkt, data, data_length), GFP_ATOMIC);
 		if (!pkt)
 			return 0;
 

-- 
2.40.1


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

* Re: [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt
  2024-06-19 19:42 ` [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt Javier Carrasco
@ 2024-06-19 19:55   ` Gustavo A. R. Silva
  2024-06-19 20:33   ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Gustavo A. R. Silva @ 2024-06-19 19:55 UTC (permalink / raw)
  To: Javier Carrasco, Johan Hovold, Greg Kroah-Hartman, Kees Cook,
	Gustavo A. R. Silva, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt
  Cc: linux-usb, linux-kernel, linux-hardening, llvm



On 19/06/24 21:42, Javier Carrasco wrote:
> Use the struct_size macro to calculate the size of the pkt, which
> includes a trailing flexible array.
> 
> Suggested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks
--
Gustavo

> ---
>   drivers/usb/serial/garmin_gps.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
> index 57df6ad183ff..6d6ec7eed87c 100644
> --- a/drivers/usb/serial/garmin_gps.c
> +++ b/drivers/usb/serial/garmin_gps.c
> @@ -267,8 +267,7 @@ static int pkt_add(struct garmin_data *garmin_data_p,
>   
>   	/* process only packets containing data ... */
>   	if (data_length) {
> -		pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
> -								GFP_ATOMIC);
> +		pkt = kmalloc(struct_size(pkt, data, data_length), GFP_ATOMIC);
>   		if (!pkt)
>   			return 0;
>   
> 

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

* Re: [PATCH v2 1/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
  2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
@ 2024-06-19 19:56   ` Gustavo A. R. Silva
  2024-06-19 20:34   ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Gustavo A. R. Silva @ 2024-06-19 19:56 UTC (permalink / raw)
  To: Javier Carrasco, Johan Hovold, Greg Kroah-Hartman, Kees Cook,
	Gustavo A. R. Silva, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt
  Cc: linux-usb, linux-kernel, linux-hardening, llvm



On 19/06/24 21:42, Javier Carrasco wrote:
> Use the __counted_by compiler attribute for the data[] flexible array
> member to improve the results of array bound sanitizers.
> 
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks
--
Gustavo

> ---
>   drivers/usb/serial/garmin_gps.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
> index 670e942fdaaa..57df6ad183ff 100644
> --- a/drivers/usb/serial/garmin_gps.c
> +++ b/drivers/usb/serial/garmin_gps.c
> @@ -104,7 +104,7 @@ struct garmin_packet {
>   	int               seq;
>   	/* the real size of the data array, always > 0 */
>   	int               size;
> -	__u8              data[];
> +	__u8              data[] __counted_by(size);
>   };
>   
>   /* structure used to keep the current state of the driver */
> 

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

* Re: [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt
  2024-06-19 19:42 ` [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt Javier Carrasco
  2024-06-19 19:55   ` Gustavo A. R. Silva
@ 2024-06-19 20:33   ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2024-06-19 20:33 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Johan Hovold, Greg Kroah-Hartman, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	linux-usb, linux-kernel, linux-hardening, llvm

On Wed, Jun 19, 2024 at 09:42:45PM +0200, Javier Carrasco wrote:
> Use the struct_size macro to calculate the size of the pkt, which
> includes a trailing flexible array.
> 
> Suggested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>

Reviewed-by: Kees Cook <kees@kernel.org>

-- 
Kees Cook

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

* Re: [PATCH v2 1/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
  2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
  2024-06-19 19:56   ` Gustavo A. R. Silva
@ 2024-06-19 20:34   ` Kees Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Kees Cook @ 2024-06-19 20:34 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Johan Hovold, Greg Kroah-Hartman, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	linux-usb, linux-kernel, linux-hardening, llvm

On Wed, Jun 19, 2024 at 09:42:44PM +0200, Javier Carrasco wrote:
> Use the __counted_by compiler attribute for the data[] flexible array
> member to improve the results of array bound sanitizers.
> 
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>

Reviewed-by: Kees Cook <kees@kernel.org>

-- 
Kees Cook

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

* Re: [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
  2024-06-19 19:42 [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Javier Carrasco
  2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
  2024-06-19 19:42 ` [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt Javier Carrasco
@ 2024-07-05 12:04 ` Johan Hovold
  2 siblings, 0 replies; 8+ messages in thread
From: Johan Hovold @ 2024-07-05 12:04 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Greg Kroah-Hartman, Kees Cook, Gustavo A. R. Silva,
	Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt,
	linux-usb, linux-kernel, linux-hardening, llvm

On Wed, Jun 19, 2024 at 09:42:43PM +0200, Javier Carrasco wrote:
> The size is assigned before the first reference to the flexible array
> (see pkt_add()), which allows for a straightforward annotation without
> further modifications.
> 
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> ---
> Changes in v2:
> - Use struct_size to calculate the size of pkt.
> - Link to v1: https://lore.kernel.org/r/20240619-garmin_gps_counted_by-v1-1-d8d816f085d9@gmail.com
> 
> ---
> Javier Carrasco (2):
>       USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by
>       USB: serial: garmin_gps: use struct_size to allocate pkt

Now applied (after adding parentheses to struct_size() in the summary).

Johan

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

end of thread, other threads:[~2024-07-05 12:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-19 19:42 [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Javier Carrasco
2024-06-19 19:42 ` [PATCH v2 1/2] " Javier Carrasco
2024-06-19 19:56   ` Gustavo A. R. Silva
2024-06-19 20:34   ` Kees Cook
2024-06-19 19:42 ` [PATCH v2 2/2] USB: serial: garmin_gps: use struct_size to allocate pkt Javier Carrasco
2024-06-19 19:55   ` Gustavo A. R. Silva
2024-06-19 20:33   ` Kees Cook
2024-07-05 12:04 ` [PATCH v2 0/2] USB: serial: garmin_gps: annotate struct garmin_packet with __counted_by Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox