1 | /* Change the protections of file relative to open directory. Linux version. |
2 | Copyright (C) 2006-2024 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 <fd_to_filename.h> |
22 | #include <not-cancel.h> |
23 | #include <stdio.h> |
24 | #include <sys/stat.h> |
25 | #include <sys/types.h> |
26 | #include <sysdep.h> |
27 | #include <unistd.h> |
28 | |
29 | #if !__ASSUME_FCHMODAT2 |
30 | static int |
31 | fchmodat_fallback (int fd, const char *file, mode_t mode, int flag) |
32 | { |
33 | if (flag != AT_SYMLINK_NOFOLLOW) |
34 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); |
35 | |
36 | /* The kernel system call does not have a mode argument. |
37 | However, we can create an O_PATH descriptor and change that |
38 | via /proc (which does not resolve symbolic links). */ |
39 | |
40 | int pathfd = __openat_nocancel (fd, file, |
41 | O_PATH | O_NOFOLLOW | O_CLOEXEC); |
42 | if (pathfd < 0) |
43 | /* This may report errors such as ENFILE and EMFILE. The |
44 | caller can treat them as temporary if necessary. */ |
45 | return pathfd; |
46 | |
47 | /* Use fstatat because fstat does not work on O_PATH descriptors |
48 | before Linux 3.6. */ |
49 | struct __stat64_t64 st; |
50 | if (__fstatat64_time64 (pathfd, "" , &st, AT_EMPTY_PATH) != 0) |
51 | { |
52 | __close_nocancel (pathfd); |
53 | return -1; |
54 | } |
55 | |
56 | /* Some Linux versions with some file systems can actually |
57 | change symbolic link permissions via /proc, but this is not |
58 | intentional, and it gives inconsistent results (e.g., error |
59 | return despite mode change). The expected behavior is that |
60 | symbolic link modes cannot be changed at all, and this check |
61 | enforces that. */ |
62 | if (S_ISLNK (st.st_mode)) |
63 | { |
64 | __close_nocancel (pathfd); |
65 | __set_errno (EOPNOTSUPP); |
66 | return -1; |
67 | } |
68 | |
69 | /* For most file systems, fchmod does not operate on O_PATH |
70 | descriptors, so go through /proc. */ |
71 | struct fd_to_filename filename; |
72 | int ret = __chmod (__fd_to_filename (descriptor: pathfd, storage: &filename), mode); |
73 | if (ret != 0) |
74 | { |
75 | if (errno == ENOENT) |
76 | /* /proc has not been mounted. Without /proc, there is no |
77 | way to upgrade the O_PATH descriptor to a full |
78 | descriptor. It is also not possible to re-open the |
79 | file without O_PATH because the file name may refer to |
80 | another file, and opening that without O_PATH may have |
81 | side effects (such as blocking, device rewinding, or |
82 | releasing POSIX locks). */ |
83 | __set_errno (EOPNOTSUPP); |
84 | } |
85 | __close_nocancel (pathfd); |
86 | return ret; |
87 | } |
88 | #endif |
89 | |
90 | int |
91 | fchmodat (int fd, const char *file, mode_t mode, int flag) |
92 | { |
93 | #if __ASSUME_FCHMODAT2 |
94 | return INLINE_SYSCALL_CALL (fchmodat2, fd, file, mode, flag); |
95 | #else |
96 | if (flag == 0) |
97 | return INLINE_SYSCALL_CALL (fchmodat, fd, file, mode); |
98 | |
99 | int r = INLINE_SYSCALL_CALL (fchmodat2, fd, file, mode, flag); |
100 | if (r != 0 && errno == ENOSYS) |
101 | return fchmodat_fallback (fd, file, mode, flag); |
102 | return r; |
103 | #endif |
104 | } |
105 | libc_hidden_def (fchmodat) |
106 | |