forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-key.c
207 lines (186 loc) · 5.23 KB
/
stress-key.c
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
/*
* Copyright (C) 2013-2017 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(HAVE_KEYUTILS_H) && defined(__linux__) && defined(__NR_add_key) && defined(__NR_keyctl)
#include <keyutils.h>
#define MAX_KEYS (256)
static long sys_keyctl(int cmd, ...)
{
va_list args;
long int arg0, arg1, arg2, ret;
va_start(args, cmd);
arg0 = va_arg(args, long int);
arg1 = va_arg(args, long int);
arg2 = va_arg(args, long int);
ret = syscall(__NR_keyctl, cmd, arg0, arg1, arg2);
va_end(args);
return ret;
}
static key_serial_t sys_add_key(
const char *type,
const char *description,
const void *payload,
size_t plen,
key_serial_t keyring)
{
return (key_serial_t)syscall(__NR_add_key, type,
description, payload, plen, keyring);
}
#if defined(__NR_request_key)
static key_serial_t sys_request_key(
const char *type,
const char *description,
const char *callout_info,
key_serial_t keyring)
{
return (key_serial_t)syscall(__NR_request_key,
type, description, callout_info, keyring);
}
#endif
/*
* stress_key
* stress key operations
*/
int stress_key(const args_t *args)
{
key_serial_t keys[MAX_KEYS];
pid_t ppid = getppid();
bool timeout_supported = true;
do {
size_t i = 0, n = 0;
char description[64];
char payload[64];
/* Add as many keys as we are allowed */
for (n = 0; n < MAX_KEYS; n++) {
(void)snprintf(description, sizeof(description),
"stress-ng-key-%u-%" PRIu32
"-%zu", ppid, args->instance, n);
(void)snprintf(payload, sizeof(payload),
"somedata-%zu", n);
keys[n] = sys_add_key("user", description,
payload, strlen(payload),
KEY_SPEC_PROCESS_KEYRING);
if (keys[n] < 0) {
if ((errno != ENOMEM) && (errno != EDQUOT))
pr_fail_err("add_key");
goto tidy;
}
#if defined(KEYCTL_SET_TIMEOUT)
if (timeout_supported) {
if (sys_keyctl(KEYCTL_SET_TIMEOUT, keys[n], 1) < 0) {
/* Some platforms don't support this */
if (errno == ENOSYS) {
timeout_supported = false;
} else {
pr_fail_err("keyctl KEYCTL_SET_TIMEOUT");
}
}
}
if (!g_keep_stressing_flag)
goto tidy;
#endif
}
/* And manipulate the keys */
for (i = 0; i < n; i++) {
(void)snprintf(description, sizeof(description),
"stress-ng-key-%u-%" PRIu32
"-%zu", ppid, args->instance, i);
#if defined(KEYCTL_DESCRIBE)
if (sys_keyctl(KEYCTL_DESCRIBE, keys[i], description) < 0)
pr_fail_err("keyctl KEYCTL_DESCRIBE");
if (!g_keep_stressing_flag)
goto tidy;
#endif
(void)snprintf(payload, sizeof(payload),
"somedata-%zu", n);
#if defined(KEYCTL_UPDATE)
if (sys_keyctl(KEYCTL_UPDATE, keys[i],
payload, strlen(payload)) < 0) {
if ((errno != ENOMEM) && (errno != EDQUOT))
pr_fail_err("keyctl KEYCTL_UPDATE");
}
if (!g_keep_stressing_flag)
goto tidy;
#endif
#if defined(KEYCTL_READ)
(void)memset(payload, 0, sizeof(payload));
if (sys_keyctl(KEYCTL_READ, keys[i],
payload, sizeof(payload)) < 0)
pr_fail_err("keyctl KEYCTL_READ");
if (!g_keep_stressing_flag)
goto tidy;
#endif
#if defined(__NR_request_key)
(void)snprintf(description, sizeof(description),
"stress-ng-key-%u-%" PRIu32
"-%zu", ppid, args->instance, i);
if (sys_request_key("user", description, NULL,
KEY_SPEC_PROCESS_KEYRING) < 0) {
pr_fail_err("request_key");
}
if (!g_keep_stressing_flag)
goto tidy;
#endif
#if defined(KEYCTL_CHOWN)
(void)sys_keyctl(KEYCTL_CHOWN, keys[i], getuid(), -1);
(void)sys_keyctl(KEYCTL_CHOWN, keys[i], -1, getgid());
#endif
#if defined(KEYCTL_SETPERM)
(void)sys_keyctl(KEYCTL_SETPERM, keys[i], KEY_USR_ALL);
#endif
#if defined(KEYCTL_CLEAR)
(void)sys_keyctl(KEYCTL_CLEAR, keys[i]);
#endif
#if defined(KEYCTL_REVOKE)
if (mwc32() & 1)
(void)sys_keyctl(KEYCTL_REVOKE, keys[i]);
#endif
#if defined(KEYCTL_INVALIDATE)
(void)sys_keyctl(KEYCTL_INVALIDATE, keys[i]);
#endif
inc_counter(args);
}
tidy:
/* If we hit too many errors and bailed out early, clean up */
while (i < n) {
if (keys[i] >= 0) {
#if defined(KEYCTL_CLEAR)
(void)sys_keyctl(KEYCTL_CLEAR, keys[i]);
#endif
#if defined(KEYCTL_INVALIDATE)
(void)sys_keyctl(KEYCTL_INVALIDATE, keys[i]);
#endif
}
i++;
}
} while (keep_stressing());
return EXIT_SUCCESS;
}
#else
int stress_key(const args_t *args)
{
return stress_not_implemented(args);
}
#endif