I have the following code:
static struct kobj_attribute my_attribute =__ATTR(my_node, 0444, my_show_fn, my_store_fn);
...
kobject_init(&my_module->kobj, &ktype_my_module);
ret = kobject_add(&my_module->kobj, kernel_kobj, "%s", "my_module");
...
ret = sysfs_create_file(&my_module->kobj, &my_attribute.attr);
This creates a /sys/kernel/my_module/my_node node
, owned by root with 0444 permissions.
How can I make a non-root user own this node, from within the kernel code?
Owner's and group's identifiers of a sysfs attribute are determined by the the kobject for which this attribute is created.
Specifically, it is get_ownership
field of the struct kobj_type
which may contain a callback for set owner's and group's identifiers. The field has following signature:
struct kobj_type {
...
void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
};
When the callback is called, both *uid
and *gid
contain 0 (which corresponds to the root user). The callback may set these output parameters to other values.