3

I'm trying to modify Linux kernel and I need to get the user ID and the process group ID from a task_struct and a pid_namespace. Although I searched their definitions in the source code, I couldn't find any global variables or functions (maybe I am missing because of the lack of the comments in codes) to access them.

Is there a method to get those inside the kernel space since I can not use user-space functions like getuid(), etc.?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
user5508821
  • 33
  • 1
  • 3

1 Answers1

7

You should be able to use task_struct->cred->uid or task_struct->real_cred->uid. That being said, I have not tested this and this is just from a cursory reading of LXR (include/linux/sched.h line 1508 and include/linux/cred.h line 127).

If you want the PGID, try pid_vnr(task_pgrp(task_struct)). This code is from kernel/sys.c line 990.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Joel C
  • 2,958
  • 2
  • 15
  • 18
  • thank you. If you know can you answer for pid_namespace? – user5508821 Oct 30 '15 at 23:44
  • From my quick perusal (and I am by no means a kernel developer), it doesn't appear that PID namespaces have any users or groups associated with them. See http://lwn.net/Articles/531419/ – Joel C Oct 31 '15 at 00:32
  • actually I am changing fs/proc/base.c the function has_pid_permission. In default one they used pid->hide_pid, pid->pid_gid (pid is a pid_namespace pointer). I thought pid_namespace is a special structure for a process to hold just its pid values, so it should contain pgid,uid etc. Am I correct? Although it has pid_gid ( thread group, or process group not so sure), hide_pid, last_pid(?) variables, It doesn't have any uid type variable. Actually what i want to do is just compare the current process with the task. I thought that function does the same. – user5508821 Oct 31 '15 at 02:08
  • @JoelC I tried to use task_struct->real_cred->uid, and i gets error: dereferencing pointer to incomplete type ‘const struct cred’? – smalinux Apr 14 '20 at 05:10
  • 1
    @Sohaib please make a new question and post the code you're using. – Joel C Apr 14 '20 at 15:36
  • @JoelC thank you )) https://stackoverflow.com/q/61211757/5688267 – smalinux Apr 14 '20 at 15:47