Skip to content

Commit b5198fc

Browse files
charsyamnamjaejeon
authored andcommitted
ntfs: fix NULL dereference in ntfs_index_walk_down()
ntfs_index_walk_down() allocates ictx->ib when descending from the root into an index allocation block. If that allocation fails, the old code still passes the NULL buffer to ntfs_ib_read(), which can write through it via ntfs_inode_attr_pread(). Allocate the index block into a temporary pointer and return -ENOMEM before changing the index context on allocation failure. Also propagate ERR_PTR() through ntfs_index_next() and ntfs_readdir() so walk-down allocation or index block read failures are not mistaken for normal index iteration inside the filesystem. ntfs_readdir() keeps the existing userspace-visible behavior of suppressing readdir errors after marking end_in_iterate; this change only prevents the walk-down failure path from dereferencing NULL internally. The failure was reproduced with failslab fail-nth injection on getdents64; the original module hits a NULL pointer dereference in memcpy_orig through ntfs_ib_read(), while the patched module reaches the same ntfs_index_walk_down() allocation failure without crashing. Fixes: 0a8ac0c ("ntfs: update directory operations") Signed-off-by: DaeMyung Kang <charsyam@gmail.com> Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
1 parent 897d540 commit b5198fc

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

fs/ntfs/dir.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,23 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
911911

912912
if (next->flags & INDEX_ENTRY_NODE) {
913913
next = ntfs_index_walk_down(next, ictx);
914-
if (!next) {
915-
err = -EIO;
914+
if (IS_ERR(next)) {
915+
err = PTR_ERR(next);
916916
goto out;
917917
}
918918
}
919919

920920
if (next && !(next->flags & INDEX_ENTRY_END))
921921
goto nextdir;
922922

923-
while ((next = ntfs_index_next(next, ictx)) != NULL) {
923+
while (1) {
924+
next = ntfs_index_next(next, ictx);
925+
if (IS_ERR(next)) {
926+
err = PTR_ERR(next);
927+
goto out;
928+
}
929+
if (!next)
930+
break;
924931
nextdir:
925932
/* Check the consistency of an index entry */
926933
if (ntfs_index_entry_inconsistent(ictx, vol, next, COLLATION_FILE_NAME,

fs/ntfs/index.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,15 +1969,19 @@ int ntfs_index_remove(struct ntfs_inode *dir_ni, const void *key, const u32 keyl
19691969
struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_index_context *ictx)
19701970
{
19711971
struct index_entry *entry;
1972+
struct index_block *ib;
19721973
s64 vcn;
19731974

19741975
entry = ie;
19751976
do {
19761977
vcn = ntfs_ie_get_vcn(entry);
19771978
if (ictx->is_in_root) {
1979+
ib = kvzalloc(ictx->block_size, GFP_NOFS);
1980+
if (!ib)
1981+
return ERR_PTR(-ENOMEM);
19781982
/* down from level zero */
19791983
ictx->ir = NULL;
1980-
ictx->ib = kvzalloc(ictx->block_size, GFP_NOFS);
1984+
ictx->ib = ib;
19811985
ictx->pindex = 1;
19821986
ictx->is_in_root = false;
19831987
} else {
@@ -1991,8 +1995,8 @@ struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_ind
19911995
ictx->entry = ntfs_ie_get_first(&ictx->ib->index);
19921996
entry = ictx->entry;
19931997
} else
1994-
entry = NULL;
1995-
} while (entry && (entry->flags & INDEX_ENTRY_NODE));
1998+
entry = ERR_PTR(-EIO);
1999+
} while (!IS_ERR(entry) && (entry->flags & INDEX_ENTRY_NODE));
19962000

19972001
return entry;
19982002
}
@@ -2097,10 +2101,15 @@ struct index_entry *ntfs_index_next(struct index_entry *ie, struct ntfs_index_co
20972101

20982102
/* walk down if it has a subnode */
20992103
if (flags & INDEX_ENTRY_NODE) {
2100-
if (!ictx->ia_ni)
2104+
if (!ictx->ia_ni) {
21012105
ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni);
2106+
if (!ictx->ia_ni)
2107+
return ERR_PTR(-EIO);
2108+
}
21022109

21032110
next = ntfs_index_walk_down(next, ictx);
2111+
if (IS_ERR(next))
2112+
return next;
21042113
} else {
21052114

21062115
/* walk up it has no subnode, nor data */

0 commit comments

Comments
 (0)