* [PATCH 0/8] creds: add {scoped_}with_kernel_creds()
@ 2025-11-02 23:12 Christian Brauner
2025-11-02 23:12 ` [PATCH 1/8] cleanup: fix scoped_class() Christian Brauner
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
Hey,
A few months ago I did work to make override_creds()/revert_creds()
completely reference count free - mostly for the sake of
overlayfs but it has been beneficial to everyone using this.
In a recent pull request from Jens that introduced another round of
override_creds()/revert_creds() for nbd Linus asked whether we could
avoide the prepare_kernel_creds() calls that duplicate the kernel
credentials and then drop them again later.
Yes, we can actually. We can use the guard infrastructure to completely
avoid the allocation and then also to never expose the temporary
variable to hold the kernel credentials anywhere in the callers.
So add with_kernel_creds() and scoped_with_kernel_creds() for this
purpose. Also take the opportunity to fixup the scoped_class() macro I
introduced two cycles ago.
I've put this into kernel-6.19.cred now. Linus, not sure if you're
paying attention but if you want you can give this a final look.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
Christian Brauner (8):
cleanup: fix scoped_class()
cred: add kernel_cred() helper
cred: make init_cred static
cred: add {scoped_}with_kernel_creds
firmware: don't copy kernel creds
nbd: don't copy kernel creds
target: don't copy kernel creds
unix: don't copy creds
drivers/base/firmware_loader/main.c | 59 +++++++++++++++--------------------
drivers/block/nbd.c | 17 ++--------
drivers/target/target_core_configfs.c | 14 ++-------
include/linux/cleanup.h | 15 ++++-----
include/linux/cred.h | 18 +++++++++++
include/linux/init_task.h | 1 -
init/init_task.c | 27 ++++++++++++++++
kernel/cred.c | 27 ----------------
net/unix/af_unix.c | 17 +++-------
security/keys/process_keys.c | 2 +-
10 files changed, 87 insertions(+), 110 deletions(-)
---
base-commit: dcb6fa37fd7bc9c3d2b066329b0d27dedf8becaa
change-id: 20251103-work-creds-init_cred-114f45a2676f
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/8] cleanup: fix scoped_class()
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 2/8] cred: add kernel_cred() helper Christian Brauner
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
This is a class, not a guard so why on earth is it checking for guard
pointers or conditional lock acquisition? None of it makes any sense at
all.
I'm not sure what happened back then. Maybe I had a brief psychedelic
period that I completely forgot about and spaced out into a zone where
that initial macro implementation made any sense at all.
Fixes: 5c21c5f22d07 ("cleanup: add a scoped version of CLASS()")
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/cleanup.h | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 2573585b7f06..19c7e475d3a4 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -290,15 +290,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
class_##_name##_t var __cleanup(class_##_name##_destructor) = \
class_##_name##_constructor
-#define scoped_class(_name, var, args) \
- for (CLASS(_name, var)(args); \
- __guard_ptr(_name)(&var) || !__is_cond_ptr(_name); \
- ({ goto _label; })) \
- if (0) { \
-_label: \
- break; \
+#define __scoped_class(_name, var, _label, args...) \
+ for (CLASS(_name, var)(args); ; ({ goto _label; })) \
+ if (0) { \
+_label: \
+ break; \
} else
+#define scoped_class(_name, var, args...) \
+ __scoped_class(_name, var, __UNIQUE_ID(label), args)
+
/*
* DEFINE_GUARD(name, type, lock, unlock):
* trivial wrapper around DEFINE_CLASS() above specifically
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/8] cred: add kernel_cred() helper
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
2025-11-02 23:12 ` [PATCH 1/8] cleanup: fix scoped_class() Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 3/8] cred: make init_cred static Christian Brauner
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
Access kernel creds based off of init_task. This will let us avoid any
direct access to init_cred.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-1-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/cred.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/include/linux/cred.h b/include/linux/cred.h
index 89ae50ad2ace..8ab3718184ad 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -20,6 +20,8 @@
struct cred;
struct inode;
+extern struct task_struct init_task;
+
/*
* COW Supplementary groups list
*/
@@ -156,6 +158,11 @@ extern struct cred *prepare_exec_creds(void);
extern int commit_creds(struct cred *);
extern void abort_creds(struct cred *);
extern struct cred *prepare_kernel_cred(struct task_struct *);
+static inline const struct cred *kernel_cred(void)
+{
+ /* shut up sparse */
+ return rcu_dereference_raw(init_task.cred);
+}
extern int set_security_override(struct cred *, u32);
extern int set_security_override_from_ctx(struct cred *, const char *);
extern int set_create_files_as(struct cred *, struct inode *);
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/8] cred: make init_cred static
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
2025-11-02 23:12 ` [PATCH 1/8] cleanup: fix scoped_class() Christian Brauner
2025-11-02 23:12 ` [PATCH 2/8] cred: add kernel_cred() helper Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 4/8] cred: add {scoped_}with_kernel_creds Christian Brauner
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
There's zero need to expose struct init_cred. The very few places that
need access can just go through init_task which is already exported.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-2-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/init_task.h | 1 -
init/init_task.c | 27 +++++++++++++++++++++++++++
kernel/cred.c | 27 ---------------------------
security/keys/process_keys.c | 2 +-
4 files changed, 28 insertions(+), 29 deletions(-)
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index bccb3f1f6262..a6cb241ea00c 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -25,7 +25,6 @@
extern struct files_struct init_files;
extern struct fs_struct init_fs;
extern struct nsproxy init_nsproxy;
-extern struct cred init_cred;
#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
#define INIT_PREV_CPUTIME(x) .prev_cputime = { \
diff --git a/init/init_task.c b/init/init_task.c
index a55e2189206f..d970a847b657 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -62,6 +62,33 @@ unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)] = {
};
#endif
+/* init to 2 - one for init_task, one to ensure it is never freed */
+static struct group_info init_groups = { .usage = REFCOUNT_INIT(2) };
+
+/*
+ * The initial credentials for the initial task
+ */
+static struct cred init_cred = {
+ .usage = ATOMIC_INIT(4),
+ .uid = GLOBAL_ROOT_UID,
+ .gid = GLOBAL_ROOT_GID,
+ .suid = GLOBAL_ROOT_UID,
+ .sgid = GLOBAL_ROOT_GID,
+ .euid = GLOBAL_ROOT_UID,
+ .egid = GLOBAL_ROOT_GID,
+ .fsuid = GLOBAL_ROOT_UID,
+ .fsgid = GLOBAL_ROOT_GID,
+ .securebits = SECUREBITS_DEFAULT,
+ .cap_inheritable = CAP_EMPTY_SET,
+ .cap_permitted = CAP_FULL_SET,
+ .cap_effective = CAP_FULL_SET,
+ .cap_bset = CAP_FULL_SET,
+ .user = INIT_USER,
+ .user_ns = &init_user_ns,
+ .group_info = &init_groups,
+ .ucounts = &init_ucounts,
+};
+
/*
* Set up the first task table, touch at your own risk!. Base=0,
* limit=0x1fffff (=2MB)
diff --git a/kernel/cred.c b/kernel/cred.c
index dbf6b687dc5c..ac87ed9d43b1 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -35,33 +35,6 @@ do { \
static struct kmem_cache *cred_jar;
-/* init to 2 - one for init_task, one to ensure it is never freed */
-static struct group_info init_groups = { .usage = REFCOUNT_INIT(2) };
-
-/*
- * The initial credentials for the initial task
- */
-struct cred init_cred = {
- .usage = ATOMIC_INIT(4),
- .uid = GLOBAL_ROOT_UID,
- .gid = GLOBAL_ROOT_GID,
- .suid = GLOBAL_ROOT_UID,
- .sgid = GLOBAL_ROOT_GID,
- .euid = GLOBAL_ROOT_UID,
- .egid = GLOBAL_ROOT_GID,
- .fsuid = GLOBAL_ROOT_UID,
- .fsgid = GLOBAL_ROOT_GID,
- .securebits = SECUREBITS_DEFAULT,
- .cap_inheritable = CAP_EMPTY_SET,
- .cap_permitted = CAP_FULL_SET,
- .cap_effective = CAP_FULL_SET,
- .cap_bset = CAP_FULL_SET,
- .user = INIT_USER,
- .user_ns = &init_user_ns,
- .group_info = &init_groups,
- .ucounts = &init_ucounts,
-};
-
/*
* The RCU callback to actually dispose of a set of credentials
*/
diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c
index b5d5333ab330..a63c46bb2d14 100644
--- a/security/keys/process_keys.c
+++ b/security/keys/process_keys.c
@@ -51,7 +51,7 @@ static struct key *get_user_register(struct user_namespace *user_ns)
if (!reg_keyring) {
reg_keyring = keyring_alloc(".user_reg",
user_ns->owner, INVALID_GID,
- &init_cred,
+ kernel_cred(),
KEY_POS_WRITE | KEY_POS_SEARCH |
KEY_USR_VIEW | KEY_USR_READ,
0,
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 4/8] cred: add {scoped_}with_kernel_creds
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (2 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 3/8] cred: make init_cred static Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 5/8] firmware: don't copy kernel creds Christian Brauner
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
Add a new cleanup class for override creds. We can make use of this in a
bunch of places going forward.
Based on this add with_kernel_creds() and scoped_with_kernel_creds()
that can be used to temporarily assume kernel credentials for specific
tasks such as firmware loading, or coredump socket connections. At no
point will the caller interact with the kernel credentials directly.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-1-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/cred.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/cred.h b/include/linux/cred.h
index 8ab3718184ad..c4f7630763f4 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -187,6 +187,17 @@ static inline const struct cred *revert_creds(const struct cred *revert_cred)
return rcu_replace_pointer(current->cred, revert_cred, 1);
}
+DEFINE_CLASS(override_creds,
+ const struct cred *,
+ revert_creds(_T),
+ override_creds(override_cred), const struct cred *override_cred)
+
+#define with_kernel_creds() \
+ CLASS(override_creds, __UNIQUE_ID(cred))(kernel_cred())
+
+#define scoped_with_kernel_creds() \
+ scoped_class(override_creds, __UNIQUE_ID(cred), kernel_cred())
+
/**
* get_cred_many - Get references on a set of credentials
* @cred: The credentials to reference
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 5/8] firmware: don't copy kernel creds
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (3 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 4/8] cred: add {scoped_}with_kernel_creds Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 6/8] nbd: " Christian Brauner
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
No need to copy kernel credentials.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-3-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
drivers/base/firmware_loader/main.c | 59 ++++++++++++++++---------------------
1 file changed, 25 insertions(+), 34 deletions(-)
diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
index 6942c62fa59d..bee3050a20d9 100644
--- a/drivers/base/firmware_loader/main.c
+++ b/drivers/base/firmware_loader/main.c
@@ -829,8 +829,6 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
size_t offset, u32 opt_flags)
{
struct firmware *fw = NULL;
- struct cred *kern_cred = NULL;
- const struct cred *old_cred;
bool nondirect = false;
int ret;
@@ -871,45 +869,38 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
* called by a driver when serving an unrelated request from userland, we use
* the kernel credentials to read the file.
*/
- kern_cred = prepare_kernel_cred(&init_task);
- if (!kern_cred) {
- ret = -ENOMEM;
- goto out;
- }
- old_cred = override_creds(kern_cred);
+ scoped_with_kernel_creds() {
+ ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
- ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
-
- /* Only full reads can support decompression, platform, and sysfs. */
- if (!(opt_flags & FW_OPT_PARTIAL))
- nondirect = true;
+ /* Only full reads can support decompression, platform, and sysfs. */
+ if (!(opt_flags & FW_OPT_PARTIAL))
+ nondirect = true;
#ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
- if (ret == -ENOENT && nondirect)
- ret = fw_get_filesystem_firmware(device, fw->priv, ".zst",
- fw_decompress_zstd);
+ if (ret == -ENOENT && nondirect)
+ ret = fw_get_filesystem_firmware(device, fw->priv, ".zst",
+ fw_decompress_zstd);
#endif
#ifdef CONFIG_FW_LOADER_COMPRESS_XZ
- if (ret == -ENOENT && nondirect)
- ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
- fw_decompress_xz);
+ if (ret == -ENOENT && nondirect)
+ ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
+ fw_decompress_xz);
#endif
- if (ret == -ENOENT && nondirect)
- ret = firmware_fallback_platform(fw->priv);
+ if (ret == -ENOENT && nondirect)
+ ret = firmware_fallback_platform(fw->priv);
- if (ret) {
- if (!(opt_flags & FW_OPT_NO_WARN))
- dev_warn(device,
- "Direct firmware load for %s failed with error %d\n",
- name, ret);
- if (nondirect)
- ret = firmware_fallback_sysfs(fw, name, device,
- opt_flags, ret);
- } else
- ret = assign_fw(fw, device);
-
- revert_creds(old_cred);
- put_cred(kern_cred);
+ if (ret) {
+ if (!(opt_flags & FW_OPT_NO_WARN))
+ dev_warn(device,
+ "Direct firmware load for %s failed with error %d\n",
+ name, ret);
+ if (nondirect)
+ ret = firmware_fallback_sysfs(fw, name, device,
+ opt_flags, ret);
+ } else {
+ ret = assign_fw(fw, device);
+ }
+ }
out:
if (ret < 0) {
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 6/8] nbd: don't copy kernel creds
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (4 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 5/8] firmware: don't copy kernel creds Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 7/8] target: " Christian Brauner
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
No need to copy kernel credentials.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-4-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
drivers/block/nbd.c | 17 ++---------------
1 file changed, 2 insertions(+), 15 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index a853c65ac65d..1f0d89e21ec8 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -52,7 +52,6 @@
static DEFINE_IDR(nbd_index_idr);
static DEFINE_MUTEX(nbd_index_mutex);
static struct workqueue_struct *nbd_del_wq;
-static struct cred *nbd_cred;
static int nbd_total_devices = 0;
struct nbd_sock {
@@ -555,7 +554,6 @@ static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
int result;
struct msghdr msg = {} ;
unsigned int noreclaim_flag;
- const struct cred *old_cred;
if (unlikely(!sock)) {
dev_err_ratelimited(disk_to_dev(nbd->disk),
@@ -564,10 +562,10 @@ static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
return -EINVAL;
}
- old_cred = override_creds(nbd_cred);
-
msg.msg_iter = *iter;
+ with_kernel_creds();
+
noreclaim_flag = memalloc_noreclaim_save();
do {
sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
@@ -590,8 +588,6 @@ static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
memalloc_noreclaim_restore(noreclaim_flag);
- revert_creds(old_cred);
-
return result;
}
@@ -2683,15 +2679,7 @@ static int __init nbd_init(void)
return -ENOMEM;
}
- nbd_cred = prepare_kernel_cred(&init_task);
- if (!nbd_cred) {
- destroy_workqueue(nbd_del_wq);
- unregister_blkdev(NBD_MAJOR, "nbd");
- return -ENOMEM;
- }
-
if (genl_register_family(&nbd_genl_family)) {
- put_cred(nbd_cred);
destroy_workqueue(nbd_del_wq);
unregister_blkdev(NBD_MAJOR, "nbd");
return -EINVAL;
@@ -2746,7 +2734,6 @@ static void __exit nbd_cleanup(void)
/* Also wait for nbd_dev_remove_work() completes */
destroy_workqueue(nbd_del_wq);
- put_cred(nbd_cred);
idr_destroy(&nbd_index_idr);
unregister_blkdev(NBD_MAJOR, "nbd");
}
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 7/8] target: don't copy kernel creds
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (5 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 6/8] nbd: " Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-02 23:12 ` [PATCH 8/8] unix: don't copy creds Christian Brauner
2025-11-03 15:59 ` [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Jens Axboe
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
Get rid of all the boilerplate and tightly scope when the task runs with
kernel creds.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-5-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
drivers/target/target_core_configfs.c | 14 ++------------
1 file changed, 2 insertions(+), 12 deletions(-)
diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index b19acd662726..9e51c535ba8c 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -3670,8 +3670,6 @@ static int __init target_core_init_configfs(void)
{
struct configfs_subsystem *subsys = &target_core_fabrics;
struct t10_alua_lu_gp *lu_gp;
- struct cred *kern_cred;
- const struct cred *old_cred;
int ret;
pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
@@ -3748,16 +3746,8 @@ static int __init target_core_init_configfs(void)
if (ret < 0)
goto out;
- /* We use the kernel credentials to access the target directory */
- kern_cred = prepare_kernel_cred(&init_task);
- if (!kern_cred) {
- ret = -ENOMEM;
- goto out;
- }
- old_cred = override_creds(kern_cred);
- target_init_dbroot();
- revert_creds(old_cred);
- put_cred(kern_cred);
+ scoped_with_kernel_creds()
+ target_init_dbroot();
return 0;
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 8/8] unix: don't copy creds
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (6 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 7/8] target: " Christian Brauner
@ 2025-11-02 23:12 ` Christian Brauner
2025-11-03 15:59 ` [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Jens Axboe
8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2025-11-02 23:12 UTC (permalink / raw)
To: Jens Axboe, Linus Torvalds; +Cc: linux-block, linux-fsdevel, Christian Brauner
No need to copy kernel credentials.
Link: https://patch.msgid.link/20251031-work-creds-init_cred-v1-6-cbf0400d6e0e@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
net/unix/af_unix.c | 17 ++++-------------
1 file changed, 4 insertions(+), 13 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 768098dec231..68c94f49f7b5 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1210,25 +1210,16 @@ static struct sock *unix_find_bsd(struct sockaddr_un *sunaddr, int addr_len,
unix_mkname_bsd(sunaddr, addr_len);
if (flags & SOCK_COREDUMP) {
- const struct cred *cred;
- struct cred *kcred;
struct path root;
- kcred = prepare_kernel_cred(&init_task);
- if (!kcred) {
- err = -ENOMEM;
- goto fail;
- }
-
task_lock(&init_task);
get_fs_root(init_task.fs, &root);
task_unlock(&init_task);
- cred = override_creds(kcred);
- err = vfs_path_lookup(root.dentry, root.mnt, sunaddr->sun_path,
- LOOKUP_BENEATH | LOOKUP_NO_SYMLINKS |
- LOOKUP_NO_MAGICLINKS, &path);
- put_cred(revert_creds(cred));
+ scoped_with_kernel_creds()
+ err = vfs_path_lookup(root.dentry, root.mnt, sunaddr->sun_path,
+ LOOKUP_BENEATH | LOOKUP_NO_SYMLINKS |
+ LOOKUP_NO_MAGICLINKS, &path);
path_put(&root);
if (err)
goto fail;
--
2.47.3
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 0/8] creds: add {scoped_}with_kernel_creds()
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
` (7 preceding siblings ...)
2025-11-02 23:12 ` [PATCH 8/8] unix: don't copy creds Christian Brauner
@ 2025-11-03 15:59 ` Jens Axboe
8 siblings, 0 replies; 10+ messages in thread
From: Jens Axboe @ 2025-11-03 15:59 UTC (permalink / raw)
To: Christian Brauner, Linus Torvalds; +Cc: linux-block, linux-fsdevel
On 11/2/25 4:12 PM, Christian Brauner wrote:
> Hey,
>
> A few months ago I did work to make override_creds()/revert_creds()
> completely reference count free - mostly for the sake of
> overlayfs but it has been beneficial to everyone using this.
>
> In a recent pull request from Jens that introduced another round of
> override_creds()/revert_creds() for nbd Linus asked whether we could
> avoide the prepare_kernel_creds() calls that duplicate the kernel
> credentials and then drop them again later.
>
> Yes, we can actually. We can use the guard infrastructure to completely
> avoid the allocation and then also to never expose the temporary
> variable to hold the kernel credentials anywhere in the callers.
>
> So add with_kernel_creds() and scoped_with_kernel_creds() for this
> purpose. Also take the opportunity to fixup the scoped_class() macro I
> introduced two cycles ago.
>
> I've put this into kernel-6.19.cred now. Linus, not sure if you're
> paying attention but if you want you can give this a final look.
>
> Signed-off-by: Christian Brauner <brauner@kernel.org>
> ---
> Christian Brauner (8):
> cleanup: fix scoped_class()
> cred: add kernel_cred() helper
> cred: make init_cred static
> cred: add {scoped_}with_kernel_creds
> firmware: don't copy kernel creds
> nbd: don't copy kernel creds
> target: don't copy kernel creds
> unix: don't copy creds
>
> drivers/base/firmware_loader/main.c | 59 +++++++++++++++--------------------
> drivers/block/nbd.c | 17 ++--------
> drivers/target/target_core_configfs.c | 14 ++-------
> include/linux/cleanup.h | 15 ++++-----
> include/linux/cred.h | 18 +++++++++++
> include/linux/init_task.h | 1 -
> init/init_task.c | 27 ++++++++++++++++
> kernel/cred.c | 27 ----------------
> net/unix/af_unix.c | 17 +++-------
> security/keys/process_keys.c | 2 +-
> 10 files changed, 87 insertions(+), 110 deletions(-)
Looks good to me, and love diffstats like that:
Reviewed-by: Jens Axboe <axboe@kernel.dk>
--
Jens Axboe
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-11-03 15:59 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-02 23:12 [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Christian Brauner
2025-11-02 23:12 ` [PATCH 1/8] cleanup: fix scoped_class() Christian Brauner
2025-11-02 23:12 ` [PATCH 2/8] cred: add kernel_cred() helper Christian Brauner
2025-11-02 23:12 ` [PATCH 3/8] cred: make init_cred static Christian Brauner
2025-11-02 23:12 ` [PATCH 4/8] cred: add {scoped_}with_kernel_creds Christian Brauner
2025-11-02 23:12 ` [PATCH 5/8] firmware: don't copy kernel creds Christian Brauner
2025-11-02 23:12 ` [PATCH 6/8] nbd: " Christian Brauner
2025-11-02 23:12 ` [PATCH 7/8] target: " Christian Brauner
2025-11-02 23:12 ` [PATCH 8/8] unix: don't copy creds Christian Brauner
2025-11-03 15:59 ` [PATCH 0/8] creds: add {scoped_}with_kernel_creds() Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).