Consider running the following Python code as root:
import os
f=os.open("/etc/shadow", os.O_RDONLY)
os.setuid(65535)
os.open(f"/proc/self/fd/{f}", os.O_RDONLY)
Here is a one-liner convenient for pasting:
python3 -c 'import os; f=os.open("/etc/shadow", os.O_RDONLY); os.setuid(65535); os.open(f"/proc/self/fd/{f}", os.O_RDONLY)'
Given the comment of proc_fd_permission, I would expect this code to succeed. However, I actually observe -EACCES
. Why is this use of /proc/self/fd/N
not permitted and what is the source code comment actually trying to convey?
Update: If the permission only applies to the symlink itself and not the target file, why can I open sockets and deleted files via /proc/self/fd/N
? (e.g. exec 3>foo; echo hello >&3; rm foo; cat /proc/self/fd/3
prints hello
)