Skip to contentSkip to conversation

hfsplus: terminate xattr names before listing them

PATCH In review Kyle Zeng 2 participants 5 messages Jun 11 – Jul 1

Subsystems (3)
lore
Share & tools
lore

Kyle Zeng

author
#0 lore raw

PATCH 1/1 hfsplus: terminate xattr names before listing them

hfsplus_uni2asc_xattr_str() returns the converted byte count but does not append a trailing NUL. hfsplus_listxattr() then passes the reusable conversion buffer to string helpers such as can_list(), name_len(), and copy_name().

If a shorter converted xattr name follows a longer one, stale bytes after the new byte count can make strscpy() fail with -E2BIG. The caller adds copy_name()'s return value to the running output offset, so a negative return can move the next write before the listxattr buffer.

Explicitly terminate the converted name at the returned byte count before treating it as a C string.

Fixes 127e5f5ae51ef ("hfsplus: rework functionality of getting, setting and deleting of extended attributes")
Assisted-by Codex:gpt-5.5
---
 fs/hfsplus/xattr.c | 1 +
 1 file changed, 1 insertion(+)
fs/hfsplus/xattr.c +1 -0
index 452a1f9becb2..35fcbc397b62 100644
@@ -870,6 +870,7 @@ ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
 			res = -EIO;
 			goto end_listxattr;
 		}
