简体   繁体   English

setuid不更改uid

[英]setuid not changing uid

I am working in Linux and trying to execute a C program with setuid on Linux. 我在Linux上工作,试图在Linux上使用setuid执行C程序。 Here is my code: 这是我的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

int main()
{
    int ret;
    fprintf ( stderr, "Orig: %d   Effective: %d\n", getuid(), geteuid() );

    ret = setuid ( 122 );
    if ( ret < 0 )
    {
        perror ( "Problem in setuid  " );
        return ( 1 );
    }

    fprintf ( stderr, "UID : %d   Effective: %d\n", getuid(), geteuid() );
    execlp ( "/bin/id", "/bin/id", NULL );

    fprintf ( stderr, "Problem executing execlp\n" );
    return ( 0 );
}

Upon execution, the UID is not changed. 执行后,UID不会更改。 The output is: 输出为:

[hauschild@hoare7 ~]$ ~sanjiv/bin/a.out
Orig: 155   Effective: 122
UID : 155   Effective: 122
uid=155(hauschild) gid=100(users) euid=122(sanjiv) groups=100(users)
[hauschild@hoare7 ~]$ 

I have looked at other questions in SO but unable to figure this one out. 我已经看过SO中的其他问题,但是无法弄清楚这个问题。 The permissions on executable are rwsr-sr-x . 可执行文件的权限为rwsr-sr-x Notice how the code runs to completion and the exit status is reported as 0 . 请注意,代码如何运行完成,并且退出状态报告为0 However, when I run it through strace , it gives me an error on setuid and exists with a 1 . 但是,当我通过strace运行它时,它在setuid上给我一个错误,并且存在1 as follows: 如下:

geteuid()                               = 155
getuid()                                = 155
write(2, "Orig: 155   Effective: 155\n", 27Orig: 155   Effective: 155
) = 27
setuid(122)                             = -1 EPERM (Operation not permitted)
write(2, "Problem in setuid  : Operation n"..., 45Problem in setuid  : Operation not permitted
) = 45
exit_group(1)                           = ?
+++ exited with 1 +++

Can anyone see what I could be doing wrong? 谁能看到我做错了什么?

In POSIX and Linux, setuid() only sets the effective UID of the process, unless the effective UID is root, in which case it also sets the real UID and the saved set-user-ID. 在POSIX和Linux中,除非有效的UID是root,否则setuid()仅设置进程的有效UID,在这种情况下,它还要设置实际的UID和保存的set-user-ID。 To set the real UID, use setreuid() . 要设置实际的UID,请使用setreuid() BSD setuid sets all of them regardless of the effective UID of the process. 无论进程的有效UID如何,BSD setuid设置所有它们。

To set the real UID, use setreuid : 要设置实际的UID,请使用setreuid

ret = setreuid(122, 122);

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

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