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>
---
include/linux/cred.h | 18 ++++++++++++++++++
kernel/cred.c | 6 +++---
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/include/linux/cred.h b/include/linux/cred.h
index 2976f534a7a3..e4a3155fe409 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -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
diff --git a/kernel/cred.c b/kernel/cred.c
index 075cfa7c896f..da7da250f7c8 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -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);
--
2.47.0
Hi Vinicius, kernel test robot noticed the following build warnings: [auto build test WARNING on next-20241105] [also build test WARNING on v6.12-rc6] [cannot apply to brauner-vfs/vfs.all linus/master v6.12-rc6 v6.12-rc5 v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Vinicius-Costa-Gomes/cred-Add-a-light-version-of-override-revert_creds/20241106-033748 base: next-20241105 patch link: https://lore.kernel.org/r/20241105193514.828616-2-vinicius.gomes%40intel.com patch subject: [PATCH overlayfs-next v3 1/4] cred: Add a light version of override/revert_creds() config: x86_64-randconfig-121-20241106 (https://download.01.org/0day-ci/archive/20241107/202411070234.EOrhSGRU-lkp@intel.com/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241107/202411070234.EOrhSGRU-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202411070234.EOrhSGRU-lkp@intel.com/ sparse warnings: (new ones prefixed by >>) kernel/cred.c:104:9: sparse: sparse: incompatible types in comparison expression (different address spaces): kernel/cred.c:104:9: sparse: struct cred * kernel/cred.c:104:9: sparse: struct cred const [noderef] __rcu * kernel/cred.c:105:9: sparse: sparse: incompatible types in comparison expression (different address spaces): kernel/cred.c:105:9: sparse: struct cred * kernel/cred.c:105:9: sparse: struct cred const [noderef] __rcu * kernel/cred.c:121:9: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct atomic64_t const [usertype] *v @@ got struct atomic64_t const [noderef] __rcu * @@ kernel/cred.c:121:9: sparse: expected struct atomic64_t const [usertype] *v kernel/cred.c:121:9: sparse: got struct atomic64_t const [noderef] __rcu * kernel/cred.c:124:22: sparse: sparse: cast removes address space '__rcu' of expression kernel/cred.c:127:17: sparse: sparse: cast removes address space '__rcu' of expression kernel/cred.c:218:13: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@ kernel/cred.c:218:13: sparse: expected struct cred const *old kernel/cred.c:218:13: sparse: got struct cred const [noderef] __rcu *cred kernel/cred.c:305:47: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct cred const *cred @@ got struct cred const [noderef] __rcu *cred @@ kernel/cred.c:305:47: sparse: expected struct cred const *cred kernel/cred.c:305:47: sparse: got struct cred const [noderef] __rcu *cred kernel/cred.c:305:30: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct cred const [noderef] __rcu *real_cred @@ got struct cred const * @@ kernel/cred.c:305:30: sparse: expected struct cred const [noderef] __rcu *real_cred kernel/cred.c:305:30: sparse: got struct cred const * kernel/cred.c:306:17: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct atomic64_t const [usertype] *v @@ got struct atomic64_t const [noderef] __rcu * @@ kernel/cred.c:306:17: sparse: expected struct atomic64_t const [usertype] *v kernel/cred.c:306:17: sparse: got struct atomic64_t const [noderef] __rcu * kernel/cred.c:344:32: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct cred const [noderef] __rcu *real_cred @@ got struct cred const * @@ kernel/cred.c:344:32: sparse: expected struct cred const [noderef] __rcu *real_cred kernel/cred.c:344:32: sparse: got struct cred const * kernel/cred.c:395:38: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *real_cred @@ kernel/cred.c:395:38: sparse: expected struct cred const *old kernel/cred.c:395:38: sparse: got struct cred const [noderef] __rcu *real_cred kernel/cred.c:400:9: sparse: sparse: incompatible types in comparison expression (different address spaces): kernel/cred.c:400:9: sparse: struct cred const [noderef] __rcu * kernel/cred.c:400:9: sparse: struct cred const * kernel/cred.c:519:46: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *override @@ got struct cred const [noderef] __rcu *cred @@ kernel/cred.c:519:46: sparse: expected struct cred const *override kernel/cred.c:519:46: sparse: got struct cred const [noderef] __rcu *cred kernel/cred.c:301:19: sparse: sparse: dereference of noderef expression kernel/cred.c: note: in included file: >> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@ include/linux/cred.h:182:41: sparse: expected struct cred const *old include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred vim +182 include/linux/cred.h 174 175 /* 176 * Override creds without bumping reference count. Caller must ensure 177 * reference remains valid or has taken reference. Almost always not the 178 * interface you want. Use override_creds()/revert_creds() instead. 179 */ 180 static inline const struct cred *override_creds_light(const struct cred *override_cred) 181 { > 182 const struct cred *old = current->cred; 183 184 rcu_assign_pointer(current->cred, override_cred); 185 return old; 186 } 187 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2024 Red Hat, Inc.