| 1 |
/* provide a replacement openat function |
| 2 |
Copyright (C) 2004-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* written by Jim Meyering */ |
| 18 |
|
| 19 |
/* If the user's config.h happens to include <fcntl.h>, let it include only |
| 20 |
the system's <fcntl.h> here, so that orig_openat doesn't recurse to |
| 21 |
rpl_openat. */ |
| 22 |
#define __need_system_fcntl_h |
| 23 |
#include <config.h> |
| 24 |
|
| 25 |
/* Get the original definition of open. It might be defined as a macro. */ |
| 26 |
#include <fcntl.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#undef __need_system_fcntl_h |
| 29 |
|
| 30 |
#if HAVE_OPENAT |
| 31 |
static int |
| 32 |
orig_openat (int fd, char const *filename, int flags, mode_t mode) |
| 33 |
{ |
| 34 |
return openat (fd, filename, flags, mode); |
| 35 |
} |
| 36 |
#endif |
| 37 |
|
| 38 |
/* Specification. */ |
| 39 |
#include <fcntl.h> |
| 40 |
|
| 41 |
#include "openat.h" |
| 42 |
|
| 43 |
#include "cloexec.h" |
| 44 |
|
| 45 |
#include <errno.h> |
| 46 |
#include <stdarg.h> |
| 47 |
#include <stddef.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <string.h> |
| 50 |
#include <sys/stat.h> |
| 51 |
|
| 52 |
#ifndef HAVE_WORKING_O_DIRECTORY |
| 53 |
# define HAVE_WORKING_O_DIRECTORY false |
| 54 |
#endif |
| 55 |
|
| 56 |
#ifndef OPEN_TRAILING_SLASH_BUG |
| 57 |
# define OPEN_TRAILING_SLASH_BUG false |
| 58 |
#endif |
| 59 |
|
| 60 |
#if HAVE_OPENAT |
| 61 |
|
| 62 |
/* Like openat, but support O_CLOEXEC and work around Solaris 9 bugs |
| 63 |
with trailing slash. */ |
| 64 |
int |
| 65 |
rpl_openat (int dfd, char const *filename, int flags, ...) |
| 66 |
{ |
| 67 |
mode_t mode = 0; |
| 68 |
|
| 69 |
if (flags & O_CREAT) |
| 70 |
{ |
| 71 |
va_list arg; |
| 72 |
va_start (arg, flags); |
| 73 |
|
| 74 |
/* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4 |
| 75 |
creates crashing code when 'mode_t' is smaller than 'int'. */ |
| 76 |
mode = va_arg (arg, PROMOTED_MODE_T); |
| 77 |
|
| 78 |
va_end (arg); |
| 79 |
} |
| 80 |
|
| 81 |
/* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename |
| 82 |
ends in a slash, as POSIX says such a filename must name a directory |
| 83 |
<https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>: |
| 84 |
"A pathname that contains at least one non-<slash> character and that |
| 85 |
ends with one or more trailing <slash> characters shall not be resolved |
| 86 |
successfully unless the last pathname component before the trailing |
| 87 |
<slash> characters names an existing directory" |
| 88 |
If the named file already exists as a directory, then |
| 89 |
- if O_CREAT is specified, open() must fail because of the semantics |
| 90 |
of O_CREAT, |
| 91 |
- if O_WRONLY or O_RDWR is specified, open() must fail because POSIX |
| 92 |
<https://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html> |
| 93 |
says that it fails with errno = EISDIR in this case. |
| 94 |
If the named file does not exist or does not name a directory, then |
| 95 |
- if O_CREAT is specified, open() must fail since open() cannot create |
| 96 |
directories, |
| 97 |
- if O_WRONLY or O_RDWR is specified, open() must fail because the |
| 98 |
file does not contain a '.' directory. */ |
| 99 |
bool check_for_slash_bug; |
| 100 |
if (OPEN_TRAILING_SLASH_BUG) |
| 101 |
{ |
| 102 |
size_t len = strlen (filename); |
| 103 |
check_for_slash_bug = len && filename[len - 1] == '/'; |
| 104 |
} |
| 105 |
else |
| 106 |
check_for_slash_bug = false; |
| 107 |
|
| 108 |
if (check_for_slash_bug |
| 109 |
&& (flags & O_CREAT |
| 110 |
|| (flags & O_ACCMODE) == O_RDWR |
| 111 |
|| (flags & O_ACCMODE) == O_WRONLY)) |
| 112 |
{ |
| 113 |
errno = EISDIR; |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
/* With the trailing slash bug or without working O_DIRECTORY, check with |
| 118 |
stat first lest we hang trying to open a fifo. Although there is |
| 119 |
a race between this and opening the file, we can do no better. |
| 120 |
After opening the file we will check again with fstat. */ |
| 121 |
bool check_directory = |
| 122 |
(check_for_slash_bug |
| 123 |
|| (!HAVE_WORKING_O_DIRECTORY && flags & O_DIRECTORY)); |
| 124 |
if (check_directory) |
| 125 |
{ |
| 126 |
struct stat statbuf; |
| 127 |
int fstatat_flags = flags & O_NOFOLLOW ? AT_SYMLINK_NOFOLLOW : 0; |
| 128 |
if (fstatat (dfd, filename, &statbuf, fstatat_flags) < 0) |
| 129 |
{ |
| 130 |
if (! (flags & O_CREAT && errno == ENOENT)) |
| 131 |
return -1; |
| 132 |
} |
| 133 |
else if (!S_ISDIR (statbuf.st_mode)) |
| 134 |
{ |
| 135 |
errno = ENOTDIR; |
| 136 |
return -1; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* 0 = unknown, 1 = yes, -1 = no. */ |
| 141 |
# if GNULIB_defined_O_CLOEXEC |
| 142 |
int have_cloexec = -1; |
| 143 |
# else |
| 144 |
static int have_cloexec; |
| 145 |
# endif |
| 146 |
|
| 147 |
int fd = orig_openat (dfd, filename, |
| 148 |
flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode); |
| 149 |
|
| 150 |
if (flags & O_CLOEXEC) |
| 151 |
{ |
| 152 |
if (! have_cloexec) |
| 153 |
{ |
| 154 |
if (0 <= fd) |
| 155 |
have_cloexec = 1; |
| 156 |
else if (errno == EINVAL) |
| 157 |
{ |
| 158 |
fd = orig_openat (dfd, filename, flags & ~O_CLOEXEC, mode); |
| 159 |
have_cloexec = -1; |
| 160 |
} |
| 161 |
} |
| 162 |
if (have_cloexec < 0 && 0 <= fd) |
| 163 |
set_cloexec_flag (fd, true); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/* If checking for directories, fail if fd does not refer to a directory. |
| 168 |
Rationale: A filename ending in slash cannot name a non-directory |
| 169 |
<https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>: |
| 170 |
"A pathname that contains at least one non-<slash> character and that |
| 171 |
ends with one or more trailing <slash> characters shall not be resolved |
| 172 |
successfully unless the last pathname component before the trailing |
| 173 |
<slash> characters names an existing directory" |
| 174 |
If the named file without the slash is not a directory, open() must fail |
| 175 |
with ENOTDIR. */ |
| 176 |
if (check_directory && 0 <= fd) |
| 177 |
{ |
| 178 |
struct stat statbuf; |
| 179 |
int r = fstat (fd, &statbuf); |
| 180 |
if (r < 0 || !S_ISDIR (statbuf.st_mode)) |
| 181 |
{ |
| 182 |
int err = r < 0 ? errno : ENOTDIR; |
| 183 |
close (fd); |
| 184 |
errno = err; |
| 185 |
return -1; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return fd; |
| 190 |
} |
| 191 |
|
| 192 |
#else /* !HAVE_OPENAT */ |
| 193 |
|
| 194 |
# include "filename.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */ |
| 195 |
# include "openat-priv.h" |
| 196 |
# include "save-cwd.h" |
| 197 |
|
| 198 |
/* Replacement for Solaris' openat function. |
| 199 |
<https://www.google.com/search?q=openat+site:docs.oracle.com> |
| 200 |
First, try to simulate it via open ("/proc/self/fd/FD/FILE"). |
| 201 |
Failing that, simulate it by doing save_cwd/fchdir/open/restore_cwd. |
| 202 |
If either the save_cwd or the restore_cwd fails (relatively unlikely), |
| 203 |
then give a diagnostic and exit nonzero. |
| 204 |
Otherwise, upon failure, set errno and return -1, as openat does. |
| 205 |
Upon successful completion, return a file descriptor. */ |
| 206 |
int |
| 207 |
openat (int fd, char const *file, int flags, ...) |
| 208 |
{ |
| 209 |
mode_t mode = 0; |
| 210 |
|
| 211 |
if (flags & O_CREAT) |
| 212 |
{ |
| 213 |
va_list arg; |
| 214 |
va_start (arg, flags); |
| 215 |
|
| 216 |
/* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4 |
| 217 |
creates crashing code when 'mode_t' is smaller than 'int'. */ |
| 218 |
mode = va_arg (arg, PROMOTED_MODE_T); |
| 219 |
|
| 220 |
va_end (arg); |
| 221 |
} |
| 222 |
|
| 223 |
return openat_permissive (fd, file, flags, mode, NULL); |
| 224 |
} |
| 225 |
|
| 226 |
/* Like openat (FD, FILE, FLAGS, MODE), but if CWD_ERRNO is |
| 227 |
nonnull, set *CWD_ERRNO to an errno value if unable to save |
| 228 |
or restore the initial working directory. This is needed only |
| 229 |
the first time remove.c's remove_dir opens a command-line |
| 230 |
directory argument. |
| 231 |
|
| 232 |
If a previous attempt to restore the current working directory |
| 233 |
failed, then we must not even try to access a '.'-relative name. |
| 234 |
It is the caller's responsibility not to call this function |
| 235 |
in that case. */ |
| 236 |
|
| 237 |
int |
| 238 |
openat_permissive (int fd, char const *file, int flags, mode_t mode, |
| 239 |
int *cwd_errno) |
| 240 |
{ |
| 241 |
if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file)) |
| 242 |
return open (file, flags, mode); |
| 243 |
|
| 244 |
{ |
| 245 |
char buf[OPENAT_BUFFER_SIZE]; |
| 246 |
char *proc_file = openat_proc_name (buf, fd, file); |
| 247 |
if (proc_file) |
| 248 |
{ |
| 249 |
int open_result = open (proc_file, flags, mode); |
| 250 |
int open_errno = errno; |
| 251 |
if (proc_file != buf) |
| 252 |
free (proc_file); |
| 253 |
/* If the syscall succeeds, or if it fails with an unexpected |
| 254 |
errno value, then return right away. Otherwise, fall through |
| 255 |
and resort to using save_cwd/restore_cwd. */ |
| 256 |
if (0 <= open_result || ! EXPECTED_ERRNO (open_errno)) |
| 257 |
{ |
| 258 |
errno = open_errno; |
| 259 |
return open_result; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
struct saved_cwd saved_cwd; |
| 265 |
int save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0; |
| 266 |
|
| 267 |
/* If save_cwd allocated a descriptor DFD other than FD, do another |
| 268 |
save_cwd and then close DFD, so that the later open (if successful) |
| 269 |
returns DFD (the lowest-numbered descriptor) as POSIX requires. */ |
| 270 |
int dfd = saved_cwd.desc; |
| 271 |
if (0 <= dfd && dfd != fd) |
| 272 |
{ |
| 273 |
save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0; |
| 274 |
close (dfd); |
| 275 |
dfd = saved_cwd.desc; |
| 276 |
} |
| 277 |
|
| 278 |
/* If saving the working directory collides with the user's requested fd, |
| 279 |
then the user's fd must have been closed to begin with. */ |
| 280 |
if (0 <= dfd && dfd == fd) |
| 281 |
{ |
| 282 |
free_cwd (&saved_cwd); |
| 283 |
errno = EBADF; |
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
if (save_failed) |
| 288 |
{ |
| 289 |
if (! cwd_errno) |
| 290 |
openat_save_fail (save_failed); |
| 291 |
*cwd_errno = save_failed; |
| 292 |
} |
| 293 |
|
| 294 |
int err = fchdir (fd); |
| 295 |
int saved_errno = errno; |
| 296 |
|
| 297 |
if (0 <= err) |
| 298 |
{ |
| 299 |
err = open (file, flags, mode); |
| 300 |
saved_errno = errno; |
| 301 |
if (!save_failed && restore_cwd (&saved_cwd) < 0) |
| 302 |
{ |
| 303 |
int restore_cwd_errno = errno; |
| 304 |
if (! cwd_errno) |
| 305 |
{ |
| 306 |
/* Do not leak ERR. This also stops openat_restore_fail |
| 307 |
from messing up if ERR happens to equal STDERR_FILENO. */ |
| 308 |
if (0 <= err) |
| 309 |
close (err); |
| 310 |
openat_restore_fail (restore_cwd_errno); |
| 311 |
} |
| 312 |
*cwd_errno = restore_cwd_errno; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
free_cwd (&saved_cwd); |
| 317 |
errno = saved_errno; |
| 318 |
return err; |
| 319 |
} |
| 320 |
|
| 321 |
/* Return true if our openat implementation must resort to |
| 322 |
using save_cwd and restore_cwd. */ |
| 323 |
bool |
| 324 |
openat_needs_fchdir (void) |
| 325 |
{ |
| 326 |
bool needs_fchdir = true; |
| 327 |
int fd = open ("/", O_SEARCH | O_CLOEXEC); |
| 328 |
|
| 329 |
if (0 <= fd) |
| 330 |
{ |
| 331 |
char buf[OPENAT_BUFFER_SIZE]; |
| 332 |
char *proc_file = openat_proc_name (buf, fd, "."); |
| 333 |
if (proc_file) |
| 334 |
{ |
| 335 |
needs_fchdir = false; |
| 336 |
if (proc_file != buf) |
| 337 |
free (proc_file); |
| 338 |
} |
| 339 |
close (fd); |
| 340 |
} |
| 341 |
|
| 342 |
return needs_fchdir; |
| 343 |
} |
| 344 |
|
| 345 |
#endif /* !HAVE_OPENAT */ |