简体   繁体   English

可执行文件设置了root suid,但是access(path,W_OK)仍然返回-1?

[英]Executable file set root suid, but access(path, W_OK) still return -1?

Why executable file set root suid, but access(path, W_OK) still return -1? 为什么可执行文件设置了root suid,但是access(path,W_OK)仍然返回-1?

Code: 码:

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

int main()
{
    printf("privilege => %d\n", access("/usr/local/etc/t.conf", W_OK));
    return 0;
}

Test run: 测试运行:

[www@mypy access]$ ll
总用量 12
-rwsrwxr-x. 1 root root 6600 1月  22 10:05 access
-rw-rw-r--. 1 www  www   135 1月  22 10:05 access.c

[www@mypy access]$ ./access 
privilege => -1

[root@mypy access]# ./access 
privilege => 0

The access library function deliberately checks the access rights of the real user, ignoring the fact that the executable has a different effective UID/GID. access库功能故意检查实际用户的访问权限,而忽略了可执行文件具有不同的有效UID / GID的事实。

If you only want to know whether read or write access is possible, you can open the file and see if there was an error. 如果仅想知道是否可以进行读取或写入访问,则可以打开文件并查看是否有错误。 However, careful setuid executables often want to know whether the real user would have been able to perform an action on the file. 但是,小心谨慎的setuid可执行文件通常想知道真实用户是否能够对文件执行操作。 To find out, they can use the access library function. 为了找到答案,他们可以使用access库功能。

This is explained in man 2 access : 这在man 2 access解释:

The check is done using the calling process's real UID and GID, rather than the effective IDs as is done when actually attempting an operation (eg, open(2) ) on the file.… 检查是使用调用进程的实际UID和GID完成的,而不是像实际在文件上尝试执行操作(例如open(2) )时所执行的有效ID。…

This allows set-user-ID programs and capability-endowed programs to easily determine the invoking user's authority. 这允许设置用户ID程序和赋予功能的程序轻松确定调用用户的权限。 In other words, access() does not answer the "can I read/write/execute this file?" 换句话说,access()无法回答“我可以读取/写入/执行此文件吗?” question. 题。 It answers a slightly different question: "(assuming I'm a setuid binary) can the user who invoked me read/write/execute this file?", which gives set-user-ID programs the possibility to prevent malicious users from causing them to read files which users shouldn't be able to read. 它回答了一个稍有不同的问题:“(假设我是setuid二进制文件)调用我的用户可以读取/写入/执行此文件吗?”,这使set-user-ID程序可以防止恶意用户导致它们出现。读取用户不应该读取的文件。

thanks rici's answer!I completed it as follow 感谢rici的回答!我按如下完成了它

int accesswriteable(char const *path)
{
    if(access(path, F_OK))
    {
        return 1;
    }
    FILE *fp = fopen(path, "a");
    if(fp == NULL)
    {
        return 1;
    }
    fclose(fp);
    return 0;
}

#define PATH_WRITE_ABLE(path) (accesswriteable(path) == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM