2

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.

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
RishabhHardas
  • 495
  • 1
  • 5
  • 25
  • We need more context. Someone has put in a check so that an invalid value is detected at compile time, in a similar way to `assert` being used to show errors at runtime. Saying something stopped in `assert` tells us that someone thought to put in a check, but doesn't tell us what the check is. –  Nov 23 '16 at 07:28
  • This is regarding the eudyptula challenge task 09. I am editing my question by adding the snippet of output of make – RishabhHardas Nov 23 '16 at 08:19

2 Answers2

3

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)
meuh
  • 11,500
  • 2
  • 29
  • 45
  • This compiled it, but the file does not have read-write permissions for all. Is there any other restriction that the kernel imposes? – RishabhHardas Nov 23 '16 at 10:07
  • Perhaps the kernel code creating the file also dynamically restricts the mask. It must be part of the challenge to find out where this code is and how to bypass it. – meuh Nov 23 '16 at 10:15
  • Ok. I will try to see what happens exactly. – RishabhHardas Nov 23 '16 at 10:39
2

__ATTR_RW(id) should be a correct way (and eudyptula accepted that ;)). Definition in sysfs.h says, that it set rights to 0644, which are correct rights you want - no one, except root user, can't write to /sys/kernel files (and it's specified in the task too).

sysfs.h part:

#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO),             \
                         _name##_show, _name##_store)
stderr
  • 141
  • 5
  • Yes I had done this, but the statement said that it should be readable and writable by all. Hence, i was trying this. Then the id and foo file both are to be set via __ATTR_RW() ? – RishabhHardas Nov 23 '16 at 10:38
  • The statement should be for previous task in debugfs, if I remember correctly. The task for sysfs edit this: "- fix up the permissions of the files to not allow world writable values, but only be able to be written to by root.". And yes, the `foo` file should have the same rights. – stderr Nov 23 '16 at 10:46
  • Great! Thank you! – RishabhHardas Nov 23 '16 at 10:52