出于保存的用户ID的目的进行扩展,它还允许使用提升特权运行的进程,然后在需要时将其删除以恢复为提升特权。
假设ID为1001的用户运行setuid-root程序。程序启动时,各种用户ID设置如下:
在启动时将保存的用户ID设置为有效用户ID,可以使用户在需要时随时返回该用户ID。
此时,该程序具有根特权。该程序然后可以执行以下操作:
// perform privileged commands
seteuid(1001); // drop privileges, euid is now 1001
// perform unprivileged commands
seteuid(0); // raise privileges, euid is now 0, allowed because saved UID is 0
// perform more privileged commands
seteuid(1001); // drop privileges, euid is now 1001
// perform more unprivileged commands
Run Code Online (Sandbox Code Playgroud)