linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: Annotate struct urb_priv with __counted_by
@ 2023-09-15 19:58 Kees Cook
  2023-09-29 19:21 ` Kees Cook
  2023-10-02 11:42 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2023-09-15 19:58 UTC (permalink / raw)
  To: Alan Stern
  Cc: Kees Cook, Greg Kroah-Hartman, Mathias Nyman, linux-usb,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-kernel, llvm,
	linux-hardening

Prepare for the coming implementation by GCC and Clang of the __counted_by
attribute. Flexible array members annotated with __counted_by can have
their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
(for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).

As found with Coccinelle[1], add __counted_by for struct urb_priv.

[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci

Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Mathias Nyman <mathias.nyman@intel.com>
Cc: linux-usb@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/usb/host/ohci.h | 2 +-
 drivers/usb/host/xhci.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/host/ohci.h b/drivers/usb/host/ohci.h
index aac6285b37f8..1aba22784e05 100644
--- a/drivers/usb/host/ohci.h
+++ b/drivers/usb/host/ohci.h
@@ -337,7 +337,7 @@ typedef struct urb_priv {
 	u16			length;		// # tds in this request
 	u16			td_cnt;		// tds already serviced
 	struct list_head	pending;
-	struct td		*td[];		// all TDs in this request
+	struct td		*td[] __counted_by(length); // all TDs in this request
 
 } urb_priv_t;
 
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 7e282b4522c0..2f21c3a8565c 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1666,7 +1666,7 @@ struct xhci_scratchpad {
 struct urb_priv {
 	int	num_tds;
 	int	num_tds_done;
-	struct	xhci_td	td[];
+	struct	xhci_td	td[] __counted_by(num_tds);
 };
 
 /*
-- 
2.34.1


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

* Re: [PATCH] usb: Annotate struct urb_priv with __counted_by
  2023-09-15 19:58 [PATCH] usb: Annotate struct urb_priv with __counted_by Kees Cook
@ 2023-09-29 19:21 ` Kees Cook
  2023-10-02 11:42 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2023-09-29 19:21 UTC (permalink / raw)
  To: Alan Stern, Kees Cook
  Cc: Greg Kroah-Hartman, Mathias Nyman, linux-usb, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-kernel, llvm, linux-hardening

On Fri, 15 Sep 2023 12:58:16 -0700, Kees Cook wrote:
> Prepare for the coming implementation by GCC and Clang of the __counted_by
> attribute. Flexible array members annotated with __counted_by can have
> their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
> (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
> functions).
> 
> As found with Coccinelle[1], add __counted_by for struct urb_priv.
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] usb: Annotate struct urb_priv with __counted_by
      https://git.kernel.org/kees/c/a5683963b3f5

Take care,

-- 
Kees Cook


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

* Re: [PATCH] usb: Annotate struct urb_priv with __counted_by
  2023-09-15 19:58 [PATCH] usb: Annotate struct urb_priv with __counted_by Kees Cook
  2023-09-29 19:21 ` Kees Cook
@ 2023-10-02 11:42 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2023-10-02 11:42 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alan Stern, Mathias Nyman, linux-usb, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-kernel, llvm, linux-hardening

On Fri, Sep 15, 2023 at 12:58:16PM -0700, Kees Cook wrote:
> Prepare for the coming implementation by GCC and Clang of the __counted_by
> attribute. Flexible array members annotated with __counted_by can have
> their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
> (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
> functions).
> 
> As found with Coccinelle[1], add __counted_by for struct urb_priv.
> 
> [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
> 
> Cc: Alan Stern <stern@rowland.harvard.edu>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Mathias Nyman <mathias.nyman@intel.com>
> Cc: linux-usb@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  drivers/usb/host/ohci.h | 2 +-
>  drivers/usb/host/xhci.h | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)

Thanks for taking these changes through your tree, I was too slow...

greg k-h

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

end of thread, other threads:[~2023-10-02 11:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-15 19:58 [PATCH] usb: Annotate struct urb_priv with __counted_by Kees Cook
2023-09-29 19:21 ` Kees Cook
2023-10-02 11:42 ` Greg Kroah-Hartman

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).