Skip to contentSkip to conversation

security: fix the key_permission LSM hook function type

PATCH In review Sami Tolvanen 4 participants 6 messages Jun 15, 2020 – Jun 22, 2020 Security (LSM)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 security: fix the key_permission LSM hook function type

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")
---
 include/linux/lsm_hook_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/lsm_hook_defs.h +1 -1
index 6791813cd439..24f6683f1cfc 100644
@@ -360,7 +360,7 @@ LSM_HOOK(int, 0, key_alloc, struct key *key, const struct cred *cred,
 	 unsigned long flags)
 LSM_HOOK(void, LSM_RET_VOID, key_free, struct key *key)
 LSM_HOOK(int, 0, key_permission, key_ref_t key_ref, const struct cred *cred,
-	 unsigned perm)
+	 enum key_need_perm need_perm)
 LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
 #endif /* CONFIG_KEYS */
 

base-commit: b3a9e3b9622ae10064826dccb4f7a52bd88c7407

Signature · 1 line
2.27.0.290.gba653c62da-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Sami Tolvanen

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")

Thanks for fixing this!

I wonder if there is any compiler option we can turn on to catch the implicit enum/int casting that would help for these kinds of things without absolutely exploding the build warnings.

I see -Wenum-conversion, but that seems to be between enums, not between int.

I see this, but it's external:
https://noamlewis.wordpress.com/2017/10/05/type-safe-enums-in-c-using-a-clang-plugin/

-Kees

Quoted 22 lines
---
 include/linux/lsm_hook_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/lsm_hook_defs.h +1 -1
index 6791813cd439..24f6683f1cfc 100644
@@ -360,7 +360,7 @@ LSM_HOOK(int, 0, key_alloc, struct key *key, const struct cred *cred,
 	 unsigned long flags)
 LSM_HOOK(void, LSM_RET_VOID, key_free, struct key *key)
 LSM_HOOK(int, 0, key_permission, key_ref_t key_ref, const struct cred *cred,
-	 unsigned perm)
+	 enum key_need_perm need_perm)
 LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
 #endif /* CONFIG_KEYS */
 

base-commit: b3a9e3b9622ae10064826dccb4f7a52bd88c7407

Signature · 1 line
2.27.0.290.gba653c62da-goog
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 19 lines · Kees Cook
Quoted 8 lines · Sami Tolvanen

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")

Thanks for fixing this!

I wonder if there is any compiler option we can turn on to catch the implicit enum/int casting that would help for these kinds of things without absolutely exploding the build warnings.

Quoted 5 lines

I see -Wenum-conversion, but that seems to be between enums, not between int.

I see this, but it's external:
https://noamlewis.wordpress.com/2017/10/05/type-safe-enums-in-c-using-a-clang-plugin/

Unfortunately, using a looser integral type is the typical workaround for -Wenum-conversion warnings (which is on by default, GCC recently added that flag, too). For warning on enum to int, the kernel would probably blow up with warnings. I don't know of any such existing warning flag.

Quoted 33 lines

-Kees

Quoted 18 lines
---
 include/linux/lsm_hook_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/lsm_hook_defs.h +1 -1
index 6791813cd439..24f6683f1cfc 100644
@@ -360,7 +360,7 @@ LSM_HOOK(int, 0, key_alloc, struct key *key, const struct cred *cred,
       unsigned long flags)
 LSM_HOOK(void, LSM_RET_VOID, key_free, struct key *key)
 LSM_HOOK(int, 0, key_permission, key_ref_t key_ref, const struct cred *cred,
-      unsigned perm)
+      enum key_need_perm need_perm)
 LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
 #endif /* CONFIG_KEYS */
 
Quoted 4 lines

base-commit: b3a9e3b9622ae10064826dccb4f7a52bd88c7407 --
2.27.0.290.gba653c62da-goog

--
Kees Cook

--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/202006151113.7A2F638A7%40keescook.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#3 lore raw
Quoted 23 lines · Nick Desaulniers
Quoted 22 lines · Kees Cook
Quoted 8 lines · Sami Tolvanen

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")

Thanks for fixing this!

I wonder if there is any compiler option we can turn on to catch the implicit enum/int casting that would help for these kinds of things without absolutely exploding the build warnings.

I see -Wenum-conversion, but that seems to be between enums, not between int.

Quoted 8 lines
Quoted 2 lines

I see this, but it's external:
https://noamlewis.wordpress.com/2017/10/05/type-safe-enums-in-c-using-a-clang-plugin/

Unfortunately, using a looser integral type is the typical workaround for -Wenum-conversion warnings (which is on by default, GCC recently added that flag, too). For warning on enum to int, the kernel would probably blow up with warnings. I don't know of any such existing warning flag.

Yeah, I couldn't find anything either. The closest I could find to avoid one of the common classes of enum -> int conversion is the bitwise operations, which is covered by __attribute__((flag_enum)):
https://clang.llvm.org/docs/AttributeReference.html#flag-enum

Ah well. We'll let CFI find them. ;)

Signature · 1 line
Kees Cook
#4 lore raw
In reply to Sami Tolvanen
Quoted 26 lines · Sami Tolvanen

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")
---
 include/linux/lsm_hook_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/lsm_hook_defs.h +1 -1
index 6791813cd439..24f6683f1cfc 100644
@@ -360,7 +360,7 @@ LSM_HOOK(int, 0, key_alloc, struct key *key, const struct cred *cred,
 	 unsigned long flags)
 LSM_HOOK(void, LSM_RET_VOID, key_free, struct key *key)
 LSM_HOOK(int, 0, key_permission, key_ref_t key_ref, const struct cred *cred,
-	 unsigned perm)
+	 enum key_need_perm need_perm)
 LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer)
 #endif /* CONFIG_KEYS */
 
Quoted 1 line

base-commit: b3a9e3b9622ae10064826dccb4f7a52bd88c7407

David, James? Can someone please take this?

Signature · 1 line
Kees Cook
maintainer
#5 lore raw
In reply to Sami Tolvanen
Quoted 8 lines · Sami Tolvanen

Commit 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask") changed the type of the key_permission callback functions, but didn't change the type of the hook, which trips indirect call checking with Control-Flow Integrity (CFI). This change fixes the issue by changing the hook type to match the functions.

Fixes 8c0637e950d6 ("keys: Make the KEY_NEED_* perms an enum rather than a mask")

Applied to
git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git fixes-v5.8

NOTE: please cc: the LSM list with patches such as these.

Signature · 2 lines
James Morris
<[email protected]>