Skip to contentSkip to conversation

ntfs: prevent modification of $Extend system metadata

PATCH In review Namjae Jeon 3 participants 3 messages Nov 25 – Nov 26 NTFS filesystem

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH v2 1/1 ntfs: prevent modification of $Extend system metadata

The $Extend directory contains system files such as $ObjId, $Quota, and $Reparse. show_sys_files only controls directory enumeration, so hidden system metadata remains reachable by explicit path. With the default mount options, forced immutability covers reserved regular files, allowing removal of $Extend and its entries. This can make later mounts fail when load_system_files() opens FILE_Extend.

Make FILE_Extend and its direct children immutable independently of sys_immutable. Classify the children during inode loading so protection covers directories and files with an unnamed $DATA attribute. This prevents userspace from changing directory entries or writing to the protected files. Return -EPERM for file flag updates before writing the $LINUXFLAGS EA or updating ctime.

Fixes af0db57d4293 ("ntfs: update inode operations")

---
Changes in v2
- Use Baolin's inode-loading change with the existing lookup helper.
- Block file flag writes with a direct check in ntfs_fileattr_set().

 fs/ntfs/file.c  |  4 ++++
 fs/ntfs/inode.c | 19 +++++++++++++++----
 fs/ntfs/inode.h |  3 +++
 3 files changed, 22 insertions(+), 4 deletions(-)
fs/ntfs/file.c +4 -0
index 7d93a520566e..612498b45fbb 100644
@@ -231,6 +231,10 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (fa->flags & ~allowed)
 		return -EOPNOTSUPP;
 
+	/* File flag updates would modify $Extend metadata. */
+	if (ni->mft_no == FILE_Extend || NInoExtendSys(ni))
+		return -EPERM;
+
 	forced_immutable = ntfs_is_forced_immutable(vi);
 	if (!(fa->flags & FS_IMMUTABLE_FL) && forced_immutable)
 		return -EPERM;
fs/ntfs/inode.c +15 -4
index 4fbdcd459652..3aac1d7d310f 100644
@@ -960,6 +960,15 @@ skip_attr_list_load:
 		goto unm_err_out;
 	}
 
+	/* Classify $Extend children regardless of their data or index layout. */
+	if (ni->mft_no >= FILE_first_user) {
+		err = ntfs_is_extended_system_file(ctx);
+		if (err < 0)
+			goto unm_err_out;
+		if (err > 0)
+			NInoSetExtendSys(ni);
+	}
+
 	if (ni->flags & FILE_ATTR_REPARSE_POINT) {
 		unsigned int mode;
 
@@ -1307,11 +1316,13 @@ no_data_attr_special_case:
 
 	/*
 	 * System files such as $Bitmap and $MFT are maintained by the driver
-	 * itself, and writing them from userspace corrupts the volume.
-	 * Always make them immutable regardless of the sys_immutable option.
-	 * Directories are skipped so the root and $Extend stay usable.
+	 * itself. Always make them immutable regardless of the sys_immutable
+	 * option. Protect $Extend and its system files too; modifying them
+	 * through the VFS can corrupt NTFS metadata.
 	 */
-	if (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode))
+	if (NInoExtendSys(ni) ||
+	    ni->mft_no == FILE_Extend ||
+	    (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode)))
 		vi->i_flags |= S_IMMUTABLE;
 
 	/*
fs/ntfs/inode.h +3 -0
index 7ea52c59c04b..8c1929bebe44 100644
@@ -182,6 +182,7 @@ struct ntfs_inode {
  * NI_HasEA			ntfs inode has EA attribute.
  * NI_RunlistDirty		runlist need to be updated.
  * NI_StreamUnlinked		Named stream is unlinked but still open.
+ * NI_ExtendSys			Inode is a system file in $Extend.
  */
 enum {
 	NI_Dirty,
@@ -204,6 +205,7 @@ enum {
 	NI_HasEA,
 	NI_RunlistDirty,
 	NI_StreamUnlinked,
+	NI_ExtendSys,
 };
 
 /*
@@ -265,6 +267,7 @@ NINO_FNS(BeingDeleted)
 NINO_FNS(HasEA)
 NINO_FNS(RunlistDirty)
 NINO_FNS(StreamUnlinked)
+NINO_FNS(ExtendSys)
 
 /*
  * The full structure containing a ntfs_inode and a vfs struct inode. Used for

base-commit: f674e4b6c315ebe742d08af80659386992d58ce9

Reviewed-by Baolin Liu on v2 1/1
#1 lore raw
Quoted 109 lines · Namjae Jeon

The $Extend directory contains system files such as $ObjId, $Quota, and $Reparse. show_sys_files only controls directory enumeration, so hidden system metadata remains reachable by explicit path. With the default mount options, forced immutability covers reserved regular files, allowing removal of $Extend and its entries. This can make later mounts fail when load_system_files() opens FILE_Extend.

Make FILE_Extend and its direct children immutable independently of sys_immutable. Classify the children during inode loading so protection covers directories and files with an unnamed $DATA attribute. This prevents userspace from changing directory entries or writing to the protected files. Return -EPERM for file flag updates before writing the $LINUXFLAGS EA or updating ctime.

Fixes af0db57d4293 ("ntfs: update inode operations")

---
Changes in v2
- Use Baolin's inode-loading change with the existing lookup helper.
- Block file flag writes with a direct check in ntfs_fileattr_set().

  fs/ntfs/file.c  |  4 ++++
  fs/ntfs/inode.c | 19 +++++++++++++++----
  fs/ntfs/inode.h |  3 +++
  3 files changed, 22 insertions(+), 4 deletions(-)
fs/ntfs/file.c +4 -0
index 7d93a520566e..612498b45fbb 100644
@@ -231,6 +231,10 @@ int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
  	if (fa->flags & ~allowed)
  		return -EOPNOTSUPP;
 
+	/* File flag updates would modify $Extend metadata. */
+	if (ni->mft_no == FILE_Extend || NInoExtendSys(ni))
+		return -EPERM;
+
  	forced_immutable = ntfs_is_forced_immutable(vi);
  	if (!(fa->flags & FS_IMMUTABLE_FL) && forced_immutable)
  		return -EPERM;
fs/ntfs/inode.c +15 -4
index 4fbdcd459652..3aac1d7d310f 100644
@@ -960,6 +960,15 @@ skip_attr_list_load:
  		goto unm_err_out;
  	}
 
