Skip to contentSkip to conversation

fs: don't block write during exec on pre-content watched files

PATCH In review Amir Goldstein 6 participants 15 messages Nov 28, 2024 – Dec 9, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: don't block write during exec on pre-content watched files

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

---

Jan,

This patch is on top of your fsnotify_hsm rebased branch. It passed LTP sanity tests, but did not test filling an executable on-the-fly.

Josef, can you verify that this works as expected.

Also pushed to fsnotify_hsm branch in my github.

Thanks,
Amir.

 fs/binfmt_elf.c       |  4 ++--
 fs/binfmt_elf_fdpic.c |  4 ++--
 fs/exec.c             |  8 ++++----
 include/linux/fs.h    | 17 +++++++++++++++++
 kernel/fork.c         | 12 ++++++------
 5 files changed, 31 insertions(+), 14 deletions(-)
fs/binfmt_elf.c +2 -2
index 106f0e8af177..8054f44d39cf 100644
@@ -1257,7 +1257,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		}
 		reloc_func_desc = interp_load_addr;
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 
 		kfree(interp_elf_ex);
@@ -1354,7 +1354,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	kfree(interp_elf_ex);
 	kfree(interp_elf_phdata);
 out_free_file:
-	allow_write_access(interpreter);
+	exe_file_allow_write_access(interpreter);
 	if (interpreter)
 		fput(interpreter);
 out_free_ph:
fs/binfmt_elf_fdpic.c +2 -2
index f1a7c4875c4a..c13ee8180b17 100644
@@ -394,7 +394,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 			goto error;
 		}
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 		interpreter = NULL;
 	}
@@ -467,7 +467,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
 error:
 	if (interpreter) {
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 	}
 	kfree(interpreter_name);
fs/exec.c +4 -4
index 98cb7ba9983c..c41cfd35c74c 100644
@@ -912,7 +912,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
 	    path_noexec(&file->f_path))
 		return ERR_PTR(-EACCES);
 
-	err = deny_write_access(file);
+	err = exe_file_deny_write_access(file);
 	if (err)
 		return ERR_PTR(err);
 
@@ -927,7 +927,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
  * Returns ERR_PTR on failure or allocated struct file on success.
  *
  * As this is a wrapper for the internal do_open_execat(), callers
- * must call allow_write_access() before fput() on release. Also see
+ * must call exe_file_allow_write_access() before fput() on release. Also see
  * do_close_execat().
  */
 struct file *open_exec(const char *name)
@@ -1471,7 +1471,7 @@ static void do_close_execat(struct file *file)
 {
 	if (!file)
 		return;
-	allow_write_access(file);
+	exe_file_allow_write_access(file);
 	fput(file);
 }
 
@@ -1797,7 +1797,7 @@ static int exec_binprm(struct linux_binprm *bprm)
 		bprm->file = bprm->interpreter;
 		bprm->interpreter = NULL;
 
-		allow_write_access(exec);
+		exe_file_allow_write_access(exec);
 		if (unlikely(bprm->have_execfd)) {
 			if (bprm->executable) {
 				fput(exec);
include/linux/fs.h +17 -0
index dd6d0eddea9b..2aeab643f1ab 100644
@@ -3089,6 +3089,23 @@ static inline void allow_write_access(struct file *file)
 	if (file)
 		atomic_inc(&file_inode(file)->i_writecount);
 }
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return 0;
+	return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return;
+	allow_write_access(exe_file);
+}
+
 static inline bool inode_is_open_for_write(const struct inode *inode)
 {
 	return atomic_read(&inode->i_writecount) > 0;
kernel/fork.c +6 -6
index 1450b461d196..015c397f47ca 100644
@@ -625,8 +625,8 @@ static void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm)
 	 * We depend on the oldmm having properly denied write access to the
 	 * exe_file already.
 	 */
-	if (exe_file && deny_write_access(exe_file))
-		pr_warn_once("deny_write_access() failed in %s\n", __func__);
+	if (exe_file && exe_file_deny_write_access(exe_file))
+		pr_warn_once("exe_file_deny_write_access() failed in %s\n", __func__);
 }
 
 #ifdef CONFIG_MMU
@@ -1424,13 +1424,13 @@ int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 		 * We expect the caller (i.e., sys_execve) to already denied
 		 * write access, so this is unlikely to fail.
 		 */
