1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. All rights reserved. */
4 #include <linux/module.h>
5 #include <linux/device.h>
6 #include <linux/ndctl.h>
7 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/key.h>
12 #include <linux/key-type.h>
13 #include <keys/user-type.h>
14 #include <keys/encrypted-type.h>
18 #define NVDIMM_BASE_KEY 0
19 #define NVDIMM_NEW_KEY 1
21 static bool key_revalidate
= true;
22 module_param(key_revalidate
, bool, 0444);
23 MODULE_PARM_DESC(key_revalidate
, "Require key validation at init.");
25 static void *key_data(struct key
*key
)
27 struct encrypted_key_payload
*epayload
= dereference_key_locked(key
);
29 lockdep_assert_held_read(&key
->sem
);
31 return epayload
->decrypted_data
;
34 static void nvdimm_put_key(struct key
*key
)
44 * Retrieve kernel key for DIMM and request from user space if
45 * necessary. Returns a key held for read and must be put by
46 * nvdimm_put_key() before the usage goes out of scope.
48 static struct key
*nvdimm_request_key(struct nvdimm
*nvdimm
)
50 struct key
*key
= NULL
;
51 static const char NVDIMM_PREFIX
[] = "nvdimm:";
52 char desc
[NVDIMM_KEY_DESC_LEN
+ sizeof(NVDIMM_PREFIX
)];
53 struct device
*dev
= &nvdimm
->dev
;
55 sprintf(desc
, "%s%s", NVDIMM_PREFIX
, nvdimm
->dimm_id
);
56 key
= request_key(&key_type_encrypted
, desc
, "");
58 if (PTR_ERR(key
) == -ENOKEY
)
59 dev_dbg(dev
, "request_key() found no key\n");
61 dev_dbg(dev
, "request_key() upcall failed\n");
64 struct encrypted_key_payload
*epayload
;
67 epayload
= dereference_key_locked(key
);
68 if (epayload
->decrypted_datalen
!= NVDIMM_PASSPHRASE_LEN
) {
78 static struct key
*nvdimm_lookup_user_key(struct nvdimm
*nvdimm
,
79 key_serial_t id
, int subclass
)
83 struct encrypted_key_payload
*epayload
;
84 struct device
*dev
= &nvdimm
->dev
;
86 keyref
= lookup_user_key(id
, 0, 0);
90 key
= key_ref_to_ptr(keyref
);
91 if (key
->type
!= &key_type_encrypted
) {
96 dev_dbg(dev
, "%s: key found: %#x\n", __func__
, key_serial(key
));
98 down_read_nested(&key
->sem
, subclass
);
99 epayload
= dereference_key_locked(key
);
100 if (epayload
->decrypted_datalen
!= NVDIMM_PASSPHRASE_LEN
) {
108 static struct key
*nvdimm_key_revalidate(struct nvdimm
*nvdimm
)
113 if (!nvdimm
->sec
.ops
->change_key
)
116 key
= nvdimm_request_key(nvdimm
);
121 * Send the same key to the hardware as new and old key to
122 * verify that the key is good.
124 rc
= nvdimm
->sec
.ops
->change_key(nvdimm
, key_data(key
),
125 key_data(key
), NVDIMM_USER
);
133 static int __nvdimm_security_unlock(struct nvdimm
*nvdimm
)
135 struct device
*dev
= &nvdimm
->dev
;
136 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
137 struct key
*key
= NULL
;
140 /* The bus lock should be held at the top level of the call stack */
141 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
143 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->unlock
144 || nvdimm
->sec
.state
< 0)
147 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
148 dev_dbg(dev
, "Security operation in progress.\n");
153 * If the pre-OS has unlocked the DIMM, attempt to send the key
154 * from request_key() to the hardware for verification. Failure
155 * to revalidate the key against the hardware results in a
156 * freeze of the security configuration. I.e. if the OS does not
157 * have the key, security is being managed pre-OS.
159 if (nvdimm
->sec
.state
== NVDIMM_SECURITY_UNLOCKED
) {
163 key
= nvdimm_key_revalidate(nvdimm
);
165 return nvdimm_security_freeze(nvdimm
);
167 key
= nvdimm_request_key(nvdimm
);
172 rc
= nvdimm
->sec
.ops
->unlock(nvdimm
, key_data(key
));
173 dev_dbg(dev
, "key: %d unlock: %s\n", key_serial(key
),
174 rc
== 0 ? "success" : "fail");
177 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
181 int nvdimm_security_unlock(struct device
*dev
)
183 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
186 nvdimm_bus_lock(dev
);
187 rc
= __nvdimm_security_unlock(nvdimm
);
188 nvdimm_bus_unlock(dev
);
192 int nvdimm_security_disable(struct nvdimm
*nvdimm
, unsigned int keyid
)
194 struct device
*dev
= &nvdimm
->dev
;
195 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
199 /* The bus lock should be held at the top level of the call stack */
200 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
202 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->disable
203 || nvdimm
->sec
.state
< 0)
206 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
207 dev_dbg(dev
, "Incorrect security state: %d\n",
212 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
213 dev_dbg(dev
, "Security operation in progress.\n");
217 key
= nvdimm_lookup_user_key(nvdimm
, keyid
, NVDIMM_BASE_KEY
);
221 rc
= nvdimm
->sec
.ops
->disable(nvdimm
, key_data(key
));
222 dev_dbg(dev
, "key: %d disable: %s\n", key_serial(key
),
223 rc
== 0 ? "success" : "fail");
226 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
230 int nvdimm_security_update(struct nvdimm
*nvdimm
, unsigned int keyid
,
231 unsigned int new_keyid
,
232 enum nvdimm_passphrase_type pass_type
)
234 struct device
*dev
= &nvdimm
->dev
;
235 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
236 struct key
*key
, *newkey
;
239 /* The bus lock should be held at the top level of the call stack */
240 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
242 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->change_key
243 || nvdimm
->sec
.state
< 0)
246 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
247 dev_dbg(dev
, "Incorrect security state: %d\n",
255 key
= nvdimm_lookup_user_key(nvdimm
, keyid
, NVDIMM_BASE_KEY
);
260 newkey
= nvdimm_lookup_user_key(nvdimm
, new_keyid
, NVDIMM_NEW_KEY
);
266 rc
= nvdimm
->sec
.ops
->change_key(nvdimm
, key
? key_data(key
) : NULL
,
267 key_data(newkey
), pass_type
);
268 dev_dbg(dev
, "key: %d %d update%s: %s\n",
269 key_serial(key
), key_serial(newkey
),
270 pass_type
== NVDIMM_MASTER
? "(master)" : "(user)",
271 rc
== 0 ? "success" : "fail");
273 nvdimm_put_key(newkey
);
275 if (pass_type
== NVDIMM_MASTER
)
276 nvdimm
->sec
.ext_state
= nvdimm_security_state(nvdimm
,
279 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
,
284 int nvdimm_security_erase(struct nvdimm
*nvdimm
, unsigned int keyid
,
285 enum nvdimm_passphrase_type pass_type
)
287 struct device
*dev
= &nvdimm
->dev
;
288 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
292 /* The bus lock should be held at the top level of the call stack */
293 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
295 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->erase
296 || nvdimm
->sec
.state
< 0)
299 if (atomic_read(&nvdimm
->busy
)) {
300 dev_dbg(dev
, "Unable to secure erase while DIMM active.\n");
304 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
305 dev_dbg(dev
, "Incorrect security state: %d\n",
310 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
311 dev_dbg(dev
, "Security operation in progress.\n");
315 if (nvdimm
->sec
.ext_state
!= NVDIMM_SECURITY_UNLOCKED
316 && pass_type
== NVDIMM_MASTER
) {
318 "Attempt to secure erase in wrong master state.\n");
322 key
= nvdimm_lookup_user_key(nvdimm
, keyid
, NVDIMM_BASE_KEY
);
326 rc
= nvdimm
->sec
.ops
->erase(nvdimm
, key_data(key
), pass_type
);
327 dev_dbg(dev
, "key: %d erase%s: %s\n", key_serial(key
),
328 pass_type
== NVDIMM_MASTER
? "(master)" : "(user)",
329 rc
== 0 ? "success" : "fail");
332 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
336 int nvdimm_security_overwrite(struct nvdimm
*nvdimm
, unsigned int keyid
)
338 struct device
*dev
= &nvdimm
->dev
;
339 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
343 /* The bus lock should be held at the top level of the call stack */
344 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
346 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->overwrite
347 || nvdimm
->sec
.state
< 0)
350 if (atomic_read(&nvdimm
->busy
)) {
351 dev_dbg(dev
, "Unable to overwrite while DIMM active.\n");
355 if (dev
->driver
== NULL
) {
356 dev_dbg(dev
, "Unable to overwrite while DIMM active.\n");
360 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
361 dev_dbg(dev
, "Incorrect security state: %d\n",
366 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
367 dev_dbg(dev
, "Security operation in progress.\n");
374 key
= nvdimm_lookup_user_key(nvdimm
, keyid
, NVDIMM_BASE_KEY
);
379 rc
= nvdimm
->sec
.ops
->overwrite(nvdimm
, key
? key_data(key
) : NULL
);
380 dev_dbg(dev
, "key: %d overwrite submission: %s\n", key_serial(key
),
381 rc
== 0 ? "success" : "fail");
385 set_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
386 set_bit(NDD_WORK_PENDING
, &nvdimm
->flags
);
387 nvdimm
->sec
.state
= NVDIMM_SECURITY_OVERWRITE
;
389 * Make sure we don't lose device while doing overwrite
393 queue_delayed_work(system_wq
, &nvdimm
->dwork
, 0);
399 void __nvdimm_security_overwrite_query(struct nvdimm
*nvdimm
)
401 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nvdimm
->dev
);
405 /* The bus lock should be held at the top level of the call stack */
406 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
409 * Abort and release device if we no longer have the overwrite
410 * flag set. It means the work has been canceled.
412 if (!test_bit(NDD_WORK_PENDING
, &nvdimm
->flags
))
415 tmo
= nvdimm
->sec
.overwrite_tmo
;
417 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->query_overwrite
418 || nvdimm
->sec
.state
< 0)
421 rc
= nvdimm
->sec
.ops
->query_overwrite(nvdimm
);
424 /* setup delayed work again */
426 queue_delayed_work(system_wq
, &nvdimm
->dwork
, tmo
* HZ
);
427 nvdimm
->sec
.overwrite_tmo
= min(15U * 60U, tmo
);
432 dev_dbg(&nvdimm
->dev
, "overwrite failed\n");
434 dev_dbg(&nvdimm
->dev
, "overwrite completed\n");
436 if (nvdimm
->sec
.overwrite_state
)
437 sysfs_notify_dirent(nvdimm
->sec
.overwrite_state
);
438 nvdimm
->sec
.overwrite_tmo
= 0;
439 clear_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
440 clear_bit(NDD_WORK_PENDING
, &nvdimm
->flags
);
441 put_device(&nvdimm
->dev
);
442 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
443 nvdimm
->sec
.ext_state
= nvdimm_security_state(nvdimm
, NVDIMM_MASTER
);
446 void nvdimm_security_overwrite_query(struct work_struct
*work
)
448 struct nvdimm
*nvdimm
=
449 container_of(work
, typeof(*nvdimm
), dwork
.work
);
451 nvdimm_bus_lock(&nvdimm
->dev
);
452 __nvdimm_security_overwrite_query(nvdimm
);
453 nvdimm_bus_unlock(&nvdimm
->dev
);