Skip to contentSkip to conversation

sysfs: Refine is_visible API

PATCH In review Guenter Roeck 3 participants 9 messages Jan 19, 2015 – Jan 20, 2015 Driver core

lore
Share & tools
lore
#0 lore raw

cover sysfs: Refine is_visible API

Up to now, is_visible can only be used to either remove visibility of a file entirely or to add permissions, but not to reduce permissions. This makes it impossible, for example, to use DEVICE_ATTR_RW to define file attributes and reduce permissions to read-only.

This behavior is undesirable and unnecessarily complicates code which needs to reduce permissions; instead of just returning the desired permissions, it has to ensure that the permissions in the attribute variable declaration only reflect the minimal permissions ever needed.

Change semantics of is_visible to only use the permissions returned from it instead of oring the returned value with the hard-coded permissions.

The code now dumps a warning to the console if an is_visible function returns unexpected permissions.

Also document struct attribute_group.

Tested with v3.19-rc5.

#1 lore raw

PATCH 1/3 sysfs: Use only return value from is_visible for the file mode

Up to now, is_visible can only be used to either remove visibility of a file entirely or to add permissions, but not to reduce permissions. This makes it impossible, for example, to use DEVICE_ATTR_RW to define file attributes and reduce permissions to read-only.

This behavior is undesirable and unnecessarily complicates code which needs to reduce permissions; instead of just returning the desired permissions, it has to ensure that the permissions in the attribute variable declaration only reflect the minimal permissions ever needed.

Change semantics of is_visible to only use the permissions returned from it instead of oring the returned value with the hard-coded permissions.

---
 fs/sysfs/group.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/sysfs/group.c +2 -3
index 7d2a860..305eccb 100644
@@ -41,7 +41,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 
 	if (grp->attrs) {
 		for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
-			umode_t mode = 0;
+			umode_t mode = (*attr)->mode;
 
 			/*
 			 * In update mode, we're changing the permissions or
@@ -56,8 +56,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 					continue;
 			}
 			error = sysfs_add_file_mode_ns(parent, *attr, false,
-						       (*attr)->mode | mode,
-						       NULL);
+						       mode, NULL);
 			if (unlikely(error))
 				break;
 		}
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/3 sysfs: Only accept read/write permissions for file attributes

In reply to Guenter Roeck

For sysfs file attributes, only read and write permisssions make sense. Mask provided attribute permissions accordingly and send a warning to the console if invalid permission bits are set.

---
 fs/sysfs/group.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/sysfs/group.c +6 -0
index 305eccb..0de6473 100644
@@ -55,6 +55,12 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 				if (!mode)
 					continue;
 			}
+
+			WARN(mode & ~(S_IRUGO | S_IWUGO | SYSFS_PREALLOC),
+			     "Attribute %s: Invalid permission 0x%x\n",
+			     (*attr)->name, mode);
+
+			mode &= S_IRUGO | S_IWUGO | SYSFS_PREALLOC;
 			error = sysfs_add_file_mode_ns(parent, *attr, false,
 						       mode, NULL);
 			if (unlikely(error))
Signature · 1 line
2.1.0
#3 lore raw

Re: [PATCH 2/3] sysfs: Only accept read/write permissions for file attributes

Hi Guenter,

Quoted 1 line

For sysfs file attributes, only read and write permisssions make sense.

Minor typo, there's an extra 's' to permissions.

Quoted 14 lines

Mask provided attribute permissions accordingly and send a warning to the console if invalid permission bits are set.

---
 fs/sysfs/group.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/sysfs/group.c +0 -0
index 305eccb..0de6473 100644

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 61

Quoted 7 lines
fs/sysfs/group.c +4 -0
@@ -55,6 +55,12 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
                                 if (!mode)
                                         continue;
                         }
+
+                        WARN(mode & ~(S_IRUGO | S_IWUGO | SYSFS_PREALLOC),
+                             "Attribute %s: Invalid permission 0x%x\n",
+                             (*attr)->name, mode);

To print permissions, I would suggest unsigned octal ("0%o").

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 63

Quoted 2 lines
fs/sysfs/group.c +2 -0
+
+                        mode &= S_IRUGO | S_IWUGO | SYSFS_PREALLOC;

As readable attributes are created with S_IRUGO and writable attributes are created with S_IWUSR, I would limit the scope of is_visible to only:
S_IRUGO | S_IWUSR. Write permission for group and others feels wrong.

Then, I think we may want to keep the extra bits (all mode bits > 0777) from the default attribute mode. Can they be used for sysfs attributes?

My suggestion is something like this:

        /* Limit the scope to S_IRUGO | S_IWUSR */
        if (mode & ~(S_IRUGO | S_IWUSR))
                pr_warn("Attribute %s: Invalid permissions 0%o\n",
                        (*attr)->name, mode);

        mode &= S_IRUGO | S_IWUSR;

        /* Use only returned bits and defaults > 0777 */
        mode |= (*attr)->mode & ~S_IRWXUGO;

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 66

Quoted 3 lines
fs/sysfs/group.c +0 -0
                         error = sysfs_add_file_mode_ns(parent, *attr, false,
                                                        mode, NULL);
                         if (unlikely(error))

The code hitting this warning actually is drivers/pci/pci-sysfs.c, which declares write-only attributes with S_IWUSR|S_IWGRP (0220). Is that correct to have write access for group for these attributes?

Thanks,
-v

#4 lore raw

Re: [PATCH 2/3] sysfs: Only accept read/write permissions for file attributes

Quoted 15 lines · Vivien Didelot

Hi Guenter,

Quoted 1 line

For sysfs file attributes, only read and write permisssions make sense.

Minor typo, there's an extra 's' to permissions.

Quoted 9 lines

Mask provided attribute permissions accordingly and send a warning to the console if invalid permission bits are set.

---
  fs/sysfs/group.c | 6 ++++++
  1 file changed, 6 insertions(+)
Quoted 15 lines

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 61

Quoted 12 lines
fs/sysfs/group.c +4 -0
index 305eccb..0de6473 100644
@@ -55,6 +55,12 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
                                  if (!mode)
                                          continue;
                          }
