Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nvmem: core: constify 'struct bin_attribute'

The sysfs core now allows instances of 'struct bin_attribute' to be
moved into read-only memory. Make use of that to protect them against
accidental or malicious modifications.

Also adapt the dynamic sysfs cell logic to handle the const attributes.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20241230143035.265518-2-srinivas.kandagatla@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Thomas Weißschuh and committed by
Greg Kroah-Hartman
78dc14da 90154d08

+19 -16
+19 -16
drivers/nvmem/core.c
··· 213 213 }; 214 214 215 215 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, 216 - struct bin_attribute *attr, char *buf, 216 + const struct bin_attribute *attr, char *buf, 217 217 loff_t pos, size_t count) 218 218 { 219 219 struct device *dev; ··· 246 246 } 247 247 248 248 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, 249 - struct bin_attribute *attr, char *buf, 249 + const struct bin_attribute *attr, char *buf, 250 250 loff_t pos, size_t count) 251 251 { 252 252 struct device *dev; ··· 340 340 const char *id, int index); 341 341 342 342 static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj, 343 - struct bin_attribute *attr, char *buf, 343 + const struct bin_attribute *attr, char *buf, 344 344 loff_t pos, size_t count) 345 345 { 346 346 struct nvmem_cell_entry *entry; ··· 374 374 } 375 375 376 376 /* default read/write permissions */ 377 - static struct bin_attribute bin_attr_rw_nvmem = { 377 + static const struct bin_attribute bin_attr_rw_nvmem = { 378 378 .attr = { 379 379 .name = "nvmem", 380 380 .mode = 0644, 381 381 }, 382 - .read = bin_attr_nvmem_read, 383 - .write = bin_attr_nvmem_write, 382 + .read_new = bin_attr_nvmem_read, 383 + .write_new = bin_attr_nvmem_write, 384 384 }; 385 385 386 - static struct bin_attribute *nvmem_bin_attributes[] = { 386 + static const struct bin_attribute *const nvmem_bin_attributes[] = { 387 387 &bin_attr_rw_nvmem, 388 388 NULL, 389 389 }; 390 390 391 391 static const struct attribute_group nvmem_bin_group = { 392 - .bin_attrs = nvmem_bin_attributes, 392 + .bin_attrs_new = nvmem_bin_attributes, 393 393 .attrs = nvmem_attrs, 394 394 .is_bin_visible = nvmem_bin_attr_is_visible, 395 395 .bin_size = nvmem_bin_attr_size, ··· 401 401 NULL, 402 402 }; 403 403 404 - static struct bin_attribute bin_attr_nvmem_eeprom_compat = { 404 + static const struct bin_attribute bin_attr_nvmem_eeprom_compat = { 405 405 .attr = { 406 406 .name = "eeprom", 407 407 }, 408 - .read = bin_attr_nvmem_read, 409 - .write = bin_attr_nvmem_write, 408 + .read_new = bin_attr_nvmem_read, 409 + .write_new = bin_attr_nvmem_write, 410 410 }; 411 411 412 412 /* ··· 461 461 .name = "cells", 462 462 }; 463 463 struct nvmem_cell_entry *entry; 464 + const struct bin_attribute **pattrs; 464 465 struct bin_attribute *attrs; 465 466 unsigned int ncells = 0, i = 0; 466 467 int ret = 0; ··· 473 472 474 473 /* Allocate an array of attributes with a sentinel */ 475 474 ncells = list_count_nodes(&nvmem->cells); 476 - group.bin_attrs = devm_kcalloc(&nvmem->dev, ncells + 1, 477 - sizeof(struct bin_attribute *), GFP_KERNEL); 478 - if (!group.bin_attrs) { 475 + pattrs = devm_kcalloc(&nvmem->dev, ncells + 1, 476 + sizeof(struct bin_attribute *), GFP_KERNEL); 477 + if (!pattrs) { 479 478 ret = -ENOMEM; 480 479 goto unlock_mutex; 481 480 } ··· 495 494 entry->bit_offset); 496 495 attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem); 497 496 attrs[i].size = entry->bytes; 498 - attrs[i].read = &nvmem_cell_attr_read; 497 + attrs[i].read_new = &nvmem_cell_attr_read; 499 498 attrs[i].private = entry; 500 499 if (!attrs[i].attr.name) { 501 500 ret = -ENOMEM; 502 501 goto unlock_mutex; 503 502 } 504 503 505 - group.bin_attrs[i] = &attrs[i]; 504 + pattrs[i] = &attrs[i]; 506 505 i++; 507 506 } 507 + 508 + group.bin_attrs_new = pattrs; 508 509 509 510 ret = device_add_group(&nvmem->dev, &group); 510 511 if (ret)