Skip to contentSkip to conversation

landlock: Annotate struct landlock_rule with __counted_by

PATCH Applied Kees Cook 4 participants 4 messages Aug 17, 2023 – Aug 18, 2023

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 landlock: Annotate struct landlock_rule 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 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 landlock_rule.

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

Cc "Mickaël Salaün" <[email protected]>
Cc Paul Moore <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
---
 security/landlock/ruleset.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/ruleset.h +1 -1
index d43231b783e4..55b1df8f66a8 100644
@@ -67,7 +67,7 @@ struct landlock_rule {
 	 * @layers: Stack of layers, from the latest to the newest, implemented
 	 * as a flexible array member (FAM).
 	 */
-	struct landlock_layer layers[];
+	struct landlock_layer layers[] __counted_by(num_layers);
 };
 
 /**
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 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 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 landlock_rule.

Quoted 8 lines

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

Cc "Mickaël Salaün" <[email protected]>
Cc Paul Moore <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Quoted 20 lines
---
 security/landlock/ruleset.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/ruleset.h +1 -2
index d43231b783e4..55b1df8f66a8 100644
@@ -67,7 +67,7 @@ struct landlock_rule {
         * @layers: Stack of layers, from the latest to the newest, implemented
         * as a flexible array member (FAM).
         */
-       struct landlock_layer layers[];
+       struct landlock_layer layers[] __counted_by(num_layers);
 };
 
 /**
--

2.34.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 8 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 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 landlock_rule.

Quoted 8 lines

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

Cc "Mickaël Salaün" <[email protected]>
Cc Paul Moore <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>

Thanks
--
Gustavo

Quoted 17 lines
---
  security/landlock/ruleset.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/ruleset.h +1 -1
index d43231b783e4..55b1df8f66a8 100644
@@ -67,7 +67,7 @@ struct landlock_rule {
  	 * @layers: Stack of layers, from the latest to the newest, implemented
  	 * as a flexible array member (FAM).
  	 */
-	struct landlock_layer layers[];
+	struct landlock_layer layers[] __counted_by(num_layers);
  };
 
  /**
maintainer Applied
#3 lore raw
In reply to Kees Cook

Thanks! Applied to my next tree. Feel free to carry it yourself if you prefer.

Quoted 36 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 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 landlock_rule.

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

Cc "Mickaël Salaün" <[email protected]>
Cc Paul Moore <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
---
 security/landlock/ruleset.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/landlock/ruleset.h +1 -1
index d43231b783e4..55b1df8f66a8 100644
@@ -67,7 +67,7 @@ struct landlock_rule {
 	 * @layers: Stack of layers, from the latest to the newest, implemented
 	 * as a flexible array member (FAM).
 	 */
-	struct landlock_layer layers[];
+	struct landlock_layer layers[] __counted_by(num_layers);
 };
 
 /**
Signature · 1 line
2.34.1