Skip to content

Commit cbd399f

Browse files
Li Huafeigregkh
authored andcommitted
topology: Keep the cpumask unchanged when printing cpumap
During fuzz testing, the following warning was discovered: different return values (15 and 11) from vsnprintf("%*pbl ", ...) test:keyward is WARNING in kvasprintf WARNING: CPU: 55 PID: 1168477 at lib/kasprintf.c:30 kvasprintf+0x121/0x130 Call Trace: kvasprintf+0x121/0x130 kasprintf+0xa6/0xe0 bitmap_print_to_buf+0x89/0x100 core_siblings_list_read+0x7e/0xb0 kernfs_file_read_iter+0x15b/0x270 new_sync_read+0x153/0x260 vfs_read+0x215/0x290 ksys_read+0xb9/0x160 do_syscall_64+0x56/0x100 entry_SYSCALL_64_after_hwframe+0x78/0xe2 The call trace shows that kvasprintf() reported this warning during the printing of core_siblings_list. kvasprintf() has several steps: (1) First, calculate the length of the resulting formatted string. (2) Allocate a buffer based on the returned length. (3) Then, perform the actual string formatting. (4) Check whether the lengths of the formatted strings returned in steps (1) and (2) are consistent. If the core_cpumask is modified between steps (1) and (3), the lengths obtained in these two steps may not match. Indeed our test includes cpu hotplugging, which should modify core_cpumask while printing. To fix this issue, cache the cpumask into a temporary variable before calling cpumap_print_{list, cpumask}_to_buf(), to keep it unchanged during the printing process. Fixes: bb9ec13 ("topology: use bin_attribute to break the size limitation of cpumap ABI") Cc: stable <stable@kernel.org> Signed-off-by: Li Huafei <lihuafei1@huawei.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Link: https://lore.kernel.org/r/20241114110141.94725-1-lihuafei1@huawei.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 24edfbd commit cbd399f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

drivers/base/topology.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,35 @@ static ssize_t name##_read(struct file *file, struct kobject *kobj, \
2727
loff_t off, size_t count) \
2828
{ \
2929
struct device *dev = kobj_to_dev(kobj); \
30+
cpumask_var_t mask; \
31+
ssize_t n; \
3032
\
31-
return cpumap_print_bitmask_to_buf(buf, topology_##mask(dev->id), \
32-
off, count); \
33+
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) \
34+
return -ENOMEM; \
35+
\
36+
cpumask_copy(mask, topology_##mask(dev->id)); \
37+
n = cpumap_print_bitmask_to_buf(buf, mask, off, count); \
38+
free_cpumask_var(mask); \
39+
\
40+
return n; \
3341
} \
3442
\
3543
static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
3644
const struct bin_attribute *attr, char *buf, \
3745
loff_t off, size_t count) \
3846
{ \
3947
struct device *dev = kobj_to_dev(kobj); \
48+
cpumask_var_t mask; \
49+
ssize_t n; \
50+
\
51+
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) \
52+
return -ENOMEM; \
53+
\
54+
cpumask_copy(mask, topology_##mask(dev->id)); \
55+
n = cpumap_print_list_to_buf(buf, mask, off, count); \
56+
free_cpumask_var(mask); \
4057
\
41-
return cpumap_print_list_to_buf(buf, topology_##mask(dev->id), \
42-
off, count); \
58+
return n; \
4359
}
4460

4561
define_id_show_func(physical_package_id, "%d");

0 commit comments

Comments
 (0)