+
+                        WARN(mode & ~(S_IRUGO | S_IWUGO | SYSFS_PREALLOC),
+                             "Attribute %s: Invalid permission 0x%x\n",
+                             (*attr)->name, mode);

To print permissions, I would suggest unsigned octal ("0%o").

Fine with me.

Quoted 6 lines

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 63

Quoted 2 lines
fs/sysfs/group.c +2 -0
+
+                        mode &= S_IRUGO | S_IWUGO | SYSFS_PREALLOC;

As readable attributes are created with S_IRUGO and writable attributes are created with S_IWUSR, I would limit the scope of is_visible to only:
S_IRUGO | S_IWUSR. Write permission for group and others feels wrong.

That seems to be too restrictive to me. There are several attributes (I count 32) which permit group writes (search for "DEVICE_ATTR.*IWGRP").

Quoted 4 lines

Then, I think we may want to keep the extra bits (all mode bits > 0777) from the default attribute mode. Can they be used for sysfs attributes?

I have not seen it anywhere, except for execute permissions in drivers/hid/hid-lg4ff.c (which should be fixed).

Of course, I may have missed some.

Quoted 7 lines

My suggestion is something like this:

         /* Limit the scope to S_IRUGO | S_IWUSR */
         if (mode & ~(S_IRUGO | S_IWUSR))
                 pr_warn("Attribute %s: Invalid permissions 0%o\n",
                         (*attr)->name, mode);

The reason for WARN() was to give the implementer a strong incentive to fix it, and to show the calling path. Only displaying the attribute name makes it difficult to identify the culprit, at least for widely used attribute names.

Quoted 5 lines
         mode &= S_IRUGO | S_IWUSR;

         /* Use only returned bits and defaults > 0777 */
         mode |= (*attr)->mode & ~S_IRWXUGO;
Quoted 7 lines

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 66

Quoted 3 lines
fs/sysfs/group.c +0 -0
                          error = sysfs_add_file_mode_ns(parent, *attr, false,
                                                         mode, NULL);
                          if (unlikely(error))

The code hitting this warning actually is drivers/pci/pci-sysfs.c, which declares write-only attributes with S_IWUSR|S_IWGRP (0220). Is that correct to have write access for group for these attributes?

