* [PATCH 01/16] cred: add {scoped_}with_creds() guards
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 02/16] aio: use credential guards Christian Brauner
` (15 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
and implement with_kernel_creds() and scoped_with_kernel_creds() on top
of them.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/cred.h | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/include/linux/cred.h b/include/linux/cred.h
index c4f7630763f4..1778c0535b90 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -192,11 +192,15 @@ DEFINE_CLASS(override_creds,
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 with_creds(cred) \
+ CLASS(override_creds, __UNIQUE_ID(label))(cred)
-#define scoped_with_kernel_creds() \
- scoped_class(override_creds, __UNIQUE_ID(cred), kernel_cred())
+#define scoped_with_creds(cred) \
+ scoped_class(override_creds, __UNIQUE_ID(label), cred)
+
+#define with_kernel_creds() with_creds(kernel_cred())
+
+#define scoped_with_kernel_creds() scoped_with_creds(kernel_cred())
/**
* get_cred_many - Get references on a set of credentials
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 02/16] aio: use credential guards
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
2025-11-03 11:26 ` [PATCH 01/16] cred: add {scoped_}with_creds() guards Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 03/16] backing-file: use credential guards for reads Christian Brauner
` (14 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/aio.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/fs/aio.c b/fs/aio.c
index 5bc133386407..0a23a8c0717f 100644
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -1640,10 +1640,10 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb,
static void aio_fsync_work(struct work_struct *work)
{
struct aio_kiocb *iocb = container_of(work, struct aio_kiocb, fsync.work);
- const struct cred *old_cred = override_creds(iocb->fsync.creds);
- iocb->ki_res.res = vfs_fsync(iocb->fsync.file, iocb->fsync.datasync);
- revert_creds(old_cred);
+ scoped_with_creds(iocb->fsync.creds)
+ iocb->ki_res.res = vfs_fsync(iocb->fsync.file, iocb->fsync.datasync);
+
put_cred(iocb->fsync.creds);
iocb_put(iocb);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 03/16] backing-file: use credential guards for reads
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
2025-11-03 11:26 ` [PATCH 01/16] cred: add {scoped_}with_creds() guards Christian Brauner
2025-11-03 11:26 ` [PATCH 02/16] aio: use credential guards Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 04/16] backing-file: use credential guards for writes Christian Brauner
` (13 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/backing-file.c | 52 ++++++++++++++++++++++++++++------------------------
1 file changed, 28 insertions(+), 24 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 15a7f8031084..4cb7276e7ead 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -157,13 +157,37 @@ static int backing_aio_init_wq(struct kiocb *iocb)
return sb_init_dio_done_wq(sb);
}
+static int do_backing_file_read_iter(struct file *file, struct iov_iter *iter,
+ struct kiocb *iocb, int flags)
+{
+ struct backing_aio *aio = NULL;
+ int ret;
+
+ if (is_sync_kiocb(iocb)) {
+ rwf_t rwf = iocb_to_rw_flags(flags);
+
+ return vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
+ }
+
+ aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
+ if (!aio)
+ return -ENOMEM;
+
+ aio->orig_iocb = iocb;
+ kiocb_clone(&aio->iocb, iocb, get_file(file));
+ aio->iocb.ki_complete = backing_aio_rw_complete;
+ refcount_set(&aio->ref, 2);
+ ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
+ backing_aio_put(aio);
+ if (ret != -EIOCBQUEUED)
+ backing_aio_cleanup(aio, ret);
+ return ret;
+}
ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
struct kiocb *iocb, int flags,
struct backing_file_ctx *ctx)
{
- struct backing_aio *aio = NULL;
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
@@ -176,28 +200,8 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
!(file->f_mode & FMODE_CAN_ODIRECT))
return -EINVAL;
- old_cred = override_creds(ctx->cred);
- if (is_sync_kiocb(iocb)) {
- rwf_t rwf = iocb_to_rw_flags(flags);
-
- ret = vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
- } else {
- ret = -ENOMEM;
- aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
- if (!aio)
- goto out;
-
- aio->orig_iocb = iocb;
- kiocb_clone(&aio->iocb, iocb, get_file(file));
- aio->iocb.ki_complete = backing_aio_rw_complete;
- refcount_set(&aio->ref, 2);
- ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
- backing_aio_put(aio);
- if (ret != -EIOCBQUEUED)
- backing_aio_cleanup(aio, ret);
- }
-out:
- revert_creds(old_cred);
+ scoped_with_creds(ctx->cred)
+ do_backing_file_read_iter(file, iter, iocb, flags);
if (ctx->accessed)
ctx->accessed(iocb->ki_filp);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 04/16] backing-file: use credential guards for writes
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (2 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 03/16] backing-file: use credential guards for reads Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 13:24 ` Amir Goldstein
2025-11-03 11:26 ` [PATCH 05/16] backing-file: use credential guards for splice read Christian Brauner
` (12 subsequent siblings)
16 siblings, 1 reply; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/backing-file.c | 74 +++++++++++++++++++++++++++++--------------------------
1 file changed, 39 insertions(+), 35 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 4cb7276e7ead..9bea737d5bef 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -210,11 +210,47 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
}
EXPORT_SYMBOL_GPL(backing_file_read_iter);
+static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
+ struct kiocb *iocb, int flags,
+ void (*end_write)(struct kiocb *, ssize_t))
+{
+ struct backing_aio *aio;
+ int ret;
+
+ if (is_sync_kiocb(iocb)) {
+ rwf_t rwf = iocb_to_rw_flags(flags);
+
+ ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
+ if (end_write)
+ end_write(iocb, ret);
+ return ret;
+ }
+
+ ret = backing_aio_init_wq(iocb);
+ if (ret)
+ return ret;
+
+ aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
+ if (!aio)
+ return -ENOMEM;
+
+ aio->orig_iocb = iocb;
+ aio->end_write = end_write;
+ kiocb_clone(&aio->iocb, iocb, get_file(file));
+ aio->iocb.ki_flags = flags;
+ aio->iocb.ki_complete = backing_aio_queue_completion;
+ refcount_set(&aio->ref, 2);
+ ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
+ backing_aio_put(aio);
+ if (ret != -EIOCBQUEUED)
+ backing_aio_cleanup(aio, ret);
+ return ret;
+}
+
ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
struct kiocb *iocb, int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
@@ -237,40 +273,8 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
*/
flags &= ~IOCB_DIO_CALLER_COMP;
- old_cred = override_creds(ctx->cred);
- if (is_sync_kiocb(iocb)) {
- rwf_t rwf = iocb_to_rw_flags(flags);
-
- ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
- if (ctx->end_write)
- ctx->end_write(iocb, ret);
- } else {
- struct backing_aio *aio;
-
- ret = backing_aio_init_wq(iocb);
- if (ret)
- goto out;
-
- ret = -ENOMEM;
- aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
- if (!aio)
- goto out;
-
- aio->orig_iocb = iocb;
- aio->end_write = ctx->end_write;
- kiocb_clone(&aio->iocb, iocb, get_file(file));
- aio->iocb.ki_flags = flags;
- aio->iocb.ki_complete = backing_aio_queue_completion;
- refcount_set(&aio->ref, 2);
- ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
- backing_aio_put(aio);
- if (ret != -EIOCBQUEUED)
- backing_aio_cleanup(aio, ret);
- }
-out:
- revert_creds(old_cred);
-
- return ret;
+ with_creds(ctx->cred);
+ return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
}
EXPORT_SYMBOL_GPL(backing_file_write_iter);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* Re: [PATCH 04/16] backing-file: use credential guards for writes
2025-11-03 11:26 ` [PATCH 04/16] backing-file: use credential guards for writes Christian Brauner
@ 2025-11-03 13:24 ` Amir Goldstein
0 siblings, 0 replies; 23+ messages in thread
From: Amir Goldstein @ 2025-11-03 13:24 UTC (permalink / raw)
To: Christian Brauner
Cc: Linus Torvalds, linux-fsdevel, linux-kernel, linux-aio,
linux-unionfs, linux-erofs, linux-nfs, linux-cifs,
samba-technical, cgroups, netdev
On Mon, Nov 3, 2025 at 12:30 PM Christian Brauner <brauner@kernel.org> wrote:
>
> Use credential guards for scoped credential override with automatic
> restoration on scope exit.
>
> Signed-off-by: Christian Brauner <brauner@kernel.org>
> ---
> fs/backing-file.c | 74 +++++++++++++++++++++++++++++--------------------------
> 1 file changed, 39 insertions(+), 35 deletions(-)
>
> diff --git a/fs/backing-file.c b/fs/backing-file.c
> index 4cb7276e7ead..9bea737d5bef 100644
> --- a/fs/backing-file.c
> +++ b/fs/backing-file.c
> @@ -210,11 +210,47 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
> }
> EXPORT_SYMBOL_GPL(backing_file_read_iter);
>
> +static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
> + struct kiocb *iocb, int flags,
> + void (*end_write)(struct kiocb *, ssize_t))
> +{
> + struct backing_aio *aio;
> + int ret;
> +
> + if (is_sync_kiocb(iocb)) {
> + rwf_t rwf = iocb_to_rw_flags(flags);
> +
> + ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
> + if (end_write)
> + end_write(iocb, ret);
> + return ret;
> + }
> +
> + ret = backing_aio_init_wq(iocb);
> + if (ret)
> + return ret;
> +
> + aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
> + if (!aio)
> + return -ENOMEM;
> +
> + aio->orig_iocb = iocb;
> + aio->end_write = end_write;
> + kiocb_clone(&aio->iocb, iocb, get_file(file));
> + aio->iocb.ki_flags = flags;
> + aio->iocb.ki_complete = backing_aio_queue_completion;
> + refcount_set(&aio->ref, 2);
> + ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
> + backing_aio_put(aio);
> + if (ret != -EIOCBQUEUED)
> + backing_aio_cleanup(aio, ret);
> + return ret;
> +}
> +
> ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
> struct kiocb *iocb, int flags,
> struct backing_file_ctx *ctx)
> {
> - const struct cred *old_cred;
> ssize_t ret;
>
> if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
> @@ -237,40 +273,8 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
> */
> flags &= ~IOCB_DIO_CALLER_COMP;
>
> - old_cred = override_creds(ctx->cred);
> - if (is_sync_kiocb(iocb)) {
> - rwf_t rwf = iocb_to_rw_flags(flags);
> -
> - ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
> - if (ctx->end_write)
> - ctx->end_write(iocb, ret);
> - } else {
> - struct backing_aio *aio;
> -
> - ret = backing_aio_init_wq(iocb);
> - if (ret)
> - goto out;
> -
> - ret = -ENOMEM;
> - aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
> - if (!aio)
> - goto out;
> -
> - aio->orig_iocb = iocb;
> - aio->end_write = ctx->end_write;
> - kiocb_clone(&aio->iocb, iocb, get_file(file));
> - aio->iocb.ki_flags = flags;
> - aio->iocb.ki_complete = backing_aio_queue_completion;
> - refcount_set(&aio->ref, 2);
> - ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
> - backing_aio_put(aio);
> - if (ret != -EIOCBQUEUED)
> - backing_aio_cleanup(aio, ret);
> - }
> -out:
> - revert_creds(old_cred);
> -
> - return ret;
> + with_creds(ctx->cred);
> + return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
> }
Pointing out the obvious that do_backing_file_write_iter() feels
unnecessary here.
But I am fine with keeping it for symmetry with
do_backing_file_read_iter() and in case we will want to call the sync
end_write() callback outside of creds override context as we do in the
read case.
Thanks,
Amir.
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 05/16] backing-file: use credential guards for splice read
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (3 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 04/16] backing-file: use credential guards for writes Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 06/16] backing-file: use credential guards for splice write Christian Brauner
` (11 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/backing-file.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 9bea737d5bef..8ebc62f49bad 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -283,15 +283,13 @@ ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
unsigned int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
return -EIO;
- old_cred = override_creds(ctx->cred);
- ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);
- revert_creds(old_cred);
+ scoped_with_creds(ctx->cred)
+ ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);
if (ctx->accessed)
ctx->accessed(iocb->ki_filp);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 06/16] backing-file: use credential guards for splice write
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (4 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 05/16] backing-file: use credential guards for splice read Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 07/16] backing-file: use credential guards for mmap Christian Brauner
` (10 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/backing-file.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 8ebc62f49bad..9c63a3368b66 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -303,7 +303,6 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
size_t len, unsigned int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
@@ -316,11 +315,11 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
if (ret)
return ret;
- old_cred = override_creds(ctx->cred);
- file_start_write(out);
- ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
- file_end_write(out);
- revert_creds(old_cred);
+ scoped_with_creds(ctx->cred) {
+ file_start_write(out);
+ ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
+ file_end_write(out);
+ }
if (ctx->end_write)
ctx->end_write(iocb, ret);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 07/16] backing-file: use credential guards for mmap
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (5 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 06/16] backing-file: use credential guards for splice write Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 08/16] binfmt_misc: use credential guards Christian Brauner
` (9 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/backing-file.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 9c63a3368b66..5cc4b59aa460 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -331,7 +331,6 @@ EXPORT_SYMBOL_GPL(backing_file_splice_write);
int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
struct file *user_file = vma->vm_file;
int ret;
@@ -343,9 +342,8 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
vma_set_file(vma, file);
- old_cred = override_creds(ctx->cred);
- ret = vfs_mmap(vma->vm_file, vma);
- revert_creds(old_cred);
+ scoped_with_creds(ctx->cred)
+ ret = vfs_mmap(vma->vm_file, vma);
if (ctx->accessed)
ctx->accessed(user_file);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 08/16] binfmt_misc: use credential guards
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (6 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 07/16] backing-file: use credential guards for mmap Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 09/16] erofs: " Christian Brauner
` (8 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/binfmt_misc.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index a839f960cd4a..558db4bd6c2a 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -782,8 +782,6 @@ static ssize_t bm_register_write(struct file *file, const char __user *buffer,
return PTR_ERR(e);
if (e->flags & MISC_FMT_OPEN_FILE) {
- const struct cred *old_cred;
-
/*
* Now that we support unprivileged binfmt_misc mounts make
* sure we use the credentials that the register @file was
@@ -791,9 +789,8 @@ static ssize_t bm_register_write(struct file *file, const char __user *buffer,
* didn't matter much as only a privileged process could open
* the register file.
*/
- old_cred = override_creds(file->f_cred);
- f = open_exec(e->interpreter);
- revert_creds(old_cred);
+ scoped_with_creds(file->f_cred)
+ f = open_exec(e->interpreter);
if (IS_ERR(f)) {
pr_notice("register: failed to install interpreter file %s\n",
e->interpreter);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 09/16] erofs: use credential guards
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (7 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 08/16] binfmt_misc: use credential guards Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 10/16] nfs: use credential guards in nfs_local_call_read() Christian Brauner
` (7 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/erofs/fileio.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/fs/erofs/fileio.c b/fs/erofs/fileio.c
index b7b3432a9882..d27938435b2f 100644
--- a/fs/erofs/fileio.c
+++ b/fs/erofs/fileio.c
@@ -47,7 +47,6 @@ static void erofs_fileio_ki_complete(struct kiocb *iocb, long ret)
static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
{
- const struct cred *old_cred;
struct iov_iter iter;
int ret;
@@ -61,9 +60,8 @@ static void erofs_fileio_rq_submit(struct erofs_fileio_rq *rq)
rq->iocb.ki_flags = IOCB_DIRECT;
iov_iter_bvec(&iter, ITER_DEST, rq->bvecs, rq->bio.bi_vcnt,
rq->bio.bi_iter.bi_size);
- old_cred = override_creds(rq->iocb.ki_filp->f_cred);
- ret = vfs_iocb_iter_read(rq->iocb.ki_filp, &rq->iocb, &iter);
- revert_creds(old_cred);
+ scoped_with_creds(rq->iocb.ki_filp->f_cred)
+ ret = vfs_iocb_iter_read(rq->iocb.ki_filp, &rq->iocb, &iter);
if (ret != -EIOCBQUEUED)
erofs_fileio_ki_complete(&rq->iocb, ret);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 10/16] nfs: use credential guards in nfs_local_call_read()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (8 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 09/16] erofs: " Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:26 ` [PATCH 11/16] nfs: use credential guards in nfs_local_call_write() Christian Brauner
` (6 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/nfs/localio.c | 31 ++++++++++++++-----------------
1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c
index 2c0455e91571..48bfe54b48a4 100644
--- a/fs/nfs/localio.c
+++ b/fs/nfs/localio.c
@@ -595,29 +595,26 @@ static void nfs_local_call_read(struct work_struct *work)
struct nfs_local_kiocb *iocb =
container_of(work, struct nfs_local_kiocb, work);
struct file *filp = iocb->kiocb.ki_filp;
- const struct cred *save_cred;
ssize_t status;
- save_cred = override_creds(filp->f_cred);
-
- for (int i = 0; i < iocb->n_iters ; i++) {
- if (iocb->iter_is_dio_aligned[i]) {
- iocb->kiocb.ki_flags |= IOCB_DIRECT;
- iocb->kiocb.ki_complete = nfs_local_read_aio_complete;
- iocb->aio_complete_work = nfs_local_read_aio_complete_work;
- }
+ scoped_with_creds(filp->f_cred) {
+ for (int i = 0; i < iocb->n_iters ; i++) {
+ if (iocb->iter_is_dio_aligned[i]) {
+ iocb->kiocb.ki_flags |= IOCB_DIRECT;
+ iocb->kiocb.ki_complete = nfs_local_read_aio_complete;
+ iocb->aio_complete_work = nfs_local_read_aio_complete_work;
+ }
- iocb->kiocb.ki_pos = iocb->offset[i];
- status = filp->f_op->read_iter(&iocb->kiocb, &iocb->iters[i]);
- if (status != -EIOCBQUEUED) {
- nfs_local_pgio_done(iocb->hdr, status);
- if (iocb->hdr->task.tk_status)
- break;
+ iocb->kiocb.ki_pos = iocb->offset[i];
+ status = filp->f_op->read_iter(&iocb->kiocb, &iocb->iters[i]);
+ if (status != -EIOCBQUEUED) {
+ nfs_local_pgio_done(iocb->hdr, status);
+ if (iocb->hdr->task.tk_status)
+ break;
+ }
}
}
- revert_creds(save_cred);
-
if (status != -EIOCBQUEUED) {
nfs_local_read_done(iocb, status);
nfs_local_pgio_release(iocb);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 11/16] nfs: use credential guards in nfs_local_call_write()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (9 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 10/16] nfs: use credential guards in nfs_local_call_read() Christian Brauner
@ 2025-11-03 11:26 ` Christian Brauner
2025-11-03 11:27 ` [PATCH 12/16] nfs: use credential guards in nfs_idmap_get_key() Christian Brauner
` (5 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:26 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/nfs/localio.c | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c
index 48bfe54b48a4..0c89a9d1e089 100644
--- a/fs/nfs/localio.c
+++ b/fs/nfs/localio.c
@@ -781,18 +781,11 @@ static void nfs_local_write_aio_complete(struct kiocb *kiocb, long ret)
nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_write_aio_complete_work */
}
-static void nfs_local_call_write(struct work_struct *work)
+static ssize_t do_nfs_local_call_write(struct nfs_local_kiocb *iocb,
+ struct file *filp)
{
- struct nfs_local_kiocb *iocb =
- container_of(work, struct nfs_local_kiocb, work);
- struct file *filp = iocb->kiocb.ki_filp;
- unsigned long old_flags = current->flags;
- const struct cred *save_cred;
ssize_t status;
- current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
- save_cred = override_creds(filp->f_cred);
-
file_start_write(filp);
for (int i = 0; i < iocb->n_iters ; i++) {
if (iocb->iter_is_dio_aligned[i]) {
@@ -837,7 +830,22 @@ static void nfs_local_call_write(struct work_struct *work)
}
file_end_write(filp);
- revert_creds(save_cred);
+ return status;
+}
+
+static void nfs_local_call_write(struct work_struct *work)
+{
+ struct nfs_local_kiocb *iocb =
+ container_of(work, struct nfs_local_kiocb, work);
+ struct file *filp = iocb->kiocb.ki_filp;
+ unsigned long old_flags = current->flags;
+ ssize_t status;
+
+ current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
+
+ scoped_with_creds(filp->f_cred)
+ status = do_nfs_local_call_write(iocb, filp);
+
current->flags = old_flags;
if (status != -EIOCBQUEUED) {
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 12/16] nfs: use credential guards in nfs_idmap_get_key()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (10 preceding siblings ...)
2025-11-03 11:26 ` [PATCH 11/16] nfs: use credential guards in nfs_local_call_write() Christian Brauner
@ 2025-11-03 11:27 ` Christian Brauner
2025-11-03 11:27 ` [PATCH 13/16] smb: use credential guards in cifs_get_spnego_key() Christian Brauner
` (4 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:27 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/nfs/nfs4idmap.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/fs/nfs/nfs4idmap.c b/fs/nfs/nfs4idmap.c
index 00932500fce4..9e1c48c5c0b8 100644
--- a/fs/nfs/nfs4idmap.c
+++ b/fs/nfs/nfs4idmap.c
@@ -306,15 +306,12 @@ static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
const char *type, void *data,
size_t data_size, struct idmap *idmap)
{
- const struct cred *saved_cred;
struct key *rkey;
const struct user_key_payload *payload;
ssize_t ret;
- saved_cred = override_creds(id_resolver_cache);
- rkey = nfs_idmap_request_key(name, namelen, type, idmap);
- revert_creds(saved_cred);
-
+ scoped_with_creds(id_resolver_cache)
+ rkey = nfs_idmap_request_key(name, namelen, type, idmap);
if (IS_ERR(rkey)) {
ret = PTR_ERR(rkey);
goto out;
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 13/16] smb: use credential guards in cifs_get_spnego_key()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (11 preceding siblings ...)
2025-11-03 11:27 ` [PATCH 12/16] nfs: use credential guards in nfs_idmap_get_key() Christian Brauner
@ 2025-11-03 11:27 ` Christian Brauner
2025-11-03 11:27 ` [PATCH 14/16] act: use credential guards in acct_write_process() Christian Brauner
` (3 subsequent siblings)
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:27 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/smb/client/cifs_spnego.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/fs/smb/client/cifs_spnego.c b/fs/smb/client/cifs_spnego.c
index 9891f55bac1e..da935bd1ce87 100644
--- a/fs/smb/client/cifs_spnego.c
+++ b/fs/smb/client/cifs_spnego.c
@@ -90,7 +90,6 @@ cifs_get_spnego_key(struct cifs_ses *sesInfo,
size_t desc_len;
struct key *spnego_key;
const char *hostname = server->hostname;
- const struct cred *saved_cred;
/* length of fields (with semicolons): ver=0xyz ip4=ipaddress
host=hostname sec=mechanism uid=0xFF user=username */
@@ -158,9 +157,8 @@ cifs_get_spnego_key(struct cifs_ses *sesInfo,
dp += sprintf(dp, ";upcall_target=app");
cifs_dbg(FYI, "key description = %s\n", description);
- saved_cred = override_creds(spnego_cred);
- spnego_key = request_key(&cifs_spnego_key_type, description, "");
- revert_creds(saved_cred);
+ scoped_with_creds(spnego_cred)
+ spnego_key = request_key(&cifs_spnego_key_type, description, "");
#ifdef CONFIG_CIFS_DEBUG2
if (cifsFYI && !IS_ERR(spnego_key)) {
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 14/16] act: use credential guards in acct_write_process()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (12 preceding siblings ...)
2025-11-03 11:27 ` [PATCH 13/16] smb: use credential guards in cifs_get_spnego_key() Christian Brauner
@ 2025-11-03 11:27 ` Christian Brauner
2025-11-03 23:04 ` Linus Torvalds
2025-11-03 11:27 ` [PATCH 15/16] cgroup: use credential guards in cgroup_attach_permissions() Christian Brauner
` (2 subsequent siblings)
16 siblings, 1 reply; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:27 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
kernel/acct.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/kernel/acct.c b/kernel/acct.c
index 61630110e29d..c1028f992529 100644
--- a/kernel/acct.c
+++ b/kernel/acct.c
@@ -520,12 +520,10 @@ static void fill_ac(struct bsd_acct_struct *acct)
static void acct_write_process(struct bsd_acct_struct *acct)
{
struct file *file = acct->file;
- const struct cred *cred;
acct_t *ac = &acct->ac;
/* Perform file operations on behalf of whoever enabled accounting */
- cred = override_creds(file->f_cred);
-
+ with_creds(file->f_cred);
/*
* First check to see if there is enough free_space to continue
* the process accounting system. Then get freeze protection. If
@@ -538,8 +536,6 @@ static void acct_write_process(struct bsd_acct_struct *acct)
__kernel_write(file, ac, sizeof(acct_t), &pos);
file_end_write(file);
}
-
- revert_creds(cred);
}
static void do_acct_process(struct bsd_acct_struct *acct)
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* Re: [PATCH 14/16] act: use credential guards in acct_write_process()
2025-11-03 11:27 ` [PATCH 14/16] act: use credential guards in acct_write_process() Christian Brauner
@ 2025-11-03 23:04 ` Linus Torvalds
2025-11-04 9:45 ` Amir Goldstein
2025-11-04 11:40 ` Christian Brauner
0 siblings, 2 replies; 23+ messages in thread
From: Linus Torvalds @ 2025-11-03 23:04 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev
On Mon, 3 Nov 2025 at 20:27, Christian Brauner <brauner@kernel.org> wrote:
>
> /* Perform file operations on behalf of whoever enabled accounting */
> - cred = override_creds(file->f_cred);
> -
> + with_creds(file->f_cred);
I'd almost prefer if we *only* did "scoped_with_creds()" and didn't
have this version at all.
Most of the cases want that anyway, and the couple of plain
"with_creds()" cases look like they would only be cleaned up by making
the cred scoping more explicit.
What do you think?
Anyway, I approve of the whole series, obviously, I just suspect we
could narrow down the new interface a bit more.
Linus
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 14/16] act: use credential guards in acct_write_process()
2025-11-03 23:04 ` Linus Torvalds
@ 2025-11-04 9:45 ` Amir Goldstein
2025-11-04 11:40 ` Christian Brauner
1 sibling, 0 replies; 23+ messages in thread
From: Amir Goldstein @ 2025-11-04 9:45 UTC (permalink / raw)
To: Linus Torvalds
Cc: Christian Brauner, linux-fsdevel, linux-kernel, linux-aio,
linux-unionfs, linux-erofs, linux-nfs, linux-cifs,
samba-technical, cgroups, netdev
On Tue, Nov 4, 2025 at 12:04 AM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> On Mon, 3 Nov 2025 at 20:27, Christian Brauner <brauner@kernel.org> wrote:
> >
> > /* Perform file operations on behalf of whoever enabled accounting */
> > - cred = override_creds(file->f_cred);
> > -
> > + with_creds(file->f_cred);
>
> I'd almost prefer if we *only* did "scoped_with_creds()" and didn't
> have this version at all.
>
> Most of the cases want that anyway, and the couple of plain
> "with_creds()" cases look like they would only be cleaned up by making
> the cred scoping more explicit.
>
> What do you think?
I had a similar reaction but for another reason.
The 'with' lingo reminds me of python with statement (e.g.
with open_file('example.txt', 'w') as file:), which implies a scope.
So in my head I am reading "with_creds" as with_creds_do.
Add to that the dubious practice (IMO) of scoped statements
without an explicit {} scope and this can become a source of
human brainos, but maybe the only problematic brain is mine..
Thanks,
Amir.
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 14/16] act: use credential guards in acct_write_process()
2025-11-03 23:04 ` Linus Torvalds
2025-11-04 9:45 ` Amir Goldstein
@ 2025-11-04 11:40 ` Christian Brauner
1 sibling, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-04 11:40 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev
On Tue, Nov 04, 2025 at 08:04:28AM +0900, Linus Torvalds wrote:
> On Mon, 3 Nov 2025 at 20:27, Christian Brauner <brauner@kernel.org> wrote:
> >
> > /* Perform file operations on behalf of whoever enabled accounting */
> > - cred = override_creds(file->f_cred);
> > -
> > + with_creds(file->f_cred);
>
> I'd almost prefer if we *only* did "scoped_with_creds()" and didn't
> have this version at all.
>
> Most of the cases want that anyway, and the couple of plain
> "with_creds()" cases look like they would only be cleaned up by making
> the cred scoping more explicit.
>
> What do you think?
Yeah, good idea. I reworked it all so now we're only left with:
scoped_with_creds()
scoped_with_kernel_creds()
It increases the indentation for about 3 cases but otherwise is safer.
It's all in:
https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/log/?h=kernel-6.19.cred
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 15/16] cgroup: use credential guards in cgroup_attach_permissions()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (13 preceding siblings ...)
2025-11-03 11:27 ` [PATCH 14/16] act: use credential guards in acct_write_process() Christian Brauner
@ 2025-11-03 11:27 ` Christian Brauner
2025-11-03 11:27 ` [PATCH 16/16] net/dns_resolver: use credential guards in dns_query() Christian Brauner
2025-11-03 13:29 ` [PATCH 00/16] credentials guards: the easy cases Amir Goldstein
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:27 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
kernel/cgroup/cgroup.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index fdee387f0d6b..9f61f7cfc8d1 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5363,7 +5363,6 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
struct cgroup_file_ctx *ctx = of->priv;
struct cgroup *src_cgrp, *dst_cgrp;
struct task_struct *task;
- const struct cred *saved_cred;
ssize_t ret;
enum cgroup_attach_lock_mode lock_mode;
@@ -5386,11 +5385,10 @@ static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
* permissions using the credentials from file open to protect against
* inherited fd attacks.
*/
- saved_cred = override_creds(of->file->f_cred);
- ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
- of->file->f_path.dentry->d_sb,
- threadgroup, ctx->ns);
- revert_creds(saved_cred);
+ scoped_with_creds(of->file->f_cred)
+ ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
+ of->file->f_path.dentry->d_sb,
+ threadgroup, ctx->ns);
if (ret)
goto out_finish;
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* [PATCH 16/16] net/dns_resolver: use credential guards in dns_query()
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (14 preceding siblings ...)
2025-11-03 11:27 ` [PATCH 15/16] cgroup: use credential guards in cgroup_attach_permissions() Christian Brauner
@ 2025-11-03 11:27 ` Christian Brauner
2025-11-03 13:29 ` [PATCH 00/16] credentials guards: the easy cases Amir Goldstein
16 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 11:27 UTC (permalink / raw)
To: Linus Torvalds
Cc: linux-fsdevel, linux-kernel, linux-aio, linux-unionfs,
linux-erofs, linux-nfs, linux-cifs, samba-technical, cgroups,
netdev, Christian Brauner
Use credential guards for scoped credential override with automatic
restoration on scope exit.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
net/dns_resolver/dns_query.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/net/dns_resolver/dns_query.c b/net/dns_resolver/dns_query.c
index 82b084cc1cc6..53da62984447 100644
--- a/net/dns_resolver/dns_query.c
+++ b/net/dns_resolver/dns_query.c
@@ -78,7 +78,6 @@ int dns_query(struct net *net,
{
struct key *rkey;
struct user_key_payload *upayload;
- const struct cred *saved_cred;
size_t typelen, desclen;
char *desc, *cp;
int ret, len;
@@ -124,9 +123,8 @@ int dns_query(struct net *net,
/* make the upcall, using special credentials to prevent the use of
* add_key() to preinstall malicious redirections
*/
- saved_cred = override_creds(dns_resolver_cache);
- rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
- revert_creds(saved_cred);
+ scoped_with_creds(dns_resolver_cache)
+ rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
kfree(desc);
if (IS_ERR(rkey)) {
ret = PTR_ERR(rkey);
--
2.47.3
^ permalink raw reply related [flat|nested] 23+ messages in thread* Re: [PATCH 00/16] credentials guards: the easy cases
2025-11-03 11:26 [PATCH 00/16] credentials guards: the easy cases Christian Brauner
` (15 preceding siblings ...)
2025-11-03 11:27 ` [PATCH 16/16] net/dns_resolver: use credential guards in dns_query() Christian Brauner
@ 2025-11-03 13:29 ` Amir Goldstein
2025-11-03 14:53 ` Christian Brauner
16 siblings, 1 reply; 23+ messages in thread
From: Amir Goldstein @ 2025-11-03 13:29 UTC (permalink / raw)
To: Christian Brauner
Cc: Linus Torvalds, linux-fsdevel, linux-kernel, linux-aio,
linux-unionfs, linux-erofs, linux-nfs, linux-cifs,
samba-technical, cgroups, netdev
On Mon, Nov 3, 2025 at 12:28 PM Christian Brauner <brauner@kernel.org> wrote:
>
> This converts all users of override_creds() to rely on credentials
> guards. Leave all those that do the prepare_creds() + modify creds +
> override_creds() dance alone for now. Some of them qualify for their own
> variant.
Nice!
What about with_ovl_creator_cred()/scoped_with_ovl_creator_cred()?
Is there any reason not to do it as well?
I can try to clear some time for this cleanup.
For this series, feel free to add:
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Thanks,
Amir.
>
> Signed-off-by: Christian Brauner <brauner@kernel.org>
> ---
> Christian Brauner (16):
> cred: add {scoped_}with_creds() guards
> aio: use credential guards
> backing-file: use credential guards for reads
> backing-file: use credential guards for writes
> backing-file: use credential guards for splice read
> backing-file: use credential guards for splice write
> backing-file: use credential guards for mmap
> binfmt_misc: use credential guards
> erofs: use credential guards
> nfs: use credential guards in nfs_local_call_read()
> nfs: use credential guards in nfs_local_call_write()
> nfs: use credential guards in nfs_idmap_get_key()
> smb: use credential guards in cifs_get_spnego_key()
> act: use credential guards in acct_write_process()
> cgroup: use credential guards in cgroup_attach_permissions()
> net/dns_resolver: use credential guards in dns_query()
>
> fs/aio.c | 6 +-
> fs/backing-file.c | 147 ++++++++++++++++++++++---------------------
> fs/binfmt_misc.c | 7 +--
> fs/erofs/fileio.c | 6 +-
> fs/nfs/localio.c | 59 +++++++++--------
> fs/nfs/nfs4idmap.c | 7 +--
> fs/smb/client/cifs_spnego.c | 6 +-
> include/linux/cred.h | 12 ++--
> kernel/acct.c | 6 +-
> kernel/cgroup/cgroup.c | 10 ++-
> net/dns_resolver/dns_query.c | 6 +-
> 11 files changed, 133 insertions(+), 139 deletions(-)
> ---
> base-commit: fea79c89ff947a69a55fed5ce86a70840e6d719c
> change-id: 20251103-work-creds-guards-simple-619ef2200d22
>
>
^ permalink raw reply [flat|nested] 23+ messages in thread* Re: [PATCH 00/16] credentials guards: the easy cases
2025-11-03 13:29 ` [PATCH 00/16] credentials guards: the easy cases Amir Goldstein
@ 2025-11-03 14:53 ` Christian Brauner
0 siblings, 0 replies; 23+ messages in thread
From: Christian Brauner @ 2025-11-03 14:53 UTC (permalink / raw)
To: Amir Goldstein
Cc: Linus Torvalds, linux-fsdevel, linux-kernel, linux-aio,
linux-unionfs, linux-erofs, linux-nfs, linux-cifs,
samba-technical, cgroups, netdev
On Mon, Nov 03, 2025 at 02:29:40PM +0100, Amir Goldstein wrote:
> On Mon, Nov 3, 2025 at 12:28 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > This converts all users of override_creds() to rely on credentials
> > guards. Leave all those that do the prepare_creds() + modify creds +
> > override_creds() dance alone for now. Some of them qualify for their own
> > variant.
>
> Nice!
>
> What about with_ovl_creator_cred()/scoped_with_ovl_creator_cred()?
> Is there any reason not to do it as well?
No, I don't think there is other than that the complexity of it warrants
a separate patch series.
When override_creds()/revert_creds() still was a reference count
bonanza, we struggled with two issues related to overlayfs:
(1) reference counting was sometimes very non-obvious
(think:
cred = get_cred(creator_cred);
old_cred = override_cred(cred);
put_cred(revert_creds(old_cred));
put_cred(cred); or worse )
and thus the credential override logic when creating files where you
change the fsuid and you essentially override credentials _twice_
lead to pretty twisted logic that wasn't necessarily clarified by
the scope-based semantics.
This problem is now resolved since my prior rework.
(2) The scope based cleanup did struggle in switch() statements that
messed with the scope-based logic iirc. I don't have the details in
my head right now anymore but basically this is why we originally
punted on the original conversion so we wouldn't end up chasing bugs
in two semantic changes done at the same time.
I think we're ready to do (2) now.
What I tried in this series is to reduce the amount of scope switching
due to gotos and that's why I also moved some code around. It also helps
to visually clarify the guards when the scope is reduced by moving large
portions where work is done under a guard out to a helper. That's
especially true when the guard overrides credentials imho. That's
something I already aimed for during the first conversion.
>
> I can try to clear some time for this cleanup.
>
> For this series, feel free to add:
>
> Reviewed-by: Amir Goldstein <amir73il@gmail.com>
>
> Thanks,
> Amir.
>
> >
> > Signed-off-by: Christian Brauner <brauner@kernel.org>
> > ---
> > Christian Brauner (16):
> > cred: add {scoped_}with_creds() guards
> > aio: use credential guards
> > backing-file: use credential guards for reads
> > backing-file: use credential guards for writes
> > backing-file: use credential guards for splice read
> > backing-file: use credential guards for splice write
> > backing-file: use credential guards for mmap
> > binfmt_misc: use credential guards
> > erofs: use credential guards
> > nfs: use credential guards in nfs_local_call_read()
> > nfs: use credential guards in nfs_local_call_write()
> > nfs: use credential guards in nfs_idmap_get_key()
> > smb: use credential guards in cifs_get_spnego_key()
> > act: use credential guards in acct_write_process()
> > cgroup: use credential guards in cgroup_attach_permissions()
> > net/dns_resolver: use credential guards in dns_query()
> >
> > fs/aio.c | 6 +-
> > fs/backing-file.c | 147 ++++++++++++++++++++++---------------------
> > fs/binfmt_misc.c | 7 +--
> > fs/erofs/fileio.c | 6 +-
> > fs/nfs/localio.c | 59 +++++++++--------
> > fs/nfs/nfs4idmap.c | 7 +--
> > fs/smb/client/cifs_spnego.c | 6 +-
> > include/linux/cred.h | 12 ++--
> > kernel/acct.c | 6 +-
> > kernel/cgroup/cgroup.c | 10 ++-
> > net/dns_resolver/dns_query.c | 6 +-
> > 11 files changed, 133 insertions(+), 139 deletions(-)
> > ---
> > base-commit: fea79c89ff947a69a55fed5ce86a70840e6d719c
> > change-id: 20251103-work-creds-guards-simple-619ef2200d22
> >
> >
^ permalink raw reply [flat|nested] 23+ messages in thread