Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright(c) 2018 Intel Corporation. All rights reserved. */ |
| 3 | |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/device.h> |
| 6 | #include <linux/ndctl.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/mm.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> |
| 15 | #include "nd-core.h" |
| 16 | #include "nd.h" |
| 17 | |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 18 | #define NVDIMM_BASE_KEY 0 |
| 19 | #define NVDIMM_NEW_KEY 1 |
| 20 | |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 21 | static bool key_revalidate = true; |
| 22 | module_param(key_revalidate, bool, 0444); |
| 23 | MODULE_PARM_DESC(key_revalidate, "Require key validation at init."); |
| 24 | |
Dave Jiang | 037c848 | 2019-03-27 11:10:44 -0700 | [diff] [blame] | 25 | static const char zero_key[NVDIMM_PASSPHRASE_LEN]; |
| 26 | |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 27 | static void *key_data(struct key *key) |
| 28 | { |
| 29 | struct encrypted_key_payload *epayload = dereference_key_locked(key); |
| 30 | |
| 31 | lockdep_assert_held_read(&key->sem); |
| 32 | |
| 33 | return epayload->decrypted_data; |
| 34 | } |
| 35 | |
| 36 | static void nvdimm_put_key(struct key *key) |
| 37 | { |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 38 | if (!key) |
| 39 | return; |
| 40 | |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 41 | up_read(&key->sem); |
| 42 | key_put(key); |
| 43 | } |
| 44 | |
| 45 | /* |
| 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. |
| 49 | */ |
| 50 | static struct key *nvdimm_request_key(struct nvdimm *nvdimm) |
| 51 | { |
| 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; |
| 56 | |
| 57 | sprintf(desc, "%s%s", NVDIMM_PREFIX, nvdimm->dimm_id); |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 58 | key = request_key(&key_type_encrypted, desc, ""); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 59 | if (IS_ERR(key)) { |
| 60 | if (PTR_ERR(key) == -ENOKEY) |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 61 | dev_dbg(dev, "request_key() found no key\n"); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 62 | else |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 63 | dev_dbg(dev, "request_key() upcall failed\n"); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 64 | key = NULL; |
| 65 | } else { |
| 66 | struct encrypted_key_payload *epayload; |
| 67 | |
| 68 | down_read(&key->sem); |
| 69 | epayload = dereference_key_locked(key); |
| 70 | if (epayload->decrypted_datalen != NVDIMM_PASSPHRASE_LEN) { |
| 71 | up_read(&key->sem); |
| 72 | key_put(key); |
| 73 | key = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return key; |
| 78 | } |
| 79 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 80 | static const void *nvdimm_get_key_payload(struct nvdimm *nvdimm, |
| 81 | struct key **key) |
| 82 | { |
| 83 | *key = nvdimm_request_key(nvdimm); |
| 84 | if (!*key) |
| 85 | return zero_key; |
| 86 | |
| 87 | return key_data(*key); |
| 88 | } |
| 89 | |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 90 | static struct key *nvdimm_lookup_user_key(struct nvdimm *nvdimm, |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 91 | key_serial_t id, int subclass) |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 92 | { |
| 93 | key_ref_t keyref; |
| 94 | struct key *key; |
| 95 | struct encrypted_key_payload *epayload; |
| 96 | struct device *dev = &nvdimm->dev; |
| 97 | |
| 98 | keyref = lookup_user_key(id, 0, 0); |
| 99 | if (IS_ERR(keyref)) |
| 100 | return NULL; |
| 101 | |
| 102 | key = key_ref_to_ptr(keyref); |
| 103 | if (key->type != &key_type_encrypted) { |
| 104 | key_put(key); |
| 105 | return NULL; |
| 106 | } |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 107 | |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 108 | dev_dbg(dev, "%s: key found: %#x\n", __func__, key_serial(key)); |
| 109 | |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 110 | down_read_nested(&key->sem, subclass); |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 111 | epayload = dereference_key_locked(key); |
| 112 | if (epayload->decrypted_datalen != NVDIMM_PASSPHRASE_LEN) { |
| 113 | up_read(&key->sem); |
| 114 | key_put(key); |
| 115 | key = NULL; |
| 116 | } |
| 117 | return key; |
| 118 | } |
| 119 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 120 | static const void *nvdimm_get_user_key_payload(struct nvdimm *nvdimm, |
| 121 | key_serial_t id, int subclass, struct key **key) |
| 122 | { |
| 123 | *key = NULL; |
| 124 | if (id == 0) { |
| 125 | if (subclass == NVDIMM_BASE_KEY) |
| 126 | return zero_key; |
| 127 | else |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | *key = nvdimm_lookup_user_key(nvdimm, id, subclass); |
| 132 | if (!*key) |
| 133 | return NULL; |
| 134 | |
| 135 | return key_data(*key); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static int nvdimm_key_revalidate(struct nvdimm *nvdimm) |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 140 | { |
| 141 | struct key *key; |
| 142 | int rc; |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 143 | const void *data; |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 144 | |
| 145 | if (!nvdimm->sec.ops->change_key) |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 146 | return -EOPNOTSUPP; |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 147 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 148 | data = nvdimm_get_key_payload(nvdimm, &key); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Send the same key to the hardware as new and old key to |
| 152 | * verify that the key is good. |
| 153 | */ |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 154 | rc = nvdimm->sec.ops->change_key(nvdimm, data, data, NVDIMM_USER); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 155 | if (rc < 0) { |
| 156 | nvdimm_put_key(key); |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 157 | return rc; |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 158 | } |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 159 | |
| 160 | nvdimm_put_key(key); |
| 161 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
| 162 | return 0; |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int __nvdimm_security_unlock(struct nvdimm *nvdimm) |
| 166 | { |
| 167 | struct device *dev = &nvdimm->dev; |
| 168 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 169 | struct key *key; |
| 170 | const void *data; |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 171 | int rc; |
| 172 | |
| 173 | /* The bus lock should be held at the top level of the call stack */ |
| 174 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 175 | |
| 176 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->unlock |
| 177 | || nvdimm->sec.state < 0) |
| 178 | return -EIO; |
| 179 | |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 180 | if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 181 | dev_dbg(dev, "Security operation in progress.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 182 | return -EBUSY; |
| 183 | } |
| 184 | |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 185 | /* |
| 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. |
| 191 | */ |
| 192 | if (nvdimm->sec.state == NVDIMM_SECURITY_UNLOCKED) { |
| 193 | if (!key_revalidate) |
| 194 | return 0; |
| 195 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 196 | return nvdimm_key_revalidate(nvdimm); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 197 | } else |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 198 | data = nvdimm_get_key_payload(nvdimm, &key); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 199 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 200 | rc = nvdimm->sec.ops->unlock(nvdimm, data); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 201 | dev_dbg(dev, "key: %d unlock: %s\n", key_serial(key), |
| 202 | rc == 0 ? "success" : "fail"); |
| 203 | |
| 204 | nvdimm_put_key(key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 205 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
Dave Jiang | 4c6926a | 2018-12-06 12:40:01 -0800 | [diff] [blame] | 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | int nvdimm_security_unlock(struct device *dev) |
| 210 | { |
| 211 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 212 | int rc; |
| 213 | |
| 214 | nvdimm_bus_lock(dev); |
| 215 | rc = __nvdimm_security_unlock(nvdimm); |
| 216 | nvdimm_bus_unlock(dev); |
| 217 | return rc; |
| 218 | } |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 219 | |
| 220 | int nvdimm_security_disable(struct nvdimm *nvdimm, unsigned int keyid) |
| 221 | { |
| 222 | struct device *dev = &nvdimm->dev; |
| 223 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 224 | struct key *key; |
| 225 | int rc; |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 226 | const void *data; |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 227 | |
| 228 | /* The bus lock should be held at the top level of the call stack */ |
| 229 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 230 | |
| 231 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->disable |
| 232 | || nvdimm->sec.state < 0) |
| 233 | return -EOPNOTSUPP; |
| 234 | |
| 235 | if (nvdimm->sec.state >= NVDIMM_SECURITY_FROZEN) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 236 | dev_dbg(dev, "Incorrect security state: %d\n", |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 237 | nvdimm->sec.state); |
| 238 | return -EIO; |
| 239 | } |
| 240 | |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 241 | if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 242 | dev_dbg(dev, "Security operation in progress.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 243 | return -EBUSY; |
| 244 | } |
| 245 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 246 | data = nvdimm_get_user_key_payload(nvdimm, keyid, |
| 247 | NVDIMM_BASE_KEY, &key); |
| 248 | if (!data) |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 249 | return -ENOKEY; |
| 250 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 251 | rc = nvdimm->sec.ops->disable(nvdimm, data); |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 252 | dev_dbg(dev, "key: %d disable: %s\n", key_serial(key), |
| 253 | rc == 0 ? "success" : "fail"); |
| 254 | |
| 255 | nvdimm_put_key(key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 256 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
Dave Jiang | 03b65b2 | 2018-12-07 10:33:30 -0700 | [diff] [blame] | 257 | return rc; |
| 258 | } |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 259 | |
| 260 | int nvdimm_security_update(struct nvdimm *nvdimm, unsigned int keyid, |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 261 | unsigned int new_keyid, |
| 262 | enum nvdimm_passphrase_type pass_type) |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 263 | { |
| 264 | struct device *dev = &nvdimm->dev; |
| 265 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 266 | struct key *key, *newkey; |
| 267 | int rc; |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 268 | const void *data, *newdata; |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 269 | |
| 270 | /* The bus lock should be held at the top level of the call stack */ |
| 271 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 272 | |
| 273 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->change_key |
| 274 | || nvdimm->sec.state < 0) |
| 275 | return -EOPNOTSUPP; |
| 276 | |
| 277 | if (nvdimm->sec.state >= NVDIMM_SECURITY_FROZEN) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 278 | dev_dbg(dev, "Incorrect security state: %d\n", |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 279 | nvdimm->sec.state); |
| 280 | return -EIO; |
| 281 | } |
| 282 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 283 | data = nvdimm_get_user_key_payload(nvdimm, keyid, |
| 284 | NVDIMM_BASE_KEY, &key); |
| 285 | if (!data) |
| 286 | return -ENOKEY; |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 287 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 288 | newdata = nvdimm_get_user_key_payload(nvdimm, new_keyid, |
| 289 | NVDIMM_NEW_KEY, &newkey); |
| 290 | if (!newdata) { |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 291 | nvdimm_put_key(key); |
| 292 | return -ENOKEY; |
| 293 | } |
| 294 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 295 | rc = nvdimm->sec.ops->change_key(nvdimm, data, newdata, pass_type); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 296 | dev_dbg(dev, "key: %d %d update%s: %s\n", |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 297 | key_serial(key), key_serial(newkey), |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 298 | pass_type == NVDIMM_MASTER ? "(master)" : "(user)", |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 299 | rc == 0 ? "success" : "fail"); |
| 300 | |
| 301 | nvdimm_put_key(newkey); |
| 302 | nvdimm_put_key(key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 303 | if (pass_type == NVDIMM_MASTER) |
| 304 | nvdimm->sec.ext_state = nvdimm_security_state(nvdimm, |
| 305 | NVDIMM_MASTER); |
| 306 | else |
| 307 | nvdimm->sec.state = nvdimm_security_state(nvdimm, |
| 308 | NVDIMM_USER); |
Dave Jiang | d2a4ac7 | 2018-12-07 13:29:09 -0700 | [diff] [blame] | 309 | return rc; |
| 310 | } |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 311 | |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 312 | int nvdimm_security_erase(struct nvdimm *nvdimm, unsigned int keyid, |
| 313 | enum nvdimm_passphrase_type pass_type) |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 314 | { |
| 315 | struct device *dev = &nvdimm->dev; |
| 316 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
Dave Jiang | 037c848 | 2019-03-27 11:10:44 -0700 | [diff] [blame] | 317 | struct key *key = NULL; |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 318 | int rc; |
Dave Jiang | 037c848 | 2019-03-27 11:10:44 -0700 | [diff] [blame] | 319 | const void *data; |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 320 | |
| 321 | /* The bus lock should be held at the top level of the call stack */ |
| 322 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 323 | |
| 324 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->erase |
| 325 | || nvdimm->sec.state < 0) |
| 326 | return -EOPNOTSUPP; |
| 327 | |
| 328 | if (atomic_read(&nvdimm->busy)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 329 | dev_dbg(dev, "Unable to secure erase while DIMM active.\n"); |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 330 | return -EBUSY; |
| 331 | } |
| 332 | |
| 333 | if (nvdimm->sec.state >= NVDIMM_SECURITY_FROZEN) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 334 | dev_dbg(dev, "Incorrect security state: %d\n", |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 335 | nvdimm->sec.state); |
| 336 | return -EIO; |
| 337 | } |
| 338 | |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 339 | if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 340 | dev_dbg(dev, "Security operation in progress.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 341 | return -EBUSY; |
| 342 | } |
| 343 | |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 344 | if (nvdimm->sec.ext_state != NVDIMM_SECURITY_UNLOCKED |
| 345 | && pass_type == NVDIMM_MASTER) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 346 | dev_dbg(dev, |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 347 | "Attempt to secure erase in wrong master state.\n"); |
| 348 | return -EOPNOTSUPP; |
| 349 | } |
| 350 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 351 | data = nvdimm_get_user_key_payload(nvdimm, keyid, |
| 352 | NVDIMM_BASE_KEY, &key); |
| 353 | if (!data) |
| 354 | return -ENOKEY; |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 355 | |
Dave Jiang | 037c848 | 2019-03-27 11:10:44 -0700 | [diff] [blame] | 356 | rc = nvdimm->sec.ops->erase(nvdimm, data, pass_type); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 357 | dev_dbg(dev, "key: %d erase%s: %s\n", key_serial(key), |
| 358 | pass_type == NVDIMM_MASTER ? "(master)" : "(user)", |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 359 | rc == 0 ? "success" : "fail"); |
| 360 | |
| 361 | nvdimm_put_key(key); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 362 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
Dave Jiang | 64e77c8 | 2018-12-07 14:02:12 -0700 | [diff] [blame] | 363 | return rc; |
| 364 | } |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 365 | |
| 366 | int nvdimm_security_overwrite(struct nvdimm *nvdimm, unsigned int keyid) |
| 367 | { |
| 368 | struct device *dev = &nvdimm->dev; |
| 369 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 370 | struct key *key = NULL; |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 371 | int rc; |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 372 | const void *data; |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 373 | |
| 374 | /* The bus lock should be held at the top level of the call stack */ |
| 375 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 376 | |
| 377 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->overwrite |
| 378 | || nvdimm->sec.state < 0) |
| 379 | return -EOPNOTSUPP; |
| 380 | |
| 381 | if (atomic_read(&nvdimm->busy)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 382 | dev_dbg(dev, "Unable to overwrite while DIMM active.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 383 | return -EBUSY; |
| 384 | } |
| 385 | |
| 386 | if (dev->driver == NULL) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 387 | dev_dbg(dev, "Unable to overwrite while DIMM active.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | if (nvdimm->sec.state >= NVDIMM_SECURITY_FROZEN) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 392 | dev_dbg(dev, "Incorrect security state: %d\n", |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 393 | nvdimm->sec.state); |
| 394 | return -EIO; |
| 395 | } |
| 396 | |
| 397 | if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) { |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 398 | dev_dbg(dev, "Security operation in progress.\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 399 | return -EBUSY; |
| 400 | } |
| 401 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 402 | data = nvdimm_get_user_key_payload(nvdimm, keyid, |
| 403 | NVDIMM_BASE_KEY, &key); |
| 404 | if (!data) |
| 405 | return -ENOKEY; |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 406 | |
Dave Jiang | d2e5b64 | 2019-03-27 11:12:45 -0700 | [diff] [blame] | 407 | rc = nvdimm->sec.ops->overwrite(nvdimm, data); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 408 | dev_dbg(dev, "key: %d overwrite submission: %s\n", key_serial(key), |
| 409 | rc == 0 ? "success" : "fail"); |
| 410 | |
| 411 | nvdimm_put_key(key); |
| 412 | if (rc == 0) { |
| 413 | set_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags); |
| 414 | set_bit(NDD_WORK_PENDING, &nvdimm->flags); |
| 415 | nvdimm->sec.state = NVDIMM_SECURITY_OVERWRITE; |
| 416 | /* |
| 417 | * Make sure we don't lose device while doing overwrite |
| 418 | * query. |
| 419 | */ |
| 420 | get_device(dev); |
| 421 | queue_delayed_work(system_wq, &nvdimm->dwork, 0); |
| 422 | } |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 423 | |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | void __nvdimm_security_overwrite_query(struct nvdimm *nvdimm) |
| 428 | { |
| 429 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nvdimm->dev); |
| 430 | int rc; |
| 431 | unsigned int tmo; |
| 432 | |
| 433 | /* The bus lock should be held at the top level of the call stack */ |
| 434 | lockdep_assert_held(&nvdimm_bus->reconfig_mutex); |
| 435 | |
| 436 | /* |
| 437 | * Abort and release device if we no longer have the overwrite |
| 438 | * flag set. It means the work has been canceled. |
| 439 | */ |
| 440 | if (!test_bit(NDD_WORK_PENDING, &nvdimm->flags)) |
| 441 | return; |
| 442 | |
| 443 | tmo = nvdimm->sec.overwrite_tmo; |
| 444 | |
| 445 | if (!nvdimm->sec.ops || !nvdimm->sec.ops->query_overwrite |
| 446 | || nvdimm->sec.state < 0) |
| 447 | return; |
| 448 | |
| 449 | rc = nvdimm->sec.ops->query_overwrite(nvdimm); |
| 450 | if (rc == -EBUSY) { |
| 451 | |
| 452 | /* setup delayed work again */ |
| 453 | tmo += 10; |
| 454 | queue_delayed_work(system_wq, &nvdimm->dwork, tmo * HZ); |
| 455 | nvdimm->sec.overwrite_tmo = min(15U * 60U, tmo); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | if (rc < 0) |
Dan Williams | 37379cf | 2018-12-22 11:35:41 -0800 | [diff] [blame] | 460 | dev_dbg(&nvdimm->dev, "overwrite failed\n"); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 461 | else |
| 462 | dev_dbg(&nvdimm->dev, "overwrite completed\n"); |
| 463 | |
| 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); |
Dave Jiang | 89fa9d8 | 2018-12-10 10:53:22 -0700 | [diff] [blame] | 470 | nvdimm->sec.state = nvdimm_security_state(nvdimm, NVDIMM_USER); |
| 471 | nvdimm->sec.ext_state = nvdimm_security_state(nvdimm, NVDIMM_MASTER); |
Dave Jiang | 7d98809 | 2018-12-13 15:36:18 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void nvdimm_security_overwrite_query(struct work_struct *work) |
| 475 | { |
| 476 | struct nvdimm *nvdimm = |
| 477 | container_of(work, typeof(*nvdimm), dwork.work); |
| 478 | |
| 479 | nvdimm_bus_lock(&nvdimm->dev); |
| 480 | __nvdimm_security_overwrite_query(nvdimm); |
| 481 | nvdimm_bus_unlock(&nvdimm->dev); |
| 482 | } |