-		if (unlikely(deny_write_access(new_exe_file)))
+		if (unlikely(exe_file_deny_write_access(new_exe_file)))
 			return -EACCES;
 		get_file(new_exe_file);
 	}
 	rcu_assign_pointer(mm->exe_file, new_exe_file);
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
@@ -1471,7 +1471,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 			return ret;
 	}
 
-	ret = deny_write_access(new_exe_file);
+	ret = exe_file_deny_write_access(new_exe_file);
 	if (ret)
 		return -EACCES;
 	get_file(new_exe_file);
@@ -1483,7 +1483,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 	mmap_write_unlock(mm);
 
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 23 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

include/linux/fs.h +7 -0
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))

include/linux/fs.h static inline void allow_write_access(struct file *file) · line 3108

Quoted 10 lines
include/linux/fs.h +10 -0
+		return 0;
+	return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return;
+	allow_write_access(exe_file);
+}
+

so this depends on FMODE_FSNOTIFY_HSM showing up on the file before any of the above calls and staying there for its lifetime -- does that hold?

I think it would be less error prone down the road to maintain the counters, except not return the error if HSM is on.

#2 lore raw
Quoted 30 lines · Mateusz Guzik
Quoted 28 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

include/linux/fs.h +12 -0
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+             return 0;
+     return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
Quoted 8 lines

include/linux/fs.h static inline void allow_write_access(struct file *file) · line 3108

Quoted 5 lines
include/linux/fs.h +5 -0
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+             return;
+     allow_write_access(exe_file);
+}
+

so this depends on FMODE_FSNOTIFY_HSM showing up on the file before any of the above calls and staying there for its lifetime -- does that hold?

Yes!

Quoted 3 lines

I think it would be less error prone down the road to maintain the counters, except not return the error if HSM is on.

Cannot.
The "deny write counter" and "writers counter" are implemented on the same counter, so open cannot get_write_access() if we maintain the negative deny counters from exec.

Also, note that there are still calls to {allow,deny}_write_access() that are not bypassed even with HSM, so for example, mixing fsverity and HSM is probably not a good idea...

Thanks,
Amir.

#3 lore raw
Quoted 41 lines · Amir Goldstein
Quoted 30 lines · Mateusz Guzik
Quoted 28 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

include/linux/fs.h +12 -0
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+             return 0;
+     return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
Quoted 8 lines

include/linux/fs.h static inline void allow_write_access(struct file *file) · line 3108

Quoted 5 lines
include/linux/fs.h +5 -0
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+             return;
+     allow_write_access(exe_file);
+}
+

so this depends on FMODE_FSNOTIFY_HSM showing up on the file before any of the above calls and staying there for its lifetime -- does that hold?

Quoted 2 lines

Yes!

ok

In this case the new routines should come with a comment denoting it, otherwise the code looks incredibly suspicious.

Quoted 9 lines
Quoted 3 lines

I think it would be less error prone down the road to maintain the counters, except not return the error if HSM is on.

Cannot.
The "deny write counter" and "writers counter" are implemented on the same counter, so open cannot get_write_access() if we maintain the negative deny counters from exec.

I'm aware, in the above suggestion they would have to be split. Not great by any means but would beat the counter suddenly getting modified if the above did not hold.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#4 lore raw
Quoted 43 lines · Mateusz Guzik
Quoted 41 lines · Amir Goldstein
Quoted 38 lines · Mateusz Guzik
Quoted 23 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

include/linux/fs.h +7 -0
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))

include/linux/fs.h static inline void allow_write_access(struct file *file) · line 3108

Quoted 10 lines
include/linux/fs.h +10 -0
+             return 0;
+     return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
+     if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+             return;
+     allow_write_access(exe_file);
+}
+

so this depends on FMODE_FSNOTIFY_HSM showing up on the file before any of the above calls and staying there for its lifetime -- does that hold?

Quoted 7 lines
Quoted 2 lines

Yes!

ok

In this case the new routines should come with a comment denoting it, otherwise the code looks incredibly suspicious.

How's this:

/*
* Do not prevent write to executable file when watched by pre-content events. *
* (*) FMODE_FSNOTIFY_HSM mode is set depending on pre-content watches at the

 *     time of file open and remains for entire lifetime of the file, so if
 *     pre-content watches are added post execution or removed before the end
 *     of the execution, it will not cause i_writecount reference leak.

*/

Jan, can you add this on commit?

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

I think it would be less error prone down the road to maintain the counters, except not return the error if HSM is on.

