Skip to content

Commit 2dc6b0d

Browse files
duanegtytso
authored andcommitted
ext4: tighten restrictions on inode flags
At the moment there are few restrictions on which flags may be set on which inodes. Specifically DIRSYNC may only be set on directories and IMMUTABLE and APPEND may not be set on links. Tighten that to disallow TOPDIR being set on non-directories and only NODUMP and NOATIME to be set on non-regular file, non-directories. Introduces a flags masking function which masks flags based on mode and use it during inode creation and when flags are set via the ioctl to facilitate future consistency. Signed-off-by: Duane Griffin <duaneg@dghda.com> Acked-by: Andreas Dilger <adilger@sun.com> Cc: <linux-ext4@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 8fa43a8 commit 2dc6b0d

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ struct flex_groups {
246246
EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL |\
247247
EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL)
248248

249+
/* Flags that are appropriate for regular files (all but dir-specific ones). */
250+
#define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL))
251+
252+
/* Flags that are appropriate for non-directories/regular files. */
253+
#define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
254+
255+
/* Mask out flags that are inappropriate for the given type of inode. */
256+
static inline __u32 ext4_mask_flags(umode_t mode, __u32 flags)
257+
{
258+
if (S_ISDIR(mode))
259+
return flags;
260+
else if (S_ISREG(mode))
261+
return flags & EXT4_REG_FLMASK;
262+
else
263+
return flags & EXT4_OTHER_FLMASK;
264+
}
265+
249266
/*
250267
* Inode dynamic state flags
251268
*/

‎fs/ext4/ialloc.c‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -885,16 +885,12 @@ struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
885885
ei->i_disksize = 0;
886886

887887
/*
888-
* Don't inherit extent flag from directory. We set extent flag on
889-
* newly created directory and file only if -o extent mount option is
890-
* specified
888+
* Don't inherit extent flag from directory, amongst others. We set
889+
* extent flag on newly created directory and file only if -o extent
890+
* mount option is specified
891891
*/
892-
ei->i_flags = EXT4_I(dir)->i_flags & EXT4_FL_INHERITED;
893-
if (S_ISLNK(mode))
894-
ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
895-
/* dirsync only applies to directories */
896-
if (!S_ISDIR(mode))
897-
ei->i_flags &= ~EXT4_DIRSYNC_FL;
892+
ei->i_flags =
893+
ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
898894
ei->i_file_acl = 0;
899895
ei->i_dtime = 0;
900896
ei->i_block_group = group;

‎fs/ext4/ioctl.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4848
if (err)
4949
return err;
5050

51-
if (!S_ISDIR(inode->i_mode))
52-
flags &= ~EXT4_DIRSYNC_FL;
51+
flags = ext4_mask_flags(inode->i_mode, flags);
5352

5453
err = -EPERM;
5554
mutex_lock(&inode->i_mutex);

0 commit comments

Comments
 (0)