tpm-sysfs.c 13 KB

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