Skip to content

Commit 18d2f10

Browse files
andrealmeidbrauner
authored andcommitted
tmpfs: Fix type for sysfs' casefold attribute
DEVICE_STRING_ATTR_RO should be only used by device drivers since it relies on `struct device` to use device_show_string() function. Using this with non device code led to a kCFI violation: > cat /sys/fs/tmpfs/features/casefold [ 70.558496] CFI failure at kobj_attr_show+0x2c/0x4c (target: device_show_string+0x0/0x38; expected type: 0xc527b809) Like the other filesystems, fix this by manually declaring the attribute using kobj_attribute() and writing a proper show() function. Also, leave macros for anyone that need to expand tmpfs sysfs' with more attributes. Fixes: 5132f08 ("tmpfs: Expose filesystem features via sysfs") Reported-by: Nathan Chancellor <[email protected]> Closes: https://lore.kernel.org/lkml/20241031051822.GA2947788@thelio-3990X/ Signed-off-by: André Almeida <[email protected]> Link: https://lore.kernel.org/r/[email protected] Tested-by: Nathan Chancellor <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 33b091c commit 18d2f10

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

mm/shmem.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,13 +5548,38 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
55485548
EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
55495549

55505550
#if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
5551+
5552+
#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
5553+
{ \
5554+
.attr = { .name = __stringify(_name), .mode = _mode }, \
5555+
.show = _show, \
5556+
.store = _store, \
5557+
}
5558+
5559+
#define TMPFS_ATTR_W(_name, _store) \
5560+
static struct kobj_attribute tmpfs_attr_##_name = \
5561+
__INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
5562+
5563+
#define TMPFS_ATTR_RW(_name, _show, _store) \
5564+
static struct kobj_attribute tmpfs_attr_##_name = \
5565+
__INIT_KOBJ_ATTR(_name, 0644, _show, _store)
5566+
5567+
#define TMPFS_ATTR_RO(_name, _show) \
5568+
static struct kobj_attribute tmpfs_attr_##_name = \
5569+
__INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
5570+
55515571
#if IS_ENABLED(CONFIG_UNICODE)
5552-
static DEVICE_STRING_ATTR_RO(casefold, 0444, "supported");
5572+
static ssize_t casefold_show(struct kobject *kobj, struct kobj_attribute *a,
5573+
char *buf)
5574+
{
5575+
return sysfs_emit(buf, "supported\n");
5576+
}
5577+
TMPFS_ATTR_RO(casefold, casefold_show);
55535578
#endif
55545579

55555580
static struct attribute *tmpfs_attributes[] = {
55565581
#if IS_ENABLED(CONFIG_UNICODE)
5557-
&dev_attr_casefold.attr.attr,
5582+
&tmpfs_attr_casefold.attr,
55585583
#endif
55595584
NULL
55605585
};

0 commit comments

Comments
 (0)