Block fixes for 6.18-rc3
Subsystems (11)
Jens Axboe
maintainerHi Linus,
A few fixes for block that should go into the 6.18-rc3 kernel release. This pull request contains:
- Fix dma alignment for PI
- Fix some selinux bogosity with nbd, where sendmsg would get rejected
Please pull!
The following changes since commit f0624c6646435c1b56652193cce3e34062d50e3f:
Merge tag 'nvme-6.18-2025-10-16' of git://git.infradead.org/nvme into block-6.18 (2025-10-16 13:25:40 -0600)
are available in the Git repository at:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git tags/block-6.18-20251023
for you to fetch changes up to 4c8cf6bd28d6fea23819f082ddc8063fd6fa963a:
block: require LBA dma_alignment when using PI (2025-10-22 10:02:54 -0600)
----------------------------------------------------------------
block-6.18-20251023
----------------------------------------------------------------
Christoph Hellwig (1):
block: require LBA dma_alignment when using PI
Ondrej Mosnacek (1):
nbd: override creds to kernel when calling sock_{send,recv}msg()
block/blk-settings.c | 10 ++++++++++ drivers/block/nbd.c | 15 +++++++++++++++ 2 files changed, 25 insertions(+)
Signature · 1 line
Jens Axboe
[ Adding LSM people. Also Christian, because he did the cred refcount cleanup with override_creds() and friends last year, and I'm suggesting taking that one step further ]
Quoted 3 lines · Jens Axboe
Ondrej Mosnacek (1):
nbd: override creds to kernel when calling sock_{send,recv}msg()
I've pulled this, but looking at the patch, I note that more than half the patch - 75% to be exact - is just boilerplate for "I need to allocate the kernel cred and deal with error handling there".
It literally has three lines of new actual useful code (two statements and one local variable declaration), and then nine lines of the "setup dance".
Which isn't wrong, but when the infrastructure boilerplate is three times more than the actual code, it makes me think we should maybe just get rid of the
my_kernel_cred = prepare_kernel_cred(&init_task);
pattern for this use-case, and just let people use "init_cred" directly for things like this.
Because that's essentially what that prepare_kernel_cred() thing returns, except it allocates a new copy of said thing, so now you have error handling and you have to free it after-the-fact.
And I'm not seeing that the extra error handling and freeing dance actually buys us anything at all.
Now, some *other* users actually go on to change the creds: they want that prepare_kernel_cred() dance because they then actually do something else like using their own keyring or whatever (eg the NFS idmap code or some other filesystem stuff).
So it's not like prepare_kernel_cred() is wrong, but in this kind of case where people just go "I'm a driver with hardware access, I want to do something with kernel privileges not user privileges", it actually seems counterproductive to have extra code just to complicate things.
Now, my gut feel is that if we just let people use 'init_cred' directly, we should also make sure that it's always exposed as a 'const struct cred' , but wouldn't that be a whole lot simpler and more straightforward?
This is *not* the only use case of that.
We now have at least four use-cases of this "raw kernel cred" pattern:
core-dumping over unix domain socket, nbd, firmware loading and SCSI target all do this exact thing as far as I can tell.
So they all just want that bare kernel cred, and this interface then forces it to do extra work instead of just doing
old_cred = override_creds(&init_cred);
...
revert_creds(old_cred);and it ends up being extra code for allocating and freeing that copy of a cred that we already *had* and could just have used directly.
I did just check that making 'init_cred' be const
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -28 +28 @@ extern struct nsproxy init_nsproxy; -extern struct cred init_cred;
+extern const struct cred init_cred;
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -44 +44 @@ static struct group_info init_groups = { .usage =REFCOUNT_INIT(2) };
-struct cred init_cred = {
+const struct cred init_cred = {
seems to build just fine and would seem to be the right thing to do even if we *don't* expect people to use it. And override_creds() is perfectly happy with a
Maybe there's some reason for that extra work that I'm not seeing and thinking of? But it all smells like make-believe work to me that probably has a historical reason for it, but doesn't seem to make a lot of sense any more.
Hmm?
Linus
Serge E. Hallyn
maintainerQuoted 79 lines · Linus Torvalds
[ Adding LSM people. Also Christian, because he did the cred refcount cleanup with
override_creds()and friends last year, and I'm suggesting taking that one step further ]Quoted 3 lines · Jens Axboe
Ondrej Mosnacek (1):
nbd: override creds to kernel when calling sock_{send,recv}msg()I've pulled this, but looking at the patch, I note that more than half the patch - 75% to be exact - is just boilerplate for "I need to allocate the kernel cred and deal with error handling there".
It literally has three lines of new actual useful code (two statements and one local variable declaration), and then nine lines of the "setup dance".
Which isn't wrong, but when the infrastructure boilerplate is three times more than the actual code, it makes me think we should maybe just get rid of the
my_kernel_cred = prepare_kernel_cred(&init_task);pattern for this use-case, and just let people use "
init_cred" directly for things like this.Because that's essentially what that
prepare_kernel_cred()thing returns, except it allocates a new copy of said thing, so now you have error handling and you have to free it after-the-fact.And I'm not seeing that the extra error handling and freeing dance actually buys us anything at all.
Now, some *other* users actually go on to change the creds: they want that
prepare_kernel_cred()dance because they then actually do something else like using their own keyring or whatever (eg the NFS idmap code or some other filesystem stuff).So it's not like
prepare_kernel_cred()is wrong, but in this kind of case where people just go "I'm a driver with hardware access, I want to do something with kernel privileges not user privileges", it actually seems counterproductive to have extra code just to complicate things.Now, my gut feel is that if we just let people use '
init_cred' directly, we should also make sure that it's always exposed as a 'const struct cred' , but wouldn't that be a whole lot simpler and more straightforward?This is *not* the only use case of that.
We now have at least four use-cases of this "raw kernel cred" pattern:
core-dumping over unix domain socket, nbd, firmware loading and SCSI target all do this exact thing as far as I can tell.So they all just want that bare kernel cred, and this interface then forces it to do extra work instead of just doing
old_cred = override_creds(&init_cred); ... revert_creds(old_cred);and it ends up being extra code for allocating and freeing that copy of a cred that we already *had* and could just have used directly.
I did just check that making '
init_cred' be const---
a/include/linux/init_task.h
+++b/include/linux/init_task.h
@@ -28 +28 @@ extern struct nsproxyinit_nsproxy; -extern struct credinit_cred;
+extern const struct credinit_cred;
---a/kernel/cred.c
+++b/kernel/cred.c
@@ -44 +44 @@ static structgroup_infoinit_groups= { .usage =REFCOUNT_INIT(2)};
-struct credinit_cred= {
+const struct credinit_cred= {
Quoted 6 lines
seems to build just fine and would seem to be the right thing to do even if we *don't* expect people to use it. And
override_creds()is perfectly happy with aMaybe there's some reason for that extra work that I'm not seeing and thinking of? But it all smells like make-believe work to me that
The keychains are all NULL and won't be allocated (by init) without copying a new cred, right? And it seems like smack, selinux, and apparmor at least each set the security field to a copy of the daemon's. Now, in theory, some LSM *could* come by and try to merge current's info with init's, but that would probably be misguided.
So this does seem like it should work.
Quoted 6 lines
probably has a historical reason for it, but doesn't seem to make a lot of sense any more.
Hmm?
Linus
Quoted 3 lines · Serge E. Hallyn
The keychains are all NULL and won't be allocated (by init) without copying a new cred, right?
Right. As mentioned, 'struct init_cred' really should be 'const' - it's not *technically* really constant, because the reference counting casts away the const, but refs are designed to be copy-on-write apart from the reference counting.
So whenever you change it, that's when you are supposed to always copy things. So that prepare_kernel_cred() thing exists for a good reason.
But the pattern here in nbd (and the other three usage cases I found) is really just "use the kernel creds as-is".
They don't even need any reference counting as long as they can just rely on the cred staying around for the duration of the use - which obviously is the case for init_cred.
Quoted 4 lines
Now, in theory, some LSM *could* come by and try to merge current's info with init's, but that would probably be misguided.
So this does seem like it should work.
Yeah, I can't see how any LSM could possibly do anything about init_cred - it really ends up being the source of all other creds. You can't really validly mess with it or deny it anything.
Linus
Paul Moore
maintainerQuoted 20 lines · Linus Torvalds
Quoted 3 lines · Serge E. Hallyn
The keychains are all NULL and won't be allocated (by init) without copying a new cred, right?
Right. As mentioned, 'struct
init_cred' really should be 'const' - it's not *technically* really constant, because the reference counting casts away the const, but refs are designed to be copy-on-write apart from the reference counting.So whenever you change it, that's when you are supposed to always copy things. So that
prepare_kernel_cred()thing exists for a good reason.But the pattern here in nbd (and the other three usage cases I found) is really just "use the kernel creds as-is".
They don't even need any reference counting as long as they can just rely on the cred staying around for the duration of the use - which obviously is the case for
init_cred.
Quoted 8 lines
Quoted 4 lines
Now, in theory, some LSM *could* come by and try to merge current's info with init's, but that would probably be misguided.
So this does seem like it should work.
Yeah, I can't see how any LSM could possibly do anything about
init_cred- it really ends up being the source of all other creds. You can't really validly mess with it or deny it anything.
This came in just as I was logging off for the weekend and I've been kicking it around in my head and I can't think of any *good* LSM related reason why this should be a problem, however I do have a somewhat generic concern about potential future issues caused by someone choosing the wrong access pattern and causing an odd bug. In theory, a const attribute should catch a lot of that before it starts, but that assumes we don't have some casting somewhere doing odd things.
If we care about this, and I'm not sure we do, or rather I'm not sure how much I care about this, we could create a new cred instance, say 'kernel_cred', that is purely for things like nbd where no changes are expected and it can be accessed directly. This would limit the direct-access pattern to just kernel_cred, making code inspection/review easier and leaving the door open for WARN_ON-esque assertions in things like prepare_creds() and similar*.
* This reminds me that we need to talk some more with the keys folks and see if we can get rid of the ugliness that is key_change_session_keyring()/security_transfer_creds(). Jann had some patches for that, but if I recall correctly there was a concern about backwards compatibility.
Signature · 1 line
paul-moore.com
Quoted 1 line · Linus Torvalds
[ Adding LSM people. Also Christian, because he did the cred refcount
Sorry, late to the party. I was working on other stuf. Let me see...
Quoted 57 lines
cleanup with
override_creds()and friends last year, and I'm suggesting taking that one step further ]Quoted 3 lines · Jens Axboe
Ondrej Mosnacek (1):
nbd: override creds to kernel when calling sock_{send,recv}msg()I've pulled this, but looking at the patch, I note that more than half the patch - 75% to be exact - is just boilerplate for "I need to allocate the kernel cred and deal with error handling there".
It literally has three lines of new actual useful code (two statements and one local variable declaration), and then nine lines of the "setup dance".
Which isn't wrong, but when the infrastructure boilerplate is three times more than the actual code, it makes me think we should maybe just get rid of the
my_kernel_cred = prepare_kernel_cred(&init_task);pattern for this use-case, and just let people use "
init_cred" directly for things like this.Because that's essentially what that
prepare_kernel_cred()thing returns, except it allocates a new copy of said thing, so now you have error handling and you have to free it after-the-fact.And I'm not seeing that the extra error handling and freeing dance actually buys us anything at all.
Now, some *other* users actually go on to change the creds: they want that
prepare_kernel_cred()dance because they then actually do something else like using their own keyring or whatever (eg the NFS idmap code or some other filesystem stuff).So it's not like
prepare_kernel_cred()is wrong, but in this kind of case where people just go "I'm a driver with hardware access, I want to do something with kernel privileges not user privileges", it actually seems counterproductive to have extra code just to complicate things.Now, my gut feel is that if we just let people use '
init_cred' directly, we should also make sure that it's always exposed as a 'const struct cred' , but wouldn't that be a whole lot simpler and more straightforward?This is *not* the only use case of that.
We now have at least four use-cases of this "raw kernel cred" pattern:
core-dumping over unix domain socket, nbd, firmware loading and SCSI target all do this exact thing as far as I can tell.So they all just want that bare kernel cred, and this interface then forces it to do extra work instead of just doing
Quoted 8 lines
old_cred = override_creds(&init_cred); ... revert_creds(old_cred);and it ends up being extra code for allocating and freeing that copy of a cred that we already *had* and could just have used directly.
I did just check that making '
init_cred' be const
Hm, two immediate observations before I go off and write the series.
(1) The thing is that init_cred would have to be exposed to modules via EXPORT_SYMBOL() for this to work. It would be easier to just force the use of init_task->cred instead.
That pointer deref won't matter in the face of the allocations and refcounts we wipe out with this. Then we should also move init_cred to init/init_task.c and make it static const. Nobody really needs it currently.
(2) I think the plain override_creds() would work but we can do better. I envision we can leverage CLASS() to completely hide any access to init_cred and force a scope with kernel creds.
/me goess off to write that up.
Ok, so I have it and it survives the coredump socket tests. They are a prime example for this sort of thing. Any unprivileged task needs to be able to connect to the coredump socket when it coredumps so we override credentials only for the path lookup. With my patchset this becomes:
if (flags & SOCK_COREDUMP) {
struct path root;
task_lock(&init_task);
get_fs_root(init_task.fs, &root);
task_unlock(&init_task);
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;
} else {Patches appended.
Don't needlessly duplicate the initial credentials just to briefly use them. Avoid all that work and add guards that hide all this away.
Note, using init_task in the macros to get at the credentials has the advantage that we don't actually need to export init_cred to modules such as nbd.
---
Christian Brauner (6):
cred: add {scoped_}with_kernel_creds
creds: make init_cred static
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/cred.h | 25 +++++++++++++++ include/linux/init_task.h | 1 - init/init_task.c | 24 ++++++++++++++ kernel/cred.c | 24 -------------- net/unix/af_unix.c | 17 +++------- security/keys/process_keys.c | 2 +- 9 files changed, 83 insertions(+), 100 deletions(-)
---
base-commit: d2818517e3486d11c9bd55aca3e14059e4c69886
change-id: 20251031-work-creds-init_cred-db9556a70d67
Add a new CLASS(with_kernel_creds) allowing code to run with kernel creds without having to duplicate them first. Only use the CLASS() never the plain helpers!
--- include/linux/cred.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/cred.h +25 -0
@@ -20,6 +20,8 @@struct cred ;struct inode ; +extern struct task_struct init_task; +/* * COW Supplementary groups list */
@@ -180,6 +182,29 @@ static inline const struct cred *revert_creds(const struct cred *revert_cred)return rcu_replace_pointer (current->cred, revert_cred,1 ); } +static inline const struct cred *__assume_kernel_creds (void ) +{ +return rcu_replace_pointer (current->cred, init_task.cred,1 ); +} + +static inline void __yield_kernel_creds (const struct cred *revert_cred) +{ +WARN_ON_ONCE (current->cred != init_task.cred); +rcu_replace_pointer (current->cred, revert_cred,1 ); +} + +DEFINE_CLASS (with_kernel_creds, + const struct cred *, +__yield_kernel_creds (_T), + __assume_kernel_creds(), void) + +#define with_kernel_creds () \ + CLASS(with_kernel_creds, __UNIQUE_ID(cred))() + +#define scoped_with_kernel_creds () \ + for (CLASS(with_kernel_creds, __UNIQUE_ID(cred))(), \ + *__p = (void *)1; __p; __p = NULL) +/** * get_cred_many - Get references on a set of credentials * @cred: The credentials to reference
Signature · 1 line
2.47.3
There's zero need to expose struct init_cred.
The very few places that need direct access can just go through init_task and be done with it.
--- include/linux/init_task.h | 1 - init/init_task.c | 24 ++++++++++++++++++++++++ kernel/cred.c | 24 ------------------------ security/keys/process_keys.c | 2 +- 4 files changed, 25 insertions(+), 26 deletions(-)
include/linux/init_task.h +0 -1
@@ -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 = { \
init/init_task.c +24 -0
@@ -62,6 +62,30 @@ unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)] = { };#endif +/* +* The initial credentials for the initial task +*/ +static const 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)
kernel/cred.c +0 -24
@@ -38,30 +38,6 @@ 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 */
security/keys/process_keys.c +1 -1
@@ -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, + init_task.cred, KEY_POS_WRITE | KEY_POS_SEARCH | KEY_USR_VIEW | KEY_USR_READ, 0,
Signature · 1 line
2.47.3
No need to copy kernel credentials.
--- drivers/base/firmware_loader/main.c | 59 ++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 34 deletions(-)
drivers/base/firmware_loader/main.c +25 -34
78 lines not on this page · raw message
Signature · 1 line
2.47.3
No need to copy kernel credentials.
--- drivers/block/nbd.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/block/nbd.c +2 -15
@@ -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" ); }
Signature · 1 line
2.47.3
Get rid of all the boilerplate and tightly scope when the task runs with kernel creds.
--- drivers/target/target_core_configfs.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/target/target_core_configfs.c +2 -12
@@ -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 ;
Signature · 1 line
2.47.3
No need to copy kernel credentials.
--- net/unix/af_unix.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
net/unix/af_unix.c +4 -13
@@ -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;
Signature · 1 line
2.47.3
Quoted 109 lines · Christian Brauner
Quoted 1 line · Linus Torvalds
[ Adding LSM people. Also Christian, because he did the cred refcount
Sorry, late to the party. I was working on other stuf. Let me see...
Quoted 57 lines
cleanup with
override_creds()and friends last year, and I'm suggesting taking that one step further ]Quoted 3 lines · Jens Axboe
Ondrej Mosnacek (1):
nbd: override creds to kernel when calling sock_{send,recv}msg()I've pulled this, but looking at the patch, I note that more than half the patch - 75% to be exact - is just boilerplate for "I need to allocate the kernel cred and deal with error handling there".
It literally has three lines of new actual useful code (two statements and one local variable declaration), and then nine lines of the "setup dance".
Which isn't wrong, but when the infrastructure boilerplate is three times more than the actual code, it makes me think we should maybe just get rid of the
my_kernel_cred = prepare_kernel_cred(&init_task);pattern for this use-case, and just let people use "
init_cred" directly for things like this.Because that's essentially what that
prepare_kernel_cred()thing returns, except it allocates a new copy of said thing, so now you have error handling and you have to free it after-the-fact.And I'm not seeing that the extra error handling and freeing dance actually buys us anything at all.
Now, some *other* users actually go on to change the creds: they want that
prepare_kernel_cred()dance because they then actually do something else like using their own keyring or whatever (eg the NFS idmap code or some other filesystem stuff).So it's not like
prepare_kernel_cred()is wrong, but in this kind of case where people just go "I'm a driver with hardware access, I want to do something with kernel privileges not user privileges", it actually seems counterproductive to have extra code just to complicate things.Now, my gut feel is that if we just let people use '
init_cred' directly, we should also make sure that it's always exposed as a 'const struct cred' , but wouldn't that be a whole lot simpler and more straightforward?This is *not* the only use case of that.
We now have at least four use-cases of this "raw kernel cred" pattern:
core-dumping over unix domain socket, nbd, firmware loading and SCSI target all do this exact thing as far as I can tell.So they all just want that bare kernel cred, and this interface then forces it to do extra work instead of just doing
Quoted 8 lines
old_cred = override_creds(&init_cred); ... revert_creds(old_cred);and it ends up being extra code for allocating and freeing that copy of a cred that we already *had* and could just have used directly.
I did just check that making '
init_cred' be constHm, two immediate observations before I go off and write the series.
(1) The thing is that
init_credwould have to be exposed to modules viaEXPORT_SYMBOL()for this to work. It would be easier to just force the use ofinit_task->credinstead.That pointer deref won't matter in the face of the allocations and refcounts we wipe out with this. Then we should also move
init_credtoinit/init_task.cand make it static const. Nobody really needs it currently.(2) I think the plain
override_creds()would work but we can do better. I envision we can leverageCLASS()to completely hide any access toinit_credand force a scope with kernel creds./me goess off to write that up.
Ok, so I have it and it survives the coredump socket tests. They are a prime example for this sort of thing. Any unprivileged task needs to be able to connect to the coredump socket when it coredumps so we override credentials only for the path lookup. With my patchset this becomes:
if (flags & SOCK_COREDUMP) { struct path root; task_lock(&init_task); get_fs_root(init_task.fs, &root); task_unlock(&init_task); 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; } else {
Quoted 7 lines
Patches appended.
Date: Fri, 31 Oct 2025 16:37:35 +0100
From: Christian Brauner <[email protected]>
To: Christian Brauner <[email protected]>
Subject: [PATCH 0/6] creds: add {scoped_}with_kernel_creds()
Message-Id: <[email protected]>
Needs one diff I forgot to fold:
init/init_task.c +3 -0
@@ -62,6 +62,9 @@ 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 */
kernel/cred.c +0 -3
@@ -35,9 +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 RCU callback to actually dispose of a set of credentials */
Quoted 6 lines · Christian Brauner
Hm, two immediate observations before I go off and write the series.
(1) The thing is that
init_credwould have to be exposed to modules viaEXPORT_SYMBOL()for this to work. It would be easier to just force the use ofinit_task->credinstead.
Yea, I guess we already export that.
Quoted 4 lines
That pointer deref won't matter in the face of the allocations and refcounts we wipe out with this. Then we should also move
init_credtoinit/init_task.cand make it static const. Nobody really needs it currently.
Well, I did the "does it compile ok" with it marked as 'const', but as mentioned, those 'struct cred' instances aren't *really* const, they are only pseudo-const things in that they are *marked* const so that nobody modifies them by mistake, but then the ref-counting will cast the constness away in order to update references.
So I don't think we can *actually* mark it "static const", because that will put the data structure in the const data section, and then the refcounting will trigger kernel page faults.
End result: I think we can indeed move it to init/init_task.c. And yes, we can and should make it static to that file, but not plain 'const'.
If we expose it to others - but I think you're right that maybe it's not a good idea - we should *expose* it as a 'const' data structure.
But we should probably put it in some explicitly writable section (I was going to suggest marking it "__read_mostly", but it turns out some architectures #define that to be empty, so a "const __read_mosyly" data structure could still end up in a read-only section).
Quoted 3 lines
(2) I think the plain
override_creds()would work but we can do better. I envision we can leverageCLASS()to completely hide any access toinit_credand force a scope with kernel creds.
Ack.
Linus
Quoted 29 lines · Linus Torvalds
Quoted 6 lines · Christian Brauner
Hm, two immediate observations before I go off and write the series.
(1) The thing is that
init_credwould have to be exposed to modules viaEXPORT_SYMBOL()for this to work. It would be easier to just force the use ofinit_task->credinstead.Yea, I guess we already export that.
Quoted 4 lines
That pointer deref won't matter in the face of the allocations and refcounts we wipe out with this. Then we should also move
init_credtoinit/init_task.cand make it static const. Nobody really needs it currently.Well, I did the "does it compile ok" with it marked as 'const', but as mentioned, those 'struct cred' instances aren't *really* const, they are only pseudo-const things in that they are *marked* const so that nobody modifies them by mistake, but then the ref-counting will cast the constness away in order to update references.
So I don't think we can *actually* mark it "static const", because that will put the data structure in the const data section, and then the refcounting will trigger kernel page faults.
End result: I think we can indeed move it to
init/init_task.c. And yes, we can and should make it static to that file, but not plain 'const'.
Quoted 7 lines
If we expose it to others - but I think you're right that maybe it's not a good idea - we should *expose* it as a 'const' data structure.
But we should probably put it in some explicitly writable section (I was going to suggest marking it "
__read_mostly", but it turns out some architectures #define that to be empty, so a "const__read_mosyly" data structure could still end up in a read-only section).
For some init data structures that are heavily used such as:
init_pid_ns
it often makes sense to just skip the refcounting completely because we know they are always around. Take the pid namespace as an example:
static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
{
if (ns != &init_pid_ns)
ns_ref_inc(ns);
return ns;
}void put_pid_ns(struct pid_namespace *ns)
{
if (ns && ns != &init_pid_ns && ns_ref_put(ns))
schedule_work(&ns->work);
}While it has the obvious disadvantage that it introduces a special-case into the refcounting and it would obviously be more elegant if we just did:
void put_pid_ns(struct pid_namespace *ns)
{
if (ns_ref_put(ns))
schedule_work(&ns->work);
}it does elide a ton of refcount increments and decrements during task creation.
While that's not true for init_creds it would still be easy to just not refcount them at all if it's worth it.
Now that I think about it: given that I reworked all the namespace reference counting completely it should be easy to make all initial namespaces not get or put reference counts at all, like:
static __always_inline bool is_initial_namespace(struct ns_common *ns)
{
VFS_WARN_ON_ONCE(ns->ns_id == 0);
/* initial namespaces have fixed ids and the ids aren't recycled */
return ns->ns_id <= NS_LAST_INIT_ID;
}include/linux/ns_common.h +6 -1
@@ -136,9 +136,8 @@ struct ns_common *__must_check ns_owner(struct ns_common *ns);#define to_ns_common (__ns) \
@@ -225,6 +224,8 @@ static __always_inline __must_check int __ns_ref_active_read(const struct ns_com static __always_inline __must_check bool __ns_ref_put(struct ns_common *ns) { +if (is_initial_namespace (ns)) +return false;if (refcount_dec_and_test (&ns->__ns_ref)) {VFS_WARN_ON_ONCE (__ns_ref_active_read (ns));return true;
@@ -234,6 +235,8 @@ static __always_inline __must_check bool __ns_ref_put(struct ns_common *ns) static __always_inline __must_check bool __ns_ref_get(struct ns_common *ns) { +if (is_initial_namespace (ns)) +return true;if (refcount_inc_not_zero (&ns->__ns_ref))return true;VFS_WARN_ON_ONCE (__ns_ref_active_read (ns));
@@ -246,7 +249,8 @@ static __always_inline __must_check int __ns_ref_read(const struct ns_common *ns }#define ns_ref_read (__ns) __ns_ref_read(to_ns_common((__ns))) -#define ns_ref_inc (__ns) refcount_inc(&to_ns_common((__ns))->__ns_ref) +#define ns_ref_inc (__ns) \ + do { if (!is_initial_namespace(to_ns_common(__ns))) refcount_inc(&to_ns_common((__ns))->__ns_ref); } while (0)#define ns_ref_get (__ns) __ns_ref_get(to_ns_common((__ns)))#define ns_ref_put (__ns) __ns_ref_put(to_ns_common((__ns)))#define ns_ref_put_and_lock (__ns, __lock) \
This effectively means we can drop all the special-casing in the namespace helpers like:
include/linux/pid_namespace.h +1 -3
@@ -61,9 +61,7 @@ static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)static inline struct pid_namespace *get_pid_ns (struct pid_namespace *ns) { -if (ns != &init_pid_ns) -ns_ref_inc (ns); -return ns; +ns_ref_inc (ns); }#if defined(CONFIG_SYSCTL ) && defined(CONFIG_MEMFD_CREATE )
kernel/pid_namespace.c +1 -1
@@ -184,7 +184,7 @@ struct pid_namespace *copy_pid_ns(u64 flags,void put_pid_ns (struct pid_namespace *ns) { -if (ns && ns != &init_pid_ns &&ns_ref_put (ns)) +if (ns &&ns_ref_put (ns))schedule_work (&ns->work); }EXPORT_SYMBOL_GPL (put_pid_ns);
And all the other ones - without having looked into any potential pitfalls - would get the same behavior as the pidns for free. Worth it?
I think especially for the network namespace that might potentially avoid a bunch of cacheline ping-pong. But idk, it's just a theory. But it's easy enough to implement.
Quoted 8 lines
Quoted 4 lines
(2) I think the plain
override_creds()would work but we can do better. I envision we can leverageCLASS()to completely hide any access toinit_credand force a scope with kernel creds.Ack.
Linus
The pull request you sent on Fri, 24 Oct 2025 07:58:20 -0600:
The pull request you sent on Fri, 24 Oct 2025 07:58:20 -0600:
Quoted 1 line
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git tags/block-6.18-20251023
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/d2818517e3486d11c9bd55aca3e14059e4c69886
Thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/prtracker.html