I have a question about getuid() and geteuid() in linux.
I know that getuid will return the real user id of the current process. Also geteuid() will return the effective user id of the current process.
My question is, where the informations about id are stored. Apart from the existence of /etc/passwd, I think every process should store their own id information somewhere.
If I'm right, please tell me where is the information stored (say the area like the stack). If I'm wrong, how does the process get its id?
This is something maintained by the kernel in its internal in-memory structures.
Linux kernel uses something called struct task_struct:
Every process under Linux is dynamically allocated a
struct task_structstructure.
In Linux kernel 4.12.10 this is defined as follows:
task_struct.h:
struct task_struct {
...
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
cred.h:
struct cred {
...
kuid_t uid; /* real UID of the task */
kgid_t gid; /* real GID of the task */
kuid_t suid; /* saved UID of the task */
kgid_t sgid; /* saved GID of the task */
kuid_t euid; /* effective UID of the task */
kgid_t egid; /* effective GID of the task */
kuid_t fsuid; /* UID for VFS ops */
kgid_t fsgid; /* GID for VFS ops */
These structures cannot be accessed directly by a user space process. To get this information, such processes have to use either system calls (such as getuid() and geteuid()) or the /proc file system.
Read Advanced Linux Programming and perhaps Operating System: Three Easy Pieces (both are freely downloadable).
(several books are needed to answer your question)
getuid(2) is (like getpid(2) and many others) a system call provided and implemented by the Linux kernel. syscalls(2) is a list of them.
(please take time to read more about system calls in general)
where the informations about id are stored.
The kernel manages data describing every process (in kernel memory, see NPE's answer for details). Each system call is a primitive atomic operation (from user-space perspective) and returns a result (usually in some register, not in memory). Read about CPU modes.
So that information is not in the user-level virtual address space of the process, it is returned at every invocation of getuid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With