| 1 |
/* |
| 2 |
* Proc Helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-stdaux.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include "util/error.h" |
| 10 |
#include "util/proc.h" |
| 11 |
#include "util/string.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* A file in /proc can be at most 4M minus one. If required, we start with a 4K |
| 15 |
* read, then try a 4M one if that fails. In the vast majority of cases, 4K |
| 16 |
* will be enough. |
| 17 |
*/ |
| 18 |
#define PROC_SIZE_MIN (4U*1024U) |
| 19 |
#define PROC_SIZE_MAX (4U*1024U*1024U - 1U) |
| 20 |
|
| 21 |
/** |
| 22 |
* proc_field() - Extract individual field from proc-text-file |
| 23 |
* @data: content of the file |
| 24 |
* @key: key to find |
| 25 |
* @valuep: output variable to store the duplicated value |
| 26 |
* |
| 27 |
* Search through a key-value proc-file for the specified key and return the |
| 28 |
* value as newly allocated string. |
| 29 |
* |
| 30 |
* The caller is responsible to free the value via `free()`. |
| 31 |
* |
| 32 |
* The proc-file must be a standard text-file with no embedded string |
| 33 |
* terminators. It is the caller's responsibility to use this only on |
| 34 |
* suitable files. |
| 35 |
* |
| 36 |
* Return: 0 on success, PROC_E_NOT_FOUND if the key was not found, and |
| 37 |
* negative error code on failure. |
| 38 |
*/ |
| 39 |
int proc_field(const char *data, const char *key, char **valuep) { |
| 40 |
const size_t n_key = strlen(key); |
| 41 |
const char *pos, *t; |
| 42 |
char *value; |
| 43 |
|
| 44 |
pos = data; |
| 45 |
do { |
| 46 |
do { |
| 47 |
/* Find next occurrence of they key. */ |
| 48 |
t = strstr(pos, key); |
| 49 |
if (!t) |
| 50 |
return PROC_E_NOT_FOUND; |
| 51 |
|
| 52 |
pos = t + n_key; |
| 53 |
|
| 54 |
/* Continue if the key does not start a line. */ |
| 55 |
} while (t != data && t[-1] != '\n'); |
| 56 |
|
| 57 |
/* Skip possible whitespace before the colon. */ |
| 58 |
pos += strspn(pos, " \t"); |
| 59 |
|
| 60 |
/* Continue if the key is not complete. */ |
| 61 |
} while (*pos != ':'); |
| 62 |
|
| 63 |
/* Skip over the colon and whitespace. */ |
| 64 |
++pos; |
| 65 |
pos += strspn(pos, " \t"); |
| 66 |
|
| 67 |
/* Extract the value. */ |
| 68 |
value = strndup(pos, strcspn(pos, " \t\n\r")); |
| 69 |
if (!value) |
| 70 |
return error_origin(-ENOMEM); |
| 71 |
|
| 72 |
*valuep = value; |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* proc_read() - Read a proc-file into memory |
| 78 |
* @fd: file-descriptor to a file in procfs |
| 79 |
* @datap: output variable for the read data |
| 80 |
* @n_datap: output variable for the length of the data blob |
| 81 |
* |
| 82 |
* Read the entire proc-fs file given as @fd into memory and return it to the |
| 83 |
* caller. This will always read from file position 0 regardless of the current |
| 84 |
* file position. |
| 85 |
* |
| 86 |
* The resulting data block is always terminated by a binary zero. This allows |
| 87 |
* string operations on the data blob without any length checks. @n_datap will |
| 88 |
* not include this sentinal zero, unless it was actually part of the file. |
| 89 |
* |
| 90 |
* Note that standard procfs files cannot exceed 4M-1 in size, and their API |
| 91 |
* implementation actually limits it to 4M-2. |
| 92 |
* |
| 93 |
* If the proc-fs file in question does not follow the standard proc-fs rules, |
| 94 |
* the caller should be aware of the limitations of this function. |
| 95 |
* |
| 96 |
* It is the responsibility of the caller to free the data via `free()`. |
| 97 |
* |
| 98 |
* Return: 0 on success, negative error code on failure. |
| 99 |
*/ |
| 100 |
int proc_read(int fd, char **datap, size_t *n_datap) { |
| 101 |
_c_cleanup_(c_freep) char *data = NULL; |
| 102 |
ssize_t l; |
| 103 |
|
| 104 |
data = malloc(PROC_SIZE_MIN); |
| 105 |
if (!data) |
| 106 |
return error_origin(-ENOMEM); |
| 107 |
|
| 108 |
l = pread(fd, data, PROC_SIZE_MIN, 0); |
| 109 |
if (l < 0) |
| 110 |
return error_origin(-errno); |
| 111 |
|
| 112 |
/* |
| 113 |
* Proc never returns short reads unless end-of-file was reached. Thus, |
| 114 |
* a short read implies end-of-file. Furthermore, in case the proc file |
| 115 |
* is backed by a direct driver read, it might always return fresh data |
| 116 |
* on each read, as if we used `pread(..., 0)`. Hence, we rely on short |
| 117 |
* reads to know how long the file was. |
| 118 |
* |
| 119 |
* Lastly, note that we cannot ever attempt a read longer than |
| 120 |
* PROC_SIZE_MAX, since it would be immediately refused by the kernel. |
| 121 |
* So the longest successful read we can return to the caller is |
| 122 |
* actually `PROC_SIZE_MAX - 1`, otherwise we wouldn't know whether it |
| 123 |
* was complete. |
| 124 |
*/ |
| 125 |
if (l >= (ssize_t)PROC_SIZE_MIN) { |
| 126 |
data = c_free(data); |
| 127 |
data = malloc(PROC_SIZE_MAX); |
| 128 |
if (!data) |
| 129 |
return error_origin(-ENOMEM); |
| 130 |
|
| 131 |
l = pread(fd, data, PROC_SIZE_MAX, 0); |
| 132 |
if (l < 0) |
| 133 |
return error_origin(-errno); |
| 134 |
if (l >= (ssize_t)PROC_SIZE_MAX) |
| 135 |
return error_origin(-E2BIG); |
| 136 |
} |
| 137 |
|
| 138 |
/* Ensure a terminating 0 to allow direct searches of text-files. */ |
| 139 |
data[l] = 0; |
| 140 |
|
| 141 |
if (datap) { |
| 142 |
*datap = data; |
| 143 |
data = NULL; |
| 144 |
} |
| 145 |
if (n_datap) |
| 146 |
*n_datap = (size_t)l; |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
int proc_get_seclabel(pid_t pid, char **labelp, size_t *n_labelp) { |
| 151 |
_c_cleanup_(c_fclosep) FILE *f = NULL; |
| 152 |
char path[64], buffer[LINE_MAX] = {}, *c, *label; |
| 153 |
|
| 154 |
if (pid == PROC_PID_SELF) |
| 155 |
strcpy(path, "/proc/self/attr/current"); |
| 156 |
else if (pid > 0) |
| 157 |
sprintf(path, "/proc/%"PRIu32"/attr/current", (uint32_t)pid); |
| 158 |
else |
| 159 |
return error_origin(-EINVAL); |
| 160 |
|
| 161 |
f = fopen(path, "re"); |
| 162 |
if (f) { |
| 163 |
errno = 0; |
| 164 |
if (!fgets(buffer, sizeof(buffer), f)) { |
| 165 |
/* |
| 166 |
* If LSM core code is enabled, but no LSM is loaded, |
| 167 |
* the kernel returns EINVAL. In that case, we treat |
| 168 |
* the seclabel as empty string, similar to how the |
| 169 |
* user-space LSM libraries do. |
| 170 |
*/ |
| 171 |
if (ferror(f) && errno != EINVAL) |
| 172 |
return errno ? error_origin(-errno) : error_origin(-ENOTRECOVERABLE); |
| 173 |
} |
| 174 |
} else if (errno != ENOENT) { |
| 175 |
return error_origin(-errno); |
| 176 |
} |
| 177 |
|
| 178 |
c = strchrnul(buffer, '\n'); |
| 179 |
label = strndup(buffer, c - buffer); |
| 180 |
if (!label) |
| 181 |
return error_origin(-ENOMEM); |
| 182 |
|
| 183 |
if (n_labelp) |
| 184 |
*n_labelp = strlen(label); |
| 185 |
*labelp = label; |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
int proc_resolve_pidfd(int pidfd, pid_t *pidp) { |
| 190 |
_c_cleanup_(c_freep) char *data = NULL, *field = NULL; |
| 191 |
_c_cleanup_(c_closep) int fd = -1; |
| 192 |
char path[64]; |
| 193 |
int r; |
| 194 |
|
| 195 |
sprintf(path, "/proc/self/fdinfo/%d", pidfd); |
| 196 |
fd = open(path, O_RDONLY | O_CLOEXEC); |
| 197 |
if (fd < 0) |
| 198 |
return error_origin(-errno); |
| 199 |
|
| 200 |
r = proc_read(fd, &data, NULL); |
| 201 |
if (r) |
| 202 |
return error_fold(r); |
| 203 |
|
| 204 |
r = proc_field(data, "Pid", &field); |
| 205 |
if (r) |
| 206 |
return error_fold(r); |
| 207 |
|
| 208 |
r = util_strtoint(pidp, field); |
| 209 |
if (r) |
| 210 |
return error_fold(r); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |