Skip to content
Advertisement

Get user’s group from linux kernel driver

I’m developing a simple pipe kernel driver as an excercise for university, and my task states that users from each user group should have access to their own separate pipe. But I can’t find how to get group of the user who opened my driver file.

I have functon static int pipe_open(struct inode *i, struct file *f) which receives arguments from kernel when a process opens driver’s file in /dev/. Struct file does contain infromation about the process that opens that file (struct fown_struct), from there I can get pid reference of that process, but not info about user who it belongs to. I’m using kernel 5.10, here are links to source code that I looked up: struct file, struct fown_struct, struct pid, struct inode.

Could you suggest how to solve this?

Advertisement

Answer

You’re looking at the wrong place.

When the kernel executes a syscall, current is a struct task_struct that defines the caller properties. It has a member cred, a struct cred, that refers to the callers effective credentials. It has the real, saved, effective, and filesystem user and group IDs.

This means that at open() time, in your kernel open handler, examine current->cred.egid to obtain the effective group ID of the task (thread of a process) that is doing this open call.

Advertisement