3

I am making a small linux module that is a driver to a char device. In my code i create the device class, than the device it self and thus a /dev file is created in my system. the problem is that the /dev file has only root permissions and the user has neither read nor write nor execute permissions on that file, i would like to change the /dev file permissions.

I have searched the web for answers and what i found was to change the udev file, but this solution will not work in my case because i need the permissions to change dynamicly when the module is loaded into the kernel. The module i am writing will not always run on my machine, thus i need it to change the permissions "on the fly".

major_number_firewall = register_chrdev(0, device_name_firewall, &my_file_implementation_firewall);

device_class = class_create(THIS_MODULE, class_name_firewall);

log_file_device = device_create(device_class, NULL, MKDEV(major_number_firewall, MINOR_LOG), NULL, device_name_log_file);

Is there a function for changing the permissions?

user3595668
  • 31
  • 1
  • 2

4 Answers4

4
  1. You can write a small udev rules for achieving this.

  2. If you are implementing a char device driver, then consider using misc_register() and misc_unregister() which is wrapper over the above calls (device_create()...). Refer to struct miscdevice

    struct miscdevice  {
        int minor;
        const char *name;
        const struct file_operations *fops;
        struct list_head list;
        struct device *parent;
        struct device *this_device;
        const char *nodename;
        umode_t mode;
    };
    

You can use the member (struct miscdevice *)->mode to set the appropriate permissions (S_IRUGO | S_IRWXUGO | S_IALLUGO | etc ...)

hope this helps.

askb
  • 6,501
  • 30
  • 43
1

I think what Rocoder meant was to first change the permissions of your device file using

int chmod(const char *path, mode_t mode); OR int fchmod(int fd, mode_t mode);

in your user space application. Then open the device file using open() and do whatever.

also note: we cannot have a program chmod itself to super user mode within itself. This is to ensure no illegal program takes hold of the system.

RootPhoenix
  • 1,626
  • 1
  • 22
  • 40
0

You may try this

#include <sys/stat.h>

int chmod(const char *path, mode_t mode); OR int fchmod(int fd, mode_t mode);

or inside /etc/udev/udev.conf,

you can modify the default_mode = "0660"

Rocoder
  • 1,083
  • 2
  • 15
  • 26
  • Hey, thank you for your answer, but it is impossible to inclue the file inside a kernel module. and as i mentioned changing the udev file is not an option. – user3595668 May 03 '14 at 14:40
0

To set the permission for your misc device you can use the mode field as follows.

static struct miscdevice somedevice = {
    .minor  = MISC_DYNAMIC_MINOR,
    .name   = DEVICE_NAME,
    .fops   = &some_fops,
    .mode   = 0666,
};

This will set read/write access to all. To perform read/write operation you do not need to be root.

Hope this helps.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99