I am implementing a char driver (Linux), and there are certain IOCTL commands in my driver that only ADMIN should execute.
My question is how can I verify user rights as part of my implementation of the ioctl command and restrict the access of an unprivileged user to IOCTL.
You can use a function bool capable(int cap)that returns true if the user requests an opportunity. Possible cap values are listed in the kernel sources in include/uapi/linux/capability.h(macros begin with CAP _).
bool capable(int cap)
include/uapi/linux/capability.h
, - . , . CAP_SYS_ADMIN.
ioctl, . , ioctl, struct file *file, , file->f_mode FMODE_WRITE.
ioctl
struct file *file
file->f_mode
FMODE_WRITE
if (!(file->f_mode & FMODE_WRITE)) return -EACCES;
, . , , -, , .
open() O_RDONLY, , .
open()
O_RDONLY
ioctl , (CAP_SYS_ADMIN, , ).
CAP_SYS_ADMIN
if (!capable(CAP_SYS_ADMIN)) return -EACCES;