Cannot.
The "deny write counter" and "writers counter" are implemented on the same counter, so open cannot get_write_access() if we maintain the negative deny counters from exec.

Quoted 3 lines

I'm aware, in the above suggestion they would have to be split. Not great by any means but would beat the counter suddenly getting modified if the above did not hold.

Well, Christian did propose to add a counter in the i_pipe/i_cdev union, but this all seems too much.

We are forced to carry the legacy behavior to avoid ABI breakage, but maintaining a more complex code because of that is not a good idea IMO.

Hopefully, the suggested solution is a fair compromise.

Thanks,
Amir.

#5 lore raw
Quoted 19 lines · Amir Goldstein
Quoted 7 lines · Mateusz Guzik
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Mateusz Guzik

so this depends on FMODE_FSNOTIFY_HSM showing up on the file before any of the above calls and staying there for its lifetime -- does that hold?

Quoted 7 lines
Quoted 2 lines

Yes!

ok

In this case the new routines should come with a comment denoting it, otherwise the code looks incredibly suspicious.

How's this:

Quoted 9 lines

/*
* Do not prevent write to executable file when watched by pre-content events. *
* (*) FMODE_FSNOTIFY_HSM mode is set depending on pre-content watches at the

 *     time of file open and remains for entire lifetime of the file, so if
 *     pre-content watches are added post execution or removed before the end
 *     of the execution, it will not cause i_writecount reference leak.

*/

Looks good, thank you!

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#6 lore raw
In reply to Amir Goldstein
kernel test robot noticed the following build errors:

Hi Amir,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on next-20241128]
[cannot apply to kees/for-next/execve brauner-vfs/vfs.all jack-fs/for_next v6.12] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Amir-Goldstein/fs-don-t-block-write-during-exec-on-pre-content-watched-files/20241128-222743
base:   linus/master
patch link:    https://lore.kernel.org/r/20241128142532.465176-1-amir73il%40gmail.com
patch subject: [PATCH] fs: don't block write during exec on pre-content watched files
config: powerpc-taishan_defconfig (https://download.01.org/0day-ci/archive/20241129/[email protected]/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 592c0fe55f6d9a811028b5f3507be91458ab2713)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241129/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/powerpc/kernel/asm-offsets.c:12:
In file included from include/linux/compat.h:17:

>> include/linux/fs.h:3068:15: error: call to undeclared function 'FMODE_FSNOTIFY_HSM'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3068 |         if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
         |                      ^
   include/linux/fs.h:3074:15: error: call to undeclared function 'FMODE_FSNOTIFY_HSM'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3074 |         if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
         |                      ^
   In file included from arch/powerpc/kernel/asm-offsets.c:19:
   In file included from include/linux/mman.h:6:
   In file included from include/linux/mm.h:2223:
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   In file included from arch/powerpc/kernel/asm-offsets.c:19:
   include/linux/mman.h:158:9: warning: division by zero is undefined [-Wdivision-by-zero]
     158 |                _calc_vm_trans(flags, MAP_LOCKED,     VM_LOCKED    ) |
         |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/mman.h:137:21: note: expanded from macro '_calc_vm_trans'
     137 |    : ((x) & (bit1)) / ((bit1) / (bit2))))
         |                     ^ ~~~~~~~~~~~~~~~~~
   include/linux/mman.h:159:9: warning: division by zero is undefined [-Wdivision-by-zero]
     159 |                _calc_vm_trans(flags, MAP_SYNC,       VM_SYNC      ) |
         |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/mman.h:137:21: note: expanded from macro '_calc_vm_trans'
     137 |    : ((x) & (bit1)) / ((bit1) / (bit2))))
         |                     ^ ~~~~~~~~~~~~~~~~~
   include/linux/mman.h:160:9: warning: division by zero is undefined [-Wdivision-by-zero]
     160 |                _calc_vm_trans(flags, MAP_STACK,      VM_NOHUGEPAGE) |
         |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/mman.h:137:21: note: expanded from macro '_calc_vm_trans'
     137 |    : ((x) & (bit1)) / ((bit1) / (bit2))))
         |                     ^ ~~~~~~~~~~~~~~~~~

4 warnings and 2 errors generated.
make[3]: *** [scripts/Makefile.build:102: arch/powerpc/kernel/asm-offsets.s] Error 1 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1211: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:224: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:224: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/FMODE_FSNOTIFY_HSM +3068 include/linux/fs.h

