This repository was archived by the owner on Mar 21, 2020. It is now read-only.
forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress-efivar.c
More file actions
345 lines (298 loc) · 7.78 KB
/
stress-efivar.c
File metadata and controls
345 lines (298 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (C) 2013-2018 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(__linux__)
typedef struct {
uint16_t varname[512];
uint8_t guid[16];
uint64_t datalen;
uint8_t data[1024];
uint64_t status;
uint32_t attributes;
} __attribute__((packed)) efi_var;
static const char vars[] = "/sys/firmware/efi/vars";
static const char efi_vars[] = "/sys/firmware/efi/efivars";
struct dirent **efi_dentries;
static bool *efi_ignore;
static int dir_count;
/*
* efi_var_ignore()
* check for filenames that are not efi vars
*/
static inline bool efi_var_ignore(char *d_name)
{
if (strcmp(d_name, "del_var") == 0)
return true;
if (strcmp(d_name, "new_var") == 0)
return true;
if (strcmp(d_name, ".") == 0)
return true;
if (strcmp(d_name, "..") == 0)
return true;
if (strstr(d_name, "MokListRT"))
return true;
return false;
}
/*
* guid_to_str()
* turn efi GUID to a string
*/
static inline void guid_to_str(const uint8_t *guid, char *guid_str, size_t guid_len)
{
if (!guid_str)
return;
if (guid_len > 36)
snprintf(guid_str, guid_len,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x-%02x%02x%02x%02x%02x%02x",
guid[3], guid[2], guid[1], guid[0], guid[5], guid[4], guid[7], guid[6],
guid[8], guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
else
*guid_str = '\0';
}
/*
* efi_str16_to_str()
* convert 16 bit string to 8 bit C string.
*/
static inline void efi_str16_to_str(char *dst, const size_t len, const uint16_t *src)
{
size_t i = len;
while ((*src) && (i > 1)) {
*dst++ = *(src++) & 0xff;
i--;
}
*dst = '\0';
}
/*
* efi_get_varname()
* fetch the UEFI variable name in terms of a 8 bit C string
*/
static inline void efi_get_varname(char *varname, const size_t len, const efi_var *var)
{
efi_str16_to_str(varname, len, var->varname);
}
/*
* efi_get_variable()
* fetch a UEFI variable given its name.
*/
static int efi_get_variable(const args_t *args, const char *varname, efi_var *var)
{
int fd, n, ret, rc = 0;
int flags;
char filename[PATH_MAX];
struct stat statbuf;
if ((!varname) || (!var))
return -1;
(void)snprintf(filename, sizeof filename,
"%s/%s/raw_var", vars, varname);
if ((fd = open(filename, O_RDONLY)) < 0)
return -1;
ret = fstat(fd, &statbuf);
if (ret < 0) {
pr_err("%s: failed to stat %s, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
rc = -1;
goto err_vars;
}
(void)memset(var, 0, sizeof(efi_var));
if ((n = read(fd, var, sizeof(efi_var))) != sizeof(efi_var)) {
if (errno != EIO) {
pr_err("%s: failed to read %s, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
}
rc = -1;
goto err_vars;
}
err_vars:
(void)close(fd);
(void)snprintf(filename, sizeof filename,
"%s/%s", efi_vars, varname);
if ((fd = open(filename, O_RDONLY)) < 0)
return -1;
ret = fstat(fd, &statbuf);
if (ret < 0) {
pr_err("%s: failed to stat %s, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
rc = -1;
goto err_vars;
}
ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
if (ret < 0) {
pr_err("%s: ioctl FS_IOC_GETFLAGS on %s failed, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
rc = -1;
goto err_efi_vars;
}
ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
if (ret < 0) {
pr_err("%s: ioctl FS_IOC_SETFLAGS on %s failed, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
rc = -1;
goto err_efi_vars;
}
err_efi_vars:
(void)close(fd);
return rc;
}
/*
* efi_vars_get()
* read EFI variables
*/
static int efi_vars_get(const args_t *args)
{
int i;
for (i = 0; i < dir_count; i++) {
efi_var var;
char *d_name = efi_dentries[i]->d_name;
int ret;
if (efi_ignore[i])
continue;
if (efi_var_ignore(d_name)) {
efi_ignore[i] = true;
continue;
}
ret = efi_get_variable(args, d_name, &var);
if (ret < 0) {
efi_ignore[i] = true;
continue;
}
if (var.attributes) {
char varname[513];
char guid_str[37];
efi_get_varname(varname, sizeof(varname), &var);
guid_to_str(var.guid, guid_str, sizeof(guid_str));
(void)guid_str;
} else {
efi_ignore[i] = true;
}
inc_counter(args);
}
return 0;
}
/*
* stress_efivar_supported()
* check if we can run this as root
*/
static int stress_efivar_supported(void)
{
DIR *dir;
if (geteuid() != 0) {
pr_inf("efivar stressor will be skipped, "
"need to be running as root for this stressor\n");
return -1;
}
dir = opendir(efi_vars);
if (!dir) {
pr_inf("efivar stressor will be skipped, "
"need to have access to EFI vars in %s\n",
vars);
return -1;
}
(void)closedir(dir);
return 0;
}
/*
* stress_efivar()
* stress that does lots of not a lot
*/
static int stress_efivar(const args_t *args)
{
pid_t pid;
int i;
size_t sz;
efi_dentries = NULL;
dir_count = scandir(vars, &efi_dentries, NULL, alphasort);
if (!efi_dentries || (dir_count <= 0)) {
pr_inf("%s: cannot read EFI vars in %s\n", args->name, vars);
return EXIT_SUCCESS;
}
sz = ((dir_count * sizeof(bool)) + args->page_size) & (args->page_size - 1);
efi_ignore = mmap(NULL, sz, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (efi_ignore == MAP_FAILED) {
pr_err("%s: cannot mmap shared memory: %d (%s))\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
}
again:
if (!g_keep_stressing_flag)
return EXIT_SUCCESS;
pid = fork();
if (pid < 0) {
if (errno == EAGAIN)
goto again;
pr_err("%s: fork failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
} else if (pid > 0) {
int status, ret;
(void)setpgid(pid, g_pgrp);
/* Parent, wait for child */
ret = waitpid(pid, &status, 0);
if (ret < 0) {
if (errno != EINTR)
pr_dbg("%s: waitpid(): errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)kill(pid, SIGTERM);
(void)kill(pid, SIGKILL);
(void)waitpid(pid, &status, 0);
} else if (WIFSIGNALED(status)) {
pr_dbg("%s: child died: %s (instance %d)\n",
args->name, stress_strsignal(WTERMSIG(status)),
args->instance);
return EXIT_FAILURE;
}
} else if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
set_oom_adjustment(args->name, true);
do {
efi_vars_get(args);
} while (keep_stressing());
_exit(0);
}
(void)munmap(efi_ignore, sz);
for (i = 0; i < dir_count; i++)
free(efi_dentries[i]);
free(efi_dentries);
return EXIT_SUCCESS;
}
stressor_info_t stress_efivar_info = {
.stressor = stress_efivar,
.supported = stress_efivar_supported,
.class = CLASS_OS
};
#else
static int stress_efivar_supported(void)
{
pr_inf("efivar stressor will be skipped, "
"it is not implemented on this platform\n");
return -1;
}
stressor_info_t stress_efivar_info = {
.stressor = stress_not_implemented,
.supported = stress_efivar_supported,
.class = CLASS_OS
};
#endif