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 const char zero_key
[NVDIMM_PASSPHRASE_LEN
];
27 static void *key_data(struct key
*key
)
29 struct encrypted_key_payload
*epayload
= dereference_key_locked(key
);
31 lockdep_assert_held_read(&key
->sem
);
33 return epayload
->decrypted_data
;
36 static void nvdimm_put_key(struct key
*key
)
46 * Retrieve kernel key for DIMM and request from user space if
47 * necessary. Returns a key held for read and must be put by
48 * nvdimm_put_key() before the usage goes out of scope.
50 static struct key
*nvdimm_request_key(struct nvdimm
*nvdimm
)
52 struct key
*key
= NULL
;
53 static const char NVDIMM_PREFIX
[] = "nvdimm:";
54 char desc
[NVDIMM_KEY_DESC_LEN
+ sizeof(NVDIMM_PREFIX
)];
55 struct device
*dev
= &nvdimm
->dev
;
57 sprintf(desc
, "%s%s", NVDIMM_PREFIX
, nvdimm
->dimm_id
);
58 key
= request_key(&key_type_encrypted
, desc
, "");
60 if (PTR_ERR(key
) == -ENOKEY
)
61 dev_dbg(dev
, "request_key() found no key\n");
63 dev_dbg(dev
, "request_key() upcall failed\n");
66 struct encrypted_key_payload
*epayload
;
69 epayload
= dereference_key_locked(key
);
70 if (epayload
->decrypted_datalen
!= NVDIMM_PASSPHRASE_LEN
) {
80 static const void *nvdimm_get_key_payload(struct nvdimm
*nvdimm
,
83 *key
= nvdimm_request_key(nvdimm
);
87 return key_data(*key
);
90 static struct key
*nvdimm_lookup_user_key(struct nvdimm
*nvdimm
,
91 key_serial_t id
, int subclass
)
95 struct encrypted_key_payload
*epayload
;
96 struct device
*dev
= &nvdimm
->dev
;
98 keyref
= lookup_user_key(id
, 0, 0);
102 key
= key_ref_to_ptr(keyref
);
103 if (key
->type
!= &key_type_encrypted
) {
108 dev_dbg(dev
, "%s: key found: %#x\n", __func__
, key_serial(key
));
110 down_read_nested(&key
->sem
, subclass
);
111 epayload
= dereference_key_locked(key
);
112 if (epayload
->decrypted_datalen
!= NVDIMM_PASSPHRASE_LEN
) {
120 static const void *nvdimm_get_user_key_payload(struct nvdimm
*nvdimm
,
121 key_serial_t id
, int subclass
, struct key
**key
)
125 if (subclass
== NVDIMM_BASE_KEY
)
131 *key
= nvdimm_lookup_user_key(nvdimm
, id
, subclass
);
135 return key_data(*key
);
139 static int nvdimm_key_revalidate(struct nvdimm
*nvdimm
)
145 if (!nvdimm
->sec
.ops
->change_key
)
148 data
= nvdimm_get_key_payload(nvdimm
, &key
);
151 * Send the same key to the hardware as new and old key to
152 * verify that the key is good.
154 rc
= nvdimm
->sec
.ops
->change_key(nvdimm
, data
, data
, NVDIMM_USER
);
161 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
165 static int __nvdimm_security_unlock(struct nvdimm
*nvdimm
)
167 struct device
*dev
= &nvdimm
->dev
;
168 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
173 /* The bus lock should be held at the top level of the call stack */
174 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
176 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->unlock
177 || nvdimm
->sec
.state
< 0)
180 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
181 dev_dbg(dev
, "Security operation in progress.\n");
186 * If the pre-OS has unlocked the DIMM, attempt to send the key
187 * from request_key() to the hardware for verification. Failure
188 * to revalidate the key against the hardware results in a
189 * freeze of the security configuration. I.e. if the OS does not
190 * have the key, security is being managed pre-OS.
192 if (nvdimm
->sec
.state
== NVDIMM_SECURITY_UNLOCKED
) {
196 return nvdimm_key_revalidate(nvdimm
);
198 data
= nvdimm_get_key_payload(nvdimm
, &key
);
200 rc
= nvdimm
->sec
.ops
->unlock(nvdimm
, data
);
201 dev_dbg(dev
, "key: %d unlock: %s\n", key_serial(key
),
202 rc
== 0 ? "success" : "fail");
205 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
209 int nvdimm_security_unlock(struct device
*dev
)
211 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
214 nvdimm_bus_lock(dev
);
215 rc
= __nvdimm_security_unlock(nvdimm
);
216 nvdimm_bus_unlock(dev
);
220 int nvdimm_security_disable(struct nvdimm
*nvdimm
, unsigned int keyid
)
222 struct device
*dev
= &nvdimm
->dev
;
223 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
228 /* The bus lock should be held at the top level of the call stack */
229 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
231 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->disable
232 || nvdimm
->sec
.state
< 0)
235 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
236 dev_dbg(dev
, "Incorrect security state: %d\n",
241 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
242 dev_dbg(dev
, "Security operation in progress.\n");
246 data
= nvdimm_get_user_key_payload(nvdimm
, keyid
,
247 NVDIMM_BASE_KEY
, &key
);
251 rc
= nvdimm
->sec
.ops
->disable(nvdimm
, data
);
252 dev_dbg(dev
, "key: %d disable: %s\n", key_serial(key
),
253 rc
== 0 ? "success" : "fail");
256 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
260 int nvdimm_security_update(struct nvdimm
*nvdimm
, unsigned int keyid
,
261 unsigned int new_keyid
,
262 enum nvdimm_passphrase_type pass_type
)
264 struct device
*dev
= &nvdimm
->dev
;
265 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
266 struct key
*key
, *newkey
;
268 const void *data
, *newdata
;
270 /* The bus lock should be held at the top level of the call stack */
271 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
273 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->change_key
274 || nvdimm
->sec
.state
< 0)
277 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
278 dev_dbg(dev
, "Incorrect security state: %d\n",
283 data
= nvdimm_get_user_key_payload(nvdimm
, keyid
,
284 NVDIMM_BASE_KEY
, &key
);
288 newdata
= nvdimm_get_user_key_payload(nvdimm
, new_keyid
,
289 NVDIMM_NEW_KEY
, &newkey
);
295 rc
= nvdimm
->sec
.ops
->change_key(nvdimm
, data
, newdata
, pass_type
);
296 dev_dbg(dev
, "key: %d %d update%s: %s\n",
297 key_serial(key
), key_serial(newkey
),
298 pass_type
== NVDIMM_MASTER
? "(master)" : "(user)",
299 rc
== 0 ? "success" : "fail");
301 nvdimm_put_key(newkey
);
303 if (pass_type
== NVDIMM_MASTER
)
304 nvdimm
->sec
.ext_state
= nvdimm_security_state(nvdimm
,
307 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
,
312 int nvdimm_security_erase(struct nvdimm
*nvdimm
, unsigned int keyid
,
313 enum nvdimm_passphrase_type pass_type
)
315 struct device
*dev
= &nvdimm
->dev
;
316 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
317 struct key
*key
= NULL
;
321 /* The bus lock should be held at the top level of the call stack */
322 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
324 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->erase
325 || nvdimm
->sec
.state
< 0)
328 if (atomic_read(&nvdimm
->busy
)) {
329 dev_dbg(dev
, "Unable to secure erase while DIMM active.\n");
333 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
334 dev_dbg(dev
, "Incorrect security state: %d\n",
339 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
340 dev_dbg(dev
, "Security operation in progress.\n");
344 if (nvdimm
->sec
.ext_state
!= NVDIMM_SECURITY_UNLOCKED
345 && pass_type
== NVDIMM_MASTER
) {
347 "Attempt to secure erase in wrong master state.\n");
351 data
= nvdimm_get_user_key_payload(nvdimm
, keyid
,
352 NVDIMM_BASE_KEY
, &key
);
356 rc
= nvdimm
->sec
.ops
->erase(nvdimm
, data
, pass_type
);
357 dev_dbg(dev
, "key: %d erase%s: %s\n", key_serial(key
),
358 pass_type
== NVDIMM_MASTER
? "(master)" : "(user)",
359 rc
== 0 ? "success" : "fail");
362 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
366 int nvdimm_security_overwrite(struct nvdimm
*nvdimm
, unsigned int keyid
)
368 struct device
*dev
= &nvdimm
->dev
;
369 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
370 struct key
*key
= NULL
;
374 /* The bus lock should be held at the top level of the call stack */
375 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
377 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->overwrite
378 || nvdimm
->sec
.state
< 0)
381 if (atomic_read(&nvdimm
->busy
)) {
382 dev_dbg(dev
, "Unable to overwrite while DIMM active.\n");
386 if (dev
->driver
== NULL
) {
387 dev_dbg(dev
, "Unable to overwrite while DIMM active.\n");
391 if (nvdimm
->sec
.state
>= NVDIMM_SECURITY_FROZEN
) {
392 dev_dbg(dev
, "Incorrect security state: %d\n",
397 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
398 dev_dbg(dev
, "Security operation in progress.\n");
402 data
= nvdimm_get_user_key_payload(nvdimm
, keyid
,
403 NVDIMM_BASE_KEY
, &key
);
407 rc
= nvdimm
->sec
.ops
->overwrite(nvdimm
, data
);
408 dev_dbg(dev
, "key: %d overwrite submission: %s\n", key_serial(key
),
409 rc
== 0 ? "success" : "fail");
413 set_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
414 set_bit(NDD_WORK_PENDING
, &nvdimm
->flags
);
415 nvdimm
->sec
.state
= NVDIMM_SECURITY_OVERWRITE
;
417 * Make sure we don't lose device while doing overwrite
421 queue_delayed_work(system_wq
, &nvdimm
->dwork
, 0);
427 void __nvdimm_security_overwrite_query(struct nvdimm
*nvdimm
)
429 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nvdimm
->dev
);
433 /* The bus lock should be held at the top level of the call stack */
434 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
437 * Abort and release device if we no longer have the overwrite
438 * flag set. It means the work has been canceled.
440 if (!test_bit(NDD_WORK_PENDING
, &nvdimm
->flags
))
443 tmo
= nvdimm
->sec
.overwrite_tmo
;
445 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->query_overwrite
446 || nvdimm
->sec
.state
< 0)
449 rc
= nvdimm
->sec
.ops
->query_overwrite(nvdimm
);
452 /* setup delayed work again */
454 queue_delayed_work(system_wq
, &nvdimm
->dwork
, tmo
* HZ
);
455 nvdimm
->sec
.overwrite_tmo
= min(15U * 60U, tmo
);
460 dev_dbg(&nvdimm
->dev
, "overwrite failed\n");
462 dev_dbg(&nvdimm
->dev
, "overwrite completed\n");
464 if (nvdimm
->sec
.overwrite_state
)
465 sysfs_notify_dirent(nvdimm
->sec
.overwrite_state
);
466 nvdimm
->sec
.overwrite_tmo
= 0;
467 clear_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
468 clear_bit(NDD_WORK_PENDING
, &nvdimm
->flags
);
469 put_device(&nvdimm
->dev
);
470 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
471 nvdimm
->sec
.ext_state
= nvdimm_security_state(nvdimm
, NVDIMM_MASTER
);
474 void nvdimm_security_overwrite_query(struct work_struct
*work
)
476 struct nvdimm
*nvdimm
=
477 container_of(work
, typeof(*nvdimm
), dwork
.work
);
479 nvdimm_bus_lock(&nvdimm
->dev
);
480 __nvdimm_security_overwrite_query(nvdimm
);
481 nvdimm_bus_unlock(&nvdimm
->dev
);