3062
3063 /*
3064 * Do not prevent write to executable file when watched by pre-content events.
3065 */
3066 static inline int exe_file_deny_write_access(struct file *exe_file)
3067 {

> 3068		if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))

3069 return 0;
3070 return deny_write_access(exe_file);
3071 }
3072 static inline void exe_file_allow_write_access(struct file *exe_file)
3073 {
3074 if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
3075 return;
3076 allow_write_access(exe_file);
3077 }
3078

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw
In reply to Amir Goldstein
kernel test robot noticed the following build errors:

Hi Amir,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on next-20241128]
[cannot apply to kees/for-next/execve brauner-vfs/vfs.all jack-fs/for_next v6.12] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Amir-Goldstein/fs-don-t-block-write-during-exec-on-pre-content-watched-files/20241128-222743
base:   linus/master
patch link:    https://lore.kernel.org/r/20241128142532.465176-1-amir73il%40gmail.com
patch subject: [PATCH] fs: don't block write during exec on pre-content watched files
config: sh-lboxre2_defconfig (https://download.01.org/0day-ci/archive/20241129/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241129/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/export.h:5,
from include/linux/linkage.h:7, from arch/sh/include/asm/bug.h:5, from include/linux/bug.h:5,
from include/linux/mmdebug.h:5, from include/linux/mm.h:6,
from arch/sh/kernel/asm-offsets.c:14:
include/linux/fs.h: In function 'exe_file_deny_write_access':

>> include/linux/fs.h:3068:22: error: implicit declaration of function 'FMODE_FSNOTIFY_HSM'; did you mean 'FMODE_NONOTIFY'? [-Wimplicit-function-declaration]
    3068 |         if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
         |                      ^~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:77:45: note: in definition of macro 'unlikely'
      77 | # define unlikely(x)    __builtin_expect(!!(x), 0)
         |                                             ^

make[3]: *** [scripts/Makefile.build:102: arch/sh/kernel/asm-offsets.s] Error 1 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1211: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:224: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:224: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +3068 include/linux/fs.h

3062
3063 /*
3064 * Do not prevent write to executable file when watched by pre-content events.
3065 */
3066 static inline int exe_file_deny_write_access(struct file *exe_file)
3067 {

> 3068		if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))

3069 return 0;
3070 return deny_write_access(exe_file);
3071 }
3072 static inline void exe_file_allow_write_access(struct file *exe_file)
3073 {
3074 if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
3075 return;
3076 allow_write_access(exe_file);
3077 }
3078

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
reviewer
#8 lore raw
In reply to Amir Goldstein
Quoted 16 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

Quoted 8 lines

---

Jan,

This patch is on top of your fsnotify_hsm rebased branch. It passed LTP sanity tests, but did not test filling an executable on-the-fly.

Just to be clear: I'm fine with this approach but I'd like to see this tested and get Christian's ack before pushing the patch into my tree (and then into linux-next because at this point this is the only outstanding problem I'm aware of).

Honza

Quoted 171 lines
 fs/binfmt_elf.c       |  4 ++--
 fs/binfmt_elf_fdpic.c |  4 ++--
 fs/exec.c             |  8 ++++----
 include/linux/fs.h    | 17 +++++++++++++++++
 kernel/fork.c         | 12 ++++++------
 5 files changed, 31 insertions(+), 14 deletions(-)
fs/binfmt_elf.c +2 -2
index 106f0e8af177..8054f44d39cf 100644
@@ -1257,7 +1257,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		}
 		reloc_func_desc = interp_load_addr;
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 
 		kfree(interp_elf_ex);
@@ -1354,7 +1354,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	kfree(interp_elf_ex);
 	kfree(interp_elf_phdata);
 out_free_file:
-	allow_write_access(interpreter);
+	exe_file_allow_write_access(interpreter);
 	if (interpreter)
 		fput(interpreter);
 out_free_ph:
fs/binfmt_elf_fdpic.c +2 -2
index f1a7c4875c4a..c13ee8180b17 100644
@@ -394,7 +394,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 			goto error;
 		}
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 		interpreter = NULL;
 	}
@@ -467,7 +467,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
 error:
 	if (interpreter) {
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 	}
 	kfree(interpreter_name);
fs/exec.c +4 -4
index 98cb7ba9983c..c41cfd35c74c 100644
@@ -912,7 +912,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
 	    path_noexec(&file->f_path))
 		return ERR_PTR(-EACCES);
 
