[PATCH] binfmt_misc: restore write access before closing files opened by open_exec()

Zilin Guan posted 1 patch 1 month, 2 weeks ago
fs/binfmt_misc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] binfmt_misc: restore write access before closing files opened by open_exec()
Posted by Zilin Guan 1 month, 2 weeks ago
bm_register_write() opens an executable file using open_exec(), which
internally calls do_open_execat() and denies write access on the file to
avoid modification while it is being executed.

However, when an error occurs, bm_register_write() closes the file using
filp_close() directly. This does not restore the write permission, which
may cause subsequent write operations on the same file to fail.

Fix this by calling exe_file_allow_write_access() before filp_close() to
restore the write permission properly.

Fixes: e7850f4d844e ("binfmt_misc: fix possible deadlock in bm_register_write")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 fs/binfmt_misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index a839f960cd4a..a8b1d79e4af0 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -837,8 +837,10 @@ static ssize_t bm_register_write(struct file *file, const char __user *buffer,
 	inode_unlock(d_inode(root));
 
 	if (err) {
-		if (f)
+		if (f) {
+			exe_file_allow_write_access(f);
 			filp_close(f, NULL);
+		}
 		kfree(e);
 		return err;
 	}
-- 
2.34.1
Re: [PATCH] binfmt_misc: restore write access before closing files opened by open_exec()
Posted by Christian Brauner 1 month, 1 week ago
On Wed, 05 Nov 2025 02:29:23 +0000, Zilin Guan wrote:
> bm_register_write() opens an executable file using open_exec(), which
> internally calls do_open_execat() and denies write access on the file to
> avoid modification while it is being executed.
> 
> However, when an error occurs, bm_register_write() closes the file using
> filp_close() directly. This does not restore the write permission, which
> may cause subsequent write operations on the same file to fail.
> 
> [...]

Thanks!

---

Applied to the vfs.fixes branch of the vfs/vfs.git tree.
Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] binfmt_misc: restore write access before closing files opened by open_exec()
      https://git.kernel.org/vfs/vfs/c/0e0c1b03d6d4