The SUID, or Set User ID, is a special file permission that allows a user to execute a file with the permissions of the file's owner, rather than their own. This is particularly useful for commands that need to perform tasks as the root user, but are executed by a regular user. When an executable file has the SUID bit set, the process that is created upon execution has the effective user ID of the file's owner.
Example 1: Setting SUID on a File
# Create an empty file
touch suid_example
# Set the SUID bit using the symbolic method
chmod u+s suid_example
# Verify the permissions
ls -l suid_example
Explanation: This code first creates a new file named suid_example. Then, chmod u+s is used to add the SUID permission for the user. The ls -l command is then used to display the file's permissions, which will now show an 's' in the user's execute permission slot, indicating that the SUID bit is set.
Example 2: Setting SUID using Octal Notation
# Create another file for this example
touch suid_octal_example
# Set read, write, and execute for the owner, and read-only for group and others, plus the SUID bit
chmod 4744 suid_octal_example
# Check the permissions
ls -l suid_octal_example
Explanation: In this example, we use the octal notation 4744 to set the permissions. The '4' at the beginning represents the SUID bit. The following three digits, '744', set the standard read, write, and execute permissions for the user, group, and others respectively.
Example 3: Finding all SUID files on the system
# Search the entire filesystem for files with the SUID bit set
find / -perm /4000
Explanation: The find command is a powerful utility for locating files. In this case, find / starts the search from the root directory. The -perm /4000 option tells find to look for files where the SUID bit is set.
Example 4: Removing SUID from a file
# Assume suid_example has the SUID bit set
# Remove the SUID bit using the symbolic method
chmod u-s suid_example
# Verify the permissions have been changed
ls -l suid_example
Explanation: To remove the SUID permission, the chmod u-s command is used. This command specifically removes the 's' bit from the user's permissions for the suid_example file. The subsequent ls -l command will confirm that the 's' is no longer present.
Example 5: SUID on a copy of the passwd command
# Copy the passwd command to the home directory
cp /usr/bin/passwd ~/mypasswd
# Set the SUID bit on the copied file
chmod u+s ~/mypasswd
# Check the permissions of the new file
ls -l ~/mypasswd
Explanation: This example demonstrates setting SUID on a copy of a system utility. The passwd command requires root privileges to modify the /etc/shadow file. By copying it and setting the SUID bit, a user could potentially run it with root privileges, highlighting a potential security risk if not managed properly.
Example 6: Invalid SUID on a script
# Create a simple shell script
echo '#!/bin/bash' > my_script.sh
echo 'whoami' >> my_script.sh
# Make the script executable
chmod +x my_script.sh
# Attempt to set the SUID bit
chmod u+s my_script.sh
# Check the permissions
ls -l my_script.sh
Explanation: Modern Linux systems, including RHEL 8, ignore the SUID bit on shell scripts for security reasons. While the 's' permission will appear in the ls -l output, when the script is executed, it will run with the permissions of the user who started it, not the owner of the script.
Example 7: Command differences in other UNIX-like systems
# On RHEL/Linux
chmod u+s filename
# On older UNIX systems (like some versions of Solaris), a different command might be used
# While `chmod` is standard, the way permissions are viewed or managed at a lower level could differ.
# For instance, `truss` on Solaris is similar to `strace` on Linux for tracing system calls
# to see the effect of SUID during execution.
strace -e execve ./suid_executable # RHEL/Linux
truss ./suid_executable # Solaris
Explanation: The core command for changing permissions, chmod, is consistent across most UNIX-like systems. However, the tools for observing the effects of SUID, such as system call tracers, may differ. On RHEL 8 and other Linux distributions, strace is used, while on systems like Solaris, truss would be the equivalent tool to see how the effective user ID changes upon execution of an SUID binary.