-	err = deny_write_access(file);
+	err = exe_file_deny_write_access(file);
 	if (err)
 		return ERR_PTR(err);
 
@@ -927,7 +927,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
  * Returns ERR_PTR on failure or allocated struct file on success.
  *
  * As this is a wrapper for the internal do_open_execat(), callers
- * must call allow_write_access() before fput() on release. Also see
+ * must call exe_file_allow_write_access() before fput() on release. Also see
  * do_close_execat().
  */
 struct file *open_exec(const char *name)
@@ -1471,7 +1471,7 @@ static void do_close_execat(struct file *file)
 {
 	if (!file)
 		return;
-	allow_write_access(file);
+	exe_file_allow_write_access(file);
 	fput(file);
 }
 
@@ -1797,7 +1797,7 @@ static int exec_binprm(struct linux_binprm *bprm)
 		bprm->file = bprm->interpreter;
 		bprm->interpreter = NULL;
 
-		allow_write_access(exec);
+		exe_file_allow_write_access(exec);
 		if (unlikely(bprm->have_execfd)) {
 			if (bprm->executable) {
 				fput(exec);
include/linux/fs.h +17 -0
index dd6d0eddea9b..2aeab643f1ab 100644
@@ -3089,6 +3089,23 @@ static inline void allow_write_access(struct file *file)
 	if (file)
 		atomic_inc(&file_inode(file)->i_writecount);
 }
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return 0;
+	return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return;
+	allow_write_access(exe_file);
+}
+
 static inline bool inode_is_open_for_write(const struct inode *inode)
 {
 	return atomic_read(&inode->i_writecount) > 0;
kernel/fork.c +6 -6
index 1450b461d196..015c397f47ca 100644
@@ -625,8 +625,8 @@ static void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm)
 	 * We depend on the oldmm having properly denied write access to the
 	 * exe_file already.
 	 */
-	if (exe_file && deny_write_access(exe_file))
-		pr_warn_once("deny_write_access() failed in %s\n", __func__);
+	if (exe_file && exe_file_deny_write_access(exe_file))
+		pr_warn_once("exe_file_deny_write_access() failed in %s\n", __func__);
 }
 
 #ifdef CONFIG_MMU
@@ -1424,13 +1424,13 @@ int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 		 * We expect the caller (i.e., sys_execve) to already denied
 		 * write access, so this is unlikely to fail.
 		 */
-		if (unlikely(deny_write_access(new_exe_file)))
+		if (unlikely(exe_file_deny_write_access(new_exe_file)))
 			return -EACCES;
 		get_file(new_exe_file);
 	}
 	rcu_assign_pointer(mm->exe_file, new_exe_file);
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
@@ -1471,7 +1471,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 			return ret;
 	}
 
-	ret = deny_write_access(new_exe_file);
+	ret = exe_file_deny_write_access(new_exe_file);
 	if (ret)
 		return -EACCES;
 	get_file(new_exe_file);
@@ -1483,7 +1483,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 	mmap_write_unlock(mm);
 
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Acked-by
#9 lore raw
Quoted 20 lines · Jan Kara
Quoted 19 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

---

Quoted 8 lines
Quoted 5 lines

Jan,

This patch is on top of your fsnotify_hsm rebased branch. It passed LTP sanity tests, but did not test filling an executable on-the-fly.

Just to be clear: I'm fine with this approach but I'd like to see this tested and get Christian's ack before pushing the patch into my tree

Thanks! This is fine by me. I already mentioned to Linus that we might have to accept some unpleasantness in this area but this looks pretty acceptable to me.

Only one question: The FMODE_FSNOTIFY_HSM(exe_file->f_mode) cannot change once it's set, right?

The reason I'm asking is that we don't want to end up in a scenario where we didn't block writecount in exe_file_deny_write_access() but then unblock someone else's writecount in exe_file_allow_write_access().

Quoted 179 lines

(and then into linux-next because at this point this is the only outstanding problem I'm aware of).

Honza

Quoted 171 lines
 fs/binfmt_elf.c       |  4 ++--
 fs/binfmt_elf_fdpic.c |  4 ++--
 fs/exec.c             |  8 ++++----
 include/linux/fs.h    | 17 +++++++++++++++++
 kernel/fork.c         | 12 ++++++------
 5 files changed, 31 insertions(+), 14 deletions(-)
fs/binfmt_elf.c +2 -2
index 106f0e8af177..8054f44d39cf 100644
@@ -1257,7 +1257,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		}
 		reloc_func_desc = interp_load_addr;
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 
 		kfree(interp_elf_ex);
