问题描述
正如LDD3第6页p175所示,我们可以通过current->uid
和current->euid
获得当前进程的UID和EUID.但是Linux Kernel 4.2的struct task_struct
定义不再包含uid
或euid
命名的字段.因此,我想知道是否还有其他获取UID和EUID的方法?谢谢!
As LDD3 chapter 6 p175 show, we can get current process UID and EUID by current->uid
and current->euid
.But the definition of struct task_struct
of Linux Kernel 4.2 don't contain fields named by uid
or euid
any more.So, I wonder if there are any other methods to get UID and EUID ?Thanks!
推荐答案
.uid
和.euid
字段已移至struct cred
,现在在struct task_struct
中显示为.cred
字段.它是在以下提交中完成的: CRED :将任务安全性上下文与task_struct 分开.如果查看include/linux/sched.h
文件的差异,您会注意到这一变化:
.uid
and .euid
fields were moved to struct cred
, which is now exposed as .cred
field in struct task_struct
. It was done in this commit: CRED: Separate task security context from task_struct. If you look at diff for include/linux/sched.h
file, you can notice this change:
- uid_t uid,euid,suid,fsuid;
- gid_t gid,egid,sgid,fsgid;
+ struct cred *cred; /* actual/objective task credentials */
所以现在代替:
current->uid;
current->euid;
您应该使用:
const struct cred *cred = current_cred();
cred->uid;
cred->euid;
请注意,应该使用current_cred()
函数来访问.cred
字段,因为它是 RCU 指针.
Notice that current_cred()
function should be used to access .cred
field, as it's RCU pointer.
还要 check_same_owner()实现例子.
这篇关于如何在Linux Kernel 4.2中获取当前进程的UID和EUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!