Sunday, July 10, 2022

[SOLVED] Cannot set Attribute Permissions to 0666 in sysfs

Issue

I am working with sysfs and I need to create a file under sysfs, the file should be readable and writable by all users, for which I set the Permissions in '__ATTR' to 0666. But the module does not compile, the moment I change the permissions to 0660, it compiles correctly.

The Error message that I get with 0666 permissions is as follows

`/home/rishabh/kernel_modules/Task09/task9.c: At top level:
include/linux/bug.h:33:45: error: negative width in bit-field ‘<anonymous>’
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859:3: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro ‘VERIFY_OCTAL_PERMISSIONS’
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
include/linux/bug.h:33:45: warning: initialization from incompatible pointer type [enabled by default]
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859:3: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro ‘VERIFY_OCTAL_PERMISSIONS’
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
include/linux/bug.h:33:45: warning: (near initialization for ‘id_attribute.show’) [enabled by default]
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
                                             ^
include/linux/kernel.h:859:3: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
   BUILD_BUG_ON_ZERO((perms) & 2) +     \
   ^
include/linux/sysfs.h:102:12: note: in expansion of macro ‘VERIFY_OCTAL_PERMISSIONS’
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },  \
            ^
/home/rishabh/kernel_modules/Task09/task9.c:65:2: note: in expansion of macro ‘__ATTR’
  __ATTR(id, 0666, id_show, id_store);
  ^
`

I also tried using __ATTR_RW(_name) macro, but it gives read-write permissions only to root and all others are left with read permission.


Solution

If you follow the error messages, the 2nd one is

kernel.h:859:3: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
BUILD_BUG_ON_ZERO((perms) & 2)

and if you look in kernel.h you will see the comment

#define VERIFY_OCTAL_PERMISSIONS(perms)                      
     ...
     /* OTHER_WRITABLE?  Generally considered a bad idea. */ \
     BUILD_BUG_ON_ZERO((perms) & 2) + \
 ...

So you can see that you are being told that it is a bad idea to make a sysfs file writeable to all. If you really want to do this, you must bypass this macro check. For example, add just before your call of __ATTR() a redefinition of the macro:

/* warning! need write-all permission so overriding check */ 
#undef VERIFY_OCTAL_PERMISSIONS
#define VERIFY_OCTAL_PERMISSIONS(perms) (perms)


Answered By - meuh
Answer Checked By - Clifford M. (WPSolving Volunteer)