Why not ? Not our call to make.

Anyway, my goal was to keep things simple. Taking some bits from the default and others from the return value of the is_visible function isn't simple, even more so since your code would require the is_visible function to mask out SYSFS_PREALLOC to avoid the warning.

[ Note that I don't like SYSFS_PREALLOC to start with; it overloads
mode and, worse, is identical to S_IFIFO and part of the S_IFMT mask. But that is a different issue. ]

Thanks,
Guenter

#5 lore raw

Re: [PATCH 2/3] sysfs: Only accept read/write permissions for file attributes

Hi Guenter,

Quoted 24 lines
Quoted 10 lines

fs/sysfs/group.c static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 61

Quoted 8 lines
fs/sysfs/group.c +4 -0
@@ -55,6 +55,12 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
                                  if (!mode)
                                          continue;
                          }
+
+                        WARN(mode & ~(S_IRUGO | S_IWUGO | SYSFS_PREALLOC),
+                             "Attribute %s: Invalid permission 0x%x\n",
+                             (*attr)->name, mode);

To print permissions, I would suggest unsigned octal ("0%o").

Fine with me.

Quoted 6 lines

Review static int create_files(struct kernfs_node *parent, struct kobject *kobj, · line 63

Quoted 2 lines
(quoted diff) +2 -0
+
+                        mode &= S_IRUGO | S_IWUGO | SYSFS_PREALLOC;

As readable attributes are created with S_IRUGO and writable attributes are created with S_IWUSR, I would limit the scope of is_visible to only:
S_IRUGO | S_IWUSR. Write permission for group and others feels wrong.

That seems to be too restrictive to me. There are several attributes (I count 32) which permit group writes (search for "DEVICE_ATTR.*IWGRP").

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Then, I think we may want to keep the extra bits (all mode bits > 0777) from the default attribute mode. Can they be used for sysfs attributes?

I have not seen it anywhere, except for execute permissions in drivers/hid/hid-lg4ff.c (which should be fixed).

Fixed and merged ;)

Quoted 4 lines

Of course, I may have missed some.

Quoted 2 lines

My suggestion is something like this:

Quoted 8 lines
Quoted 5 lines
         /* Limit the scope to S_IRUGO | S_IWUSR */
         if (mode & ~(S_IRUGO | S_IWUSR))
                 pr_warn("Attribute %s: Invalid permissions 0%o\n",
                         (*attr)->name, mode);

The reason for WARN() was to give the implementer a strong incentive to fix it, and to show the calling path. Only displaying the attribute name makes it difficult to identify the culprit, at least for widely used attribute names.

No objection with WARN(), I just decreased it to pr_warn() for testing.

Quoted 9 lines
Quoted 9 lines
         mode &= S_IRUGO | S_IWUSR;

         /* Use only returned bits and defaults > 0777 */
         mode |= (*attr)->mode & ~S_IRWXUGO;
Quoted 3 lines
fs/sysfs/group.c +0 -0
                          error = sysfs_add_file_mode_ns(parent, *attr, false,
                                                         mode, NULL);
                          if (unlikely(error))
Quoted 5 lines
Quoted 3 lines

The code hitting this warning actually is drivers/pci/pci-sysfs.c, which declares write-only attributes with S_IWUSR|S_IWGRP (0220). Is that correct to have write access for group for these attributes?

Why not ? Not our call to make.

I was concerned about attributes with group write permission, but you are right, this is another discussion.

Quoted 4 lines

Anyway, my goal was to keep things simple. Taking some bits from the default and others from the return value of the is_visible function isn't simple, even more so since your code would require the is_visible function to mask out SYSFS_PREALLOC to avoid the warning.

While I'm still not sure about the consequences of flipping this SYSFS_PREALLOC bit at runtime, I do agree with your goal.

Then to keep it simple, the scope of is_visible could be limited to any bit allowed at attribute declaration (using *_ATTR* macros). The compile-time check macro VERIFY_OCTAL_PERMISSIONS() allows any bit but S_IWOTH. The scope can be SYSFS_PREALLOC | 0775. (or 0664 if we want to avoid executables as well.)

