w1: ds2408: Constify 'struct bin_attribute'
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 11 Dec 2024 17:50:18 +0000 (18:50 +0100)
committerKrzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Sat, 14 Dec 2024 10:06:22 +0000 (11:06 +0100)
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.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/r/20241211-sysfs-const-bin_attr-w1-v1-3-c4befd2aa7cc@weissschuh.net
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
drivers/w1/slaves/w1_ds2408.c

index 56f822a1dfdbed46ccc7db136255a1d021e6189a..beccd2912d2aeb9a5f83cabba19c32946778a790 100644 (file)
@@ -65,8 +65,8 @@ static int _read_reg(struct w1_slave *sl, u8 address, unsigned char *buf)
 }
 
 static ssize_t state_read(struct file *filp, struct kobject *kobj,
-                         struct bin_attribute *bin_attr, char *buf, loff_t off,
-                         size_t count)
+                         const struct bin_attribute *bin_attr, char *buf,
+                         loff_t off, size_t count)
 {
        dev_dbg(&kobj_to_w1_slave(kobj)->dev,
                "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
@@ -77,7 +77,7 @@ static ssize_t state_read(struct file *filp, struct kobject *kobj,
 }
 
 static ssize_t output_read(struct file *filp, struct kobject *kobj,
-                          struct bin_attribute *bin_attr, char *buf,
+                          const struct bin_attribute *bin_attr, char *buf,
                           loff_t off, size_t count)
 {
        dev_dbg(&kobj_to_w1_slave(kobj)->dev,
@@ -90,7 +90,7 @@ static ssize_t output_read(struct file *filp, struct kobject *kobj,
 }
 
 static ssize_t activity_read(struct file *filp, struct kobject *kobj,
-                            struct bin_attribute *bin_attr, char *buf,
+                            const struct bin_attribute *bin_attr, char *buf,
                             loff_t off, size_t count)
 {
        dev_dbg(&kobj_to_w1_slave(kobj)->dev,
@@ -103,8 +103,8 @@ static ssize_t activity_read(struct file *filp, struct kobject *kobj,
 }
 
 static ssize_t cond_search_mask_read(struct file *filp, struct kobject *kobj,
-                                    struct bin_attribute *bin_attr, char *buf,
-                                    loff_t off, size_t count)
+                                    const struct bin_attribute *bin_attr,
+                                    char *buf, loff_t off, size_t count)
 {
        dev_dbg(&kobj_to_w1_slave(kobj)->dev,
                "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
@@ -117,7 +117,7 @@ static ssize_t cond_search_mask_read(struct file *filp, struct kobject *kobj,
 
 static ssize_t cond_search_polarity_read(struct file *filp,
                                         struct kobject *kobj,
-                                        struct bin_attribute *bin_attr,
+                                        const struct bin_attribute *bin_attr,
                                         char *buf, loff_t off, size_t count)
 {
        if (count != 1 || off != 0)
@@ -127,8 +127,8 @@ static ssize_t cond_search_polarity_read(struct file *filp,
 }
 
 static ssize_t status_control_read(struct file *filp, struct kobject *kobj,
-                                  struct bin_attribute *bin_attr, char *buf,
-                                  loff_t off, size_t count)
+                                  const struct bin_attribute *bin_attr,
+                                  char *buf, loff_t off, size_t count)
 {
        if (count != 1 || off != 0)
                return -EFAULT;
@@ -160,7 +160,7 @@ static bool optional_read_back_valid(struct w1_slave *sl, u8 expected)
 #endif
 
 static ssize_t output_write(struct file *filp, struct kobject *kobj,
-                           struct bin_attribute *bin_attr, char *buf,
+                           const struct bin_attribute *bin_attr, char *buf,
                            loff_t off, size_t count)
 {
        struct w1_slave *sl = kobj_to_w1_slave(kobj);
@@ -210,7 +210,7 @@ out:
  * Writing to the activity file resets the activity latches.
  */
 static ssize_t activity_write(struct file *filp, struct kobject *kobj,
-                             struct bin_attribute *bin_attr, char *buf,
+                             const struct bin_attribute *bin_attr, char *buf,
                              loff_t off, size_t count)
 {
        struct w1_slave *sl = kobj_to_w1_slave(kobj);
@@ -240,8 +240,8 @@ error:
 }
 
 static ssize_t status_control_write(struct file *filp, struct kobject *kobj,
-                                   struct bin_attribute *bin_attr, char *buf,
-                                   loff_t off, size_t count)
+                                   const struct bin_attribute *bin_attr,
+                                   char *buf, loff_t off, size_t count)
 {
        struct w1_slave *sl = kobj_to_w1_slave(kobj);
        u8 w1_buf[4];
@@ -310,14 +310,14 @@ out:
        return res;
 }
 
-static BIN_ATTR_RO(state, 1);
-static BIN_ATTR_RW(output, 1);
-static BIN_ATTR_RW(activity, 1);
-static BIN_ATTR_RO(cond_search_mask, 1);
-static BIN_ATTR_RO(cond_search_polarity, 1);
-static BIN_ATTR_RW(status_control, 1);
+static const BIN_ATTR_RO(state, 1);
+static const BIN_ATTR_RW(output, 1);
+static const BIN_ATTR_RW(activity, 1);
+static const BIN_ATTR_RO(cond_search_mask, 1);
+static const BIN_ATTR_RO(cond_search_polarity, 1);
+static const BIN_ATTR_RW(status_control, 1);
 
-static struct bin_attribute *w1_f29_bin_attrs[] = {
+static const struct bin_attribute *const w1_f29_bin_attrs[] = {
        &bin_attr_state,
        &bin_attr_output,
        &bin_attr_activity,
@@ -328,7 +328,7 @@ static struct bin_attribute *w1_f29_bin_attrs[] = {
 };
 
 static const struct attribute_group w1_f29_group = {
-       .bin_attrs = w1_f29_bin_attrs,
+       .bin_attrs_new = w1_f29_bin_attrs,
 };
 
 static const struct attribute_group *w1_f29_groups[] = {