美文网首页
setuid and getuid

setuid and getuid

作者: 413x | 来源:发表于2018-05-09 09:49 被阅读0次

setuid and getuid

获取某个用户的UID

id -u <username>

setuid

  If  the user is root or the pro‐gram is  set-user-ID-root,  spe‐cial   care   must   be   taken:setuid()  checks  the  effective user  ID of the caller and if it is the superuser,  all  process-related  user  ID's  are  set to uid.  After this  has  occurred,it is impossible for the program to regain root privileges.

setuid会检查调用者的euid,如果是superuser所有的进程相关的uid都会被设置,
在这之后进程不能再获取root特权

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

#define hemmy_id 1001

void ChangeId(uid_t id)
{
    setuid(id);
}

void ShowIdInfo()
{
    printf("-----------------show user info----------------\n");
    uid_t uid=getuid();
    gid_t gid=getgid();
    uid_t euid=geteuid();
    gid_t egid=getegid();
    printf("uid=%d\n",uid);
    printf("gid=%d\n",gid);
    printf("euid=%d\n",euid);
    printf("egid=%d\n",egid);
    printf("-----------------------end----------------------\n\n");
}

int main(int argc, char** argv,char** envr)
{
    ShowIdInfo();  
    setuid(1001);
    ShowIdInfo();  
    setuid(0);
    ShowIdInfo();
}
➜  linux_kernel_api sudo ./g
-----------------show user info----------------
uid=0
gid=0
euid=0
egid=0
-----------------------end----------------------

-----------------show user info----------------
uid=1001
gid=0
euid=1001
egid=0
-----------------------end----------------------

-----------------show user info----------------
uid=1001
gid=0
euid=1001
egid=0
-----------------------end----------------------

相关文章

网友评论

      本文标题:setuid and getuid

      本文链接:https://www.haomeiwen.com/subject/vfqnrftx.html