euid, ruid, suid
User Identification Variables
ruid: The real user ID denotes the user who initiated the process.[1]euid: Known as the effective user ID, it represents the user identity utilized by the system to ascertain process privileges. Generally,euidmirrorsruid, barring instances like a SetUID binary execution (when the set-user-ID transition is honored), whereeuidassumes the file owner’s identity, thus granting specific operational permissions.[1][5]suid: This saved user ID is pivotal when a high-privilege process (typically running as root) needs to temporarily relinquish its privileges to perform certain tasks, only to later reclaim its initial elevated status.[1]
Important Note
An unprivileged process can only modify its euid to match the current ruid, euid, or suid.[3]
Understanding set*uid Functions
setuid: Contrary to initial assumptions,setuidsets the calling process’seuid. For a privileged process, it also setsruidandsuidto the specified user; after all IDs are set to root, the process cannot regain a previous identity usingsetuid. Detailed insights can be found in the setuid man page.[2]setreuidandsetresuid:setreuidchangesruidandeuid, whilesetresuidchanges all three IDs. For an unprivileged process,setresuidrestricts each target to the currentruid,euid, orsuid;setreuidrestrictseuidto those values andruidto the currentruidoreuid. A process withCAP_SETUIDcan assign arbitrary values to the IDs supported by each call. More information can be gleaned from the setresuid man page and the setreuid man page.[3][4]
These functionalities are designed not as a security mechanism but to facilitate the intended operational flow, such as when a program adopts another user’s identity by altering its effective user ID.[1][3][4]
Notably, a privileged call to setuid can assign all three IDs, whereas setreuid and setresuid expose different controls; differentiating these functions is crucial for understanding user-ID transitions.[1][2][3][4]
Program Execution Mechanisms in Linux
execve System Call
- Functionality:
execveinitiates a program, determined by the first argument. It takes two array arguments,argvfor arguments andenvpfor the environment.[5] - Behavior: It retains the memory space of the caller but refreshes the stack, heap, and data segments. The program’s code is replaced by the new program.[5]
- User ID Preservation:
- Documentation: Detailed information can be found on the
execveman page.[5]
system Function
- Functionality: Unlike
execve,systembehaves as if it creates a child process usingforkand executes the command within that child process usingexecl.[6] - Command Execution: Executes the command via
shwithexecl("/bin/sh", "sh", "-c", command, (char *) NULL);.[6] - Behavior: As
execlis anexec-family call, it operates similarly toexecvebut in the context of a new child process.[1][5][6] - Documentation: Further insights can be obtained from the
systemman page.[6]
Behavior of bash and sh with SUID
bash:- Has a
-poption influencing howeuidandruidare treated.[7] - Without
-p,bashsetseuidtoruidif they initially differ.[7] - With
-p, the initialeuidis preserved.[7] - More details can be found on the
bashman page.[7]
- Has a
sh:- POSIX
shdoes not define a Bash-style-pprivilege-preservation option.[8] - Its POSIX option list includes
-i, which selects interactive mode and may be rejected when the real and effective IDs differ.[8] - Additional information is available on the
shman page.[8]
- POSIX
These mechanisms, distinct in their operation, offer a versatile range of options for executing and transitioning between programs, with specific nuances in how user IDs are managed and preserved.
Testing User ID Behaviors in Executions
Examples taken from https://0xdf.gitlab.io/2022/05/31/setuid-rabbithole.html#testing-on-jail, check it for further information.[1]
Case 1: Using setuid with system
Objective: Understanding the effect of setuid in combination with system and bash as sh.
C Code:
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setuid(1000);
system("id");
return 0;
}
Compilation and Permissions:
oxdf@hacky$ gcc a.c -o /mnt/nfsshare/a;
oxdf@hacky$ chmod 4755 /mnt/nfsshare/a
bash-4.2$ $ ./a
uid=99(nobody) gid=99(nobody) groups=99(nobody) context=system_u:system_r:unconfined_service_t:s0
Analysis:
ruidandeuidstart as 99 (nobody) and 1000 (frank) respectively.- In this unprivileged context,
setuid(1000)leavesruidat 99 andeuidat 1000.[1] systemexecutes/bin/bash -c iddue to the symlink from sh to bash.bash, without-p, adjustseuidto matchruid, resulting in both being 99 (nobody).[1]
Case 2: Using setreuid with system
C Code:
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setreuid(1000, 1000);
system("id");
return 0;
}
Compilation and Permissions:
oxdf@hacky$ gcc b.c -o /mnt/nfsshare/b; chmod 4755 /mnt/nfsshare/b
Execution and Result:
bash-4.2$ $ ./b
uid=1000(frank) gid=99(nobody) groups=99(nobody) context=system_u:system_r:unconfined_service_t:s0
Analysis:
setreuidsets both ruid and euid to 1000.systeminvokes bash, which maintains the user IDs due to their equality, effectively operating as frank.[1]
Case 3: Using setuid with execve
Objective: Exploring the interaction between setuid and execve.
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setuid(1000);
execve("/usr/bin/id", NULL, NULL);
return 0;
}
Execution and Result:
bash-4.2$ $ ./c
uid=99(nobody) gid=99(nobody) euid=1000(frank) groups=99(nobody) context=system_u:system_r:unconfined_service_t:s0
Analysis:
ruidremains 99, but euid is set to 1000, in line with setuid’s effect.[1]
C Code Example 2 (Calling Bash):
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setuid(1000);
execve("/bin/bash", NULL, NULL);
return 0;
}
Execution and Result:
bash-4.2$ $ ./d
bash-4.2$ $ id
uid=99(nobody) gid=99(nobody) groups=99(nobody) context=system_u:system_r:unconfined_service_t:s0
Analysis:
- Although
euidis set to 1000 bysetuid,bashresets euid toruid(99) due to the absence of-p.[1]
C Code Example 3 (Using bash -p):
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
char *const paramList[10] = {"/bin/bash", "-p", NULL};
setuid(1000);
execve(paramList[0], paramList, NULL);
return 0;
}
Execution and Result:
bash-4.2$ $ ./e
bash-4.2$ $ id
uid=99(nobody) gid=99(nobody) euid=1000(frank)