I have 2 files in the /tmp/.
The first one is an empty file named "file" with permission
-rw-------
and ownerroot:root
.The second file is named "script" which is a simple Ruby script with permission
-rwsr-xr-x
and ownerroot:root
. The contents of the script file is:
#!/usr/bin/env ruby
$-v = true
IO.write( File.join(Dir.pwd, 'file'), 100.times.map { rand(97..122).chr }.join << ?\n )
The file "file" has the same permission as the /etc/shadow
file:
# ls -l /etc/shadow
-rw------- 1 root root 1045 Sep 22 04:13 /etc/shadow
# ls -l file
-rw------- 1 root root 6 Nov 29 12:22 file
The ruby script "script" has the same permission as the passwd
command:
$ ls -l $(type -p passwd)
-rwsr-xr-x 1 root root 63624 Nov 13 22:28 /usr/bin/passwd
# ls -l script
-rwsr-xr-x 1 root root 78 Nov 29 12:22 script
I can run ./script
as root which replaces the contents of the file "file" with some random string.
But when I run ./script
as a non-root user, Ruby raises Errno::EACCES
.
Can I write to the the root writeable file "file" as non-root user by correctly setting up the SUID permission?