1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2004 IBM Corporation 4 * Authors: 5 * Leendert van Doorn <leendert@watson.ibm.com> 6 * Dave Safford <safford@watson.ibm.com> 7 * Reiner Sailer <sailer@watson.ibm.com> 8 * Kylene Hall <kjhall@us.ibm.com> 9 * 10 * Copyright (C) 2013 Obsidian Research Corp 11 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com> 12 * 13 * sysfs filesystem inspection interface to the TPM 14 */ 15 #include <linux/device.h> 16 #include "tpm.h" 17 18 struct tpm_readpubek_out { 19 u8 algorithm[4]; 20 u8 encscheme[2]; 21 u8 sigscheme[2]; 22 __be32 paramsize; 23 u8 parameters[12]; 24 __be32 keysize; 25 u8 modulus[256]; 26 u8 checksum[20]; 27 } __packed; 28 29 #define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256) 30 #define TPM_ORD_READPUBEK 124 31 32 static ssize_t pubek_show(struct device *dev, struct device_attribute *attr, 33 char *buf) 34 { 35 struct tpm_readpubek_out *out; 36 int i; 37 char *str = buf; 38 struct tpm_chip *chip = to_tpm_chip(dev); 39 char anti_replay[20]; 40 struct tpm_buf *tpm_buf __free(kfree) = NULL; 41 42 memset(&anti_replay, 0, sizeof(anti_replay)); 43 44 if (tpm_try_get_ops(chip)) 45 return 0; 46 47 tpm_buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 48 if (!tpm_buf) { 49 tpm_put_ops(chip); 50 return 0; 51 } 52 53 tpm_buf_init(tpm_buf, TPM_BUFSIZE); 54 tpm_buf_reset(tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK); 55 tpm_buf_append(tpm_buf, anti_replay, sizeof(anti_replay)); 56 57 if (tpm_transmit_cmd(chip, tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE, 58 "attempting to read the PUBEK")) { 59 tpm_put_ops(chip); 60 return 0; 61 } 62 63 out = (struct tpm_readpubek_out *)&tpm_buf->data[10]; 64 str += 65 sprintf(str, 66 "Algorithm: %4ph\n" 67 "Encscheme: %2ph\n" 68 "Sigscheme: %2ph\n" 69 "Parameters: %12ph\n" 70 "Modulus length: %d\n" 71 "Modulus:\n", 72 out->algorithm, 73 out->encscheme, 74 out->sigscheme, 75 out->parameters, 76 be32_to_cpu(out->keysize)); 77 78 for (i = 0; i < 256; i += 16) 79 str += sprintf(str, "%16ph\n", &out->modulus[i]); 80 81 tpm_put_ops(chip); 82 return str - buf; 83 } 84 static DEVICE_ATTR_RO(pubek); 85 86 static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr, 87 char *buf) 88 { 89 cap_t cap; 90 u8 digest[TPM_DIGEST_SIZE]; 91 u32 i, j, num_pcrs; 92 char *str = buf; 93 struct tpm_chip *chip = to_tpm_chip(dev); 94 95 if (tpm_try_get_ops(chip)) 96 return 0; 97 98 if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap, 99 "attempting to determine the number of PCRS", 100 sizeof(cap.num_pcrs))) { 101 tpm_put_ops(chip); 102 return 0; 103 } 104 105 num_pcrs = be32_to_cpu(cap.num_pcrs); 106 for (i = 0; i < num_pcrs; i++) { 107 if (tpm1_pcr_read(chip, i, digest)) { 108 str = buf; 109 break; 110 } 111 str += sprintf(str, "PCR-%02d: ", i); 112 for (j = 0; j < TPM_DIGEST_SIZE; j++) 113 str += sprintf(str, "%02X ", digest[j]); 114 str += sprintf(str, "\n"); 115 } 116 tpm_put_ops(chip); 117 return str - buf; 118 } 119 static DEVICE_ATTR_RO(pcrs); 120 121 static ssize_t enabled_show(struct device *dev, struct device_attribute *attr, 122 char *buf) 123 { 124 struct tpm_chip *chip = to_tpm_chip(dev); 125 ssize_t rc = 0; 126 cap_t cap; 127 128 if (tpm_try_get_ops(chip)) 129 return 0; 130 131 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap, 132 "attempting to determine the permanent enabled state", 133 sizeof(cap.perm_flags))) 134 goto out_ops; 135 136 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); 137 out_ops: 138 tpm_put_ops(chip); 139 return rc; 140 } 141 static DEVICE_ATTR_RO(enabled); 142 143 static ssize_t active_show(struct device *dev, struct device_attribute *attr, 144 char *buf) 145 { 146 struct tpm_chip *chip = to_tpm_chip(dev); 147 ssize_t rc = 0; 148 cap_t cap; 149 150 if (tpm_try_get_ops(chip)) 151 return 0; 152 153 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap, 154 "attempting to determine the permanent active state", 155 sizeof(cap.perm_flags))) 156 goto out_ops; 157 158 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); 159 out_ops: 160 tpm_put_ops(chip); 161 return rc; 162 } 163 static DEVICE_ATTR_RO(active); 164 165 static ssize_t owned_show(struct device *dev, struct device_attribute *attr, 166 char *buf) 167 { 168 struct tpm_chip *chip = to_tpm_chip(dev); 169 ssize_t rc = 0; 170 cap_t cap; 171 172 if (tpm_try_get_ops(chip)) 173 return 0; 174 175 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap, 176 "attempting to determine the owner state", 177 sizeof(cap.owned))) 178 goto out_ops; 179 180 rc = sprintf(buf, "%d\n", cap.owned); 181 out_ops: 182 tpm_put_ops(chip); 183 return rc; 184 } 185 static DEVICE_ATTR_RO(owned); 186 187 static ssize_t temp_deactivated_show(struct device *dev, 188 struct device_attribute *attr, char *buf) 189 { 190 struct tpm_chip *chip = to_tpm_chip(dev); 191 ssize_t rc = 0; 192 cap_t cap; 193 194 if (tpm_try_get_ops(chip)) 195 return 0; 196 197 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap, 198 "attempting to determine the temporary state", 199 sizeof(cap.stclear_flags))) 200 goto out_ops; 201 202 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); 203 out_ops: 204 tpm_put_ops(chip); 205 return rc; 206 } 207 static DEVICE_ATTR_RO(temp_deactivated); 208 209 static ssize_t caps_show(struct device *dev, struct device_attribute *attr, 210 char *buf) 211 { 212 struct tpm_chip *chip = to_tpm_chip(dev); 213 struct tpm1_version *version; 214 ssize_t rc = 0; 215 char *str = buf; 216 cap_t cap; 217 218 if (tpm_try_get_ops(chip)) 219 return 0; 220 221 if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap, 222 "attempting to determine the manufacturer", 223 sizeof(cap.manufacturer_id))) 224 goto out_ops; 225 226 str += sprintf(str, "Manufacturer: 0x%x\n", 227 be32_to_cpu(cap.manufacturer_id)); 228 229 /* TPM 1.2 */ 230 if (!tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap, 231 "attempting to determine the 1.2 version", 232 sizeof(cap.version2))) { 233 version = &cap.version2.version; 234 goto out_print; 235 } 236 237 /* TPM 1.1 */ 238 if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, 239 "attempting to determine the 1.1 version", 240 sizeof(cap.version1))) { 241 goto out_ops; 242 } 243 244 version = &cap.version1; 245 246 out_print: 247 str += sprintf(str, 248 "TCG version: %d.%d\nFirmware version: %d.%d\n", 249 version->major, version->minor, 250 version->rev_major, version->rev_minor); 251 252 rc = str - buf; 253 254 out_ops: 255 tpm_put_ops(chip); 256 return rc; 257 } 258 static DEVICE_ATTR_RO(caps); 259 260 static ssize_t cancel_store(struct device *dev, struct device_attribute *attr, 261 const char *buf, size_t count) 262 { 263 struct tpm_chip *chip = to_tpm_chip(dev); 264 265 if (tpm_try_get_ops(chip)) 266 return 0; 267 268 chip->ops->cancel(chip); 269 tpm_put_ops(chip); 270 return count; 271 } 272 static DEVICE_ATTR_WO(cancel); 273 274 static ssize_t durations_show(struct device *dev, struct device_attribute *attr, 275 char *buf) 276 { 277 struct tpm_chip *chip = to_tpm_chip(dev); 278 279 if (chip->duration[TPM_LONG] == 0) 280 return 0; 281 282 return sprintf(buf, "%d %d %d [%s]\n", 283 jiffies_to_usecs(chip->duration[TPM_SHORT]), 284 jiffies_to_usecs(chip->duration[TPM_MEDIUM]), 285 jiffies_to_usecs(chip->duration[TPM_LONG]), 286 chip->duration_adjusted 287 ? "adjusted" : "original"); 288 } 289 static DEVICE_ATTR_RO(durations); 290 291 static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr, 292 char *buf) 293 { 294 struct tpm_chip *chip = to_tpm_chip(dev); 295 296 return sprintf(buf, "%d %d %d %d [%s]\n", 297 jiffies_to_usecs(chip->timeout_a), 298 jiffies_to_usecs(chip->timeout_b), 299 jiffies_to_usecs(chip->timeout_c), 300 jiffies_to_usecs(chip->timeout_d), 301 chip->timeout_adjusted 302 ? "adjusted" : "original"); 303 } 304 static DEVICE_ATTR_RO(timeouts); 305 306 static ssize_t tpm_version_major_show(struct device *dev, 307 struct device_attribute *attr, char *buf) 308 { 309 struct tpm_chip *chip = to_tpm_chip(dev); 310 311 return sprintf(buf, "%s\n", chip->flags & TPM_CHIP_FLAG_TPM2 312 ? "2" : "1"); 313 } 314 static DEVICE_ATTR_RO(tpm_version_major); 315 316 #ifdef CONFIG_TCG_TPM2_HMAC 317 static ssize_t null_name_show(struct device *dev, struct device_attribute *attr, 318 char *buf) 319 { 320 struct tpm_chip *chip = to_tpm_chip(dev); 321 int size = TPM2_NAME_SIZE; 322 323 bin2hex(buf, chip->null_key_name, size); 324 size *= 2; 325 buf[size++] = '\n'; 326 return size; 327 } 328 static DEVICE_ATTR_RO(null_name); 329 #endif 330 331 static struct attribute *tpm1_dev_attrs[] = { 332 &dev_attr_pubek.attr, 333 &dev_attr_pcrs.attr, 334 &dev_attr_enabled.attr, 335 &dev_attr_active.attr, 336 &dev_attr_owned.attr, 337 &dev_attr_temp_deactivated.attr, 338 &dev_attr_caps.attr, 339 &dev_attr_cancel.attr, 340 &dev_attr_durations.attr, 341 &dev_attr_timeouts.attr, 342 &dev_attr_tpm_version_major.attr, 343 NULL, 344 }; 345 346 static struct attribute *tpm2_dev_attrs[] = { 347 &dev_attr_tpm_version_major.attr, 348 #ifdef CONFIG_TCG_TPM2_HMAC 349 &dev_attr_null_name.attr, 350 #endif 351 NULL 352 }; 353 354 static const struct attribute_group tpm1_dev_group = { 355 .attrs = tpm1_dev_attrs, 356 }; 357 358 static const struct attribute_group tpm2_dev_group = { 359 .attrs = tpm2_dev_attrs, 360 }; 361 362 struct tpm_pcr_attr { 363 int alg_id; 364 int pcr; 365 struct device_attribute attr; 366 }; 367 368 #define to_tpm_pcr_attr(a) container_of(a, struct tpm_pcr_attr, attr) 369 370 static ssize_t pcr_value_show(struct device *dev, 371 struct device_attribute *attr, 372 char *buf) 373 { 374 struct tpm_pcr_attr *ha = to_tpm_pcr_attr(attr); 375 struct tpm_chip *chip = to_tpm_chip(dev); 376 struct tpm_digest digest; 377 int i; 378 int digest_size = 0; 379 int rc; 380 char *str = buf; 381 382 for (i = 0; i < chip->nr_allocated_banks; i++) 383 if (ha->alg_id == chip->allocated_banks[i].alg_id) 384 digest_size = chip->allocated_banks[i].digest_size; 385 /* should never happen */ 386 if (!digest_size) 387 return -EINVAL; 388 389 digest.alg_id = ha->alg_id; 390 rc = tpm_pcr_read(chip, ha->pcr, &digest); 391 if (rc) 392 return rc; 393 for (i = 0; i < digest_size; i++) 394 str += sprintf(str, "%02X", digest.digest[i]); 395 str += sprintf(str, "\n"); 396 397 return str - buf; 398 } 399 400 /* 401 * The following set of defines represents all the magic to build 402 * the per hash attribute groups for displaying each bank of PCRs. 403 * The only slight problem with this approach is that every PCR is 404 * hard coded to be present, so you don't know if an PCR is missing 405 * until a cat of the file returns -EINVAL 406 * 407 * Also note you must ignore checkpatch warnings in this macro 408 * code. This is deep macro magic that checkpatch.pl doesn't 409 * understand. 410 */ 411 412 /* Note, this must match TPM2_PLATFORM_PCR which is fixed at 24. */ 413 #define _TPM_HELPER(_alg, _hash, F) \ 414 F(_alg, _hash, 0) \ 415 F(_alg, _hash, 1) \ 416 F(_alg, _hash, 2) \ 417 F(_alg, _hash, 3) \ 418 F(_alg, _hash, 4) \ 419 F(_alg, _hash, 5) \ 420 F(_alg, _hash, 6) \ 421 F(_alg, _hash, 7) \ 422 F(_alg, _hash, 8) \ 423 F(_alg, _hash, 9) \ 424 F(_alg, _hash, 10) \ 425 F(_alg, _hash, 11) \ 426 F(_alg, _hash, 12) \ 427 F(_alg, _hash, 13) \ 428 F(_alg, _hash, 14) \ 429 F(_alg, _hash, 15) \ 430 F(_alg, _hash, 16) \ 431 F(_alg, _hash, 17) \ 432 F(_alg, _hash, 18) \ 433 F(_alg, _hash, 19) \ 434 F(_alg, _hash, 20) \ 435 F(_alg, _hash, 21) \ 436 F(_alg, _hash, 22) \ 437 F(_alg, _hash, 23) 438 439 /* ignore checkpatch warning about trailing ; in macro. */ 440 #define PCR_ATTR(_alg, _hash, _pcr) \ 441 static struct tpm_pcr_attr dev_attr_pcr_##_hash##_##_pcr = { \ 442 .alg_id = _alg, \ 443 .pcr = _pcr, \ 444 .attr = { \ 445 .attr = { \ 446 .name = __stringify(_pcr), \ 447 .mode = 0444 \ 448 }, \ 449 .show = pcr_value_show \ 450 } \ 451 }; 452 453 #define PCR_ATTRS(_alg, _hash) \ 454 _TPM_HELPER(_alg, _hash, PCR_ATTR) 455 456 /* ignore checkpatch warning about trailing , in macro. */ 457 #define PCR_ATTR_VAL(_alg, _hash, _pcr) \ 458 &dev_attr_pcr_##_hash##_##_pcr.attr.attr, 459 460 #define PCR_ATTR_GROUP_ARRAY(_alg, _hash) \ 461 static struct attribute *pcr_group_attrs_##_hash[] = { \ 462 _TPM_HELPER(_alg, _hash, PCR_ATTR_VAL) \ 463 NULL \ 464 } 465 466 #define PCR_ATTR_GROUP(_alg, _hash) \ 467 static struct attribute_group pcr_group_##_hash = { \ 468 .name = "pcr-" __stringify(_hash), \ 469 .attrs = pcr_group_attrs_##_hash \ 470 } 471 472 #define PCR_ATTR_BUILD(_alg, _hash) \ 473 PCR_ATTRS(_alg, _hash) \ 474 PCR_ATTR_GROUP_ARRAY(_alg, _hash); \ 475 PCR_ATTR_GROUP(_alg, _hash) 476 /* 477 * End of macro structure to build an attribute group containing 24 478 * PCR value files for each supported hash algorithm 479 */ 480 481 /* 482 * The next set of macros implements the cleverness for each hash to 483 * build a static attribute group called pcr_group_<hash> which can be 484 * added to chip->groups[]. 485 * 486 * The first argument is the TPM algorithm id and the second is the 487 * hash used as both the suffix and the group name. Note: the group 488 * name is a directory in the top level tpm class with the name 489 * pcr-<hash>, so it must not clash with any other names already 490 * in the sysfs directory. 491 */ 492 PCR_ATTR_BUILD(TPM_ALG_SHA1, sha1); 493 PCR_ATTR_BUILD(TPM_ALG_SHA256, sha256); 494 PCR_ATTR_BUILD(TPM_ALG_SHA384, sha384); 495 PCR_ATTR_BUILD(TPM_ALG_SHA512, sha512); 496 PCR_ATTR_BUILD(TPM_ALG_SM3_256, sm3); 497 498 499 void tpm_sysfs_add_device(struct tpm_chip *chip) 500 { 501 int i; 502 503 WARN_ON(chip->groups_cnt != 0); 504 505 if (tpm_is_firmware_upgrade(chip)) 506 return; 507 508 if (chip->flags & TPM_CHIP_FLAG_TPM2) 509 chip->groups[chip->groups_cnt++] = &tpm2_dev_group; 510 else 511 chip->groups[chip->groups_cnt++] = &tpm1_dev_group; 512 513 /* add one group for each bank hash */ 514 for (i = 0; i < chip->nr_allocated_banks; i++) { 515 switch (chip->allocated_banks[i].alg_id) { 516 case TPM_ALG_SHA1: 517 chip->groups[chip->groups_cnt++] = &pcr_group_sha1; 518 break; 519 case TPM_ALG_SHA256: 520 chip->groups[chip->groups_cnt++] = &pcr_group_sha256; 521 break; 522 case TPM_ALG_SHA384: 523 chip->groups[chip->groups_cnt++] = &pcr_group_sha384; 524 break; 525 case TPM_ALG_SHA512: 526 chip->groups[chip->groups_cnt++] = &pcr_group_sha512; 527 break; 528 case TPM_ALG_SM3_256: 529 chip->groups[chip->groups_cnt++] = &pcr_group_sm3; 530 break; 531 default: 532 /* 533 * If triggers, send a patch to add both a 534 * PCR_ATTR_BUILD() macro above for the 535 * missing algorithm as well as an additional 536 * case in this switch statement. 537 */ 538 dev_err(&chip->dev, 539 "TPM with unsupported bank algorithm 0x%04x", 540 chip->allocated_banks[i].alg_id); 541 break; 542 } 543 } 544 545 /* 546 * This will only trigger if someone has added an additional 547 * hash to the tpm_algorithms enum without incrementing 548 * TPM_MAX_HASHES. 549 */ 550 WARN_ON(chip->groups_cnt > TPM_MAX_HASHES + 1); 551 } 552