]>
Commit | Line | Data |
---|---|---|
d15b99ac | 1 | /* Test for access to file, relative to open directory. Linux version. |
2b778ceb | 2 | Copyright (C) 2006-2021 Free Software Foundation, Inc. |
d15b99ac RM |
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 | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
d15b99ac | 18 | |
d15b99ac | 19 | #include <fcntl.h> |
d15b99ac RM |
20 | #include <unistd.h> |
21 | #include <sys/types.h> | |
01bd6251 | 22 | #include <sys/stat.h> |
d15b99ac RM |
23 | #include <sysdep.h> |
24 | ||
7c65e900 | 25 | |
d15b99ac | 26 | int |
bd2260a2 | 27 | faccessat (int fd, const char *file, int mode, int flag) |
d15b99ac | 28 | { |
3d3ab573 FW |
29 | int ret = INLINE_SYSCALL_CALL (faccessat2, fd, file, mode, flag); |
30 | #if __ASSUME_FACCESSAT2 | |
31 | return ret; | |
32 | #else | |
33 | if (ret == 0 || errno != ENOSYS) | |
34 | return ret; | |
35 | ||
d15b99ac | 36 | if (flag & ~(AT_SYMLINK_NOFOLLOW | AT_EACCESS)) |
2caca60d | 37 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); |
d15b99ac | 38 | |
809fdf0d | 39 | if ((flag == 0 || ((flag & ~AT_EACCESS) == 0 && ! __libc_enable_secure))) |
e5dee2c8 | 40 | return INLINE_SYSCALL (faccessat, 3, fd, file, mode); |
d15b99ac RM |
41 | |
42 | struct stat64 stats; | |
04986243 | 43 | if (__fstatat64 (fd, file, &stats, flag & AT_SYMLINK_NOFOLLOW)) |
d15b99ac RM |
44 | return -1; |
45 | ||
46 | mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */ | |
3d3ab573 FW |
47 | # if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH |
48 | # error Oops, portability assumptions incorrect. | |
49 | # endif | |
d15b99ac RM |
50 | |
51 | if (mode == F_OK) | |
52 | return 0; /* The file exists. */ | |
53 | ||
54 | uid_t uid = (flag & AT_EACCESS) ? __geteuid () : __getuid (); | |
55 | ||
56 | /* The super-user can read and write any file, and execute any file | |
57 | that anyone can execute. */ | |
58 | if (uid == 0 && ((mode & X_OK) == 0 | |
59 | || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))) | |
60 | return 0; | |
61 | ||
62 | int granted = (uid == stats.st_uid | |
63 | ? (unsigned int) (stats.st_mode & (mode << 6)) >> 6 | |
64 | : (stats.st_gid == ((flag & AT_EACCESS) | |
65 | ? __getegid () : __getgid ()) | |
66 | || __group_member (stats.st_gid)) | |
67 | ? (unsigned int) (stats.st_mode & (mode << 3)) >> 3 | |
68 | : (stats.st_mode & mode)); | |
69 | ||
70 | if (granted == mode) | |
71 | return 0; | |
72 | ||
2caca60d | 73 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EACCES); |
3d3ab573 | 74 | #endif /* !__ASSUME_FACCESSAT2 */ |
d15b99ac | 75 | } |