Skip to contentSkip to conversation

selinux: Annotate struct sidtab_str_cache with __counted_by

PATCH Applied Kees Cook 3 participants 5 messages Aug 17, 2023 – Sep 12, 2023 SELinux security module

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 selinux: Annotate struct sidtab_str_cache 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 sidtab_str_cache.

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

Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>
Cc Ondrej Mosnacek <[email protected]>
---
 security/selinux/ss/sidtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/ss/sidtab.c +1 -1
index d8ead463b8df..732fd8e22a12 100644
@@ -25,7 +25,7 @@ struct sidtab_str_cache {
 	struct list_head lru_member;
 	struct sidtab_entry *parent;
 	u32 len;
-	char str[];
+	char str[] __counted_by(len);
 };
 
 #define index_to_sid(index) ((index) + SECINITSID_NUM + 1)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
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 sidtab_str_cache.

Quoted 8 lines

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

Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>
Cc Ondrej Mosnacek <[email protected]>

Thanks
--
Gustavo

Quoted 17 lines
---
  security/selinux/ss/sidtab.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/ss/sidtab.c +1 -1
index d8ead463b8df..732fd8e22a12 100644
@@ -25,7 +25,7 @@ struct sidtab_str_cache {
  	struct list_head lru_member;
  	struct sidtab_entry *parent;
  	u32 len;
-	char str[];
+	char str[] __counted_by(len);
  };
 
  #define index_to_sid(index) ((index) + SECINITSID_NUM + 1)
maintainer
#2 lore raw
In reply to Kees Cook
Quoted 17 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 sidtab_str_cache.

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

Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>
Cc Ondrej Mosnacek <[email protected]>
Quoted 3 lines
---
 security/selinux/ss/sidtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This also looks good to me, but similarly to the audit patch, since we are at -rc6 I'm going to wait to merge this until after the merge window. I'll send a follow up email once it's merged.

Thanks.

Quoted 15 lines
security/selinux/ss/sidtab.c +1 -2
index d8ead463b8df..732fd8e22a12 100644
@@ -25,7 +25,7 @@ struct sidtab_str_cache {
        struct list_head lru_member;
        struct sidtab_entry *parent;
        u32 len;
-       char str[];
+       char str[] __counted_by(len);
 };
 
 #define index_to_sid(index) ((index) + SECINITSID_NUM + 1)
--

2.34.1

Signature · 1 line
paul-moore.com
author
#3 lore raw
Quoted 22 lines · Paul Moore
Quoted 17 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 sidtab_str_cache.

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

Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>
Cc Ondrej Mosnacek <[email protected]>
Quoted 3 lines
---
 security/selinux/ss/sidtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines

This also looks good to me, but similarly to the audit patch, since we are at -rc6 I'm going to wait to merge this until after the merge window. I'll send a follow up email once it's merged.

Thanks! Yeah, there's no rush. :)

Signature · 1 line
Kees Cook
maintainer Applied
#4 lore raw
In reply to Kees Cook
Quoted 17 lines · On Aug 17, 2023 Kees Cook <[email protected]> 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 sidtab_str_cache.

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

Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>
Cc Ondrej Mosnacek <[email protected]>
Quoted 3 lines
---
 security/selinux/ss/sidtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Merged into selinux/next, thanks.

--
paul-moore.com