]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/fchmodat.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / fchmodat.c
1 /* Change the protections of file relative to open directory. Linux version.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <not-cancel.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sysdep.h>
26 #include <unistd.h>
27
28 int
29 fchmodat (int fd, const char *file, mode_t mode, int flag)
30 {
31 if (flag == 0)
32 return INLINE_SYSCALL (fchmodat, 3, fd, file, mode);
33 else if (flag != AT_SYMLINK_NOFOLLOW)
34 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
35 else
36 {
37 /* The kernel system call does not have a mode argument.
38 However, we can create an O_PATH descriptor and change that
39 via /proc (which does not resolve symbolic links). */
40
41 int pathfd = __openat_nocancel (fd, file,
42 O_PATH | O_NOFOLLOW | O_CLOEXEC);
43 if (pathfd < 0)
44 /* This may report errors such as ENFILE and EMFILE. The
45 caller can treat them as temporary if necessary. */
46 return pathfd;
47
48 /* Use fstatat because fstat does not work on O_PATH descriptors
49 before Linux 3.6. */
50 struct stat64 st;
51 if (__fstatat64 (pathfd, "", &st, AT_EMPTY_PATH) != 0)
52 {
53 __close_nocancel (pathfd);
54 return -1;
55 }
56
57 /* Some Linux versions with some file systems can actually
58 change symbolic link permissions via /proc, but this is not
59 intentional, and it gives inconsistent results (e.g., error
60 return despite mode change). The expected behavior is that
61 symbolic link modes cannot be changed at all, and this check
62 enforces that. */
63 if (S_ISLNK (st.st_mode))
64 {
65 __close_nocancel (pathfd);
66 __set_errno (EOPNOTSUPP);
67 return -1;
68 }
69
70 /* For most file systems, fchmod does not operate on O_PATH
71 descriptors, so go through /proc. */
72 char buf[32];
73 if (__snprintf (buf, sizeof (buf), "/proc/self/fd/%d", pathfd) < 0)
74 {
75 /* This also may report strange error codes to the caller
76 (although snprintf really should not fail). */
77 __close_nocancel (pathfd);
78 return -1;
79 }
80
81 int ret = __chmod (buf, mode);
82 if (ret != 0)
83 {
84 if (errno == ENOENT)
85 /* /proc has not been mounted. Without /proc, there is no
86 way to upgrade the O_PATH descriptor to a full
87 descriptor. It is also not possible to re-open the
88 file without O_PATH because the file name may refer to
89 another file, and opening that without O_PATH may have
90 side effects (such as blocking, device rewinding, or
91 releasing POSIX locks). */
92 __set_errno (EOPNOTSUPP);
93 }
94 __close_nocancel (pathfd);
95 return ret;
96 }
97 }
98 libc_hidden_def (fchmodat)