tpm_eventlog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. *
  11. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  12. *
  13. * Access to the eventlog created by a system's firmware / BIOS
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <linux/seq_file.h>
  22. #include <linux/fs.h>
  23. #include <linux/security.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include "tpm.h"
  27. #include "tpm_eventlog.h"
  28. static const char* tcpa_event_type_strings[] = {
  29. "PREBOOT",
  30. "POST CODE",
  31. "",
  32. "NO ACTION",
  33. "SEPARATOR",
  34. "ACTION",
  35. "EVENT TAG",
  36. "S-CRTM Contents",
  37. "S-CRTM Version",
  38. "CPU Microcode",
  39. "Platform Config Flags",
  40. "Table of Devices",
  41. "Compact Hash",
  42. "IPL",
  43. "IPL Partition Data",
  44. "Non-Host Code",
  45. "Non-Host Config",
  46. "Non-Host Info"
  47. };
  48. static const char* tcpa_pc_event_id_strings[] = {
  49. "",
  50. "SMBIOS",
  51. "BIS Certificate",
  52. "POST BIOS ",
  53. "ESCD ",
  54. "CMOS",
  55. "NVRAM",
  56. "Option ROM",
  57. "Option ROM config",
  58. "",
  59. "Option ROM microcode ",
  60. "S-CRTM Version",
  61. "S-CRTM Contents ",
  62. "POST Contents ",
  63. "Table of Devices",
  64. };
  65. /* returns pointer to start of pos. entry of tcg log */
  66. static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
  67. {
  68. loff_t i;
  69. struct tpm_bios_log *log = m->private;
  70. void *addr = log->bios_event_log;
  71. void *limit = log->bios_event_log_end;
  72. struct tcpa_event *event;
  73. u32 converted_event_size;
  74. u32 converted_event_type;
  75. /* read over *pos measurements */
  76. for (i = 0; i < *pos; i++) {
  77. event = addr;
  78. converted_event_size =
  79. do_endian_conversion(event->event_size);
  80. converted_event_type =
  81. do_endian_conversion(event->event_type);
  82. if ((addr + sizeof(struct tcpa_event)) < limit) {
  83. if ((converted_event_type == 0) &&
  84. (converted_event_size == 0))
  85. return NULL;
  86. addr += (sizeof(struct tcpa_event) +
  87. converted_event_size);
  88. }
  89. }
  90. /* now check if current entry is valid */
  91. if ((addr + sizeof(struct tcpa_event)) >= limit)
  92. return NULL;
  93. event = addr;
  94. converted_event_size = do_endian_conversion(event->event_size);
  95. converted_event_type = do_endian_conversion(event->event_type);
  96. if (((converted_event_type == 0) && (converted_event_size == 0))
  97. || ((addr + sizeof(struct tcpa_event) + converted_event_size)
  98. >= limit))
  99. return NULL;
  100. return addr;
  101. }
  102. static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
  103. loff_t *pos)
  104. {
  105. struct tcpa_event *event = v;
  106. struct tpm_bios_log *log = m->private;
  107. void *limit = log->bios_event_log_end;
  108. u32 converted_event_size;
  109. u32 converted_event_type;
  110. converted_event_size = do_endian_conversion(event->event_size);
  111. v += sizeof(struct tcpa_event) + converted_event_size;
  112. /* now check if current entry is valid */
  113. if ((v + sizeof(struct tcpa_event)) >= limit)
  114. return NULL;
  115. event = v;
  116. converted_event_size = do_endian_conversion(event->event_size);
  117. converted_event_type = do_endian_conversion(event->event_type);
  118. if (((converted_event_type == 0) && (converted_event_size == 0)) ||
  119. ((v + sizeof(struct tcpa_event) + converted_event_size) >= limit))
  120. return NULL;
  121. (*pos)++;
  122. return v;
  123. }
  124. static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
  125. {
  126. }
  127. static int get_event_name(char *dest, struct tcpa_event *event,
  128. unsigned char * event_entry)
  129. {
  130. const char *name = "";
  131. /* 41 so there is room for 40 data and 1 nul */
  132. char data[41] = "";
  133. int i, n_len = 0, d_len = 0;
  134. struct tcpa_pc_event *pc_event;
  135. switch (do_endian_conversion(event->event_type)) {
  136. case PREBOOT:
  137. case POST_CODE:
  138. case UNUSED:
  139. case NO_ACTION:
  140. case SCRTM_CONTENTS:
  141. case SCRTM_VERSION:
  142. case CPU_MICROCODE:
  143. case PLATFORM_CONFIG_FLAGS:
  144. case TABLE_OF_DEVICES:
  145. case COMPACT_HASH:
  146. case IPL:
  147. case IPL_PARTITION_DATA:
  148. case NONHOST_CODE:
  149. case NONHOST_CONFIG:
  150. case NONHOST_INFO:
  151. name = tcpa_event_type_strings[do_endian_conversion
  152. (event->event_type)];
  153. n_len = strlen(name);
  154. break;
  155. case SEPARATOR:
  156. case ACTION:
  157. if (MAX_TEXT_EVENT >
  158. do_endian_conversion(event->event_size)) {
  159. name = event_entry;
  160. n_len = do_endian_conversion(event->event_size);
  161. }
  162. break;
  163. case EVENT_TAG:
  164. pc_event = (struct tcpa_pc_event *)event_entry;
  165. /* ToDo Row data -> Base64 */
  166. switch (do_endian_conversion(pc_event->event_id)) {
  167. case SMBIOS:
  168. case BIS_CERT:
  169. case CMOS:
  170. case NVRAM:
  171. case OPTION_ROM_EXEC:
  172. case OPTION_ROM_CONFIG:
  173. case S_CRTM_VERSION:
  174. name = tcpa_pc_event_id_strings[do_endian_conversion
  175. (pc_event->event_id)];
  176. n_len = strlen(name);
  177. break;
  178. /* hash data */
  179. case POST_BIOS_ROM:
  180. case ESCD:
  181. case OPTION_ROM_MICROCODE:
  182. case S_CRTM_CONTENTS:
  183. case POST_CONTENTS:
  184. name = tcpa_pc_event_id_strings[do_endian_conversion
  185. (pc_event->event_id)];
  186. n_len = strlen(name);
  187. for (i = 0; i < 20; i++)
  188. d_len += sprintf(&data[2*i], "%02x",
  189. pc_event->event_data[i]);
  190. break;
  191. default:
  192. break;
  193. }
  194. default:
  195. break;
  196. }
  197. return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
  198. n_len, name, d_len, data);
  199. }
  200. static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
  201. {
  202. struct tcpa_event *event = v;
  203. struct tcpa_event temp_event;
  204. char *temp_ptr;
  205. int i;
  206. memcpy(&temp_event, event, sizeof(struct tcpa_event));
  207. /* convert raw integers for endianness */
  208. temp_event.pcr_index = do_endian_conversion(event->pcr_index);
  209. temp_event.event_type = do_endian_conversion(event->event_type);
  210. temp_event.event_size = do_endian_conversion(event->event_size);
  211. temp_ptr = (char *) &temp_event;
  212. for (i = 0; i < (sizeof(struct tcpa_event) - 1) ; i++)
  213. seq_putc(m, temp_ptr[i]);
  214. temp_ptr = (char *) v;
  215. for (i = (sizeof(struct tcpa_event) - 1);
  216. i < (sizeof(struct tcpa_event) + temp_event.event_size); i++)
  217. seq_putc(m, temp_ptr[i]);
  218. return 0;
  219. }
  220. static int tpm_bios_measurements_release(struct inode *inode,
  221. struct file *file)
  222. {
  223. struct seq_file *seq = file->private_data;
  224. struct tpm_bios_log *log = seq->private;
  225. if (log) {
  226. kfree(log->bios_event_log);
  227. kfree(log);
  228. }
  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_measurments_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_measurments_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_ascii_bios_measurements_open(struct inode *inode,
  269. struct file *file)
  270. {
  271. int err;
  272. struct tpm_bios_log *log;
  273. struct seq_file *seq;
  274. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  275. if (!log)
  276. return -ENOMEM;
  277. if ((err = read_log(log)))
  278. goto out_free;
  279. /* now register seq file */
  280. err = seq_open(file, &tpm_ascii_b_measurments_seqops);
  281. if (!err) {
  282. seq = file->private_data;
  283. seq->private = log;
  284. } else {
  285. goto out_free;
  286. }
  287. out:
  288. return err;
  289. out_free:
  290. kfree(log->bios_event_log);
  291. kfree(log);
  292. goto out;
  293. }
  294. static const struct file_operations tpm_ascii_bios_measurements_ops = {
  295. .open = tpm_ascii_bios_measurements_open,
  296. .read = seq_read,
  297. .llseek = seq_lseek,
  298. .release = tpm_bios_measurements_release,
  299. };
  300. static int tpm_binary_bios_measurements_open(struct inode *inode,
  301. struct file *file)
  302. {
  303. int err;
  304. struct tpm_bios_log *log;
  305. struct seq_file *seq;
  306. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  307. if (!log)
  308. return -ENOMEM;
  309. if ((err = read_log(log)))
  310. goto out_free;
  311. /* now register seq file */
  312. err = seq_open(file, &tpm_binary_b_measurments_seqops);
  313. if (!err) {
  314. seq = file->private_data;
  315. seq->private = log;
  316. } else {
  317. goto out_free;
  318. }
  319. out:
  320. return err;
  321. out_free:
  322. kfree(log->bios_event_log);
  323. kfree(log);
  324. goto out;
  325. }
  326. static const struct file_operations tpm_binary_bios_measurements_ops = {
  327. .open = tpm_binary_bios_measurements_open,
  328. .read = seq_read,
  329. .llseek = seq_lseek,
  330. .release = tpm_bios_measurements_release,
  331. };
  332. static int is_bad(void *p)
  333. {
  334. if (!p)
  335. return 1;
  336. if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
  337. return 1;
  338. return 0;
  339. }
  340. struct dentry **tpm_bios_log_setup(char *name)
  341. {
  342. struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
  343. tpm_dir = securityfs_create_dir(name, NULL);
  344. if (is_bad(tpm_dir))
  345. goto out;
  346. bin_file =
  347. securityfs_create_file("binary_bios_measurements",
  348. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  349. &tpm_binary_bios_measurements_ops);
  350. if (is_bad(bin_file))
  351. goto out_tpm;
  352. ascii_file =
  353. securityfs_create_file("ascii_bios_measurements",
  354. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  355. &tpm_ascii_bios_measurements_ops);
  356. if (is_bad(ascii_file))
  357. goto out_bin;
  358. ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
  359. if (!ret)
  360. goto out_ascii;
  361. ret[0] = ascii_file;
  362. ret[1] = bin_file;
  363. ret[2] = tpm_dir;
  364. return ret;
  365. out_ascii:
  366. securityfs_remove(ascii_file);
  367. out_bin:
  368. securityfs_remove(bin_file);
  369. out_tpm:
  370. securityfs_remove(tpm_dir);
  371. out:
  372. return NULL;
  373. }
  374. void tpm_bios_log_teardown(struct dentry **lst)
  375. {
  376. int i;
  377. for (i = 0; i < 3; i++)
  378. securityfs_remove(lst[i]);
  379. }