c - 决定必须由 super 用户调用 setgroups() 和 initgroups() 的机制是什么?

标签 c linux privileges access-control

来自 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 calls setgroups().

决定 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/

相关文章:

c - 使用 C 将参数传递给另一个函数

c - DBL_MAX 的类型

c++ - 哪种方法可以测试乘法有符号整数溢出?

linux - 在 bash/sh 中分别通过管道传输 stderr 和 stdout

python - 检查脚本用户是否具有类似 root 权限的最佳方法是什么?

java - 以管理员身份从 java 启动命令提示符 (CMD)

mysql - MySQL 中的 INDEX、REFERENCE 和 LOCK TABLE 权限是什么?

用C创建客户端: does not connect to the server

linux - ffmpeg 修复水印大小或百分比

linux - 配置:错误:C 预处理器未能通过健全性检查