|
26 | 26 |
|
27 | 27 | #define PROC_PATH_COMPONENTS_MAX (LINUX_PATH_MAX / 2) |
28 | 28 |
|
29 | | -int path_might_use_open_intercept(const char *path) |
| 29 | +/* True when path equals prefix exactly, or extends it with '/'. Avoids the |
| 30 | + * surprise where "/sys/devices/system/cpufoo" would match a bare strncmp on |
| 31 | + * "/sys/devices/system/cpu" and pull an unrelated path through the intercept |
| 32 | + * layer. |
| 33 | + */ |
| 34 | +static bool path_prefix_match(const char *path, const char *prefix, size_t plen) |
| 35 | +{ |
| 36 | + if (strncmp(path, prefix, plen) != 0) |
| 37 | + return false; |
| 38 | + return path[plen] == '\0' || path[plen] == '/'; |
| 39 | +} |
| 40 | + |
| 41 | +#define SYSFS_CPU_PREFIX "/sys/devices/system/cpu" |
| 42 | + |
| 43 | +bool path_might_use_open_intercept(const char *path) |
30 | 44 | { |
31 | 45 | if (!path || path[0] != '/') |
32 | | - return 0; |
| 46 | + return false; |
33 | 47 |
|
34 | 48 | if (!strncmp(path, "/proc", 5)) |
35 | | - return 1; |
| 49 | + return true; |
36 | 50 | if (!strncmp(path, "/dev", 4)) |
37 | | - return 1; |
| 51 | + return true; |
| 52 | + if (path_prefix_match(path, SYSFS_CPU_PREFIX, sizeof(SYSFS_CPU_PREFIX) - 1)) |
| 53 | + return true; |
38 | 54 | if (!strcmp(path, "/etc/mtab") || !strcmp(path, "/etc/passwd") || |
39 | 55 | !strcmp(path, "/etc/group")) |
40 | | - return 1; |
| 56 | + return true; |
41 | 57 | if (!strcmp(path, "/var/run/utmp") || !strcmp(path, "/run/utmp")) |
42 | | - return 1; |
| 58 | + return true; |
43 | 59 |
|
44 | | - return 0; |
| 60 | + return false; |
45 | 61 | } |
46 | 62 |
|
47 | | -int path_might_use_stat_intercept(const char *path) |
| 63 | +bool path_might_use_stat_intercept(const char *path) |
48 | 64 | { |
49 | 65 | if (!path || path[0] != '/') |
50 | | - return 0; |
| 66 | + return false; |
51 | 67 |
|
52 | 68 | if (!strncmp(path, "/proc", 5)) |
53 | | - return 1; |
| 69 | + return true; |
54 | 70 | if (!strncmp(path, "/dev/shm", 8)) |
55 | | - return 1; |
| 71 | + return true; |
| 72 | + if (path_prefix_match(path, SYSFS_CPU_PREFIX, sizeof(SYSFS_CPU_PREFIX) - 1)) |
| 73 | + return true; |
56 | 74 |
|
57 | | - return 0; |
| 75 | + return false; |
58 | 76 | } |
59 | 77 |
|
60 | | -static int path_next_component(const char **pathp, |
61 | | - const char **comp, |
62 | | - size_t *len) |
| 78 | +int path_check_intercept_access(const struct stat *st, int mode, int flags) |
| 79 | +{ |
| 80 | + if ((mode & ~(F_OK | R_OK | W_OK | X_OK)) != 0) { |
| 81 | + errno = EINVAL; |
| 82 | + return -1; |
| 83 | + } |
| 84 | + if (mode == F_OK) |
| 85 | + return 0; |
| 86 | + |
| 87 | + mode_t granted = 0; |
| 88 | + uint32_t uid = |
| 89 | + (flags & LINUX_AT_EACCESS) ? proc_get_euid() : proc_get_uid(); |
| 90 | + uint32_t gid = |
| 91 | + (flags & LINUX_AT_EACCESS) ? proc_get_egid() : proc_get_gid(); |
| 92 | + |
| 93 | + if (uid == 0) { |
| 94 | + /* CAP_DAC_OVERRIDE: root reads and writes any file regardless of mode |
| 95 | + * bits. Execute still requires at least one x-bit set so non-executable |
| 96 | + * files cannot be run as root. Matches Linux generic_permission() in |
| 97 | + * fs/namei.c. |
| 98 | + */ |
| 99 | + granted |= R_OK | W_OK; |
| 100 | + if (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) |
| 101 | + granted |= X_OK; |
| 102 | + } else { |
| 103 | + mode_t bits; |
| 104 | + if (uid == st->st_uid) |
| 105 | + bits = (st->st_mode >> 6) & 7; |
| 106 | + else if (gid == st->st_gid) |
| 107 | + bits = (st->st_mode >> 3) & 7; |
| 108 | + else |
| 109 | + bits = st->st_mode & 7; |
| 110 | + |
| 111 | + if (bits & 4) |
| 112 | + granted |= R_OK; |
| 113 | + if (bits & 2) |
| 114 | + granted |= W_OK; |
| 115 | + if (bits & 1) |
| 116 | + granted |= X_OK; |
| 117 | + } |
| 118 | + |
| 119 | + if ((mode & granted) == mode) |
| 120 | + return 0; |
| 121 | + |
| 122 | + errno = EACCES; |
| 123 | + return -1; |
| 124 | +} |
| 125 | + |
| 126 | +static bool path_next_component(const char **pathp, |
| 127 | + const char **comp, |
| 128 | + size_t *len) |
63 | 129 | { |
64 | 130 | const char *p = *pathp; |
65 | 131 |
|
66 | 132 | while (*p == '/') |
67 | 133 | p++; |
68 | 134 | if (*p == '\0') { |
69 | 135 | *pathp = p; |
70 | | - return 0; |
| 136 | + return false; |
71 | 137 | } |
72 | 138 |
|
73 | 139 | *comp = p; |
74 | 140 | while (*p != '\0' && *p != '/') |
75 | 141 | p++; |
76 | 142 | *len = (size_t) (p - *comp); |
77 | 143 | *pathp = p; |
78 | | - return 1; |
| 144 | + return true; |
79 | 145 | } |
80 | 146 |
|
81 | | -static int path_component_is_dot(const char *comp, size_t len) |
| 147 | +static bool path_component_is_dot(const char *comp, size_t len) |
82 | 148 | { |
83 | 149 | return len == 1 && comp[0] == '.'; |
84 | 150 | } |
|
0 commit comments