tpm_eventlog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (C) 2005, 2012 IBM Corporation
  3. *
  4. * Authors:
  5. * Kent Yoder <key@linux.vnet.ibm.com>
  6. * Seiji Munetoh <munetoh@jp.ibm.com>
  7. * Stefan Berger <stefanb@us.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. * Nayna Jain <nayna@linux.vnet.ibm.com>
  11. *
  12. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  13. *
  14. * Access to the event log created by a system's firmware / BIOS
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. */
  22. #include <linux/seq_file.h>
  23. #include <linux/fs.h>
  24. #include <linux/security.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include "tpm.h"
  28. #include "tpm_eventlog.h"
  29. static const char* tcpa_event_type_strings[] = {
  30. "PREBOOT",
  31. "POST CODE",
  32. "",
  33. "NO ACTION",
  34. "SEPARATOR",
  35. "ACTION",
  36. "EVENT TAG",
  37. "S-CRTM Contents",
  38. "S-CRTM Version",
  39. "CPU Microcode",
  40. "Platform Config Flags",
  41. "Table of Devices",
  42. "Compact Hash",
  43. "IPL",
  44. "IPL Partition Data",
  45. "Non-Host Code",
  46. "Non-Host Config",
  47. "Non-Host Info"
  48. };
  49. static const char* tcpa_pc_event_id_strings[] = {
  50. "",
  51. "SMBIOS",
  52. "BIS Certificate",
  53. "POST BIOS ",
  54. "ESCD ",
  55. "CMOS",
  56. "NVRAM",
  57. "Option ROM",
  58. "Option ROM config",
  59. "",
  60. "Option ROM microcode ",
  61. "S-CRTM Version",
  62. "S-CRTM Contents ",
  63. "POST Contents ",
  64. "Table of Devices",
  65. };
  66. /* returns pointer to start of pos. entry of tcg log */
  67. static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
  68. {
  69. loff_t i;
  70. struct tpm_chip *chip = m->private;
  71. struct tpm_bios_log *log = &chip->log;
  72. void *addr = log->bios_event_log;
  73. void *limit = log->bios_event_log_end;
  74. struct tcpa_event *event;
  75. u32 converted_event_size;
  76. u32 converted_event_type;
  77. /* read over *pos measurements */
  78. for (i = 0; i < *pos; i++) {
  79. event = addr;
  80. converted_event_size =
  81. do_endian_conversion(event->event_size);
  82. converted_event_type =
  83. do_endian_conversion(event->event_type);
  84. if ((addr + sizeof(struct tcpa_event)) < limit) {
  85. if ((converted_event_type == 0) &&
  86. (converted_event_size == 0))
  87. return NULL;
  88. addr += (sizeof(struct tcpa_event) +
  89. converted_event_size);
  90. }
  91. }
  92. /* now check if current entry is valid */
  93. if ((addr + sizeof(struct tcpa_event)) >= limit)
  94. return NULL;
  95. event = addr;
  96. converted_event_size = do_endian_conversion(event->event_size);
  97. converted_event_type = do_endian_conversion(event->event_type);
  98. if (((converted_event_type == 0) && (converted_event_size == 0))
  99. || ((addr + sizeof(struct tcpa_event) + converted_event_size)
  100. >= limit))
  101. return NULL;
  102. return addr;
  103. }
  104. static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
  105. loff_t *pos)
  106. {
  107. struct tcpa_event *event = v;
  108. struct tpm_chip *chip = m->private;
  109. struct tpm_bios_log *log = &chip->log;
  110. void *limit = log->bios_event_log_end;
  111. u32 converted_event_size;
  112. u32 converted_event_type;
  113. converted_event_size = do_endian_conversion(event->event_size);
  114. v += sizeof(struct tcpa_event) + converted_event_size;
  115. /* now check if current entry is valid */
  116. if ((v + sizeof(struct tcpa_event)) >= limit)
  117. return NULL;
  118. event = v;
  119. converted_event_size = do_endian_conversion(event->event_size);
  120. converted_event_type = do_endian_conversion(event->event_type);
  121. if (((converted_event_type == 0) && (converted_event_size == 0)) ||
  122. ((v + sizeof(struct tcpa_event) + converted_event_size) >= limit))
  123. return NULL;
  124. (*pos)++;
  125. return v;
  126. }
  127. static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
  128. {
  129. }
  130. static int get_event_name(char *dest, struct tcpa_event *event,
  131. unsigned char * event_entry)
  132. {
  133. const char *name = "";
  134. /* 41 so there is room for 40 data and 1 nul */
  135. char data[41] = "";
  136. int i, n_len = 0, d_len = 0;
  137. struct tcpa_pc_event *pc_event;
  138. switch (do_endian_conversion(event->event_type)) {
  139. case PREBOOT:
  140. case POST_CODE:
  141. case UNUSED:
  142. case NO_ACTION:
  143. case SCRTM_CONTENTS:
  144. case SCRTM_VERSION:
  145. case CPU_MICROCODE:
  146. case PLATFORM_CONFIG_FLAGS:
  147. case TABLE_OF_DEVICES:
  148. case COMPACT_HASH:
  149. case IPL:
  150. case IPL_PARTITION_DATA:
  151. case NONHOST_CODE:
  152. case NONHOST_CONFIG:
  153. case NONHOST_INFO:
  154. name = tcpa_event_type_strings[do_endian_conversion
  155. (event->event_type)];
  156. n_len = strlen(name);
  157. break;
  158. case SEPARATOR:
  159. case ACTION:
  160. if (MAX_TEXT_EVENT >
  161. do_endian_conversion(event->event_size)) {
  162. name = event_entry;
  163. n_len = do_endian_conversion(event->event_size);
  164. }
  165. break;
  166. case EVENT_TAG:
  167. pc_event = (struct tcpa_pc_event *)event_entry;
  168. /* ToDo Row data -> Base64 */
  169. switch (do_endian_conversion(pc_event->event_id)) {
  170. case SMBIOS:
  171. case BIS_CERT:
  172. case CMOS:
  173. case NVRAM:
  174. case OPTION_ROM_EXEC:
  175. case OPTION_ROM_CONFIG:
  176. case S_CRTM_VERSION:
  177. name = tcpa_pc_event_id_strings[do_endian_conversion
  178. (pc_event->event_id)];
  179. n_len = strlen(name);
  180. break;
  181. /* hash data */
  182. case POST_BIOS_ROM:
  183. case ESCD:
  184. case OPTION_ROM_MICROCODE:
  185. case S_CRTM_CONTENTS:
  186. case POST_CONTENTS:
  187. name = tcpa_pc_event_id_strings[do_endian_conversion
  188. (pc_event->event_id)];
  189. n_len = strlen(name);
  190. for (i = 0; i < 20; i++)
  191. d_len += sprintf(&data[2*i], "%02x",
  192. pc_event->event_data[i]);
  193. break;
  194. default:
  195. break;
  196. }
  197. default:
  198. break;
  199. }
  200. return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
  201. n_len, name, d_len, data);
  202. }
  203. static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
  204. {
  205. struct tcpa_event *event = v;
  206. struct tcpa_event temp_event;
  207. char *temp_ptr;
  208. int i;
  209. memcpy(&temp_event, event, sizeof(struct tcpa_event));
  210. /* convert raw integers for endianness */
  211. temp_event.pcr_index = do_endian_conversion(event->pcr_index);
  212. temp_event.event_type = do_endian_conversion(event->event_type);
  213. temp_event.event_size = do_endian_conversion(event->event_size);
  214. temp_ptr = (char *) &temp_event;
  215. for (i = 0; i < (sizeof(struct tcpa_event) - 1) ; i++)
  216. seq_putc(m, temp_ptr[i]);
  217. temp_ptr = (char *) v;
  218. for (i = (sizeof(struct tcpa_event) - 1);
  219. i < (sizeof(struct tcpa_event) + temp_event.event_size); i++)
  220. seq_putc(m, temp_ptr[i]);
  221. return 0;
  222. }
  223. static int tpm_bios_measurements_release(struct inode *inode,
  224. struct file *file)
  225. {
  226. struct seq_file *seq = (struct seq_file *)file->private_data;
  227. struct tpm_chip *chip = (struct tpm_chip *)seq->private;
  228. put_device(&chip->dev);
  229. return seq_release(inode, file);
  230. }
  231. static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
  232. {
  233. int len = 0;
  234. char *eventname;
  235. struct tcpa_event *event = v;
  236. unsigned char *event_entry =
  237. (unsigned char *)(v + sizeof(struct tcpa_event));
  238. eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
  239. if (!eventname) {
  240. printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
  241. __func__);
  242. return -EFAULT;
  243. }
  244. /* 1st: PCR */
  245. seq_printf(m, "%2d ", do_endian_conversion(event->pcr_index));
  246. /* 2nd: SHA1 */
  247. seq_printf(m, "%20phN", event->pcr_value);
  248. /* 3rd: event type identifier */
  249. seq_printf(m, " %02x", do_endian_conversion(event->event_type));
  250. len += get_event_name(eventname, event, event_entry);
  251. /* 4th: eventname <= max + \'0' delimiter */
  252. seq_printf(m, " %s\n", eventname);
  253. kfree(eventname);
  254. return 0;
  255. }
  256. static const struct seq_operations tpm_ascii_b_measurements_seqops = {
  257. .start = tpm_bios_measurements_start,
  258. .next = tpm_bios_measurements_next,
  259. .stop = tpm_bios_measurements_stop,
  260. .show = tpm_ascii_bios_measurements_show,
  261. };
  262. static const struct seq_operations tpm_binary_b_measurements_seqops = {
  263. .start = tpm_bios_measurements_start,
  264. .next = tpm_bios_measurements_next,
  265. .stop = tpm_bios_measurements_stop,
  266. .show = tpm_binary_bios_measurements_show,
  267. };
  268. static int tpm_bios_measurements_open(struct inode *inode,
  269. struct file *file)
  270. {
  271. int err;
  272. struct seq_file *seq;
  273. struct tpm_chip_seqops *chip_seqops;
  274. const struct seq_operations *seqops;
  275. struct tpm_chip *chip;
  276. inode_lock(inode);
  277. if (!inode->i_private) {
  278. inode_unlock(inode);
  279. return -ENODEV;
  280. }
  281. chip_seqops = (struct tpm_chip_seqops *)inode->i_private;
  282. seqops = chip_seqops->seqops;
  283. chip = chip_seqops->chip;
  284. get_device(&chip->dev);
  285. inode_unlock(inode);
  286. /* now register seq file */
  287. err = seq_open(file, seqops);
  288. if (!err) {
  289. seq = file->private_data;
  290. seq->private = chip;
  291. }
  292. return err;
  293. }
  294. static const struct file_operations tpm_bios_measurements_ops = {
  295. .owner = THIS_MODULE,
  296. .open = tpm_bios_measurements_open,
  297. .read = seq_read,
  298. .llseek = seq_lseek,
  299. .release = tpm_bios_measurements_release,
  300. };
  301. static int tpm_read_log(struct tpm_chip *chip)
  302. {
  303. int rc;
  304. if (chip->log.bios_event_log != NULL) {
  305. dev_dbg(&chip->dev,
  306. "%s: ERROR - event log already initialized\n",
  307. __func__);
  308. return -EFAULT;
  309. }
  310. rc = tpm_read_log_acpi(chip);
  311. if (rc != -ENODEV)
  312. return rc;
  313. return tpm_read_log_of(chip);
  314. }
  315. /*
  316. * tpm_bios_log_setup() - Read the event log from the firmware
  317. * @chip: TPM chip to use.
  318. *
  319. * If an event log is found then the securityfs files are setup to
  320. * export it to userspace, otherwise nothing is done.
  321. *
  322. * Returns -ENODEV if the firmware has no event log or securityfs is not
  323. * supported.
  324. */
  325. int tpm_bios_log_setup(struct tpm_chip *chip)
  326. {
  327. const char *name = dev_name(&chip->dev);
  328. unsigned int cnt;
  329. int rc = 0;
  330. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  331. return 0;
  332. rc = tpm_read_log(chip);
  333. if (rc)
  334. return rc;
  335. cnt = 0;
  336. chip->bios_dir[cnt] = securityfs_create_dir(name, NULL);
  337. /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
  338. * compiled out. The caller should ignore the ENODEV return code.
  339. */
  340. if (IS_ERR(chip->bios_dir[cnt]))
  341. goto err;
  342. cnt++;
  343. chip->bin_log_seqops.chip = chip;
  344. chip->bin_log_seqops.seqops = &tpm_binary_b_measurements_seqops;
  345. chip->bios_dir[cnt] =
  346. securityfs_create_file("binary_bios_measurements",
  347. 0440, chip->bios_dir[0],
  348. (void *)&chip->bin_log_seqops,
  349. &tpm_bios_measurements_ops);
  350. if (IS_ERR(chip->bios_dir[cnt]))
  351. goto err;
  352. cnt++;
  353. chip->ascii_log_seqops.chip = chip;
  354. chip->ascii_log_seqops.seqops = &tpm_ascii_b_measurements_seqops;
  355. chip->bios_dir[cnt] =
  356. securityfs_create_file("ascii_bios_measurements",
  357. 0440, chip->bios_dir[0],
  358. (void *)&chip->ascii_log_seqops,
  359. &tpm_bios_measurements_ops);
  360. if (IS_ERR(chip->bios_dir[cnt]))
  361. goto err;
  362. cnt++;
  363. return 0;
  364. err:
  365. rc = PTR_ERR(chip->bios_dir[cnt]);
  366. chip->bios_dir[cnt] = NULL;
  367. tpm_bios_log_teardown(chip);
  368. return rc;
  369. }
  370. void tpm_bios_log_teardown(struct tpm_chip *chip)
  371. {
  372. int i;
  373. struct inode *inode;
  374. /* securityfs_remove currently doesn't take care of handling sync
  375. * between removal and opening of pseudo files. To handle this, a
  376. * workaround is added by making i_private = NULL here during removal
  377. * and to check it during open(), both within inode_lock()/unlock().
  378. * This design ensures that open() either safely gets kref or fails.
  379. */
  380. for (i = (TPM_NUM_EVENT_LOG_FILES - 1); i >= 0; i--) {
  381. if (chip->bios_dir[i]) {
  382. inode = d_inode(chip->bios_dir[i]);
  383. inode_lock(inode);
  384. inode->i_private = NULL;
  385. inode_unlock(inode);
  386. securityfs_remove(chip->bios_dir[i]);
  387. }
  388. }
  389. }