sysfs 属性文件的权限被拒绝

sysfs 属性文件的权限被拒绝

作为防火墙实现的一部分,我正在实现一个 sysfs 设备,其属性由以下方式定义:

#define FILE_PERMISSIONS S_IWUSR | S_IRUGO
static DEVICE_ATTR(sysfs_att, FILE_PERMISSIONS, display, modify);

我使用读取文件没有问题

cat /sys/class/secws_class/secws_class_secws_device/sysfs_att

但是,由于我支持修改(存储)操作,我计划使用以下命令与设备进行交互

echo "0" > /sys/class/secws_class/secws_class_secws_device/sysfs_att

然而,这导致

-bash: /sys/class/secws_class/secws_class_secws_device/sysfs_att: Permission denied

即使我与 sudo 一起使用时也会发生这种情况tee。我还验证了 sys 文件具有正确的权限,并且它确实具有用户的写入权限。

除了我已经尝试过的方法之外,我找不到新的方法来处理这个问题,你们有什么想法吗?

编辑:根据您的要求,日志:

fw@fw-VirtualBox:~/HW2$ make
make -C /lib/modules/4.15.0-118-generic/build M=/home/fw/HW2 modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-118-generic'
  LD [M]  /home/fw/HW2/hw2secws.o
  Building modules, stage 2.
  MODPOST 1 modules
  LD [M]  /home/fw/HW2/hw2secws.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-118-generic'
fw@fw-VirtualBox:~/HW2$ sudo insmod hw2secws.ko

fw@fw-VirtualBox:~/HW2$ cat /sys/class/secws_class/secws_class_secws_device/sysfs_att
358310

fw@fw-VirtualBox:~/HW2$ echo "0" | sudo tee /sys/class/secws_class/secws_class_secws_device/sysfs_att
0
tee: /sys/class/secws_class/secws_class_secws_device/sysfs_att: Operation not permitted

fw@fw-VirtualBox:~/HW2$ echo "0" > /sys/class/secws_class/secws_class_secws_device/sysfs_att
-bash: /sys/class/secws_class/secws_class_secws_device/sysfs_att: Permission denied

fw@fw-VirtualBox:~/HW2$ ls -l /sys/class/secws_class/secws_class_secws_device/sysfs_att
-rw-r--r-- 1 root root 4096 נוב  8 21:25 /sys/class/secws_class/secws_class_secws_device/sysfs_att

fw@fw-VirtualBox:~/HW2$ sudo chown fw /sys/class/secws_class/secws_class_secws_device/sysfs_att

fw@fw-VirtualBox:~/HW2$ ls -l /sys/class/secws_class/secws_class_secws_device/sysfs_att
-rw-r--r-- 1 fw root 4096 נוב  8 21:25 /sys/class/secws_class/secws_class_secws_device/sysfs_att

fw@fw-VirtualBox:~/HW2$ echo "0" | sudo tee /sys/class/secws_class/secws_class_secws_device/sysfs_att
0
tee: /sys/class/secws_class/secws_class_secws_device/sysfs_att: Operation not permitted

fw@fw-VirtualBox:~/HW2$ echo "0" > /sys/class/secws_class/secws_class_secws_device/sysfs_att
-bash: echo: write error: Operation not permitted

还有 MRE:

static int major_number;
static struct class* sysfs_class = NULL;
static struct device* sysfs_device = NULL;
static struct file_operations fops = {
    .owner = THIS_MODULE
}

ssize_t display(struct device *dev, struct device_attribute *attr, char *buf){
...
}
ssize_t modify(struct device *dev, struct device_attribute *attr, const char *buff, size_t data_size)
{
...
}

#define FILE_PERMISSIONS S_IWUSR | S_IRUGO
static DEVICE_ATTR(sysfs_att, FILE_PERMISSIONS, display, modify);

int init_sysfs_module(void){
    
    /* Register Char-Device */
    major_number = register_chrdev(0, CHRDEV_NAME, &fops);\
    if (major_number < 0)
        return -1;
        
    /* Create Sysfs-Class */
    sysfs_class = class_create(THIS_MODULE, CLASS_NAME);
    if (IS_ERR(sysfs_class))
    {
        unregister_chrdev(major_number, CHRDEV_NAME);
        return -1;
    }
    
    /* Link DEVICE_NAME to sysfs_class */
    sysfs_device = device_create(sysfs_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME); 
    if (IS_ERR(sysfs_device))
    {
        class_destroy(sysfs_class);
        unregister_chrdev(major_number, CHRDEV_NAME);
        return -1;
    }
    
    /* Modify the device to use defined show/store methods */
    if (device_create_file(sysfs_device, (const struct device_attribute *)&dev_attr_sysfs_att.attr))
    {
        device_destroy(sysfs_class, MKDEV(major_number, 0));
        class_destroy(sysfs_class);
        unregister_chrdev(major_number, CHRDEV_NAME);
        return -1;
    }
    return 0;
}

void deinit_sysfs_module(void){
    device_remove_file(sysfs_device, (const struct device_attribute *)&dev_attr_sysfs_att.attr);
    device_destroy(sysfs_class, MKDEV(major_number, 0));
    class_destroy(sysfs_class);
    unregister_chrdev(major_number, CHRDEV_NAME);
}

static int __init module_init_function(void) {
    init_sysfs_module();
    HANDLE_NF_HOOKS(REGISTER_NF_HOOKS);
    return 0;
}

static void __exit module_exit_function(void) {
    deinit_sysfs_module();
    HANDLE_NF_HOOKS(CLEAR_NF_HOOKS);
}

module_init(module_init_function);
module_exit(module_exit_function);

编辑2:我也尝试过:

fw@fw-VirtualVox:~/HW2$ sudo su -
root@fw-VirtualBox:~# echo "0" > /sys/class/secws_class/secws_class_secws_device/sysfs_att
-sw: echo: write error: Operation not permitted

相关内容