+		strbuf[xattr_name_len] = '\0';
 
 		if (!buffer || !size) {
 			if (can_list(strbuf))
Signature · 1 line
2.54.0
#1 lore raw
Quoted 38 lines · Kyle Zeng

hfsplus_uni2asc_xattr_str() returns the converted byte count but does not
append a trailing NUL. hfsplus_listxattr() then passes the reusable conversion buffer to string helpers such as can_list(), name_len(), and
copy_name().

If a shorter converted xattr name follows a longer one, stale bytes after
the new byte count can make strscpy() fail with -E2BIG. The caller adds
copy_name()'s return value to the running output offset, so a negative
return can move the next write before the listxattr buffer.

Explicitly terminate the converted name at the returned byte count before
treating it as a C string.

Fixes 127e5f5ae51ef ("hfsplus: rework functionality of getting,

setting and deleting of extended attributes")

Assisted-by Codex:gpt-5.5

---
 fs/hfsplus/xattr.c | 1 +

 1 file changed, 1 insertion(+)
fs/hfsplus/xattr.c +1 -0
index 452a1f9becb2..35fcbc397b62 100644
@@ -870,6 +870,7 @@ ssize_t hfsplus_listxattr(struct dentry *dentry,
 char *buffer, size_t size)
  			res = -EIO;
  			goto end_listxattr;
  		}
+		strbuf[xattr_name_len] = '\0';
Quoted 3 lines


  if (!buffer || !size) {
  if (can_list(strbuf))

Have you run xfstsests for the patch?

Thanks,
Slava.

#2 lore raw
In reply to Kyle Zeng
Quoted 33 lines · Kyle Zeng

hfsplus_uni2asc_xattr_str() returns the converted byte count but does not
append a trailing NUL. hfsplus_listxattr() then passes the reusable conversion buffer to string helpers such as can_list(), name_len(), and
copy_name().

If a shorter converted xattr name follows a longer one, stale bytes after
the new byte count can make strscpy() fail with -E2BIG. The caller adds
copy_name()'s return value to the running output offset, so a negative
return can move the next write before the listxattr buffer.

Explicitly terminate the converted name at the returned byte count before
treating it as a C string.

Fixes 127e5f5ae51ef ("hfsplus: rework functionality of getting,

setting and deleting of extended attributes")

Assisted-by Codex:gpt-5.5

---
 fs/hfsplus/xattr.c | 1 +

 1 file changed, 1 insertion(+)
fs/hfsplus/xattr.c +0 -0
index 452a1f9becb2..35fcbc397b62 100644

fs/hfsplus/xattr.c ssize_t hfsplus_listxattr(struct dentry *dentry, · line 874

Quoted 5 lines
fs/hfsplus/xattr.c +1 -0
@@ -870,6 +870,7 @@ ssize_t hfsplus_listxattr(struct dentry *dentry,
 char *buffer, size_t size)
  			res = -EIO;
  			goto end_listxattr;
  		}
+		strbuf[xattr_name_len] = '\0';

The strbuf is allocated by kzalloc() [1] and it is zeroed on every iteration [2]. Are you really sure that this code is necessary? Can you reproduce any issue without your patch?

Thanks,
Slava.

Quoted 3 lines


  if (!buffer || !size) {
  if (can_list(strbuf))

[1]
https://elixir.bootlin.com/linux/v7.1/source/fs/hfsplus/xattr.c#L833
[2]
https://elixir.bootlin.com/linux/v7.1/source/fs/hfsplus/xattr.c#L891

author
#3 lore raw

Hi Viacheslav,

I just checked the report. It seems the issue has been patched on
master (https://github.com/torvalds/linux/commit/413466f3f0f84e7356da16c611afd69d2a0872e4),
but the corresponding patch is not backported to stable branches yet.
And you are right, zeroing strbuf on every iteration can fix the issue.

I just confirmed the issue on 6.12.94 and the same vulnerable pattern seems to exist in 6.18.y as well.
The corresponding crash splash is attached.

I suggest to backport the patch to stable branches because it has security implications.

Best,
Kyle

Quoted 49 lines · Viacheslav Dubeyko
Quoted 33 lines · Kyle Zeng

hfsplus_uni2asc_xattr_str() returns the converted byte count but does not
append a trailing NUL. hfsplus_listxattr() then passes the reusable conversion buffer to string helpers such as can_list(), name_len(), and
copy_name().

If a shorter converted xattr name follows a longer one, stale bytes after
the new byte count can make strscpy() fail with -E2BIG. The caller adds
copy_name()'s return value to the running output offset, so a negative
return can move the next write before the listxattr buffer.

Explicitly terminate the converted name at the returned byte count before
treating it as a C string.

Fixes 127e5f5ae51ef ("hfsplus: rework functionality of getting,

setting and deleting of extended attributes")

Assisted-by Codex:gpt-5.5
---
 fs/hfsplus/xattr.c | 1 +
 1 file changed, 1 insertion(+)
fs/hfsplus/xattr.c +0 -0
index 452a1f9becb2..35fcbc397b62 100644

fs/hfsplus/xattr.c ssize_t hfsplus_listxattr(struct dentry *dentry, · line 874

Quoted 5 lines
fs/hfsplus/xattr.c +1 -0
@@ -870,6 +870,7 @@ ssize_t hfsplus_listxattr(struct dentry *dentry,
 char *buffer, size_t size)
                      res = -EIO;
                      goto end_listxattr;
              }
+             strbuf[xattr_name_len] = '\0';

The strbuf is allocated by kzalloc() [1] and it is zeroed on every iteration [2]. Are you really sure that this code is necessary? Can you reproduce any issue without your patch?

Thanks,
Slava.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines
              if (!buffer || !size) {
                      if (can_list(strbuf))

[1]
https://elixir.bootlin.com/linux/v7.1/source/fs/hfsplus/xattr.c#L833
[2]
https://elixir.bootlin.com/linux/v7.1/source/fs/hfsplus/xattr.c#L891

[  114.828666] ==================================================================
[  114.829588] BUG: KASAN: slab-out-of-bounds in string+0x2f7/0x330
[  114.829588] Write of size 1 at addr ffff888101273ca8 by task exp/541
[  114.829588] 
[  114.829588] CPU: 0 UID: 0 PID: 541 Comm: exp Not tainted 6.12.94 #1
[  114.829588] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[  114.829588] Call Trace:
[  114.829588]  <TASK>
[  114.829588]  dump_stack_lvl+0x64/0x80
[  114.829588]  print_report+0xce/0x660
[  114.829588]  ? __pfx__raw_spin_lock_irqsave+0x10/0x10
[  114.829588]  ? __pfx_uni2char+0x10/0x10
[  114.829588]  ? string+0x2f7/0x330
[  114.829588]  kasan_report+0xc6/0x100
[  114.829588]  ? string+0x2f7/0x330
[  114.829588]  string+0x2f7/0x330
[  114.829588]  ? __pfx_string+0x10/0x10
[  114.829588]  ? __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  vsnprintf+0x5ec/0x1680
[  114.829588]  ? __pfx_vsnprintf+0x10/0x10
[  114.829588]  ? hfsplus_bnode_read_u16+0x71/0xa0
[  114.829588]  ? __asan_memcpy+0x3c/0x60
[  114.829588]  scnprintf+0xb5/0x110
[  114.829588]  ? __pfx_scnprintf+0x10/0x10
[  114.829588]  ? uni2char+0x19/0x70
[  114.829588]  ? hfsplus_uni2asc+0x29f/0x9e0
[  114.829588]  ? __pfx_uni2char+0x10/0x10
[  114.829588]  copy_name+0x62/0x70
[  114.829588]  hfsplus_listxattr+0x4a8/0xbb0
[  114.829588]  ? __pfx_hfsplus_listxattr+0x10/0x10
[  114.829588]  ? __kasan_kmalloc+0x8f/0xa0
[  114.829588]  ? __kmalloc_node_noprof+0x19a/0x460
[  114.829588]  ? listxattr+0x42/0xf0
[  114.829588]  ? __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  ? do_syscall_64+0x58/0x120
[  114.829588]  ? kasan_save_track+0x14/0x30
[  114.829588]  ? __kmalloc_node_noprof+0x19a/0x460
[  114.829588]  ? listxattr+0x42/0xf0
[  114.829588]  ? security_inode_listxattr+0xd0/0x140
[  114.829588]  listxattr+0x5c/0xf0
[  114.829588]  __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  ? arch_exit_to_user_mode_prepare.isra.0+0xbd/0xe0
[  114.829588]  do_syscall_64+0x58/0x120
[  114.829588]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  114.829588] RIP: 0033:0x42dd4b
[  114.829588] Code: c7 c0 b8 ff ff ff 64 c7 00 0c 00 00 00 b8 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 f3 0f 1e fa b8 c4 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
[  114.829588] RSP: 002b:00007ffe600c9fa8 EFLAGS: 00000207 ORIG_RAX: 00000000000000c4
[  114.829588] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000042dd4b
[  114.829588] RDX: 0000000000000020 RSI: 00007ffe600c9fc0 RDI: 0000000000000005
[  114.829588] RBP: 00007ffe600c9ff0 R08: 00007ffe600ca708 R09: 0000000000000010
[  114.829588] R10: 0000000000000000 R11: 0000000000000207 R12: 00007ffe600d3058
[  114.829588] R13: 00007ffe600d3068 R14: 00000000004d4808 R15: 0000000000000001
[  114.829588]  </TASK>
[  114.829588] 
[  114.829588] Allocated by task 1:
[  114.829588]  kasan_save_stack+0x33/0x60
[  114.829588]  kasan_save_track+0x14/0x30
[  114.829588]  __kasan_kmalloc+0x8f/0xa0
[  114.829588]  acpi_bind_one+0x154/0x6f0
[  114.829588]  acpi_device_notify+0x17/0x260
[  114.829588]  device_add+0x22a/0x1490
[  114.829588]  container_device_attach+0x170/0x250
[  114.829588]  acpi_bus_attach+0x4f9/0xa50
[  114.829588]  device_for_each_child+0xf9/0x170
[  114.829588]  acpi_dev_for_each_child+0x7b/0xb0
[  114.829588]  acpi_bus_attach+0x733/0xa50
[  114.829588]  device_for_each_child+0xf9/0x170
[  114.829588]  acpi_dev_for_each_child+0x7b/0xb0
[  114.829588]  acpi_bus_attach+0x733/0xa50
[  114.829588]  device_for_each_child+0xf9/0x170
[  114.829588]  acpi_dev_for_each_child+0x7b/0xb0
[  114.829588]  acpi_bus_attach+0x733/0xa50
[  114.829588]  acpi_bus_scan+0xc1/0x440
[  114.829588]  acpi_scan_init+0x1e6/0x640
[  114.829588]  acpi_init+0x385/0x8f0
[  114.829588]  do_one_initcall+0xb1/0x370
[  114.829588]  kernel_init_freeable+0x494/0x7a0
[  114.829588]  kernel_init+0x1f/0x1e0
[  114.829588]  ret_from_fork+0x34/0x70
[  114.829588]  ret_from_fork_asm+0x1a/0x30
[  114.829588] 
[  114.829588] Second to last potentially related work creation:
[  114.829588] ------------[ cut here ]------------
[  114.829588] pool index -1 out of bounds (237) for stack id 75000000
[  114.829588] WARNING: CPU: 0 PID: 541 at lib/stackdepot.c:451 depot_fetch_stack+0x5a/0x80
[  114.829588] Modules linked in:
[  114.829588] CPU: 0 UID: 0 PID: 541 Comm: exp Not tainted 6.12.94 #1
[  114.829588] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[  114.829588] RIP: 0010:depot_fetch_stack+0x5a/0x80
[  114.829588] Code: 04 ed 80 49 12 8a 48 85 c0 74 25 48 01 d8 8b 50 1c 85 d2 74 21 5b 5d c3 cc cc cc cc 89 f9 48 c7 c7 50 50 57 87 e8 c6 65 e2 fd <0f> 0b 31 c0 eb e5 0f 0b 31 c0 eb df 0f 0b 31 c0 eb d9 48 89 ee 48
[  114.829588] RSP: 0018:ffffc9000143f7b0 EFLAGS: 00000082
[  114.829588] RAX: 0000000000000000 RBX: 0000000000002800 RCX: 0000000000000027
[  114.829588] RDX: 0000000000000027 RSI: 0000000000000004 RDI: ffff88811aa31a08
[  114.829588] RBP: ffff888101273ca8 R08: 0000000000000001 R09: ffffed1023546341
[  114.829588] R10: ffff88811aa31a0b R11: 0000000000000001 R12: ffffea0004049cc0
[  114.829588] R13: ffff888104e7a000 R14: 0000000000000001 R15: 000000000000006f
[  114.829588] FS:  000000003121f3c0(0000) GS:ffff88811aa00000(0000) knlGS:0000000000000000
[  114.829588] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  114.829588] CR2: 00000000004dc0d0 CR3: 00000001111ca000 CR4: 00000000000006f0
[  114.829588] Call Trace:
[  114.829588]  <TASK>
[  114.829588]  stack_depot_print+0x1b/0x50
[  114.829588]  kasan_print_aux_stacks+0x3a/0x70
[  114.829588]  print_report+0x117/0x660
[  114.829588]  ? __pfx__raw_spin_lock_irqsave+0x10/0x10
[  114.829588]  ? __pfx_uni2char+0x10/0x10
[  114.829588]  ? string+0x2f7/0x330
[  114.829588]  kasan_report+0xc6/0x100
[  114.829588]  ? string+0x2f7/0x330
[  114.829588]  string+0x2f7/0x330
[  114.829588]  ? __pfx_string+0x10/0x10
[  114.829588]  ? __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  vsnprintf+0x5ec/0x1680
[  114.829588]  ? __pfx_vsnprintf+0x10/0x10
[  114.829588]  ? hfsplus_bnode_read_u16+0x71/0xa0
[  114.829588]  ? __asan_memcpy+0x3c/0x60
[  114.829588]  scnprintf+0xb5/0x110
[  114.829588]  ? __pfx_scnprintf+0x10/0x10
[  114.829588]  ? uni2char+0x19/0x70
[  114.829588]  ? hfsplus_uni2asc+0x29f/0x9e0
[  114.829588]  ? __pfx_uni2char+0x10/0x10
[  114.829588]  copy_name+0x62/0x70
[  114.829588]  hfsplus_listxattr+0x4a8/0xbb0
[  114.829588]  ? __pfx_hfsplus_listxattr+0x10/0x10
[  114.829588]  ? __kasan_kmalloc+0x8f/0xa0
[  114.829588]  ? __kmalloc_node_noprof+0x19a/0x460
[  114.829588]  ? listxattr+0x42/0xf0
[  114.829588]  ? __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  ? do_syscall_64+0x58/0x120
[  114.829588]  ? kasan_save_track+0x14/0x30
[  114.829588]  ? __kmalloc_node_noprof+0x19a/0x460
[  114.829588]  ? listxattr+0x42/0xf0
[  114.829588]  ? security_inode_listxattr+0xd0/0x140
[  114.829588]  listxattr+0x5c/0xf0
[  114.829588]  __x64_sys_flistxattr+0x12b/0x1c0
[  114.829588]  ? arch_exit_to_user_mode_prepare.isra.0+0xbd/0xe0
[  114.829588]  do_syscall_64+0x58/0x120
[  114.829588]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  114.829588] RIP: 0033:0x42dd4b
[  114.829588] Code: c7 c0 b8 ff ff ff 64 c7 00 0c 00 00 00 b8 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 f3 0f 1e fa b8 c4 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
[  114.829588] RSP: 002b:00007ffe600c9fa8 EFLAGS: 00000207 ORIG_RAX: 00000000000000c4
[  114.829588] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000042dd4b
[  114.829588] RDX: 0000000000000020 RSI: 00007ffe600c9fc0 RDI: 0000000000000005
[  114.829588] RBP: 00007ffe600c9ff0 R08: 00007ffe600ca708 R09: 0000000000000010
[  114.829588] R10: 0000000000000000 R11: 0000000000000207 R12: 00007ffe600d3058
[  114.829588] R13: 00007ffe600d3068 R14: 00000000004d4808 R15: 0000000000000001
[  114.829588]  </TASK>
[  114.829588] ---[ end trace 0000000000000000 ]---
[  114.829588] ------------[ cut here ]------------
[  114.829588] corrupt handle or use after stack_depot_put()
[  114.829588] WARNING: CPU: 0 PID: 541 at lib/stackdepot.c:719 stack_depot_print+0x3e/0x50
[  114.829588] Modules linked in:
[  114.829588] CPU: 0 UID: 0 PID: 541 Comm: exp Tainted: G        W          6.12.94 #1
[  114.829588] Tainted: [W]=WARN
[  114.829588] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[  114.829588] RIP: 0010:stack_depot_print+0x3e/0x50
[  114.829588] Code: cc cc e8 75 fc ff ff 48 85 c0 74 12 8b 70 14 48 8d 78 20 85 f6 74 1a 31 d2 e9 5e 26 06 fe 48 c7 c7 88 50 57 87 e8 72 62 e2 fd <0f> 0b c3 cc cc cc cc c3 cc cc cc cc 66 0f 1f 44 00 00 90 90 90 90
[  114.829588] RSP: 0018:ffffc9000143f7c8 EFLAGS: 00000082
[  114.829588] RAX: 0000000000000000 RBX: ffff888101273ca0 RCX: 0000000000000027
[  114.829588] RDX: 0000000000000027 RSI: 0000000000000004 RDI: ffff88811aa31a08
[  114.829588] RBP: ffff888101273ca8 R08: 0000000000000001 R09: ffffed1023546341
[  114.829588] R10: ffff88811aa31a0b R11: 0000000000000001 R12: ffffea0004049cc0
[  114.829588] R13: ffff888104e7a000 R14: 0000000000000001 R15: 000000000000006f
[  114.829588] FS:  000000003121f3c0(0000) GS:ffff88811aa00000(0000) knlGS:0000000000000000
[  114.829588] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  114.829588] CR2: 00000000004dc0d0 CR3: 00000001111ca000 CR4: 00000000000006f0
[  114.829588] Call Trace:
[  114.829588]  <TASK>
[  114.829588]  kasan_print_aux_stacks+0x3a/0x70
[  114.829588]  print_report+0x117/0x660
[  114.829588]  ? __pfx__raw_spin_lock_irqsave+0x10/0x10
[  114.829588]  ? __pfx_uni2char+0x10/0x10
[  114.829588]  ? string+0x2f7/0x330
#4 lore raw
Quoted 1 line · Kyle Zeng

<skipped>

Quoted 4 lines

I suggest to backport the patch to stable branches because it has security implications.

You are welcomed to do it. ;)

Thanks,
Slava.