tpm1_eventlog.c 11 KB

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