@@ -1354,7 +1354,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	kfree(interp_elf_ex);
 	kfree(interp_elf_phdata);
 out_free_file:
-	allow_write_access(interpreter);
+	exe_file_allow_write_access(interpreter);
 	if (interpreter)
 		fput(interpreter);
 out_free_ph:
fs/binfmt_elf_fdpic.c +2 -2
index f1a7c4875c4a..c13ee8180b17 100644
@@ -394,7 +394,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 			goto error;
 		}
 
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 		interpreter = NULL;
 	}
@@ -467,7 +467,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
 
 error:
 	if (interpreter) {
-		allow_write_access(interpreter);
+		exe_file_allow_write_access(interpreter);
 		fput(interpreter);
 	}
 	kfree(interpreter_name);
fs/exec.c +4 -4
index 98cb7ba9983c..c41cfd35c74c 100644
@@ -912,7 +912,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
 	    path_noexec(&file->f_path))
 		return ERR_PTR(-EACCES);
 
-	err = deny_write_access(file);
+	err = exe_file_deny_write_access(file);
 	if (err)
 		return ERR_PTR(err);
 
@@ -927,7 +927,7 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
  * Returns ERR_PTR on failure or allocated struct file on success.
  *
  * As this is a wrapper for the internal do_open_execat(), callers
- * must call allow_write_access() before fput() on release. Also see
+ * must call exe_file_allow_write_access() before fput() on release. Also see
  * do_close_execat().
  */
 struct file *open_exec(const char *name)
@@ -1471,7 +1471,7 @@ static void do_close_execat(struct file *file)
 {
 	if (!file)
 		return;
-	allow_write_access(file);
+	exe_file_allow_write_access(file);
 	fput(file);
 }
 
@@ -1797,7 +1797,7 @@ static int exec_binprm(struct linux_binprm *bprm)
 		bprm->file = bprm->interpreter;
 		bprm->interpreter = NULL;
 
-		allow_write_access(exec);
+		exe_file_allow_write_access(exec);
 		if (unlikely(bprm->have_execfd)) {
 			if (bprm->executable) {
 				fput(exec);
include/linux/fs.h +17 -0
index dd6d0eddea9b..2aeab643f1ab 100644
@@ -3089,6 +3089,23 @@ static inline void allow_write_access(struct file *file)
 	if (file)
 		atomic_inc(&file_inode(file)->i_writecount);
 }
+
+/*
+ * Do not prevent write to executable file when watched by pre-content events.
+ */
+static inline int exe_file_deny_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return 0;
+	return deny_write_access(exe_file);
+}
+static inline void exe_file_allow_write_access(struct file *exe_file)
+{
+	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
+		return;
+	allow_write_access(exe_file);
+}
+
 static inline bool inode_is_open_for_write(const struct inode *inode)
 {
 	return atomic_read(&inode->i_writecount) > 0;
kernel/fork.c +6 -6
index 1450b461d196..015c397f47ca 100644
@@ -625,8 +625,8 @@ static void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm)
 	 * We depend on the oldmm having properly denied write access to the
 	 * exe_file already.
 	 */
-	if (exe_file && deny_write_access(exe_file))
-		pr_warn_once("deny_write_access() failed in %s\n", __func__);
+	if (exe_file && exe_file_deny_write_access(exe_file))
+		pr_warn_once("exe_file_deny_write_access() failed in %s\n", __func__);
 }
 
 #ifdef CONFIG_MMU
@@ -1424,13 +1424,13 @@ int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 		 * We expect the caller (i.e., sys_execve) to already denied
 		 * write access, so this is unlikely to fail.
 		 */
-		if (unlikely(deny_write_access(new_exe_file)))
+		if (unlikely(exe_file_deny_write_access(new_exe_file)))
 			return -EACCES;
 		get_file(new_exe_file);
 	}
 	rcu_assign_pointer(mm->exe_file, new_exe_file);
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
@@ -1471,7 +1471,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 			return ret;
 	}
 
-	ret = deny_write_access(new_exe_file);
+	ret = exe_file_deny_write_access(new_exe_file);
 	if (ret)
 		return -EACCES;
 	get_file(new_exe_file);
