PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/tpm/tpm-sysfs.c

https://github.com/tiwai/sound
C | 529 lines | 394 code | 77 blank | 58 comment | 35 complexity | 27aa0c3c34d9be8866aebb233b8191b9 MD5 | raw file
  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. static struct attribute *tpm1_dev_attrs[] = {
  261. &dev_attr_pubek.attr,
  262. &dev_attr_pcrs.attr,
  263. &dev_attr_enabled.attr,
  264. &dev_attr_active.attr,
  265. &dev_attr_owned.attr,
  266. &dev_attr_temp_deactivated.attr,
  267. &dev_attr_caps.attr,
  268. &dev_attr_cancel.attr,
  269. &dev_attr_durations.attr,
  270. &dev_attr_timeouts.attr,
  271. &dev_attr_tpm_version_major.attr,
  272. NULL,
  273. };
  274. static struct attribute *tpm2_dev_attrs[] = {
  275. &dev_attr_tpm_version_major.attr,
  276. NULL
  277. };
  278. static const struct attribute_group tpm1_dev_group = {
  279. .attrs = tpm1_dev_attrs,
  280. };
  281. static const struct attribute_group tpm2_dev_group = {
  282. .attrs = tpm2_dev_attrs,
  283. };
  284. struct tpm_pcr_attr {
  285. int alg_id;
  286. int pcr;
  287. struct device_attribute attr;
  288. };
  289. #define to_tpm_pcr_attr(a) container_of(a, struct tpm_pcr_attr, attr)
  290. static ssize_t pcr_value_show(struct device *dev,
  291. struct device_attribute *attr,
  292. char *buf)
  293. {
  294. struct tpm_pcr_attr *ha = to_tpm_pcr_attr(attr);
  295. struct tpm_chip *chip = to_tpm_chip(dev);
  296. struct tpm_digest digest;
  297. int i;
  298. int digest_size = 0;
  299. int rc;
  300. char *str = buf;
  301. for (i = 0; i < chip->nr_allocated_banks; i++)
  302. if (ha->alg_id == chip->allocated_banks[i].alg_id)
  303. digest_size = chip->allocated_banks[i].digest_size;
  304. /* should never happen */
  305. if (!digest_size)
  306. return -EINVAL;
  307. digest.alg_id = ha->alg_id;
  308. rc = tpm_pcr_read(chip, ha->pcr, &digest);
  309. if (rc)
  310. return rc;
  311. for (i = 0; i < digest_size; i++)
  312. str += sprintf(str, "%02X", digest.digest[i]);
  313. str += sprintf(str, "\n");
  314. return str - buf;
  315. }
  316. /*
  317. * The following set of defines represents all the magic to build
  318. * the per hash attribute groups for displaying each bank of PCRs.
  319. * The only slight problem with this approach is that every PCR is
  320. * hard coded to be present, so you don't know if an PCR is missing
  321. * until a cat of the file returns -EINVAL
  322. *
  323. * Also note you must ignore checkpatch warnings in this macro
  324. * code. This is deep macro magic that checkpatch.pl doesn't
  325. * understand.
  326. */
  327. /* Note, this must match TPM2_PLATFORM_PCR which is fixed at 24. */
  328. #define _TPM_HELPER(_alg, _hash, F) \
  329. F(_alg, _hash, 0) \
  330. F(_alg, _hash, 1) \
  331. F(_alg, _hash, 2) \
  332. F(_alg, _hash, 3) \
  333. F(_alg, _hash, 4) \
  334. F(_alg, _hash, 5) \
  335. F(_alg, _hash, 6) \
  336. F(_alg, _hash, 7) \
  337. F(_alg, _hash, 8) \
  338. F(_alg, _hash, 9) \
  339. F(_alg, _hash, 10) \
  340. F(_alg, _hash, 11) \
  341. F(_alg, _hash, 12) \
  342. F(_alg, _hash, 13) \
  343. F(_alg, _hash, 14) \
  344. F(_alg, _hash, 15) \
  345. F(_alg, _hash, 16) \
  346. F(_alg, _hash, 17) \
  347. F(_alg, _hash, 18) \
  348. F(_alg, _hash, 19) \
  349. F(_alg, _hash, 20) \
  350. F(_alg, _hash, 21) \
  351. F(_alg, _hash, 22) \
  352. F(_alg, _hash, 23)
  353. /* ignore checkpatch warning about trailing ; in macro. */
  354. #define PCR_ATTR(_alg, _hash, _pcr) \
  355. static struct tpm_pcr_attr dev_attr_pcr_##_hash##_##_pcr = { \
  356. .alg_id = _alg, \
  357. .pcr = _pcr, \
  358. .attr = { \
  359. .attr = { \
  360. .name = __stringify(_pcr), \
  361. .mode = 0444 \
  362. }, \
  363. .show = pcr_value_show \
  364. } \
  365. };
  366. #define PCR_ATTRS(_alg, _hash) \
  367. _TPM_HELPER(_alg, _hash, PCR_ATTR)
  368. /* ignore checkpatch warning about trailing , in macro. */
  369. #define PCR_ATTR_VAL(_alg, _hash, _pcr) \
  370. &dev_attr_pcr_##_hash##_##_pcr.attr.attr,
  371. #define PCR_ATTR_GROUP_ARRAY(_alg, _hash) \
  372. static struct attribute *pcr_group_attrs_##_hash[] = { \
  373. _TPM_HELPER(_alg, _hash, PCR_ATTR_VAL) \
  374. NULL \
  375. }
  376. #define PCR_ATTR_GROUP(_alg, _hash) \
  377. static struct attribute_group pcr_group_##_hash = { \
  378. .name = "pcr-" __stringify(_hash), \
  379. .attrs = pcr_group_attrs_##_hash \
  380. }
  381. #define PCR_ATTR_BUILD(_alg, _hash) \
  382. PCR_ATTRS(_alg, _hash) \
  383. PCR_ATTR_GROUP_ARRAY(_alg, _hash); \
  384. PCR_ATTR_GROUP(_alg, _hash)
  385. /*
  386. * End of macro structure to build an attribute group containing 24
  387. * PCR value files for each supported hash algorithm
  388. */
  389. /*
  390. * The next set of macros implements the cleverness for each hash to
  391. * build a static attribute group called pcr_group_<hash> which can be
  392. * added to chip->groups[].
  393. *
  394. * The first argument is the TPM algorithm id and the second is the
  395. * hash used as both the suffix and the group name. Note: the group
  396. * name is a directory in the top level tpm class with the name
  397. * pcr-<hash>, so it must not clash with any other names already
  398. * in the sysfs directory.
  399. */
  400. PCR_ATTR_BUILD(TPM_ALG_SHA1, sha1);
  401. PCR_ATTR_BUILD(TPM_ALG_SHA256, sha256);
  402. PCR_ATTR_BUILD(TPM_ALG_SHA384, sha384);
  403. PCR_ATTR_BUILD(TPM_ALG_SHA512, sha512);
  404. PCR_ATTR_BUILD(TPM_ALG_SM3_256, sm3);
  405. void tpm_sysfs_add_device(struct tpm_chip *chip)
  406. {
  407. int i;
  408. WARN_ON(chip->groups_cnt != 0);
  409. if (tpm_is_firmware_upgrade(chip))
  410. return;
  411. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  412. chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
  413. else
  414. chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
  415. /* add one group for each bank hash */
  416. for (i = 0; i < chip->nr_allocated_banks; i++) {
  417. switch (chip->allocated_banks[i].alg_id) {
  418. case TPM_ALG_SHA1:
  419. chip->groups[chip->groups_cnt++] = &pcr_group_sha1;
  420. break;
  421. case TPM_ALG_SHA256:
  422. chip->groups[chip->groups_cnt++] = &pcr_group_sha256;
  423. break;
  424. case TPM_ALG_SHA384:
  425. chip->groups[chip->groups_cnt++] = &pcr_group_sha384;
  426. break;
  427. case TPM_ALG_SHA512:
  428. chip->groups[chip->groups_cnt++] = &pcr_group_sha512;
  429. break;
  430. case TPM_ALG_SM3_256:
  431. chip->groups[chip->groups_cnt++] = &pcr_group_sm3;
  432. break;
  433. default:
  434. /*
  435. * If triggers, send a patch to add both a
  436. * PCR_ATTR_BUILD() macro above for the
  437. * missing algorithm as well as an additional
  438. * case in this switch statement.
  439. */
  440. dev_err(&chip->dev,
  441. "TPM with unsupported bank algorithm 0x%04x",
  442. chip->allocated_banks[i].alg_id);
  443. break;
  444. }
  445. }
  446. /*
  447. * This will only trigger if someone has added an additional
  448. * hash to the tpm_algorithms enum without incrementing
  449. * TPM_MAX_HASHES.
  450. */
  451. WARN_ON(chip->groups_cnt > TPM_MAX_HASHES + 1);
  452. }