+	/* Classify $Extend children regardless of their data or index layout. */
+	if (ni->mft_no >= FILE_first_user) {
+		err = ntfs_is_extended_system_file(ctx);
+		if (err < 0)
+			goto unm_err_out;
+		if (err > 0)
+			NInoSetExtendSys(ni);
+	}
+
  	if (ni->flags & FILE_ATTR_REPARSE_POINT) {
  		unsigned int mode;
 
@@ -1307,11 +1316,13 @@ no_data_attr_special_case:
 
  	/*
  	 * System files such as $Bitmap and $MFT are maintained by the driver
-	 * itself, and writing them from userspace corrupts the volume.
-	 * Always make them immutable regardless of the sys_immutable option.
-	 * Directories are skipped so the root and $Extend stay usable.
+	 * itself. Always make them immutable regardless of the sys_immutable
+	 * option. Protect $Extend and its system files too; modifying them
+	 * through the VFS can corrupt NTFS metadata.
  	 */
-	if (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode))
+	if (NInoExtendSys(ni) ||
+	    ni->mft_no == FILE_Extend ||
+	    (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode)))
  		vi->i_flags |= S_IMMUTABLE;
 
  	/*
fs/ntfs/inode.h +3 -0
index 7ea52c59c04b..8c1929bebe44 100644
@@ -182,6 +182,7 @@ struct ntfs_inode {
   * NI_HasEA			ntfs inode has EA attribute.
   * NI_RunlistDirty		runlist need to be updated.
   * NI_StreamUnlinked		Named stream is unlinked but still open.
+ * NI_ExtendSys			Inode is a system file in $Extend.
   */
  enum {
  	NI_Dirty,
@@ -204,6 +205,7 @@ enum {
  	NI_HasEA,
  	NI_RunlistDirty,
  	NI_StreamUnlinked,
+	NI_ExtendSys,
  };
 
  /*
@@ -265,6 +267,7 @@ NINO_FNS(BeingDeleted)
  NINO_FNS(HasEA)
  NINO_FNS(RunlistDirty)
  NINO_FNS(StreamUnlinked)
+NINO_FNS(ExtendSys)
 
  /*
   * The full structure containing a ntfs_inode and a vfs struct inode. Used for
Quoted 1 line

base-commit: f674e4b6c315ebe742d08af80659386992d58ce9

Looks good to me.

maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Namjae Jeon
Quoted 14 lines · Namjae Jeon

The $Extend directory contains system files such as $ObjId, $Quota, and $Reparse. show_sys_files only controls directory enumeration, so hidden system metadata remains reachable by explicit path. With the default mount options, forced immutability covers reserved regular files, allowing removal of $Extend and its entries. This can make later mounts fail when load_system_files() opens FILE_Extend.

Make FILE_Extend and its direct children immutable independently of sys_immutable. Classify the children during inode loading so protection covers directories and files with an unnamed $DATA attribute. This prevents userspace from changing directory entries or writing to the protected files. Return -EPERM for file flag updates before writing the $LINUXFLAGS EA or updating ctime.

Quoted 3 lines
Fixes af0db57d4293 ("ntfs: update inode operations")

Looks good to me.

Signature · 2 lines
Thanks,
Hyunchul