来自 APUE
#include <grp.h> /* on Linux */ int setgroups(int ngroups, const gid_t grouplist[]);
The
setgroups
function can be called by the superuser to set the supplementary group ID list for the calling process: grouplist contains the array of group IDs, and ngroups specifies the number of elements in the array. The value of ngroups cannot be larger than NGROUPS_MAX.#include <grp.h> /* on Linux and Solaris */ int initgroups(const char *username, gid_t basegid);
One must be superuser to call
initgroups()
, since it callssetgroups()
.
决定 setgroups()
和 initgroups()
必须由 super 用户调用的机制是什么?
“机制”是指与以下内容类似或对应的内容。在 Linux 中,我了解到我们可以根据访问控制列表 (ACL) 确定进程对文件的可访问性:
进程的有效用户ID、有效组ID和补充组ID
文件的权限位。
谢谢。
最佳答案
你可以看看actual code for setgroups .它是这样开始的:
SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
{
struct group_info *group_info;
int retval;
if (!may_setgroups())
return -EPERM;
// other stuff
}
这里是 may_setgroups
:
bool may_setgroups(void)
{
struct user_namespace *user_ns = current_user_ns();
return ns_capable(user_ns, CAP_SETGID) &&
userns_may_setgroups(user_ns);
}
这里是 userns_may_setgroups
,如果您没有使用用户命名空间:
static inline bool userns_may_setgroups(const struct user_namespace *ns)
{
return true;
}
所以:setgroups
将返回 EPERM
如果您没有 CAP_SETGID
错误 能力。
推论:您可以调用 setgroups
如果你有 CAP_SETGID
能力。
Root 自动拥有所有功能(因此 root 可以调用 setgroups
),但如果您不是 root 并且您拥有 CAP_SETGID
也可以调用它能力。
关于c - 决定必须由 super 用户调用 setgroups() 和 initgroups() 的机制是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174287/