Skip to content

Commit 0530ad4

Browse files
t-8chbjorn-helgaas
authored andcommitted
PCI/sysfs: 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. Link: https://lore.kernel.org/r/20241202-sysfs-const-bin_attr-pci-v1-1-c32360f495a7@weissschuh.net Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 40384c8 commit 0530ad4

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

drivers/pci/pci-sysfs.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
694694
static DEVICE_ATTR_RO(boot_vga);
695695

696696
static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
697-
struct bin_attribute *bin_attr, char *buf,
697+
const struct bin_attribute *bin_attr, char *buf,
698698
loff_t off, size_t count)
699699
{
700700
struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
@@ -769,7 +769,7 @@ static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
769769
}
770770

771771
static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
772-
struct bin_attribute *bin_attr, char *buf,
772+
const struct bin_attribute *bin_attr, char *buf,
773773
loff_t off, size_t count)
774774
{
775775
struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
@@ -837,9 +837,9 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
837837

838838
return count;
839839
}
840-
static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
840+
static const BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
841841

842-
static struct bin_attribute *pci_dev_config_attrs[] = {
842+
static const struct bin_attribute *const pci_dev_config_attrs[] = {
843843
&bin_attr_config,
844844
NULL,
845845
};
@@ -856,7 +856,7 @@ static size_t pci_dev_config_attr_bin_size(struct kobject *kobj,
856856
}
857857

858858
static const struct attribute_group pci_dev_config_attr_group = {
859-
.bin_attrs = pci_dev_config_attrs,
859+
.bin_attrs_new = pci_dev_config_attrs,
860860
.bin_size = pci_dev_config_attr_bin_size,
861861
};
862862

@@ -887,8 +887,8 @@ pci_llseek_resource(struct file *filep,
887887
* callback routine (pci_legacy_read).
888888
*/
889889
static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
890-
struct bin_attribute *bin_attr, char *buf,
891-
loff_t off, size_t count)
890+
const struct bin_attribute *bin_attr,
891+
char *buf, loff_t off, size_t count)
892892
{
893893
struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
894894

@@ -912,8 +912,8 @@ static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
912912
* callback routine (pci_legacy_write).
913913
*/
914914
static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
915-
struct bin_attribute *bin_attr, char *buf,
916-
loff_t off, size_t count)
915+
const struct bin_attribute *bin_attr,
916+
char *buf, loff_t off, size_t count)
917917
{
918918
struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
919919

@@ -1003,8 +1003,8 @@ void pci_create_legacy_files(struct pci_bus *b)
10031003
b->legacy_io->attr.name = "legacy_io";
10041004
b->legacy_io->size = 0xffff;
10051005
b->legacy_io->attr.mode = 0600;
1006-
b->legacy_io->read = pci_read_legacy_io;
1007-
b->legacy_io->write = pci_write_legacy_io;
1006+
b->legacy_io->read_new = pci_read_legacy_io;
1007+
b->legacy_io->write_new = pci_write_legacy_io;
10081008
/* See pci_create_attr() for motivation */
10091009
b->legacy_io->llseek = pci_llseek_resource;
10101010
b->legacy_io->mmap = pci_mmap_legacy_io;
@@ -1099,7 +1099,7 @@ static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
10991099
}
11001100

11011101
static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1102-
struct bin_attribute *attr, char *buf,
1102+
const struct bin_attribute *attr, char *buf,
11031103
loff_t off, size_t count, bool write)
11041104
{
11051105
#ifdef CONFIG_HAS_IOPORT
@@ -1142,14 +1142,14 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
11421142
}
11431143

11441144
static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1145-
struct bin_attribute *attr, char *buf,
1145+
const struct bin_attribute *attr, char *buf,
11461146
loff_t off, size_t count)
11471147
{
11481148
return pci_resource_io(filp, kobj, attr, buf, off, count, false);
11491149
}
11501150

11511151
static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1152-
struct bin_attribute *attr, char *buf,
1152+
const struct bin_attribute *attr, char *buf,
11531153
loff_t off, size_t count)
11541154
{
11551155
int ret;
@@ -1210,8 +1210,8 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
12101210
} else {
12111211
sprintf(res_attr_name, "resource%d", num);
12121212
if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1213-
res_attr->read = pci_read_resource_io;
1214-
res_attr->write = pci_write_resource_io;
1213+
res_attr->read_new = pci_read_resource_io;
1214+
res_attr->write_new = pci_write_resource_io;
12151215
if (arch_can_pci_mmap_io())
12161216
res_attr->mmap = pci_mmap_resource_uc;
12171217
} else {
@@ -1292,7 +1292,7 @@ void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
12921292
* writing anything except 0 enables it
12931293
*/
12941294
static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1295-
struct bin_attribute *bin_attr, char *buf,
1295+
const struct bin_attribute *bin_attr, char *buf,
12961296
loff_t off, size_t count)
12971297
{
12981298
struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
@@ -1318,7 +1318,7 @@ static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
13181318
* device corresponding to @kobj.
13191319
*/
13201320
static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1321-
struct bin_attribute *bin_attr, char *buf,
1321+
const struct bin_attribute *bin_attr, char *buf,
13221322
loff_t off, size_t count)
13231323
{
13241324
struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
@@ -1344,9 +1344,9 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
13441344

13451345
return count;
13461346
}
1347-
static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
1347+
static const BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
13481348

1349-
static struct bin_attribute *pci_dev_rom_attrs[] = {
1349+
static const struct bin_attribute *const pci_dev_rom_attrs[] = {
13501350
&bin_attr_rom,
13511351
NULL,
13521352
};
@@ -1372,7 +1372,7 @@ static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj,
13721372
}
13731373

13741374
static const struct attribute_group pci_dev_rom_attr_group = {
1375-
.bin_attrs = pci_dev_rom_attrs,
1375+
.bin_attrs_new = pci_dev_rom_attrs,
13761376
.is_bin_visible = pci_dev_rom_attr_is_visible,
13771377
.bin_size = pci_dev_rom_attr_bin_size,
13781378
};

0 commit comments

Comments
 (0)