ACPI: bgrt: Fix CFI violation
PATCH 1/2 ACPI: bgrt: Fix CFI violation
clang's Control Flow Integrity requires that every indirect call has a valid target, which is based on the type of the function pointer. The *_show() functions in this file are written as if they will be called from dev_attr_show(); however, they will be called from sysfs_kf_seq_show() because the files were created by sysfs_create_group() and the sysfs ops are based on kobj_sysfs_ops because of kobject_add_and_create(). Because the *_show() functions do not match the type of the show() member in struct kobj_attribute, there is a CFI violation.
$ cat /sys/firmware/acpi/bgrt/{status,type,version,{x,y}offset}}
1
0
1
522
307$ dmesg | grep "CFI failure" [ 267.761825] CFI failure (target: type_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762246] CFI failure (target: xoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762584] CFI failure (target: status_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762973] CFI failure (target: yoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.763330] CFI failure (target: version_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8):
Convert these functions to the type of the show() member in struct kobj_attribute so that there is no more CFI violation. Because these functions are all so similar, combine them into a macro.
--- drivers/acpi/bgrt.c | 57 ++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 39 deletions(-)
drivers/acpi/bgrt.c +18 -39
@@ -15,40 +15,19 @@static void *bgrt_image;static struct kobject *bgrt_kobj; -static ssize_t version_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab.version); -} -static DEVICE_ATTR_RO (version); - -static ssize_t status_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab.status); -} -static DEVICE_ATTR_RO (status); - -static ssize_t type_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab.image_type); -} -static DEVICE_ATTR_RO (type); - -static ssize_t xoffset_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab.image_offset_x); -} -static DEVICE_ATTR_RO (xoffset); - -static ssize_t yoffset_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab.image_offset_y); -} -static DEVICE_ATTR_RO (yoffset); +#define BGRT_SHOW (_name, _member) \ + static ssize_t _name##_show(struct kobject *kobj, \ + struct kobj_attribute *attr, char *buf) \ + { \ + return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab._member); \ + } \ + struct kobj_attribute bgrt_attr_##_name = __ATTR_RO(_name) + +BGRT_SHOW (version, version); +BGRT_SHOW (status, status); +BGRT_SHOW (type, image_type); +BGRT_SHOW (xoffset, image_offset_x); +BGRT_SHOW (yoffset, image_offset_y);static ssize_t image_read (struct file *file,struct kobject *kobj,struct bin_attribute *attr,char *buf,loff_t off,size_t count)
@@ -60,11 +39,11 @@ static ssize_t image_read(struct file *file, struct kobject *kobj, static BIN_ATTR_RO(image, 0); /* size gets filled in later */static struct attribute *bgrt_attributes[] = { - &dev_attr_version.attr, - &dev_attr_status.attr, - &dev_attr_type.attr, - &dev_attr_xoffset.attr, - &dev_attr_yoffset.attr, + &bgrt_attr_version.attr, + &bgrt_attr_status.attr, + &bgrt_attr_type.attr, + &bgrt_attr_xoffset.attr, + &bgrt_attr_yoffset.attr,NULL , };
base-commit: a51c80057a887e0f24bd8303b0791a130ff04121
Signature · 1 line
2.32.0.93.g670b81a890
PATCH 2/2 ACPI: bgrt: Use sysfs_emit
sysfs_emit is preferred to snprintf for emitting values after commit 2efc459d06f1 ("sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs output").
--- drivers/acpi/bgrt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/bgrt.c +1 -1
@@ -19,7 +19,7 @@ static struct kobject *bgrt_kobj; static ssize_t _name##_show(struct kobject *kobj, \struct kobj_attribute *attr,char *buf) \ { \ -return snprintf (buf,PAGE_SIZE ,"%d\n" , bgrt_tab._member); \ +return sysfs_emit (buf,"%d\n" , bgrt_tab._member); \ } \ struct kobj_attribute bgrt_attr_##_name = __ATTR_RO(_name)
Signature · 1 line
2.32.0.93.g670b81a890
Re: [PATCH 2/2] ACPI: bgrt: Use sysfs_emit
Quoted 5 lines · Nathan Chancellor
sysfs_emitis preferred to snprintf for emitting values after commit2efc459d06f1("sysfs: Addsysfs_emitandsysfs_emit_atto format sysfs output").Signed-off-by Nathan Chancellor <[email protected]>
Perhaps just squash this into patch 1? Looks good otherwise!
Signature · 1 line
Kees Cook
Re: [PATCH 2/2] ACPI: bgrt: Use sysfs_emit
Quoted 9 lines · Kees Cook
Quoted 5 lines · Nathan Chancellor
sysfs_emitis preferred to snprintf for emitting values after commit2efc459d06f1("sysfs: Addsysfs_emitandsysfs_emit_atto format sysfs output").Signed-off-by Nathan Chancellor <[email protected]>Perhaps just squash this into patch 1? Looks good otherwise!
I thought about it but sysfs_emit is a relatively new API and the previous change may want to be backported but I do not have a strong opinion so I can squash it if Rafael or Len feel strongly :)
Thanks for taking a look, cheers!
Nathan
Kees Cook
Reviewed-byRe: [PATCH 2/2] ACPI: bgrt: Use sysfs_emit
Quoted 6 lines · Nathan Chancellor
Quoted 5 lines · Kees Cook
Quoted 4 lines · Nathan Chancellor
sysfs_emitis preferred to snprintf for emitting values after commit2efc459d06f1("sysfs: Addsysfs_emitandsysfs_emit_atto format sysfs output").
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Nathan Chancellor <[email protected]>Perhaps just squash this into patch 1? Looks good otherwise!
I thought about it but
sysfs_emitis a relatively new API and the previous change may want to be backported but I do not have a strong opinion so I can squash it if Rafael or Len feel strongly :)
Fair enough. :) I figured since CFI is even newer than sysfs_emit(), it didn't make sense to backport. Regardless:
Signature · 1 line
Kees Cook
Rafael J. Wysocki
maintainerRe: [PATCH 2/2] ACPI: bgrt: Use sysfs_emit
Quoted 13 lines · Kees Cook
Quoted 11 lines · Nathan Chancellor
Quoted 9 lines · Kees Cook
Quoted 5 lines · Nathan Chancellor
sysfs_emitis preferred to snprintf for emitting values after commit2efc459d06f1("sysfs: Addsysfs_emitandsysfs_emit_atto format sysfs output").Signed-off-by Nathan Chancellor <[email protected]>Perhaps just squash this into patch 1? Looks good otherwise!
Quoted 8 lines
Quoted 3 lines
I thought about it but
sysfs_emitis a relatively new API and the previous change may want to be backported but I do not have a strong opinion so I can squash it if Rafael or Len feel strongly :)Fair enough. :) I figured since CFI is even newer than
sysfs_emit(), it didn't make sense to backport. Regardless:Reviewed-by Kees Cook <[email protected]>
Applied along with the [1/2] as 5.14 material, thanks!
Quoted 24 lines · Nathan Chancellor
clang's Control Flow Integrity requires that every indirect call has a valid target, which is based on the type of the function pointer. The
*_show()functions in this file are written as if they will be called fromdev_attr_show(); however, they will be called fromsysfs_kf_seq_show()because the files were created bysysfs_create_group()and the sysfs ops are based onkobj_sysfs_opsbecause ofkobject_add_and_create(). Because the*_show()functions do not match the type of theshow()member in structkobj_attribute, there is a CFI violation.$ cat /sys/firmware/acpi/bgrt/{status,type,version,{x,y}offset}} 1 0 1 522 307$ dmesg | grep "CFI failure" [ 267.761825] CFI failure (target: type_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762246] CFI failure (target: xoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762584] CFI failure (target: status_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762973] CFI failure (target: yoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.763330] CFI failure (target: version_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8):
Quoted 7 lines
Convert these functions to the type of the
show()member in structkobj_attributeso that there is no more CFI violation. Because these functions are all so similar, combine them into a macro.Fixes d1ff4b1cdbab ("ACPI: Add support for exposing BGRT data")Signed-off-by Nathan Chancellor <[email protected]>
Thanks for solving this!
Signature · 1 line
Kees Cook