【问题标题】:Is there a way to run seteuid() (as a non-root user) and temporarily change the euid to another non-root user without root/sudo privileges?有没有办法运行 seteuid() (作为非 root 用户)并临时将 euid 更改为另一个没有 root/sudo 权限的非 root 用户?
【发布时间】:2022-11-10 11:34:43
【问题描述】:

我正在尝试编写一个 C 程序,该程序应该打开一个只能由(非 root)用户 A 读取/写入的文件。当由既不是 root 也不是用户 A 的用户运行时,程序应该允许用户打开文件,有效用户为用户 A。

目前,我的程序只有在作为 sudo 运行时才能更改 euid,这是有道理的。

int main(int argc, char const *argv[]) {


    // should be userB's uid
    uid_t ruid = getuid(); 

    uid_t fake_uid = <userA-uid>;
    seteuid(fake_uid);

    /* open, read, write to and close file here */

    // returning euid to original uid
    seteuid(ruid);

    return 0;

}

【问题讨论】:

  • 但是...为什么不只授予用户 A 对该文件的权限?
  • 将可以访问文件的用户添加到组中,并授予文件组写入权限。
  • 一个干净的解决方案是定义一个组 G 用于具有足够权限的文件,并将该组 G 作为补充组添加到应该访问该文件的所有用户。或者使程序 setgid G 允许任何人通过运行程序访问文件而不添加补充组。
  • @KamilCuk 用户 A 已经具有该文件的读/写权限,我正在尝试创建一个程序,该程序将允许用户 A(和 root)以外的用户临时访问该文件。

标签: c sudo setuid


【解决方案1】:

考虑对 userA 使用 setuid:

chown userA program
chmod 4555 program

然后程序可以在打开文件后立即放弃权限:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

static void drop_privileges(void)
{
    uid_t uid = getuid();
    if (seteuid(uid)) {
        perror("drop_privileges");
        abort();
    }
}

int main()
{
    /* ... privileged operations */
    drop_privileges();
    /* ... rest */
}

【讨论】:

  • 重新排序 chomdchown 行。否则,chown 命令将覆盖模式设置。
猜你喜欢
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 2014-07-21
相关资源
最近更新 更多