Skip to contentSkip to conversation

hfsplus: fix OOB of hfsplus_unistr in hfsplus_uni2asc()

PATCH In review Liu Shixin 2 participants 3 messages Nov 29, 2022 – Dec 1, 2022

Subsystems (2)
lore
Share & tools
lore

Liu Shixin

author
#0 lore raw

PATCH 1/1 hfsplus: fix OOB of hfsplus_unistr in hfsplus_uni2asc()

syzbot found a slab-out-of-bounds Read in hfsplus_uni2asc:

 BUG: KASAN: slab-out-of-bounds in hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
 Read of size 2 at addr ffff88801887a40c by task syz-executor412/3632

 CPU: 1 PID: 3632 Comm: syz-executor412 Not tainted 6.1.0-rc6-syzkaller-00315-gfaf68e3523c2 #0
 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022
 Call Trace:
  <TASK>
  __dump_stack lib/dump_stack.c:88 [inline]
  dump_stack_lvl+0x1b1/0x28e lib/dump_stack.c:106
  print_address_description+0x74/0x340 mm/kasan/report.c:284
  print_report+0x107/0x1f0 mm/kasan/report.c:395
  kasan_report+0xcd/0x100 mm/kasan/report.c:495
  hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
  hfsplus_readdir+0x8be/0x1230 fs/hfsplus/dir.c:207
  iterate_dir+0x257/0x5f0
  __do_sys_getdents64 fs/readdir.c:369 [inline]
  __se_sys_getdents64+0x1db/0x4c0 fs/readdir.c:354
  do_syscall_x64 arch/x86/entry/common.c:50 [inline]
  do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
  entry_SYSCALL_64_after_hwframe+0x63/0xcd

The length of arrags ustr->unicode is HFSPLUS_MAX_STRLEN. Limit the value of ustr->length to no more than HFSPLUS_MAX_STRLEN.

---
 fs/hfsplus/unicode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/hfsplus/unicode.c +2 -0
