Skip to contentSkip to conversation

fs/ntfs3: fix slab-out-of-bounds write in index buffer replay

PATCH New Xue Boyang 1 participant 1 message Sep 29 NTFS3 filesystem

lore
Share & tools
lore

Xue Boyang

author
#0 lore raw

PATCH 1/1 fs/ntfs3: fix slab-out-of-bounds write in index buffer replay

During $LogFile replay, the allocation-op preamble in do_action() computes the buffer size as:

	bytes = max(oe->bytes_per_index, lcns_follow << cluster_bits);
	bytes += roff;
	bytes = ALIGN(bytes, 512);
	buffer_le = kmalloc(bytes, GFP_NOFS);

and then sets ib = buffer_le + roff, so the index buffer starts roff bytes into the allocation. However, all six allocation cases pass the FULL allocation size to check_index_buffer() and ntfs_fix_pre_write() instead of the space actually available behind ib (bytes - roff):

	check_index_buffer(ib, bytes)       // validates entries up to ib+bytes
	ntfs_fix_pre_write(&ib->rhdr, bytes) // writes fixups up to ib+bytes-2

This lets check_index_buffer() approve entries that extend roff bytes past the end of the kmalloc() allocation, and makes ntfs_fix_pre_write() write the last USA fixup at ib + bytes - 2, which is roff - 2 bytes past the allocation.

Reproduced deterministically on rw mount of a crafted $LogFile with oe->bytes_per_index = 2560 and record_off = 1536 (bytes = 4096, ib = buffer_le + 1536, available = 2560). The USA fixup write at ib + 4094 = buffer_le + 5630 runs 1534 bytes past the 4096-byte allocation:

  BUG: KASAN: slab-out-of-bounds in ntfs_fix_pre_write+0xb4/0x100
  Read of size 2 at addr ffff8880044891fe by task mount/70
   ...
   ntfs_fix_pre_write+0xb4/0x100
   do_action+0x1c67/0x1e09
   log_replay (fs/ntfs3/fslog.c)
   ntfs_loadlog_and_replay+0x2cb/0x300
   ntfs_fill_super+0x1375/0x22d0
   ...

Pass bytes - roff (the space behind ib) to both functions, matching the intended semantics when roff is nonzero. With roff = 0 the argument is unchanged.

Fixes b46acd6a6a62 ("fs/ntfs3: Add NTFS journal")
Assisted-by GLM:zhipu-coding-plan/glm-5.3
---
 fs/ntfs3/fslog.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
fs/ntfs3/fslog.c +12 -12
index ba61767cab3f..4b83a21532a3 100644
@@ -3624,7 +3624,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		used = le32_to_cpu(hdr->used);
 
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff) ||
 		    Add2Ptr(e, esize) > Add2Ptr(lrh, rec_len) ||
 		    used + esize > le32_to_cpu(hdr->total)) {
@@ -3639,7 +3639,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		a_dirty = true;
 
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	case DeleteIndexEntryAllocation:
@@ -3653,7 +3653,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 		if (!check_lsn(&ib->rhdr, rlsn))
 			goto out;
 
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff)) {
 			goto dirty_vol;
 		}
@@ -3682,7 +3682,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		a_dirty = true;
 
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	case WriteEndOfIndexBuffer:
@@ -3694,7 +3694,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 			goto dirty_vol;
 		if (!check_lsn(&ib->rhdr, rlsn))
 			goto out;
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff) ||
 		    aoff + dlen > offsetof(struct INDEX_BUFFER, ihdr) +
 					  le32_to_cpu(hdr->total)) {
@@ -3705,7 +3705,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 		memmove(e, data, dlen);
 
 		a_dirty = true;
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	case SetIndexEntryVcnAllocation:
@@ -3718,7 +3718,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		if (!check_lsn(&ib->rhdr, rlsn))
 			goto out;
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff)) {
 			goto dirty_vol;
 		}
@@ -3726,7 +3726,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 		de_set_vbn_le(e, *(__le64 *)data);
 
 		a_dirty = true;
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	case UpdateFileNameAllocation:
@@ -3739,7 +3739,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		if (!check_lsn(&ib->rhdr, rlsn))
 			goto out;
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff)) {
 			goto dirty_vol;
 		}
@@ -3750,7 +3750,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 		memmove(&fname->dup, data, sizeof(fname->dup));
 
 		a_dirty = true;
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	case SetBitsInNonresidentBitMap:
@@ -3789,7 +3789,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 
 		if (!check_lsn(&ib->rhdr, rlsn))
 			goto out;
-		if (!check_index_buffer(ib, bytes) ||
+		if (!check_index_buffer(ib, bytes - roff) ||
 		    !check_if_alloc_index(hdr, aoff)) {
 			goto dirty_vol;
 		}
@@ -3802,7 +3802,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
 		memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
 
 		a_dirty = true;
-		ntfs_fix_pre_write(&ib->rhdr, bytes);
+		ntfs_fix_pre_write(&ib->rhdr, bytes - roff);
 		break;
 
 	default:
Signature · 1 line
2.53.0