Branch data Line data Source code
1 : : #include "precizer.h"
2 : : #include <errno.h>
3 : :
4 : : /**
5 : : * @brief Check access to a path using an open directory as its base
6 : : *
7 : : * @details
8 : : * Passes the supplied path to `faccessat()` without changing the process
9 : : * working directory or constructing an absolute path. A relative path is
10 : : * resolved from @p directory_fd. As specified by `faccessat()`, an absolute
11 : : * path is resolved independently and causes @p directory_fd to be ignored
12 : : *
13 : : * The function uses the process real user and group IDs, matching `access()`
14 : : * semantics. A successful check returns `FILE_ACCESS_ALLOWED`; a failed check
15 : : * is translated from `errno` into the corresponding `FileAccessStatus`
16 : : *
17 : : * @param[in] directory_fd File descriptor used as the base for a relative path
18 : : * @param[in] relative_path String memory descriptor whose text is passed to
19 : : * `faccessat()`. Callers that require root confinement must ensure
20 : : * that this value is relative
21 : : * @param[in] mode Access mode such as `F_OK`, `R_OK`, `W_OK`, or `X_OK`
22 : : * @return `FILE_ACCESS_ALLOWED`, `FILE_ACCESS_DENIED`, `FILE_NOT_FOUND`, or
23 : : * `FILE_ACCESS_ERROR`. A NULL path descriptor returns
24 : : * `FILE_ACCESS_ERROR`
25 : : */
26 : 20348 : FileAccessStatus file_check_access(
27 : : const int directory_fd,
28 : : const memory *relative_path,
29 : : const int mode)
30 : : {
31 [ - + ]: 20348 : if(relative_path == NULL)
32 : : {
33 : 0 : return(FILE_ACCESS_ERROR);
34 : : }
35 : :
36 : 20348 : const char *runtime_relative_path = m_text(relative_path);
37 : :
38 : : #ifdef TESTITALL_TEST_HOOKS
39 : 20348 : FileAccessStatus forced_status = FILE_ACCESS_ALLOWED;
40 : :
41 [ + + ]: 20348 : if(testitall_file_access_status_override(runtime_relative_path,&forced_status) == true)
42 : : {
43 : 31 : return(forced_status);
44 : : }
45 : : #endif
46 : :
47 [ + + ]: 20317 : if(faccessat(directory_fd,runtime_relative_path,mode,0) == 0)
48 : : {
49 : 20255 : return(FILE_ACCESS_ALLOWED);
50 : : }
51 : :
52 : 62 : return(file_access_status(errno));
53 : : }
|