fs/ntfs3: Do copy_to_user out of run_lock
Subsystems (2)
Sasha Levin
maintainerPATCH 1/2 fs/ntfs3: Do copy_to_user out of run_lock
From: Konstantin Komarov <[email protected]>
[ Upstream commit d57431c6f511bf020e474026d9f3123d7bfbea8c ]
In order not to call copy_to_user (from fiemap_fill_next_extent) we allocate memory in the kernel, fill it and copy it to user memory after up_read(run_lock).
Reported-by [email protected]
Signed-off-by Konstantin Komarov <[email protected]>
Signed-off-by Sasha Levin <[email protected]>
--- fs/ntfs3/frecord.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 3 deletions(-)
fs/ntfs3/frecord.c +72 -3
@@ -1897,6 +1897,47 @@ enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,return REPARSE_LINK ; } +/* +* fiemap_fill_next_extent_k - a copy of fiemap_fill_next_extent +* but it accepts kernel address for fi_extents_start +*/ +static int fiemap_fill_next_extent_k (struct fiemap_extent_info *fieinfo, +u64 logical,u64 phys,u64 len,u32 flags) +{ +struct fiemap_extent extent; +struct fiemap_extent __user *dest = fieinfo->fi_extents_start; + +/* only count the extents */ +if (fieinfo->fi_extents_max ==0 ) { + fieinfo->fi_extents_mapped++; +return (flags &FIEMAP_EXTENT_LAST ) ?1 :0 ; + } + +if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) +return 1 ; + +if (flags &FIEMAP_EXTENT_DELALLOC ) + flags |=FIEMAP_EXTENT_UNKNOWN ; +if (flags &FIEMAP_EXTENT_DATA_ENCRYPTED ) + flags |=FIEMAP_EXTENT_ENCODED ; +if (flags & (FIEMAP_EXTENT_DATA_TAIL |FIEMAP_EXTENT_DATA_INLINE )) + flags |=FIEMAP_EXTENT_NOT_ALIGNED ; + +memset (&extent,0 ,sizeof (extent)); + extent.fe_logical = logical; + extent.fe_physical = phys; + extent.fe_length = len; + extent.fe_flags = flags; + + dest += fieinfo->fi_extents_mapped; +memcpy (dest, &extent,sizeof (extent)); + + fieinfo->fi_extents_mapped++; +if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) +return 1 ; +return (flags &FIEMAP_EXTENT_LAST ) ?1 :0 ; +} +/* * ni_fiemap - Helper for file_fiemap(). *
@@ -1907,6 +1948,8 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo, __u64 vbo, __u64 len) { int err = 0; +struct fiemap_extent __user *fe_u = fieinfo->fi_extents_start; +struct fiemap_extent *fe_k =NULL ;struct ntfs_sb_info *sbi = ni->mi.sbi;u8 cluster_bits = sbi->cluster_bits;struct runs_tree *run;
@@ -1954,6 +1997,18 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo, goto out; } +/* +* To avoid lock problems replace pointer to user memory by pointer to kernel memory. +*/ + fe_k =kmalloc_array (fieinfo->fi_extents_max, +sizeof (struct fiemap_extent ), +GFP_NOFS | __GFP_ZERO); +if (!fe_k) { + err = -ENOMEM ; + goto out; + } + fieinfo->fi_extents_start = fe_k; + end = vbo + len; alloc_size =le64_to_cpu (attr->nres.alloc_size);if (end > alloc_size)
@@ -2042,8 +2097,9 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,if (vbo + dlen >= end) flags |=FIEMAP_EXTENT_LAST ; - err =fiemap_fill_next_extent (fieinfo, vbo, lbo, dlen, - flags); + err =fiemap_fill_next_extent_k (fieinfo, vbo, lbo, dlen, + flags); +if (err <0 )break ;if (err ==1 ) {
@@ -2063,7 +2119,8 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,if (vbo + bytes >= end) flags |=FIEMAP_EXTENT_LAST ; - err =fiemap_fill_next_extent (fieinfo, vbo, lbo, bytes, flags); + err =fiemap_fill_next_extent_k (fieinfo, vbo, lbo, bytes, + flags);if (err <0 )break ;if (err ==1 ) {
@@ -2076,7 +2133,19 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,up_read (run_lock); +/* +* Copy to user memory out of lock +*/ +if (copy_to_user (fe_u, fe_k, + fieinfo->fi_extents_max * +sizeof (struct fiemap_extent ))) { + err = -EFAULT ; + } + out: +/* Restore original pointer. */ + fieinfo->fi_extents_start = fe_u; +kfree (fe_k);return err; }
Signature · 1 line
2.43.0
Sasha Levin
maintainerPATCH 2/2 fs/ntfs3: Check more cases when directory is corrupted
From: Konstantin Komarov <[email protected]>
[ Upstream commit 744375343662058cbfda96d871786e5a5cbe1947 ]
Mark ntfs dirty in this case.
Rename ntfs_filldir to ntfs_dir_emit.
Signed-off-by Konstantin Komarov <[email protected]>
Signed-off-by Sasha Levin <[email protected]>
--- fs/ntfs3/dir.c | 52 +++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-)
fs/ntfs3/dir.c +32 -20
@@ -272,9 +272,12 @@ struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,return err == -ENOENT ?NULL : err ?ERR_PTR (err) : inode; } -static inline int ntfs_filldir (struct ntfs_sb_info *sbi,struct ntfs_inode *ni, -const struct NTFS_DE *e,u8 *name, -struct dir_context *ctx) +/* +* returns false if 'ctx' if full +*/ +static inline bool ntfs_dir_emit (struct ntfs_sb_info *sbi, +struct ntfs_inode *ni,const struct NTFS_DE *e, +u8 *name,struct dir_context *ctx) {const struct ATTR_FILE_NAME *fname;unsigned long ino;
@@ -284,29 +287,29 @@ static inline int ntfs_filldir(struct ntfs_sb_info *sbi, struct ntfs_inode *ni, fname =Add2Ptr (e,sizeof (struct NTFS_DE ));if (fname->type ==FILE_NAME_DOS ) -return 0 ; +return true;if (!mi_is_ref (&ni->mi, &fname->home)) -return 0 ; +return true; ino =ino_get (&e->ref);if (ino ==MFT_REC_ROOT ) -return 0 ; +return true;/* Skip meta files. Unless option to show metafiles is set. */ if (!sbi->options->showmeta &&ntfs_is_meta_file (sbi, ino)) -return 0 ; +return true;if (sbi->options->nohidden && (fname->dup.fa &FILE_ATTRIBUTE_HIDDEN )) -return 0 ; +return true; name_len =ntfs_utf16_to_nls (sbi, fname->name, fname->name_len, name,PATH_MAX );if (name_len <=0 ) {ntfs_warn (sbi->sb,"failed to convert name for inode %lx." , ino); -return 0 ; +return true; }/*
@@ -335,17 +338,20 @@ static inline int ntfs_filldir(struct ntfs_sb_info *sbi, struct ntfs_inode *ni, } } -return !dir_emit (ctx, (s8 *)name, name_len, ino, dt_type); +return dir_emit (ctx, (s8 *)name, name_len, ino, dt_type); }/* * ntfs_read_hdr - Helper function for ntfs_readdir(). +* +* returns 0 if ok. +* returns -EINVAL if directory is corrupted. +* returns +1 if 'ctx' is full. */ static int ntfs_read_hdr (struct ntfs_sb_info *sbi,struct ntfs_inode *ni,const struct INDEX_HDR *hdr,u64 vbo,u64 pos,u8 *name,struct dir_context *ctx) { -int err;const struct NTFS_DE *e;u32 e_size;u32 end =le32_to_cpu (hdr->used);
@@ -353,12 +359,12 @@ static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,for (;; off += e_size) {if (off +sizeof (struct NTFS_DE ) > end) -return -1 ; +return -EINVAL ; e =Add2Ptr (hdr, off); e_size =le16_to_cpu (e->size);if (e_size <sizeof (struct NTFS_DE ) || off + e_size > end) -return -1 ; +return -EINVAL ;if (de_is_last (e))return 0 ;
@@ -368,14 +374,15 @@ static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,continue ;if (le16_to_cpu (e->key_size) <SIZEOF_ATTRIBUTE_FILENAME ) -return -1 ; +return -EINVAL ; ctx->pos = vbo + off;/* Submit the name to the filldir callback. */ - err =ntfs_filldir (sbi, ni, e, name, ctx); -if (err) -return err; +if (!ntfs_dir_emit (sbi, ni, e, name, ctx)) { +/* ctx is full. */ +return +1 ; + } } }
@@ -474,8 +481,6 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx) vbo = (u64 )bit << index_bits;if (vbo >= i_size) { -ntfs_inode_err (dir,"Looks like your dir is corrupt" ); - ctx->pos = eod; err = -EINVAL ; goto out; }
@@ -498,9 +503,16 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)__putname (name);put_indx_node (node); -if (err == -ENOENT ) { +if (err ==1 ) { +/* 'ctx' is full. */ + err =0 ; + }else if (err == -ENOENT ) { err =0 ; ctx->pos = pos; + }else if (err <0 ) { +if (err == -EINVAL ) +ntfs_inode_err (dir,"directory corrupted" ); + ctx->pos = eod; }return err;
Signature · 1 line
2.43.0