]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
cred: Add a light version of override/revert_creds()
authorVinicius Costa Gomes <vinicius.gomes@intel.com>
Thu, 7 Nov 2024 00:57:17 +0000 (16:57 -0800)
committerAmir Goldstein <amir73il@gmail.com>
Mon, 11 Nov 2024 09:45:04 +0000 (10:45 +0100)
Add a light version of override/revert_creds(), this should only be
used when the credentials in question will outlive the critical
section and the critical section doesn't change the ->usage of the
credentials.

Suggested-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
include/linux/cred.h
kernel/cred.c

index 2976f534a7a32f050410125df97e9880cc429296..e4a3155fe409d6b991fa6639005ebc233fc17dcc 100644 (file)
@@ -172,6 +172,24 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred)
                                          cred->cap_inheritable));
 }
 
+/*
+ * Override creds without bumping reference count. Caller must ensure
+ * reference remains valid or has taken reference. Almost always not the
+ * interface you want. Use override_creds()/revert_creds() instead.
+ */
+static inline const struct cred *override_creds_light(const struct cred *override_cred)
+{
+       const struct cred *old = current->cred;
+
+       rcu_assign_pointer(current->cred, override_cred);
+       return old;
+}
+
+static inline void revert_creds_light(const struct cred *revert_cred)
+{
+       rcu_assign_pointer(current->cred, revert_cred);
+}
+
 /**
  * get_new_cred_many - Get references on a new set of credentials
  * @cred: The new credentials to reference
index 075cfa7c896f978f68477e1a93f5f17944cf963a..da7da250f7c8b5ad91feb938f1e949c5ccb4914b 100644 (file)
@@ -485,7 +485,7 @@ EXPORT_SYMBOL(abort_creds);
  */
 const struct cred *override_creds(const struct cred *new)
 {
-       const struct cred *old = current->cred;
+       const struct cred *old;
 
        kdebug("override_creds(%p{%ld})", new,
               atomic_long_read(&new->usage));
@@ -499,7 +499,7 @@ const struct cred *override_creds(const struct cred *new)
         * visible to other threads under RCU.
         */
        get_new_cred((struct cred *)new);
-       rcu_assign_pointer(current->cred, new);
+       old = override_creds_light(new);
 
        kdebug("override_creds() = %p{%ld}", old,
               atomic_long_read(&old->usage));
@@ -521,7 +521,7 @@ void revert_creds(const struct cred *old)
        kdebug("revert_creds(%p{%ld})", old,
               atomic_long_read(&old->usage));
 
-       rcu_assign_pointer(current->cred, old);
+       revert_creds_light(old);
        put_cred(override);
 }
 EXPORT_SYMBOL(revert_creds);