coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

readlink: Behaviour differs from GNU Coreutils when path exists but cannot be read

Open aidanharris opened this issue 2 years ago • 2 comments

Running as a non-root user that does not have permission to read /proc/1/exe (since the init process is started as root):

Expected:

$ readlink -f /proc/1/exe /etc/hosts
/etc/hosts

Actual:

$ readlink -f /proc/1/exe /etc/hosts
/proc/1/exe
/etc/hosts

We do see the error raised correctly when Uutils doesn't canonicalize the path:

$ readlink -v /proc/1/exe /etc/hosts
readlink: /proc/1/exe: Permission denied

But it's returning early instead of processing the rest of the files:

GNU Coreutils

$ readlink -v -e /proc/1/exe /etc/hosts
readlink: /proc/1/exe: Permission denied
/etc/hosts

Running

aidanharris avatar Sep 08 '24 22:09 aidanharris

I wonder whether GNU readlink -f /proc/1/exe /etc/hosts displaying only /etc/hosts is right. The description for -f on https://www.gnu.org/software/coreutils/manual/html_node/readlink-invocation.html#readlink-invocation is:

Activate canonicalize mode. If any component of the file name except the last one is missing or unavailable, readlink produces no output and exits with a nonzero exit code. A trailing slash is ignored.

Aren't all the components of the file name /proc/1/exe except the last one present and available?

samueltardieu avatar Sep 14 '24 08:09 samueltardieu

Aren't all the components of the file name /proc/1/exe except the last one present and available?

@samueltardieu Yes, I tried to show /proc and proc/1 on my machine, both permission of /proc and /proc/1 are 555.

$ ls -ld /proc 
dr-xr-xr-x 200 root root 0 Nov 24 13:40 /proc
$ ls -ld /proc/1
dr-xr-xr-x 9 root root 0 Nov 24 13:40 /proc/1

Even though I tried to read a non-exist file along with /etc/hosts, GNU readlink also only displaying /etc/hosts.

$ readlink -f /tmp/not_exist/file /etc/hostname
/etc/hostname

I think that GNU won't exit immediately when it encounter an error on processing the filename. Instead, it will try to process the next filename. Finally, GNU may exit with nonzero code if any of the previous processing is failed.

If you think my idea is correct, I think I can help you fix this issue.

zgzhong avatar Nov 24 '24 16:11 zgzhong