linux-cifs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-aio@kvack.org, linux-unionfs@vger.kernel.org,
	 linux-erofs@lists.ozlabs.org, linux-nfs@vger.kernel.org,
	 linux-cifs@vger.kernel.org, samba-technical@lists.samba.org,
	 cgroups@vger.kernel.org, netdev@vger.kernel.org,
	 Christian Brauner <brauner@kernel.org>
Subject: [PATCH 04/16] backing-file: use credential guards for writes
Date: Mon, 03 Nov 2025 12:26:52 +0100	[thread overview]
Message-ID: <20251103-work-creds-guards-simple-v1-4-a3e156839e7f@kernel.org> (raw)
In-Reply-To: <20251103-work-creds-guards-simple-v1-0-a3e156839e7f@kernel.org>

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


  parent reply	other threads:[~2025-11-03 11:27 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [PATCH 03/16] backing-file: use credential guards for reads Christian Brauner
2025-11-03 11:26 ` Christian Brauner [this message]
2025-11-03 13:24   ` [PATCH 04/16] backing-file: use credential guards for writes Amir Goldstein
2025-11-03 11:26 ` [PATCH 05/16] backing-file: use credential guards for splice read Christian Brauner
2025-11-03 11:26 ` [PATCH 06/16] backing-file: use credential guards for splice write Christian Brauner
2025-11-03 11:26 ` [PATCH 07/16] backing-file: use credential guards for mmap Christian Brauner
2025-11-03 11:26 ` [PATCH 08/16] binfmt_misc: use credential guards Christian Brauner
2025-11-03 11:26 ` [PATCH 09/16] erofs: " Christian Brauner
2025-11-03 11:26 ` [PATCH 10/16] nfs: use credential guards in nfs_local_call_read() Christian Brauner
2025-11-03 11:26 ` [PATCH 11/16] nfs: use credential guards in nfs_local_call_write() Christian Brauner
2025-11-03 11:27 ` [PATCH 12/16] nfs: use credential guards in nfs_idmap_get_key() Christian Brauner
2025-11-03 11:27 ` [PATCH 13/16] smb: use credential guards in cifs_get_spnego_key() Christian Brauner
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
2025-11-03 11:27 ` [PATCH 15/16] cgroup: use credential guards in cgroup_attach_permissions() 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
2025-11-03 14:53   ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251103-work-creds-guards-simple-v1-4-a3e156839e7f@kernel.org \
    --to=brauner@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=linux-aio@kvack.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-unionfs@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=samba-technical@lists.samba.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).