Let's say I need to get the file descriptor of a file (or a directory) which has only execution (or search) permission.
The X/Open norm defines a O_SEARCH flag for the open() function. See http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html
However, Linux doesn't. There are only 3 available flags (O_RDONLY, O_WRONLY and O_RDWR). See http://man7.org/linux/man-pages/man2/open.2.html
Why? And how can I get the fd of a directory with only search permission?
Thanks
It turns out Linux doesn't support, yet, this flag, as stated in W. Richard Stevens Stephen A. Rago's book "Advanced Programming in the UNIX Environment" which you can have a look at here Link to the book on google books
Actually, the flag is defined in POSIX, implemented in standard C library (which is in this case glibc, that's why you find it under man 3 open) but is not implemented in Linux kernel (thus not found under man 2 open).
EDIT 1 : Since we use GNU under Linux, it includes specific headers for Linux to be able to make appropriate system calls that are feasable by Linux (in this case, it includes fcntl-linux.h in addition to fcntl.h).
EDIT 2 : Bug ticket https://sourceware.org/bugzilla/show_bug.cgi?id=18228
Please, correct me if I'm wrong!
On Linux, you can obtain an fd for a directory that only has search permission using O_PATH
. On other POSIX systems, you can fork a process and chdir
to the directory; whenever you want to do openat
relative to that directory, you can have the process perform open
for you and send the fd to your main process (AF_UNIX
and SCM_RIGHTS
are a portable way of doing this).
Neither is strictly equivalent to O_SEARCH
in the case where search permissions are revoked from the directory before access. POSIX guarantees that there will be no further permissions checks for an fd opened with O_SEARCH
, but both O_PATH
and chdir
will check on every access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With