作者:手机用户2502854133 | 来源:互联网 | 2023-09-15 14:34
Thisisaset-root-uidprogram这是一个集根uid程序。$ls-l-rwsr-sr-x1rootroot74062011-12-1322:37.x
This is a set-root-uid program
这是一个集根uid程序。
$ls -l
-rwsr-sr-x 1 root root 7406 2011-12-13 22:37 ./x*
The source code:
int main(void) {
printf(
" UID GID \n"
"Real %d Real %d \n"
"Effective %d Effective %d \n",
getuid (), getgid (),
geteuid(), getegid()
);
seteuid(600);
printf(
" UID GID \n"
"Real %d Real %d \n"
"Effective %d Effective %d \n",
getuid (), getgid (),
geteuid(), getegid()
);
setuid(1000);
printf(
" UID GID \n"
"Real %d Real %d \n"
"Effective %d Effective %d \n",
getuid (), getgid (),
geteuid(), getegid()
);
setuid(0); // HOW DOES THIS SUCCEED IN SETTING THE EUID BACK TO 0
printf(
" UID GID \n"
"Real %d Real %d \n"
"Effective %d Effective %d \n",
getuid (), getgid (),
geteuid(), getegid()
);
return 0 ;
}
OUTPUT
UID GID
Real 1000 Real 1000
Effective 0 Effective 0
UID GID
Real 1000 Real 1000
Effective 600 Effective 0
UID GID
Real 1000 Real 1000
Effective 1000 Effective 1000
UID GID
Real 1000 Real 1000
Effective 0 Effective 1000
My question
The man page states that setuid will change the real,saved and effective uid. So after the calling setuid(1000)
, all three change to 1000
. How is that setuid(0)
let's me change euid
to 0
?
手册页声明setuid将更改真实的、保存的和有效的uid。在调用setuid(1000)之后,三个都变成了1000。如何让setuid(0)把euid改为0?
3 个解决方案
25
There are two cases,
有两种情况下,
- You want to temporarily drop root privilege while executing setuid program
- 您希望在执行setuid程序时暂时删除根特权
- You want to permanently drop root privilege while executing setuid program...
- 您希望在执行setuid程序时永久删除根特权……
- You can temporarily do it by setting the euid to the real user id and then changing the uid to anything you want.And later when you need the root privilege back you can setuid to root and the effective userid will change back to root. This is because the saved user id is not changed.
- 您可以通过将euid设置为真正的用户id,然后将uid更改为您想要的任何东西,从而暂时实现它。之后,当您需要返回root权限时,您可以将setuid设置为root,而有效的userid将更改为root。这是因为保存的用户id没有更改。
- You can drop privilege permanently by changing the uid straight away to a lesser privileged user id. After this no matter what you cannot get back the root privilege.
- 您可以将uid直接更改为较小的特权用户id,从而永久地删除特权。
Case 1:
案例1:
After a setuid program starts executing
在一个setuid程序开始执行之后
1.seteuid(600);
2.setuid(1000);
3.setuid(0);
For this case the root privilege can be gained back again.
对于这种情况,可以再次获得根特权。
+----+------+------------+
| uid|euid |saved-uid |
|----|------|------------|
1.|1000| 0 | 0 |
2.|1000| 600 | 0 |
3.|1000| 1000 | 0 |
4.|1000| 0 | 0 |
| | | |
+------------------------+
Case 2:
案例2:
After a setuid program starts executing,
在一个setuid程序开始执行后,
1.setuid(1000);
2.setuid(0);
+----+------+------------+
| uid|euid |saved-uid |
|----|------|------------|
1.|1000|0 | 0 |
2.|1000|1000 | 1000 |
| | | |
+------------------------+
In this case you cannot get back the root privilege. This can be verified by the following command,
在这种情况下,您不能返回root特权。这可以通过以下命令进行验证,
cat /proc/PROCID/task/PROCID/status | less
猫/proc/PROCID/task/PROCID/status |少
Uid: 1000 0 0 0
Gid: 1000 0 0 0
This command will display a Uid and Gid and it will have 4 fields( the first three fields are the one we are concerned with). Something like the above
这个命令将显示一个Uid和Gid,它将有4个字段(前3个字段是我们关心的)。类似上面的
The three fields represent uid,euid and saved-user-id. You can introduce a pause (an input from user) in your setuid program and check for each step the cat /proc/PROCID/task/PROCID/status | less
command. During each step you can check the saved uid getting changed as mentioned.
这三个字段表示uid、euid和saved-user-id。您可以在setuid程序中引入一个暂停(来自用户的输入),并检查每一步的cat / procid/procid/task/procid/status |命令。在每个步骤中,您都可以检查保存的uid是否被更改。
If you're euid is root and you change the uid, the privileges gets dropped permanently.If effective user id is not root then saved user id is never touched and you can regain the root privilege back anytime you want in your program.
如果euid是root,而您更改uid,特权将被永久删除。如果有效的用户id不是根用户id,那么保存的用户id就不会被触及,您可以在程序中随时恢复根特权。