drm/i915: Fix CFI violation with show_dynamic_id()
Subsystems (2)
PATCH 1/1 drm/i915: Fix CFI violation with show_dynamic_id()
When an attribute group is created with sysfs_create_group(), the ->sysfs_ops() callback is set to kobj_sysfs_ops, which sets the ->show() callback to kobj_attr_show(). kobj_attr_show() uses container_of() to get the ->show() callback from the attribute it was passed, meaning the ->show() callback needs to be the same type as the ->show() callback in 'struct kobj_attribute'.
However, show_dynamic_id() has the type of the ->show() callback in 'struct device_attribute', which causes a CFI violation when opening the 'id' sysfs node under drm/card0/metrics. This happens to work because the layout of 'struct kobj_attribute' and 'struct device_attribute' are the same, so the container_of() cast happens to allow the ->show() callback to still work.
Change the type of show_dynamic_id() to match the ->show() callback in 'struct kobj_attributes' and update the type of sysfs_metric_id to match, which resolves the CFI violation.
--- drivers/gpu/drm/i915/i915_perf.c | 4 ++-- drivers/gpu/drm/i915/i915_perf_types.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/i915_perf.c +2 -2
@@ -4050,8 +4050,8 @@ static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,return ERR_PTR (err); } -static ssize_t show_dynamic_id (struct device *dev, -struct device_attribute *attr, +static ssize_t show_dynamic_id (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) { struct i915_oa_config *oa_config =
drivers/gpu/drm/i915/i915_perf_types.h +1 -1
@@ -55,7 +55,7 @@ struct i915_oa_config {struct attribute_group sysfs_metric;struct attribute *attrs[2 ]; -struct device_attribute sysfs_metric_id; +struct kobj_attribute sysfs_metric_id;struct kref ref;struct rcu_head rcu;
base-commit: 7ecc3cc8a7b39f08eee9aea7b718187583342a70
Signature · 1 line
2.36.1
Quoted 14 lines · Nathan Chancellor
When an attribute group is created with
sysfs_create_group(), the ->sysfs_ops() callback is set tokobj_sysfs_ops, which sets the ->show() callback tokobj_attr_show().kobj_attr_show()usescontainer_of()to get the ->show() callback from the attribute it was passed, meaning the ->show() callback needs to be the same type as the ->show() callback in 'structkobj_attribute'.However,
show_dynamic_id()has the type of the ->show() callback in 'structdevice_attribute', which causes a CFI violation when opening the 'id' sysfs node under drm/card0/metrics. This happens to work because the layout of 'structkobj_attribute' and 'structdevice_attribute' are the same, so thecontainer_of()cast happens to allow the ->show() callback to still work.
Quoted 6 lines
Change the type of
show_dynamic_id()to match the ->show() callback in 'structkobj_attributes' and update the type ofsysfs_metric_idto match, which resolves the CFI violation.Fixes f89823c21224 ("drm/i915/perf: Implement I915_PERF_ADD/REMOVE_CONFIG interface")Signed-off-by Nathan Chancellor <[email protected]>
This matches my own investigation into the error. Thanks for putting the patch together! :)
Signature · 1 line
Kees Cook
Quoted 46 lines · Nathan Chancellor
When an attribute group is created with
sysfs_create_group(), the ->sysfs_ops() callback is set tokobj_sysfs_ops, which sets the ->show() callback tokobj_attr_show().kobj_attr_show()usescontainer_of()to get the ->show() callback from the attribute it was passed, meaning the ->show() callback needs to be the same type as the ->show() callback in 'structkobj_attribute'.However,
show_dynamic_id()has the type of the ->show() callback in 'structdevice_attribute', which causes a CFI violation when opening the 'id' sysfs node under drm/card0/metrics. This happens to work because the layout of 'structkobj_attribute' and 'structdevice_attribute' are the same, so thecontainer_of()cast happens to allow the ->show() callback to still work.Change the type of
show_dynamic_id()to match the ->show() callback in 'structkobj_attributes' and update the type ofsysfs_metric_idto match, which resolves the CFI violation.Fixes f89823c21224 ("drm/i915/perf: Implement I915_PERF_ADD/REMOVE_CONFIG interface")Signed-off-by Nathan Chancellor <[email protected]>--- drivers/gpu/drm/i915/i915_perf.c | 4 ++-- drivers/gpu/drm/i915/i915_perf_types.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/gpu/drm/i915/i915_perf.c +2
-2@@ -4050,8 +4050,8 @@ static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,return ERR_PTR (err); } -static ssize_t show_dynamic_id (struct device *dev, -struct device_attribute *attr, +static ssize_t show_dynamic_id (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) { struct i915_oa_config *oa_config =drivers/gpu/drm/i915/i915_perf_types.h +0
-0
drivers/gpu/drm/i915/i915_perf_types.h struct i915_oa_config { · line 61
Quoted 8 lines
drivers/gpu/drm/i915/i915_perf_types.h +1
-1@@ -55,7 +55,7 @@ struct i915_oa_config {struct attribute_group sysfs_metric;struct attribute *attrs[2 ]; -struct device_attribute sysfs_metric_id; +struct kobj_attribute sysfs_metric_id;struct kref ref;struct rcu_head rcu;
This looks correct to me. Thanks for testing this and fixing the issue!
Sami
Tvrtko Ursulin
AppliedQuoted 14 lines · Nathan Chancellor
When an attribute group is created with
sysfs_create_group(), the ->sysfs_ops() callback is set tokobj_sysfs_ops, which sets the ->show() callback tokobj_attr_show().kobj_attr_show()usescontainer_of()to get the ->show() callback from the attribute it was passed, meaning the ->show() callback needs to be the same type as the ->show() callback in 'structkobj_attribute'.However,
show_dynamic_id()has the type of the ->show() callback in 'structdevice_attribute', which causes a CFI violation when opening the 'id' sysfs node under drm/card0/metrics. This happens to work because the layout of 'structkobj_attribute' and 'structdevice_attribute' are the same, so thecontainer_of()cast happens to allow the ->show() callback to still work.
Quoted 6 lines
Change the type of
show_dynamic_id()to match the ->show() callback in 'structkobj_attributes' and update the type ofsysfs_metric_idto match, which resolves the CFI violation.Fixes f89823c21224 ("drm/i915/perf: Implement I915_PERF_ADD/REMOVE_CONFIG interface")Signed-off-by Nathan Chancellor <[email protected]>
Merged to drm-intel-gt-next, thanks for the fix and reviews!
Regards,
Tvrtko
Quoted 35 lines
--- drivers/gpu/drm/i915/i915_perf.c | 4 ++-- drivers/gpu/drm/i915/i915_perf_types.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/gpu/drm/i915/i915_perf.c +2
-2@@ -4050,8 +4050,8 @@ static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,return ERR_PTR (err); } -static ssize_t show_dynamic_id (struct device *dev, -struct device_attribute *attr, +static ssize_t show_dynamic_id (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) { struct i915_oa_config *oa_config =drivers/gpu/drm/i915/i915_perf_types.h +1
-1@@ -55,7 +55,7 @@ struct i915_oa_config {struct attribute_group sysfs_metric;struct attribute *attrs[2 ]; -struct device_attribute sysfs_metric_id; +struct kobj_attribute sysfs_metric_id;struct kref ref;struct rcu_head rcu;base-commit:
7ecc3cc8a7b39f08eee9aea7b718187583342a70