What is a good way to simulate O_NOFOLLOW on systems without this flag?
Posted
by Daniel Trebbien
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Trebbien
Published on 2010-05-26T22:48:03Z
Indexed on
2010/05/26
22:51 UTC
Read the original article
Hit count: 161
I would like to safely be able to simulate open
with O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW
and O_CREAT | O_WRONLY | O_APPEND | O_NOFOLLOW
on systems that do not support O_NOFOLLOW
. I can somewhat achieve what I am asking for with:
struct stat lst;
if (lstat(filename, &lst) != -1 && S_ISLNK(lst.st_mode)) {
errno = ELOOP;
return -1;
}
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, mode);
but then I introduce a race condition and possibly a security problem.
I thought about maybe creating a dummy file with only the user being able to write, kind of like touch
ing filename
, doing the lstat
check, and then using chmod
after I finish writing (to correct the file mode bits), but I could be overlooking something major (e.g. if the file at filename
exists, is not a regular file, or is already a symbolic link).
What do you think?
© Stack Overflow or respective owner