Skip to content

Commit

Permalink
fs/ntfs3: Check more cases when directory is corrupted
Browse files Browse the repository at this point in the history
Mark ntfs dirty in this case.
Rename ntfs_filldir to ntfs_dir_emit.

Signed-off-by: Konstantin Komarov <[email protected]>
  • Loading branch information
aalexandrovich committed Jul 11, 2024
1 parent d57431c commit 7443753
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions fs/ntfs3/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/*
Expand Down Expand Up @@ -336,30 +339,33 @@ 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);
u32 off = le32_to_cpu(hdr->de_off);

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;
Expand All @@ -369,14 +375,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;
}
}
}

Expand Down Expand Up @@ -475,8 +482,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;
}
Expand All @@ -499,9 +504,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;
Expand Down

0 comments on commit 7443753

Please sign in to comment.