[08/12] driver-core: Introduce DEVICE_ATTR_ADMIN_{RO,RW}
diff mbox series

Message ID 159312906372.1850128.11611897078988158727.stgit@dwillia2-desk3.amr.corp.intel.com
State Superseded
Headers show
Series
  • ACPI/NVDIMM: Runtime Firmware Activation
Related show

Commit Message

Dan Williams June 25, 2020, 11:51 p.m. UTC
A common pattern for using plain DEVICE_ATTR() instead of
DEVICE_ATTR_RO() and DEVICE_ATTR_RW() is for attributes that want to
limit read to only root.  I.e. many users of DEVICE_ATTR() are
specifying 0400 or 0600 for permissions.

Given the expectation that CAP_SYS_ADMIN is needed to access these
sensitive attributes and an explicit helper with the _ADMIN_ identifier
for DEVICE_ATTR_ADMIN_{RO,RW}.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 include/linux/device.h |    4 ++++
 include/linux/sysfs.h  |    7 +++++++
 2 files changed, 11 insertions(+)

Comments

Greg Kroah-Hartman June 26, 2020, 5:06 a.m. UTC | #1
On Thu, Jun 25, 2020 at 04:51:03PM -0700, Dan Williams wrote:
> A common pattern for using plain DEVICE_ATTR() instead of
> DEVICE_ATTR_RO() and DEVICE_ATTR_RW() is for attributes that want to
> limit read to only root.  I.e. many users of DEVICE_ATTR() are
> specifying 0400 or 0600 for permissions.
> 
> Given the expectation that CAP_SYS_ADMIN is needed to access these
> sensitive attributes and an explicit helper with the _ADMIN_ identifier
> for DEVICE_ATTR_ADMIN_{RO,RW}.
> 
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> ---
>  include/linux/device.h |    4 ++++
>  include/linux/sysfs.h  |    7 +++++++
>  2 files changed, 11 insertions(+)
> 
> diff --git a/include/linux/device.h b/include/linux/device.h
> index 15460a5ac024..d7c2570368fa 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -128,8 +128,12 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
>  		__ATTR_PREALLOC(_name, _mode, _show, _store)
>  #define DEVICE_ATTR_RW(_name) \
>  	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
> +#define DEVICE_ATTR_ADMIN_RW(_name) \
> +	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
>  #define DEVICE_ATTR_RO(_name) \
>  	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
> +#define DEVICE_ATTR_ADMIN_RO(_name) \
> +	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
>  #define DEVICE_ATTR_WO(_name) \
>  	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
>  #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index 86067dbe7745..34e84122f635 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -123,6 +123,13 @@ struct attribute_group {
>  	.show	= _name##_show,						\
>  }
>  
> +#define __ATTR_RW_MODE(_name, _mode) {					\
> +	.attr	= { .name = __stringify(_name),				\
> +		    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
> +	.show	= _name##_show,						\
> +	.store	= _name##_store,					\
> +}
> +
>  #define __ATTR_WO(_name) {						\
>  	.attr	= { .name = __stringify(_name), .mode = 0200 },		\
>  	.store	= _name##_store,					\
> 

Nice!  Want me to take this now, I know of many other places that could
be cleaned up to use this (like the raw device bug that I just fixed...)

If not:

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Dan Williams June 26, 2020, 5:09 a.m. UTC | #2
On Thu, Jun 25, 2020 at 10:06 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Thu, Jun 25, 2020 at 04:51:03PM -0700, Dan Williams wrote:
> > A common pattern for using plain DEVICE_ATTR() instead of
> > DEVICE_ATTR_RO() and DEVICE_ATTR_RW() is for attributes that want to
> > limit read to only root.  I.e. many users of DEVICE_ATTR() are
> > specifying 0400 or 0600 for permissions.
> >
> > Given the expectation that CAP_SYS_ADMIN is needed to access these
> > sensitive attributes and an explicit helper with the _ADMIN_ identifier
> > for DEVICE_ATTR_ADMIN_{RO,RW}.
> >
> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
> > Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> > ---
> >  include/linux/device.h |    4 ++++
> >  include/linux/sysfs.h  |    7 +++++++
> >  2 files changed, 11 insertions(+)
> >
> > diff --git a/include/linux/device.h b/include/linux/device.h
> > index 15460a5ac024..d7c2570368fa 100644
> > --- a/include/linux/device.h
> > +++ b/include/linux/device.h
> > @@ -128,8 +128,12 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
> >               __ATTR_PREALLOC(_name, _mode, _show, _store)
> >  #define DEVICE_ATTR_RW(_name) \
> >       struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
> > +#define DEVICE_ATTR_ADMIN_RW(_name) \
> > +     struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
> >  #define DEVICE_ATTR_RO(_name) \
> >       struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
> > +#define DEVICE_ATTR_ADMIN_RO(_name) \
> > +     struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
> >  #define DEVICE_ATTR_WO(_name) \
> >       struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
> >  #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
> > diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> > index 86067dbe7745..34e84122f635 100644
> > --- a/include/linux/sysfs.h
> > +++ b/include/linux/sysfs.h
> > @@ -123,6 +123,13 @@ struct attribute_group {
> >       .show   = _name##_show,                                         \
> >  }
> >
> > +#define __ATTR_RW_MODE(_name, _mode) {                                       \
> > +     .attr   = { .name = __stringify(_name),                         \
> > +                 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },          \
> > +     .show   = _name##_show,                                         \
> > +     .store  = _name##_store,                                        \
> > +}
> > +
> >  #define __ATTR_WO(_name) {                                           \
> >       .attr   = { .name = __stringify(_name), .mode = 0200 },         \
> >       .store  = _name##_store,                                        \
> >
>
> Nice!  Want me to take this now, I know of many other places that could
> be cleaned up to use this (like the raw device bug that I just fixed...)

Sure, that'd be great.

> If not:
>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

 Thanks Greg.

Patch
diff mbox series

diff --git a/include/linux/device.h b/include/linux/device.h
index 15460a5ac024..d7c2570368fa 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -128,8 +128,12 @@  ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
 		__ATTR_PREALLOC(_name, _mode, _show, _store)
 #define DEVICE_ATTR_RW(_name) \
 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
+#define DEVICE_ATTR_ADMIN_RW(_name) \
+	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
 #define DEVICE_ATTR_RO(_name) \
 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
+#define DEVICE_ATTR_ADMIN_RO(_name) \
+	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
 #define DEVICE_ATTR_WO(_name) \
 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 86067dbe7745..34e84122f635 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -123,6 +123,13 @@  struct attribute_group {
 	.show	= _name##_show,						\
 }
 
+#define __ATTR_RW_MODE(_name, _mode) {					\
+	.attr	= { .name = __stringify(_name),				\
+		    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
+	.show	= _name##_show,						\
+	.store	= _name##_store,					\
+}
+
 #define __ATTR_WO(_name) {						\
 	.attr	= { .name = __stringify(_name), .mode = 0200 },		\
 	.store	= _name##_store,					\