tpm_eventlog.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /* read over *pos measurements */
  74. for (i = 0; i < *pos; i++) {
  75. event = addr;
  76. if ((addr + sizeof(struct tcpa_event)) < limit) {
  77. if (event->event_type == 0 && event->event_size == 0)
  78. return NULL;
  79. addr += sizeof(struct tcpa_event) + event->event_size;
  80. }
  81. }
  82. /* now check if current entry is valid */
  83. if ((addr + sizeof(struct tcpa_event)) >= limit)
  84. return NULL;
  85. event = addr;
  86. if ((event->event_type == 0 && event->event_size == 0) ||
  87. ((addr + sizeof(struct tcpa_event) + event->event_size) >= limit))
  88. return NULL;
  89. return addr;
  90. }
  91. static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
  92. loff_t *pos)
  93. {
  94. struct tcpa_event *event = v;
  95. struct tpm_bios_log *log = m->private;
  96. void *limit = log->bios_event_log_end;
  97. v += sizeof(struct tcpa_event) + event->event_size;
  98. /* now check if current entry is valid */
  99. if ((v + sizeof(struct tcpa_event)) >= limit)
  100. return NULL;
  101. event = v;
  102. if (event->event_type == 0 && event->event_size == 0)
  103. return NULL;
  104. if ((event->event_type == 0 && event->event_size == 0) ||
  105. ((v + sizeof(struct tcpa_event) + event->event_size) >= limit))
  106. return NULL;
  107. (*pos)++;
  108. return v;
  109. }
  110. static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
  111. {
  112. }
  113. static int get_event_name(char *dest, struct tcpa_event *event,
  114. unsigned char * event_entry)
  115. {
  116. const char *name = "";
  117. /* 41 so there is room for 40 data and 1 nul */
  118. char data[41] = "";
  119. int i, n_len = 0, d_len = 0;
  120. struct tcpa_pc_event *pc_event;
  121. switch(event->event_type) {
  122. case PREBOOT:
  123. case POST_CODE:
  124. case UNUSED:
  125. case NO_ACTION:
  126. case SCRTM_CONTENTS:
  127. case SCRTM_VERSION:
  128. case CPU_MICROCODE:
  129. case PLATFORM_CONFIG_FLAGS:
  130. case TABLE_OF_DEVICES:
  131. case COMPACT_HASH:
  132. case IPL:
  133. case IPL_PARTITION_DATA:
  134. case NONHOST_CODE:
  135. case NONHOST_CONFIG:
  136. case NONHOST_INFO:
  137. name = tcpa_event_type_strings[event->event_type];
  138. n_len = strlen(name);
  139. break;
  140. case SEPARATOR:
  141. case ACTION:
  142. if (MAX_TEXT_EVENT > event->event_size) {
  143. name = event_entry;
  144. n_len = event->event_size;
  145. }
  146. break;
  147. case EVENT_TAG:
  148. pc_event = (struct tcpa_pc_event *)event_entry;
  149. /* ToDo Row data -> Base64 */
  150. switch (pc_event->event_id) {
  151. case SMBIOS:
  152. case BIS_CERT:
  153. case CMOS:
  154. case NVRAM:
  155. case OPTION_ROM_EXEC:
  156. case OPTION_ROM_CONFIG:
  157. case S_CRTM_VERSION:
  158. name = tcpa_pc_event_id_strings[pc_event->event_id];
  159. n_len = strlen(name);
  160. break;
  161. /* hash data */
  162. case POST_BIOS_ROM:
  163. case ESCD:
  164. case OPTION_ROM_MICROCODE:
  165. case S_CRTM_CONTENTS:
  166. case POST_CONTENTS:
  167. name = tcpa_pc_event_id_strings[pc_event->event_id];
  168. n_len = strlen(name);
  169. for (i = 0; i < 20; i++)
  170. d_len += sprintf(&data[2*i], "%02x",
  171. pc_event->event_data[i]);
  172. break;
  173. default:
  174. break;
  175. }
  176. default:
  177. break;
  178. }
  179. return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
  180. n_len, name, d_len, data);
  181. }
  182. static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
  183. {
  184. struct tcpa_event *event = v;
  185. char *data = v;
  186. int i;
  187. for (i = 0; i < sizeof(struct tcpa_event) + event->event_size; i++)
  188. seq_putc(m, data[i]);
  189. return 0;
  190. }
  191. static int tpm_bios_measurements_release(struct inode *inode,
  192. struct file *file)
  193. {
  194. struct seq_file *seq = file->private_data;
  195. struct tpm_bios_log *log = seq->private;
  196. if (log) {
  197. kfree(log->bios_event_log);
  198. kfree(log);
  199. }
  200. return seq_release(inode, file);
  201. }
  202. static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
  203. {
  204. int len = 0;
  205. char *eventname;
  206. struct tcpa_event *event = v;
  207. unsigned char *event_entry =
  208. (unsigned char *) (v + sizeof(struct tcpa_event));
  209. eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
  210. if (!eventname) {
  211. printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
  212. __func__);
  213. return -EFAULT;
  214. }
  215. seq_printf(m, "%2d ", event->pcr_index);
  216. /* 2nd: SHA1 */
  217. seq_printf(m, "%20phN", event->pcr_value);
  218. /* 3rd: event type identifier */
  219. seq_printf(m, " %02x", event->event_type);
  220. len += get_event_name(eventname, event, event_entry);
  221. /* 4th: eventname <= max + \'0' delimiter */
  222. seq_printf(m, " %s\n", eventname);
  223. kfree(eventname);
  224. return 0;
  225. }
  226. static const struct seq_operations tpm_ascii_b_measurments_seqops = {
  227. .start = tpm_bios_measurements_start,
  228. .next = tpm_bios_measurements_next,
  229. .stop = tpm_bios_measurements_stop,
  230. .show = tpm_ascii_bios_measurements_show,
  231. };
  232. static const struct seq_operations tpm_binary_b_measurments_seqops = {
  233. .start = tpm_bios_measurements_start,
  234. .next = tpm_bios_measurements_next,
  235. .stop = tpm_bios_measurements_stop,
  236. .show = tpm_binary_bios_measurements_show,
  237. };
  238. static int tpm_ascii_bios_measurements_open(struct inode *inode,
  239. struct file *file)
  240. {
  241. int err;
  242. struct tpm_bios_log *log;
  243. struct seq_file *seq;
  244. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  245. if (!log)
  246. return -ENOMEM;
  247. if ((err = read_log(log)))
  248. goto out_free;
  249. /* now register seq file */
  250. err = seq_open(file, &tpm_ascii_b_measurments_seqops);
  251. if (!err) {
  252. seq = file->private_data;
  253. seq->private = log;
  254. } else {
  255. goto out_free;
  256. }
  257. out:
  258. return err;
  259. out_free:
  260. kfree(log->bios_event_log);
  261. kfree(log);
  262. goto out;
  263. }
  264. static const struct file_operations tpm_ascii_bios_measurements_ops = {
  265. .open = tpm_ascii_bios_measurements_open,
  266. .read = seq_read,
  267. .llseek = seq_lseek,
  268. .release = tpm_bios_measurements_release,
  269. };
  270. static int tpm_binary_bios_measurements_open(struct inode *inode,
  271. struct file *file)
  272. {
  273. int err;
  274. struct tpm_bios_log *log;
  275. struct seq_file *seq;
  276. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  277. if (!log)
  278. return -ENOMEM;
  279. if ((err = read_log(log)))
  280. goto out_free;
  281. /* now register seq file */
  282. err = seq_open(file, &tpm_binary_b_measurments_seqops);
  283. if (!err) {
  284. seq = file->private_data;
  285. seq->private = log;
  286. } else {
  287. goto out_free;
  288. }
  289. out:
  290. return err;
  291. out_free:
  292. kfree(log->bios_event_log);
  293. kfree(log);
  294. goto out;
  295. }
  296. static const struct file_operations tpm_binary_bios_measurements_ops = {
  297. .open = tpm_binary_bios_measurements_open,
  298. .read = seq_read,
  299. .llseek = seq_lseek,
  300. .release = tpm_bios_measurements_release,
  301. };
  302. static int is_bad(void *p)
  303. {
  304. if (!p)
  305. return 1;
  306. if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
  307. return 1;
  308. return 0;
  309. }
  310. struct dentry **tpm_bios_log_setup(char *name)
  311. {
  312. struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
  313. tpm_dir = securityfs_create_dir(name, NULL);
  314. if (is_bad(tpm_dir))
  315. goto out;
  316. bin_file =
  317. securityfs_create_file("binary_bios_measurements",
  318. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  319. &tpm_binary_bios_measurements_ops);
  320. if (is_bad(bin_file))
  321. goto out_tpm;
  322. ascii_file =
  323. securityfs_create_file("ascii_bios_measurements",
  324. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  325. &tpm_ascii_bios_measurements_ops);
  326. if (is_bad(ascii_file))
  327. goto out_bin;
  328. ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
  329. if (!ret)
  330. goto out_ascii;
  331. ret[0] = ascii_file;
  332. ret[1] = bin_file;
  333. ret[2] = tpm_dir;
  334. return ret;
  335. out_ascii:
  336. securityfs_remove(ascii_file);
  337. out_bin:
  338. securityfs_remove(bin_file);
  339. out_tpm:
  340. securityfs_remove(tpm_dir);
  341. out:
  342. return NULL;
  343. }
  344. void tpm_bios_log_teardown(struct dentry **lst)
  345. {
  346. int i;
  347. for (i = 0; i < 3; i++)
  348. securityfs_remove(lst[i]);
  349. }