@@ -1483,7 +1483,7 @@ int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
 	mmap_write_unlock(mm);
 
 	if (old_exe_file) {
-		allow_write_access(old_exe_file);
+		exe_file_allow_write_access(old_exe_file);
 		fput(old_exe_file);
 	}
 	return 0;
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw
Quoted 7 lines · Christian Brauner

Only one question: The FMODE_FSNOTIFY_HSM(exe_file->f_mode) cannot change once it's set, right?

The reason I'm asking is that we don't want to end up in a scenario where we didn't block writecount in exe_file_deny_write_access() but then unblock someone else's writecount in exe_file_allow_write_access().

What's what I brought up, turns out its fine and the code will get a comment to that extent.

see https://lore.kernel.org/linux-fsdevel/CAGudoHGzfjpukQ1QbuH=0Fot2vAWyrez-aVdO5Fum+330v-hmA@mail.gmail.com/

#11 lore raw
Quoted 9 lines · Mateusz Guzik
Quoted 7 lines · Christian Brauner

Only one question: The FMODE_FSNOTIFY_HSM(exe_file->f_mode) cannot change once it's set, right?

The reason I'm asking is that we don't want to end up in a scenario where we didn't block writecount in exe_file_deny_write_access() but then unblock someone else's writecount in exe_file_allow_write_access().

Quoted 4 lines

What's what I brought up, turns out its fine and the code will get a comment to that extent.

see https://lore.kernel.org/linux-fsdevel/CAGudoHGzfjpukQ1QbuH=0Fot2vAWyrez-aVdO5Fum+330v-hmA@mail.gmail.com/

Thank you! Then it seems we're good to go.

reviewer
#12 lore raw
In reply to Amir Goldstein
Quoted 10 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

Quoted 7 lines

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

OK, I've picked up the patch to fsnotify_hsm branch with Christian's ack and updated comments from Amir. Still waiting for Josef to give this a final testing from their side but I've pulled the branch into for_next so that it gets some exposure in linux-next as well.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw
Quoted 18 lines · Jan Kara
Quoted 16 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

Quoted 6 lines
Quoted 1 line

OK, I've picked up the patch to fsnotify_hsm branch with Christian's ack and updated comments from Amir. Still waiting for Josef to give this a final testing from their side but I've pulled the branch into for_next so that it gets some exposure in linux-next as well.

Cool. I just pushed a test to my LTP fan_hsm branch for ETXTBSY.

It checks for the expected ETXTBSY with yes or no pre-content watchers, but it checks failure to execute a file open for write, not actually to open a file for write in the context of PRE_ACCESS event during execution.

So yeh, a test of the actual use case of large lazy populated executables from Josef
is required, but at least we have basic sanity test coverage and now we will get some extra linux-next testing coverage which is great.

Thanks,
Amir.

#14 lore raw
Quoted 28 lines · Amir Goldstein
Quoted 18 lines · Jan Kara
Quoted 16 lines · Amir Goldstein

Commit 2a010c412853 ("fs: don't block i_writecount during exec") removed the legacy behavior of getting ETXTBSY on attempt to open and executable file for write while it is being executed.

This commit was reverted because an application that depends on this legacy behavior was broken by the change.

We need to allow HSM writing into executable files while executed to fill their content on-the-fly.

To that end, disable the ETXTBSY legacy behavior for files that are watched by pre-content events.

This change is not expected to cause regressions with existing systems which do not have any pre-content event listeners.

Quoted 6 lines
Quoted 1 line

OK, I've picked up the patch to fsnotify_hsm branch with Christian's ack and updated comments from Amir. Still waiting for Josef to give this a final testing from their side but I've pulled the branch into for_next so that it gets some exposure in linux-next as well.

Cool. I just pushed a test to my LTP fan_hsm branch for ETXTBSY.

Quoted 8 lines

It checks for the expected ETXTBSY with yes or no pre-content watchers, but it checks failure to execute a file open for write, not actually to open a file for write in the context of PRE_ACCESS event during execution.

So yeh, a test of the actual use case of large lazy populated executables from Josef
is required, but at least we have basic sanity test coverage and now we will get some extra linux-next testing coverage which is great.

Sorry guys, holidays and then some PTO and other things conspired against me being useful. I just built and tested Jan's branch as of 10 minutes ago and everything works. Thanks for picking up my slack, I'll be actually responsive for the next two weeks and then I'll disappear again until Jan 6th,

Josef