Search code examples
linuxlinux-kernelkernel-moduledevice-driver

Allow non-root users ioctl access to /dev/mytest file in linux module


My project contains two things: kernel module & app, that are communicating via ioctl using /dev/mytest file.

Currently, app is required to be run as root to access /dev/mytest file. Is it possible to allow non-users to iteract with this file without sudo?

As i understood, file_operations structure property .owner = THIS_MODULE has nothing about /dev/mytest file and is associated only with driver file.

I create /dev/mytest using class_create & device_create functions in kernel module. So, perhaps i should specify some special device_create parameters? Or how i can achieve it?


Solution

  • In a modern kernel, you can do something like

      myclass = class_create(THIS_MODULE, "my_class_name");
      /* error handling if class_create fails */
      myclass->devnode = mydevnode;
    

    where your devnode function is something like

      static char *mydevnode(struct device *dev, umode_t *mode)
      {
          if (mode)
              *mode = 0666; /* or whatever permissions you want */
          return NULL; /* could override /dev name here too */
      }
    

    then if you use device_create to create your driver special file, the kernel will call mydevnode to create the file, and you can override the file permissions there.