1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Google, Inc.
4 */
5
6 /*
7 * Regression test for two related bugs:
8 *
9 * (1) CVE-2017-15299, fixed by commit 60ff5b2f547a ("KEYS: don't let add_key()
10 * update an uninstantiated key")
11 * (2) CVE-2017-15951, fixed by commit 363b02dab09b ("KEYS: Fix race between
12 * updating and finding a negative key")
13 *
14 * We test for the bugs together because the reproduction steps are essentially
15 * the same: repeatedly try to add/update a key with add_key() while requesting
16 * it with request_key() in another task. This reproduces both bugs:
17 *
18 * For CVE-2017-15299, add_key() has to run while the key being created by
19 * request_key() is still in the "uninstantiated" state. For the "encrypted" or
20 * "trusted" key types (not guaranteed to be available) this caused a NULL
21 * pointer dereference in encrypted_update() or in trusted_update(),
22 * respectively. For the "user" key type, this caused the WARN_ON() in
23 * construct_key() to be hit.
24 *
25 * For CVE-2017-15951, request_key() has to run while the key is "negatively
26 * instantiated" (from a prior request_key()) and is being concurrently changed
27 * to "positively instantiated" via add_key() updating it. This race, which is
28 * a bit more difficult to reproduce, caused the task executing request_key() to
29 * dereference an invalid pointer in __key_link_begin().
30 */
31
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <sys/wait.h>
36
37 #include "tst_test.h"
38 #include "lapi/keyctl.h"
39
40 static char *opt_bug;
41
test_with_key_type(const char * type,const char * payload,int effort)42 static void test_with_key_type(const char *type, const char *payload,
43 int effort)
44 {
45 int i;
46 int status;
47 pid_t add_key_pid;
48 pid_t request_key_pid;
49 bool info_only;
50
51 TEST(keyctl(KEYCTL_JOIN_SESSION_KEYRING, NULL));
52 if (TST_RET < 0)
53 tst_brk(TBROK | TTERRNO, "failed to join new session keyring");
54
55 TEST(add_key(type, "desc", payload, strlen(payload),
56 KEY_SPEC_SESSION_KEYRING));
57 if (TST_RET < 0 && TST_ERR != EINVAL) {
58 if (TST_ERR == ENODEV) {
59 tst_res(TCONF, "kernel doesn't support key type '%s'",
60 type);
61 return;
62 }
63 tst_brk(TBROK | TTERRNO,
64 "unexpected error checking whether key type '%s' is supported",
65 type);
66 }
67
68 /*
69 * Fork a subprocess which repeatedly tries to "add" a key of the given
70 * type. This actually will try to update the key if it already exists.
71 * Depending on the state of the key, add_key() should either succeed or
72 * fail with one of several errors:
73 *
74 * (1) key didn't exist at all: either add_key() should succeed (if the
75 * payload is valid), or it should fail with EINVAL (if the payload
76 * is invalid; this is needed for the "encrypted" and "trusted" key
77 * types because they have a quirk where the payload syntax differs
78 * for creating new keys vs. updating existing keys)
79 *
80 * (2) key was negative: add_key() should succeed
81 *
82 * (3) key was uninstantiated: add_key() should wait for the key to be
83 * negated, then fail with ENOKEY
84 *
85 * For now we also accept EDQUOT because the kernel frees up the keys
86 * quota asynchronously after keys are unlinked. So it may be hit.
87 */
88 add_key_pid = SAFE_FORK();
89 if (add_key_pid == 0) {
90 for (i = 0; i < 100 * effort; i++) {
91 usleep(rand() % 1024);
92 TEST(add_key(type, "desc", payload, strlen(payload),
93 KEY_SPEC_SESSION_KEYRING));
94 if (TST_RET < 0 && TST_ERR != EINVAL &&
95 TST_ERR != ENOKEY && TST_ERR != EDQUOT) {
96 tst_brk(TBROK | TTERRNO,
97 "unexpected error adding key of type '%s'",
98 type);
99 }
100 TEST(keyctl(KEYCTL_CLEAR, KEY_SPEC_SESSION_KEYRING));
101 if (TST_RET < 0) {
102 tst_brk(TBROK | TTERRNO,
103 "unable to clear keyring");
104 }
105 }
106 exit(0);
107 }
108
109 request_key_pid = SAFE_FORK();
110 if (request_key_pid == 0) {
111 for (i = 0; i < 5000 * effort; i++) {
112 TEST(request_key(type, "desc", "callout_info",
113 KEY_SPEC_SESSION_KEYRING));
114 if (TST_RET < 0 && TST_ERR != ENOKEY &&
115 TST_ERR != ENOENT && TST_ERR != EDQUOT) {
116 tst_brk(TBROK | TTERRNO,
117 "unexpected error requesting key of type '%s'",
118 type);
119 }
120 }
121 exit(0);
122 }
123
124 /*
125 * Verify that neither the add_key() nor the request_key() process
126 * crashed. If the add_key() process crashed it is likely due to
127 * CVE-2017-15299, while if the request_key() process crashed it is
128 * likely due to CVE-2017-15951. If testing for one of the bugs
129 * specifically, only pay attention to the corresponding process.
130 */
131
132 SAFE_WAITPID(add_key_pid, &status, 0);
133 info_only = (opt_bug && strcmp(opt_bug, "cve-2017-15299") != 0);
134 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
135 tst_res(info_only ? TINFO : TPASS,
136 "didn't crash while updating key of type '%s'",
137 type);
138 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
139 tst_res(info_only ? TINFO : TFAIL,
140 "kernel oops while updating key of type '%s'",
141 type);
142 } else {
143 tst_brk(TBROK, "add_key child %s", tst_strstatus(status));
144 }
145
146 SAFE_WAITPID(request_key_pid, &status, 0);
147 info_only = (opt_bug && strcmp(opt_bug, "cve-2017-15951") != 0);
148 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
149 tst_res(info_only ? TINFO : TPASS,
150 "didn't crash while requesting key of type '%s'",
151 type);
152 } else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
153 tst_res(info_only ? TINFO : TFAIL,
154 "kernel oops while requesting key of type '%s'",
155 type);
156 } else {
157 tst_brk(TBROK, "request_key child %s", tst_strstatus(status));
158 }
159 }
160
do_test(void)161 static void do_test(void)
162 {
163 /*
164 * Briefly test the "encrypted" and/or "trusted" key types when
165 * availaible, mainly to reproduce CVE-2017-15299.
166 */
167 test_with_key_type("encrypted", "update user:foo 32", 2);
168 test_with_key_type("trusted", "update", 2);
169
170 /*
171 * Test the "user" key type for longer, mainly in order to reproduce
172 * CVE-2017-15951. However, without the fix for CVE-2017-15299 as well,
173 * WARNs may show up in the kernel log.
174 *
175 * Note: the precise iteration count is arbitrary; it's just intended to
176 * be enough to give a decent chance of reproducing the bug, without
177 * wasting too much time.
178 */
179 test_with_key_type("user", "payload", 20);
180 }
181
182 static struct tst_test test = {
183 .test_all = do_test,
184 .forks_child = 1,
185 .options = (struct tst_option[]) {
186 {"b:", &opt_bug, "Bug to test for (cve-2017-15299 or cve-2017-15951; default is both)"},
187 {}
188 },
189 .tags = (const struct tst_tag[]) {
190 {"CVE", "2017-15299"},
191 {"linux-git", "60ff5b2f547a"},
192 {"CVE", "2017-15951"},
193 {"linux-git", "363b02dab09b"},
194 {},
195 }
196 };
197