From c483c787fcf4d98f9cf86e2422ba8e4e7226b6a1 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 7 Feb 2020 16:34:12 -0800 Subject: fchmodat: AT_SYMLINK_NOFOLLOW fix for non-symlinks Fix lchmod, and fchmodat with AT_SYMLINK_NOFOLLOW, so that they act like chmod on non-symlinks. * NEWS: * doc/glibc-functions/lchmod.texi (lchmod): * doc/posix-functions/fchmodat.texi (fchmodat): Mention this. * lib/fchmodat.c: Define __need_system_sys_stat_h before including config.h, and undef it after including sys/stat.h the first time. Include fcntl.h, stdio.h, unistd.h, intprops.h, and include sys/stat.h a second time after defining orig_fchmodat. (orig_fchmodat) [HAVE_FCHMODAT]: New function. (fchmodat) [HAVE_FCHMODAT]: Work around the AT_SYMLINK_NOFOLLOW bug. * lib/lchmod.c: New file. * lib/sys_stat.in.h (fchmodat, lchmod): Support replacing these functions. * m4/fchmodat.m4 (gl_FUNC_FCHMODAT): If fchmodat exists, test that AT_SYMLINK_NOFOLLOW works on non-symlinks. * m4/lchmod.m4 (gl_FUNC_LCHMOD): Check for lstat. Test that lchmod works on non-symlinks. * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Default REPLACE_FCHMODAT and REPLACE_LCHMOD to 0. * modules/fchmodat (Depends-on): Add fstatat, intprops, lchmod, unistd. (Depends-on, configure.ac): Check REPLACE_FCHMODAT too. * modules/lchmod (Files): Add lib/lchmod.c. (Depends-on): Add errno, fcntl-h, fchmodat, intprops, lstat, unistd. (configure.ac): Compile lchmod.c if needed. (lib_SOURCES): Add lchmod.c. * modules/sys_stat (sys/stat.h): Substitute REPLACE_FCHMODAT and REPLACE_LCHMOD. * tests/test-fchmodat.c: Include fcntl.h, sys/stat.h. (main): Test fchmodat with AT_SYMLINK_NOFOLLOW on non-symlinks. --- lib/lchmod.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lib/lchmod.c (limited to 'lib/lchmod.c') diff --git a/lib/lchmod.c b/lib/lchmod.c new file mode 100644 index 0000000000..cc260ce4dc --- /dev/null +++ b/lib/lchmod.c @@ -0,0 +1,72 @@ +/* Implement lchmod on platforms where it does not work correctly. + + Copyright 2020 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* written by Paul Eggert */ + +#include + +#include + +#include +#include +#include +#include + +#include + +/* Work like lchmod, except when FILE is a symbolic link. + In that case, set errno to EOPNOTSUPP and return -1. */ + +int +lchmod (char const *file, mode_t mode) +{ +#if HAVE_FCHMODAT + return fchmodat (AT_FDCWD, file, mode, AT_SYMLINK_NOFOLLOW); +#elif defined O_PATH && defined AT_FDCWD + int fd = openat (AT_FDCWD, file, O_PATH | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) + return fd; + static char const fmt[] = "/proc/self/fd/%d"; + char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)]; + sprintf (buf, fmt, fd); + int chmod_result = chmod (buf, mode); + int chmod_errno = errno; + close (fd); + if (chmod_result == 0) + return chmod_result; + if (chmod_errno != ENOENT) + { + errno = chmod_errno; + return chmod_result; + } + /* /proc is not mounted; fall back on racy implementation. */ +#endif + +#if HAVE_LSTAT + struct stat st; + int lstat_result = lstat (file, &st); + if (lstat_result != 0) + return lstat_result; + if (S_ISLNK (st.st_mode)) + { + errno = EOPNOTSUPP; + return -1; + } +#endif + + return chmod (file, mode); +} -- cgit v1.2.1