ntfs: prevent modification of $Extend system metadata
Namjae Jeon
maintainerPATCH 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.
---
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
@@ -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
@@ -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
@@ -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
Quoted 109 lines · Namjae Jeon
The $Extend directory contains system files such as $ObjId, $Quota, and $Reparse.
show_sys_filesonly 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 whenload_system_files()opensFILE_Extend.Make
FILE_Extendand its direct children immutable independently ofsys_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-EPERMfor file flag updates before writing the $LINUXFLAGS EA or updating ctime.Fixes af0db57d4293 ("ntfs: update inode operations")Reported-by theBIT5 x3zk <[email protected]>Signed-off-by Namjae Jeon <[email protected]>---
Changes in v2
- Use Baolin's inode-loading change with the existing lookup helper.
- Block file flag writes with a direct check inntfs_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@@ -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@@ -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@@ -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.
Quoted 14 lines · Namjae Jeon
The $Extend directory contains system files such as $ObjId, $Quota, and $Reparse.
show_sys_filesonly 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 whenload_system_files()opensFILE_Extend.Make
FILE_Extendand its direct children immutable independently ofsys_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-EPERMfor file flag updates before writing the $LINUXFLAGS EA or updating ctime.
Quoted 3 lines
Fixes af0db57d4293 ("ntfs: update inode operations")Reported-by theBIT5 x3zk <[email protected]>Signed-off-by Namjae Jeon <[email protected]>
Looks good to me.
Signature · 2 lines
Thanks, Hyunchul