summaryrefslogtreecommitdiff
path: root/lib/lchmod.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2020-02-13 10:41:10 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2020-02-13 10:41:54 -0800
commitd17d3a3cc55500709c22c7d0ade24a0845aa17b9 (patch)
tree7c1ebef1fa2efc343c37d1d93ea3643db8a504e5 /lib/lchmod.c
parent4fcedca004fd13aecb5c6f235a988a5548bcb9a4 (diff)
downloadgnulib-d17d3a3cc55500709c22c7d0ade24a0845aa17b9.tar.gz
fchmodat, lchmod: port to buggy Linux filesystems
Problem reported by Florian Weimer in: https://www.sourceware.org/ml/libc-alpha/2020-02/msg00534.html * lib/fchmodat.c (fchmodat): * lib/lchmod.c (lchmod): Don’t assume that chmod on the O_PATH-opened fd will do the right thing on a symbolic link. * lib/fchmodat.c (fchmodat): Don’t attempt to special-case any flag value other than AT_SYMLINK_NOFOLLOW.
Diffstat (limited to 'lib/lchmod.c')
-rw-r--r--lib/lchmod.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/lib/lchmod.c b/lib/lchmod.c
index 5fc658023c..c7191c07d8 100644
--- a/lib/lchmod.c
+++ b/lib/lchmod.c
@@ -37,10 +37,28 @@ lchmod (char const *file, mode_t mode)
#if HAVE_FCHMODAT
return fchmodat (AT_FDCWD, file, mode, AT_SYMLINK_NOFOLLOW);
#else
-# if defined O_PATH && defined AT_FDCWD
+# if defined AT_FDCWD && defined O_PATH && defined AT_EMPTY_PATH
int fd = openat (AT_FDCWD, file, O_PATH | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0)
return fd;
+
+ /* Use fstatat because fstat does not work on O_PATH descriptors
+ before Linux 3.6. */
+ struct stat st;
+ if (fstatat (fd, "", &st, AT_EMPTY_PATH) != 0)
+ {
+ int stat_errno = errno;
+ close (fd);
+ errno = stat_errno;
+ return -1;
+ }
+ if (S_ISLNK (st.st_mode))
+ {
+ close (fd);
+ errno = EOPNOTSUPP;
+ return -1;
+ }
+
static char const fmt[] = "/proc/self/fd/%d";
char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
sprintf (buf, fmt, fd);
@@ -54,10 +72,8 @@ lchmod (char const *file, mode_t mode)
errno = chmod_errno;
return chmod_result;
}
- /* /proc is not mounted; fall back on racy implementation. */
-# endif
-
-# if HAVE_LSTAT
+ /* /proc is not mounted. */
+# elif HAVE_LSTAT
struct stat st;
int lstat_result = lstat (file, &st);
if (lstat_result != 0)
@@ -69,6 +85,7 @@ lchmod (char const *file, mode_t mode)
}
# endif
+ /* Fall back on chmod, despite the race. */
return chmod (file, mode);
#endif
}