[ This will prevent some follow-up patches "avoid world-writable sysfs files". In the future, we may want a runtime equivalent of VERIFY_OCTAL_PERMISSIONS. ]

Thanks,
-v

#6 lore raw

Re: [PATCH 2/3] sysfs: Only accept read/write permissions for file attributes

Quoted 2 lines · Vivien Didelot

Hi Guenter,

[ ... ]

Quoted 9 lines
Quoted 5 lines

Anyway, my goal was to keep things simple. Taking some bits from the default and others from the return value of the is_visible function isn't simple, even more so since your code would require the is_visible function to mask out SYSFS_PREALLOC to avoid the warning.

While I'm still not sure about the consequences of flipping this SYSFS_PREALLOC bit at runtime, I do agree with your goal.

Quoted 8 lines

Then to keep it simple, the scope of is_visible could be limited to any bit allowed at attribute declaration (using *_ATTR* macros). The compile-time check macro VERIFY_OCTAL_PERMISSIONS() allows any bit but S_IWOTH. The scope can be SYSFS_PREALLOC | 0775. (or 0664 if we want to avoid executables as well.)

[ This will prevent some follow-up patches "avoid world-writable sysfs files". In the future, we may want a runtime equivalent of VERIFY_OCTAL_PERMISSIONS. ]

0775 and 0664 are both fine with me, with a preference for 0664. Before I resubmit - Greg, any preference from your side ?

Thanks,
Guenter

#7 lore raw

Re: [PATCH 2/3] sysfs: Only accept read/write permissions for file attributes

Quoted 18 lines · Guenter Roeck
Quoted 2 lines · Vivien Didelot

Hi Guenter,

[ ... ]

Quoted 14 lines
Quoted 5 lines

Anyway, my goal was to keep things simple. Taking some bits from the default and others from the return value of the is_visible function isn't simple, even more so since your code would require the is_visible function to mask out SYSFS_PREALLOC to avoid the warning.

While I'm still not sure about the consequences of flipping this SYSFS_PREALLOC bit at runtime, I do agree with your goal.

Then to keep it simple, the scope of is_visible could be limited to any bit allowed at attribute declaration (using *_ATTR* macros). The compile-time check macro VERIFY_OCTAL_PERMISSIONS() allows any bit but S_IWOTH. The scope can be SYSFS_PREALLOC | 0775. (or 0664 if we want to avoid executables as well.)

Quoted 5 lines
Quoted 3 lines

[ This will prevent some follow-up patches "avoid world-writable sysfs files". In the future, we may want a runtime equivalent of VERIFY_OCTAL_PERMISSIONS. ]

0775 and 0664 are both fine with me, with a preference for 0664. Before I resubmit - Greg, any preference from your side ?

I don't have the time to look at them this week, so feel free to fix up what you know about and resend and I will get to them as soon as I can.

thanks,

greg k-h

#8 lore raw

PATCH 3/3 sysfs: Document struct attribute_group

In reply to Guenter Roeck

Document variables defined in struct attribute_group to ensure correct usage.

---
 include/linux/sysfs.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/sysfs.h +15 -0
index ddad161..99382c0 100644
@@ -57,6 +57,21 @@ do {							\
 #define sysfs_attr_init(attr) do {} while (0)
 #endif
 
+/**
+ * struct attribute_group - data structure used to declare an attribute group.
+ * @name:	Optional: Attribute group name
+ *		If specified, the attribute group will be created in
+ *		a new subdirectory with this name.
+ * @is_visible:	Optional: Function to return permissions associated with an
+ *		attribute of the group. Will be called repeatedly for each
+ *		attribute in the group. Only read/write permissions as well as
+ *		SYSFS_PREALLOC are accepted. Must return 0 if an attribute is
+ *		not visible. The returned value will replace static permissions
+ *		defined in struct attribute or struct bin_attribute.
+ * @attrs:	Pointer to NULL terminated list of attributes.
+ * @bin_attrs:	Pointer to NULL terminated list of binary attributes.
+ *		Either attrs or bin_attrs or both must be provided.
+ */
 struct attribute_group {
 	const char		*name;
 	umode_t			(*is_visible)(struct kobject *,
Signature · 1 line
2.1.0