Skip to contentSkip to conversation

xfrm: Annotate struct xfrm_sec_ctx with __counted_by

PATCH Applied Kees Cook 3 participants 3 messages Oct 3, 2023 – Oct 6, 2023

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 xfrm: Annotate struct xfrm_sec_ctx with __counted_by

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

---
 include/uapi/linux/xfrm.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/uapi/linux/xfrm.h +2 -1
index 23543c33fee8..6a77328be114 100644
@@ -4,6 +4,7 @@
 
 #include <linux/in6.h>
 #include <linux/types.h>
+#include <linux/stddef.h>
 
 /* All of the structures in this file may not change size as they are
  * passed into the kernel from userspace via netlink sockets.
@@ -33,7 +34,7 @@ struct xfrm_sec_ctx {
 	__u8	ctx_alg;
 	__u16	ctx_len;
 	__u32	ctx_sid;
-	char	ctx_str[];
+	char	ctx_str[] __counted_by(ctx_len);
 };
 
 /* Security Context Domains of Interpretation */
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Kees Cook

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 via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Quoted 8 lines

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

Thanks
--
Gustavo

Quoted 25 lines
---
  include/uapi/linux/xfrm.h | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
include/uapi/linux/xfrm.h +2 -1
index 23543c33fee8..6a77328be114 100644
@@ -4,6 +4,7 @@
 
  #include <linux/in6.h>
  #include <linux/types.h>
+#include <linux/stddef.h>
 
  /* All of the structures in this file may not change size as they are
   * passed into the kernel from userspace via netlink sockets.
@@ -33,7 +34,7 @@ struct xfrm_sec_ctx {
  	__u8	ctx_alg;
  	__u16	ctx_len;
  	__u32	ctx_sid;
-	char	ctx_str[];
+	char	ctx_str[] __counted_by(ctx_len);
  };
 
  /* Security Context Domains of Interpretation */
maintainer Applied
#2 lore raw
In reply to Kees Cook
Quoted 6 lines · Kees Cook

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 via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Quoted 8 lines

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

Applied to ipsec-next, thanks!