简体   繁体   English

setuid()之后如何恢复到原始状态/用户?

[英]how to get back to original status/user after setuid()?

I have a program to run as root, and during execution this program will do a few things as different uers, so I wanted to use a serial of setuid()s. 我有一个以root用户身份运行的程序,在执行过程中,该程序将作为不同的用户来做一些事情,因此我想使用一系列setuid()。 But, I found that, after setuid(user1), I become user1 and thus don't have the privilege to do setuid(user2). 但是,我发现在setuid(user1)之后,我成为了user1,因此没有权限执行setuid(user2)。

How can I get back to root so that I can do setuid(user2)? 如何才能回到root用户,以便可以执行setuid(user2)?

Thanks. 谢谢。

You can't, Read the man : setuid 你不能,读那个男人: setuid

But you can try to chmod your file then you will be able to call setuid(0) to come back as yout first uid 但是您可以尝试对文件进行chmod修改,然后可以调用setuid(0)作为第一个uid返回

Use fork , let the child setuid and perform whatever actions that needs to be done as the second user. 使用fork ,让子setuid并执行第二个用户需要执行的任何操作。 The root parent waits for the child and continues when the child has finished executing. 根父级将等待子级,并在子级完成执行后继续。

childpid = fork();
if (childpid < 0) {
    // fork failed
} 
if (childpid == 0) {
  // Child
  setuid(user1);
  prepareUser1();  // Do some stuff as user1.
  exit(0);         // Done as user1
} else {
  // parent: wait for child to finish
  waitpid(childpid);
}
// Parent continues as root...

The setuid says the following: setuid表示以下内容:

a set-user-ID-root program wishing to temporarily drop root privileges, assume the identity of a non-root user, and then regain root privileges afterwards cannot use setuid(). 希望暂时放弃root特权,采用非root用户身份并随后重新获得root特权的set-user-ID-root程序不能使用setuid()。 You can accomplish this with seteuid(2) 您可以使用seteuid(2)完成此操作

Meaning that you cannot use setuid() become root as you are unprivileged user. 这意味着您无法使用setuid()成为root用户,因为您是非特权用户。 You have to use seteuid() to become a root user. 您必须使用seteuid()才能成为root用户。

Try this sample program to use seteuid and change the privileges. 尝试使用此示例程序来使用seteuid并更改特权。

You cannot. 你不能。 By design, once you drop root privileges, you cannot get it back 根据设计,一旦放弃root特权,您将无法找回它

man page says: 手册页说:

If the user is root or the program is set-user-ID-root, special care must be taken. 如果用户是root或程序是set-user-ID-root,则必须格外小心。 The setuid() function checks the effective user ID of the caller and if it is the superuser, all process-related user ID's are set to uid. setuid()函数检查调用方的有效用户ID,如果它是超级用户,则所有与进程相关的用户ID均设置为uid。 After this has occurred, it is impossible for the program to regain root privileges 发生这种情况后,该程序将无法重新获得root特权。

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

相关问题 如何在非root用户的程序中设置setuid位? - How to set the setuid bit in a program of a non-root user? setuid()之后失去能力 - losing capabilities after setuid() 用户修改后如何使用旧驱动程序中的 PE 标头获取原始文件类型 - how can I get the original file type after user modification using PE header in legacy driver 在移位和 OR 操作后取回原始位 - Retrieve Back the Original Bits after a Bitshift and an OR operation 如何从root用户使用setuid()成为用户,并有可能稍后再次成为root用户? - How to use setuid() from root to become user, with the possibility of becoming root again later? 重定向后如何返回标准输出? - How to get back to stdout after redirection? bash不能通过setuid程序获得root权限 - bash does not get root permissions with setuid program setuid() 当指定的用户 ID 与有效用户 ID 相同时返回 -1 - setuid() return -1 when specified user ID is the same as the effective user ID 在 C 语言中更新后如何恢复到原始数组? 像使用临时指针或任何其他方式来恢复? - How to revert back to original array after updating it in C lang? like using a temp pointer or any other way to revert? 在执行&#39;main&#39;之外的空格后如何回到&#39;main&#39;的顶部 - How to get back to the top of 'main' after executing a void outside 'main'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM