Commit e116a64f authored by Ira Weiny's avatar Ira Weiny Committed by Doug Ledford

IB/hfi: Properly set permissions for user device files

Some of the device files are required to be user accessible for PSM while
most should remain accessible only by root.

Add a parameter to hfi1_cdev_init which controls if the user should have access
to this device which places it in a different class with the appropriate
devnode callback.

In addition set the devnode call back for the existing class to be a bit more
explicit for those permissions.

Finally remove the unnecessary null check before class_destroy
Tested-by: default avatarDonald Dutile <ddutile@redhat.com>
Signed-off-by: Haralanov, Mitko (mitko.haralanov@intel.com)
Signed-off-by: default avatarIra Weiny <ira.weiny@intel.com>
Signed-off-by: default avatarDoug Ledford <dledford@redhat.com>
parent 7d630467
...@@ -57,11 +57,13 @@ ...@@ -57,11 +57,13 @@
#include "device.h" #include "device.h"
static struct class *class; static struct class *class;
static struct class *user_class;
static dev_t hfi1_dev; static dev_t hfi1_dev;
int hfi1_cdev_init(int minor, const char *name, int hfi1_cdev_init(int minor, const char *name,
const struct file_operations *fops, const struct file_operations *fops,
struct cdev *cdev, struct device **devp) struct cdev *cdev, struct device **devp,
bool user_accessible)
{ {
const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor); const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
struct device *device = NULL; struct device *device = NULL;
...@@ -78,7 +80,11 @@ int hfi1_cdev_init(int minor, const char *name, ...@@ -78,7 +80,11 @@ int hfi1_cdev_init(int minor, const char *name,
goto done; goto done;
} }
device = device_create(class, NULL, dev, NULL, "%s", name); if (user_accessible)
device = device_create(user_class, NULL, dev, NULL, "%s", name);
else
device = device_create(class, NULL, dev, NULL, "%s", name);
if (!IS_ERR(device)) if (!IS_ERR(device))
goto done; goto done;
ret = PTR_ERR(device); ret = PTR_ERR(device);
...@@ -110,6 +116,26 @@ const char *class_name(void) ...@@ -110,6 +116,26 @@ const char *class_name(void)
return hfi1_class_name; return hfi1_class_name;
} }
static char *hfi1_devnode(struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0600;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
static const char *hfi1_class_name_user = "hfi1_user";
const char *class_name_user(void)
{
return hfi1_class_name_user;
}
static char *hfi1_user_devnode(struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}
int __init dev_init(void) int __init dev_init(void)
{ {
int ret; int ret;
...@@ -125,7 +151,22 @@ int __init dev_init(void) ...@@ -125,7 +151,22 @@ int __init dev_init(void)
ret = PTR_ERR(class); ret = PTR_ERR(class);
pr_err("Could not create device class (err %d)\n", -ret); pr_err("Could not create device class (err %d)\n", -ret);
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
goto done;
} }
class->devnode = hfi1_devnode;
user_class = class_create(THIS_MODULE, class_name_user());
if (IS_ERR(user_class)) {
ret = PTR_ERR(user_class);
pr_err("Could not create device class for user accessible files (err %d)\n",
-ret);
class_destroy(class);
class = NULL;
user_class = NULL;
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
goto done;
}
user_class->devnode = hfi1_user_devnode;
done: done:
return ret; return ret;
...@@ -133,10 +174,11 @@ int __init dev_init(void) ...@@ -133,10 +174,11 @@ int __init dev_init(void)
void dev_cleanup(void) void dev_cleanup(void)
{ {
if (class) { class_destroy(class);
class_destroy(class); class = NULL;
class = NULL;
} class_destroy(user_class);
user_class = NULL;
unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
} }
...@@ -52,7 +52,8 @@ ...@@ -52,7 +52,8 @@
int hfi1_cdev_init(int minor, const char *name, int hfi1_cdev_init(int minor, const char *name,
const struct file_operations *fops, const struct file_operations *fops,
struct cdev *cdev, struct device **devp); struct cdev *cdev, struct device **devp,
bool user_accessible);
void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp); void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp);
const char *class_name(void); const char *class_name(void);
int __init dev_init(void); int __init dev_init(void);
......
...@@ -292,7 +292,7 @@ int hfi1_diag_add(struct hfi1_devdata *dd) ...@@ -292,7 +292,7 @@ int hfi1_diag_add(struct hfi1_devdata *dd)
if (atomic_inc_return(&diagpkt_count) == 1) { if (atomic_inc_return(&diagpkt_count) == 1) {
ret = hfi1_cdev_init(HFI1_DIAGPKT_MINOR, name, ret = hfi1_cdev_init(HFI1_DIAGPKT_MINOR, name,
&diagpkt_file_ops, &diagpkt_cdev, &diagpkt_file_ops, &diagpkt_cdev,
&diagpkt_device); &diagpkt_device, false);
} }
return ret; return ret;
...@@ -592,7 +592,8 @@ static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name) ...@@ -592,7 +592,8 @@ static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name)
ret = hfi1_cdev_init(HFI1_SNOOP_CAPTURE_BASE + dd->unit, name, ret = hfi1_cdev_init(HFI1_SNOOP_CAPTURE_BASE + dd->unit, name,
&snoop_file_ops, &snoop_file_ops,
&dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev); &dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev,
false);
if (ret) { if (ret) {
dd_dev_err(dd, "Couldn't create %s device: %d", name, ret); dd_dev_err(dd, "Couldn't create %s device: %d", name, ret);
......
...@@ -2090,14 +2090,16 @@ static int user_add(struct hfi1_devdata *dd) ...@@ -2090,14 +2090,16 @@ static int user_add(struct hfi1_devdata *dd)
if (atomic_inc_return(&user_count) == 1) { if (atomic_inc_return(&user_count) == 1) {
ret = hfi1_cdev_init(0, class_name(), &hfi1_file_ops, ret = hfi1_cdev_init(0, class_name(), &hfi1_file_ops,
&wildcard_cdev, &wildcard_device); &wildcard_cdev, &wildcard_device,
true);
if (ret) if (ret)
goto done; goto done;
} }
snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
ret = hfi1_cdev_init(dd->unit + 1, name, &hfi1_file_ops, ret = hfi1_cdev_init(dd->unit + 1, name, &hfi1_file_ops,
&dd->user_cdev, &dd->user_device); &dd->user_cdev, &dd->user_device,
true);
if (ret) if (ret)
goto done; goto done;
...@@ -2105,7 +2107,8 @@ static int user_add(struct hfi1_devdata *dd) ...@@ -2105,7 +2107,8 @@ static int user_add(struct hfi1_devdata *dd)
snprintf(name, sizeof(name), snprintf(name, sizeof(name),
"%s_ui%d", class_name(), dd->unit); "%s_ui%d", class_name(), dd->unit);
ret = hfi1_cdev_init(dd->unit + UI_OFFSET, name, &ui_file_ops, ret = hfi1_cdev_init(dd->unit + UI_OFFSET, name, &ui_file_ops,
&dd->ui_cdev, &dd->ui_device); &dd->ui_cdev, &dd->ui_device,
false);
if (ret) if (ret)
goto done; goto done;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment