Skip to content

Commit b64f0ae

Browse files
charsyamnamjaejeon
authored andcommitted
ntfs: validate attribute name bounds before returning it
ntfs_attr_find() validates a named attribute before comparing it with the requested name, but that check is currently after the AT_UNUSED handling. When callers enumerate attributes with AT_UNUSED, ntfs_attr_find() can return a malformed named attribute before checking whether name_offset and name_length stay within the attribute record. Some enumeration callers use the returned attribute name pointer directly. For example, one path passes (attr + name_offset, name_length) to ntfs_attr_iget(), where the name can later be copied according to name_length. A malformed on-disk name_offset/name_length pair should not be exposed to those callers. Move the existing name bounds validation before returning attributes during AT_UNUSED enumeration, and write it as an offset/remaining-size check so the subtraction cannot underflow. Extract the converted values into local variables (name_offset, attr_len, name_size) to make the intent explicit and avoid repeating the endian conversions inside the bounds check. This keeps matching attributes on the same checked path while also covering attribute enumeration. A small userspace ASAN model with attr length=32, name_offset=124 and name_length=8 reproduces a heap-buffer-overflow read in the old enumeration path. With this change the same malformed attribute is rejected before the name pointer is returned to the caller. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: DaeMyung Kang <charsyam@gmail.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
1 parent 679ee5a commit b64f0ae

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

fs/ntfs/attrib.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ static int ntfs_attr_find(const __le32 type, const __le16 *name,
661661
__le16 *upcase = vol->upcase;
662662
u32 upcase_len = vol->upcase_len;
663663
unsigned int space;
664+
u16 name_offset;
665+
u32 attr_len;
666+
u32 name_size;
664667

665668
/*
666669
* Iterate over attributes in mft record starting at @ctx->attr, or the
@@ -688,6 +691,20 @@ static int ntfs_attr_find(const __le32 type, const __le16 *name,
688691
return -ENOENT;
689692
if (unlikely(!a->length))
690693
break;
694+
if (a->name_length) {
695+
name_offset = le16_to_cpu(a->name_offset);
696+
attr_len = le32_to_cpu(a->length);
697+
name_size = a->name_length * sizeof(__le16);
698+
699+
if (name_offset > attr_len ||
700+
attr_len - name_offset < name_size) {
701+
ntfs_error(vol->sb,
702+
"Corrupt attribute name in MFT record %llu\n",
703+
ctx->ntfs_ino->mft_no);
704+
break;
705+
}
706+
}
707+
691708
if (type == AT_UNUSED)
692709
return 0;
693710
if (a->type != type)
@@ -701,14 +718,6 @@ static int ntfs_attr_find(const __le32 type, const __le16 *name,
701718
if (a->name_length)
702719
return -ENOENT;
703720
} else {
704-
if (a->name_length && ((le16_to_cpu(a->name_offset) +
705-
a->name_length * sizeof(__le16)) >
706-
le32_to_cpu(a->length))) {
707-
ntfs_error(vol->sb, "Corrupt attribute name in MFT record %llu\n",
708-
ctx->ntfs_ino->mft_no);
709-
break;
710-
}
711-
712721
if (!ntfs_are_names_equal(name, name_len,
713722
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
714723
a->name_length, ic, upcase, upcase_len)) {

0 commit comments

Comments
 (0)