index 73342c925a4b..3df43a176acb 100644
@@ -133,6 +133,8 @@ int hfsplus_uni2asc(struct super_block *sb,
 	op = astr;
 	ip = ustr->unicode;
 	ustrlen = be16_to_cpu(ustr->length);
+	if (ustrlen > HFSPLUS_MAX_STRLEN)
+		ustrlen = HFSPLUS_MAX_STRLEN;
 	len = *len_p;
 	ce1 = NULL;
 	compose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 41 lines · Liu Shixin

syzbot found a slab-out-of-bounds Read in hfsplus_uni2asc:

BUG: KASAN: slab-out-of-bounds in hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
Read of size 2 at addr ffff88801887a40c by task syz-executor412/3632

CPU: 1 PID: 3632 Comm: syz-executor412 Not tainted 6.1.0-rc6-syzkaller-00315-gfaf68e3523c2 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0x1b1/0x28e lib/dump_stack.c:106
 print_address_description+0x74/0x340 mm/kasan/report.c:284
 print_report+0x107/0x1f0 mm/kasan/report.c:395
 kasan_report+0xcd/0x100 mm/kasan/report.c:495
 hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
 hfsplus_readdir+0x8be/0x1230 fs/hfsplus/dir.c:207
 iterate_dir+0x257/0x5f0
 __do_sys_getdents64 fs/readdir.c:369 [inline]
 __se_sys_getdents64+0x1db/0x4c0 fs/readdir.c:354
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

The length of arrags ustr->unicode is HFSPLUS_MAX_STRLEN. Limit the value of ustr->length to no more than HFSPLUS_MAX_STRLEN.

---
fs/hfsplus/unicode.c | 2 ++

1 file changed, 2 insertions(+)
fs/hfsplus/unicode.c +0 -0
index 73342c925a4b..3df43a176acb 100644

fs/hfsplus/unicode.c int hfsplus_uni2asc(struct super_block *sb, · line 137

Quoted 5 lines
fs/hfsplus/unicode.c +2 -0
@@ -133,6 +133,8 @@ int hfsplus_uni2asc(struct super_block *sb,
 	op = astr;
 	ip = ustr->unicode;
 	ustrlen = be16_to_cpu(ustr->length);
+	if (ustrlen > HFSPLUS_MAX_STRLEN)
+		ustrlen = HFSPLUS_MAX_STRLEN;

Hmmm.. It’s strange. As far as I can see, we read ustr from the volume because be16_to_cpu() is used. But how ustrlen can be bigger than HFSPLUS_MAX_STRLEN if we read it from volume? Do we have corrupted volume? What the environment of the issue?

Thanks,
Slava.

Quoted 6 lines
fs/hfsplus/unicode.c +0 -0
 	len = *len_p;
 	ce1 = NULL;
 	compose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
Signature · 1 line
2.25.1
author
#2 lore raw
Quoted 50 lines · Viacheslav Dubeyko
Quoted 40 lines · Liu Shixin

syzbot found a slab-out-of-bounds Read in hfsplus_uni2asc:

BUG: KASAN: slab-out-of-bounds in hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
Read of size 2 at addr ffff88801887a40c by task syz-executor412/3632

CPU: 1 PID: 3632 Comm: syz-executor412 Not tainted 6.1.0-rc6-syzkaller-00315-gfaf68e3523c2 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0x1b1/0x28e lib/dump_stack.c:106
 print_address_description+0x74/0x340 mm/kasan/report.c:284
 print_report+0x107/0x1f0 mm/kasan/report.c:395
 kasan_report+0xcd/0x100 mm/kasan/report.c:495
 hfsplus_uni2asc+0x683/0x1290 fs/hfsplus/unicode.c:179
 hfsplus_readdir+0x8be/0x1230 fs/hfsplus/dir.c:207
 iterate_dir+0x257/0x5f0
 __do_sys_getdents64 fs/readdir.c:369 [inline]
 __se_sys_getdents64+0x1db/0x4c0 fs/readdir.c:354
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

The length of arrags ustr->unicode is HFSPLUS_MAX_STRLEN. Limit the value of ustr->length to no more than HFSPLUS_MAX_STRLEN.

---
fs/hfsplus/unicode.c | 2 ++

1 file changed, 2 insertions(+)
fs/hfsplus/unicode.c +0 -0
index 73342c925a4b..3df43a176acb 100644

fs/hfsplus/unicode.c int hfsplus_uni2asc(struct super_block *sb, · line 137

Quoted 5 lines
fs/hfsplus/unicode.c +2 -0
@@ -133,6 +133,8 @@ int hfsplus_uni2asc(struct super_block *sb,
 	op = astr;
 	ip = ustr->unicode;
 	ustrlen = be16_to_cpu(ustr->length);
+	if (ustrlen > HFSPLUS_MAX_STRLEN)
+		ustrlen = HFSPLUS_MAX_STRLEN;

Hmmm.. It’s strange. As far as I can see, we read ustr from the volume because be16_to_cpu() is used. But how ustrlen can be bigger than HFSPLUS_MAX_STRLEN if we read it from volume? Do we have corrupted volume? What the environment of the issue?

Quoted 2 lines

Thanks,
Slava.

The bug is reported by syzbot. You can find the reprodution program there.

It seems that there is a corrupted volume. But I don't know much about this so I'm not sure. Is there any useful information here?

I noticed that syzbot found another bug of hfsplus recently, which seem to related to corrupted volume too.
https://syzkaller.appspot.com/bug?id=9fa98e04385363b08013093b659020d8dedae2ec

Thanks,
.

Quoted 9 lines
Quoted 7 lines
fs/hfsplus/unicode.c +0 -0
 	len = *len_p;
 	ce1 = NULL;
 	compose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
Signature · 1 line
2.25.1

.