Given that the static calls are updated only after ->init() returns, an
added trusted key can race with the callback initializations. This can
ultimately lead into null dereference as static_call() is used unchecked.

Address the bug by updating the static calls before invoking ->init(), and
roll back on failure.

Cc: [email protected] # v5.15+
Fixes: 31de287345f4 ("KEYS: trusted: Rollback init_trusted() consistently")
Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 security/keys/trusted-keys/trusted_core.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_core.c 
b/security/keys/trusted-keys/trusted_core.c
index 0509d9955f2a..11d7d296d063 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -373,14 +373,19 @@ static int __init init_trusted(void)
                if (!get_random)
                        get_random = kernel_get_random;
 
+               static_call_update(trusted_key_seal, 
trusted_key_sources[i].ops->seal);
+               static_call_update(trusted_key_unseal, 
trusted_key_sources[i].ops->unseal);
+               static_call_update(trusted_key_get_random, get_random);
+               migratable = trusted_key_sources[i].ops->migratable;
+
                ret = trusted_key_sources[i].ops->init();
                if (!ret) {
-                       static_call_update(trusted_key_seal, 
trusted_key_sources[i].ops->seal);
-                       static_call_update(trusted_key_unseal, 
trusted_key_sources[i].ops->unseal);
-                       static_call_update(trusted_key_get_random, get_random);
-
                        trusted_key_exit = trusted_key_sources[i].ops->exit;
-                       migratable = trusted_key_sources[i].ops->migratable;
+               } else {
+                       static_call_update(trusted_key_seal, NULL);
+                       static_call_update(trusted_key_unseal, NULL);
+                       static_call_update(trusted_key_get_random, NULL);
+                       migratable = 0;
                }
 
                if (!ret || ret != -ENODEV)
-- 
2.47.3


Reply via email to