tpm_bios.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (C) 2005 IBM Corporation
  3. *
  4. * Authors:
  5. * Seiji Munetoh <munetoh@jp.ibm.com>
  6. * Stefan Berger <stefanb@us.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Access to the eventlog extended by the TCG BIOS of PC platform
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/seq_file.h>
  21. #include <linux/fs.h>
  22. #include <linux/security.h>
  23. #include <linux/module.h>
  24. #include <acpi/acpi.h>
  25. #include "tpm.h"
  26. #define TCG_EVENT_NAME_LEN_MAX 255
  27. #define MAX_TEXT_EVENT 1000 /* Max event string length */
  28. #define ACPI_TCPA_SIG "TCPA" /* 0x41504354 /'TCPA' */
  29. enum bios_platform_class {
  30. BIOS_CLIENT = 0x00,
  31. BIOS_SERVER = 0x01,
  32. };
  33. struct tpm_bios_log {
  34. void *bios_event_log;
  35. void *bios_event_log_end;
  36. };
  37. struct acpi_tcpa {
  38. struct acpi_table_header hdr;
  39. u16 platform_class;
  40. union {
  41. struct client_hdr {
  42. u32 log_max_len __attribute__ ((packed));
  43. u64 log_start_addr __attribute__ ((packed));
  44. } client;
  45. struct server_hdr {
  46. u16 reserved;
  47. u64 log_max_len __attribute__ ((packed));
  48. u64 log_start_addr __attribute__ ((packed));
  49. } server;
  50. };
  51. };
  52. struct tcpa_event {
  53. u32 pcr_index;
  54. u32 event_type;
  55. u8 pcr_value[20]; /* SHA1 */
  56. u32 event_size;
  57. u8 event_data[0];
  58. };
  59. enum tcpa_event_types {
  60. PREBOOT = 0,
  61. POST_CODE,
  62. UNUSED,
  63. NO_ACTION,
  64. SEPARATOR,
  65. ACTION,
  66. EVENT_TAG,
  67. SCRTM_CONTENTS,
  68. SCRTM_VERSION,
  69. CPU_MICROCODE,
  70. PLATFORM_CONFIG_FLAGS,
  71. TABLE_OF_DEVICES,
  72. COMPACT_HASH,
  73. IPL,
  74. IPL_PARTITION_DATA,
  75. NONHOST_CODE,
  76. NONHOST_CONFIG,
  77. NONHOST_INFO,
  78. };
  79. static const char* tcpa_event_type_strings[] = {
  80. "PREBOOT",
  81. "POST CODE",
  82. "",
  83. "NO ACTION",
  84. "SEPARATOR",
  85. "ACTION",
  86. "EVENT TAG",
  87. "S-CRTM Contents",
  88. "S-CRTM Version",
  89. "CPU Microcode",
  90. "Platform Config Flags",
  91. "Table of Devices",
  92. "Compact Hash",
  93. "IPL",
  94. "IPL Partition Data",
  95. "Non-Host Code",
  96. "Non-Host Config",
  97. "Non-Host Info"
  98. };
  99. struct tcpa_pc_event {
  100. u32 event_id;
  101. u32 event_size;
  102. u8 event_data[0];
  103. };
  104. enum tcpa_pc_event_ids {
  105. SMBIOS = 1,
  106. BIS_CERT,
  107. POST_BIOS_ROM,
  108. ESCD,
  109. CMOS,
  110. NVRAM,
  111. OPTION_ROM_EXEC,
  112. OPTION_ROM_CONFIG,
  113. OPTION_ROM_MICROCODE = 10,
  114. S_CRTM_VERSION,
  115. S_CRTM_CONTENTS,
  116. POST_CONTENTS,
  117. HOST_TABLE_OF_DEVICES,
  118. };
  119. static const char* tcpa_pc_event_id_strings[] = {
  120. "",
  121. "SMBIOS",
  122. "BIS Certificate",
  123. "POST BIOS ",
  124. "ESCD ",
  125. "CMOS",
  126. "NVRAM",
  127. "Option ROM",
  128. "Option ROM config",
  129. "",
  130. "Option ROM microcode ",
  131. "S-CRTM Version",
  132. "S-CRTM Contents ",
  133. "POST Contents ",
  134. "Table of Devices",
  135. };
  136. /* returns pointer to start of pos. entry of tcg log */
  137. static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
  138. {
  139. loff_t i;
  140. struct tpm_bios_log *log = m->private;
  141. void *addr = log->bios_event_log;
  142. void *limit = log->bios_event_log_end;
  143. struct tcpa_event *event;
  144. /* read over *pos measurements */
  145. for (i = 0; i < *pos; i++) {
  146. event = addr;
  147. if ((addr + sizeof(struct tcpa_event)) < limit) {
  148. if (event->event_type == 0 && event->event_size == 0)
  149. return NULL;
  150. addr += sizeof(struct tcpa_event) + event->event_size;
  151. }
  152. }
  153. /* now check if current entry is valid */
  154. if ((addr + sizeof(struct tcpa_event)) >= limit)
  155. return NULL;
  156. event = addr;
  157. if ((event->event_type == 0 && event->event_size == 0) ||
  158. ((addr + sizeof(struct tcpa_event) + event->event_size) >= limit))
  159. return NULL;
  160. return addr;
  161. }
  162. static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
  163. loff_t *pos)
  164. {
  165. struct tcpa_event *event = v;
  166. struct tpm_bios_log *log = m->private;
  167. void *limit = log->bios_event_log_end;
  168. v += sizeof(struct tcpa_event) + event->event_size;
  169. /* now check if current entry is valid */
  170. if ((v + sizeof(struct tcpa_event)) >= limit)
  171. return NULL;
  172. event = v;
  173. if (event->event_type == 0 && event->event_size == 0)
  174. return NULL;
  175. if ((event->event_type == 0 && event->event_size == 0) ||
  176. ((v + sizeof(struct tcpa_event) + event->event_size) >= limit))
  177. return NULL;
  178. (*pos)++;
  179. return v;
  180. }
  181. static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
  182. {
  183. }
  184. static int get_event_name(char *dest, struct tcpa_event *event,
  185. unsigned char * event_entry)
  186. {
  187. const char *name = "";
  188. /* 41 so there is room for 40 data and 1 nul */
  189. char data[41] = "";
  190. int i, n_len = 0, d_len = 0;
  191. struct tcpa_pc_event *pc_event;
  192. switch(event->event_type) {
  193. case PREBOOT:
  194. case POST_CODE:
  195. case UNUSED:
  196. case NO_ACTION:
  197. case SCRTM_CONTENTS:
  198. case SCRTM_VERSION:
  199. case CPU_MICROCODE:
  200. case PLATFORM_CONFIG_FLAGS:
  201. case TABLE_OF_DEVICES:
  202. case COMPACT_HASH:
  203. case IPL:
  204. case IPL_PARTITION_DATA:
  205. case NONHOST_CODE:
  206. case NONHOST_CONFIG:
  207. case NONHOST_INFO:
  208. name = tcpa_event_type_strings[event->event_type];
  209. n_len = strlen(name);
  210. break;
  211. case SEPARATOR:
  212. case ACTION:
  213. if (MAX_TEXT_EVENT > event->event_size) {
  214. name = event_entry;
  215. n_len = event->event_size;
  216. }
  217. break;
  218. case EVENT_TAG:
  219. pc_event = (struct tcpa_pc_event *)event_entry;
  220. /* ToDo Row data -> Base64 */
  221. switch (pc_event->event_id) {
  222. case SMBIOS:
  223. case BIS_CERT:
  224. case CMOS:
  225. case NVRAM:
  226. case OPTION_ROM_EXEC:
  227. case OPTION_ROM_CONFIG:
  228. case S_CRTM_VERSION:
  229. name = tcpa_pc_event_id_strings[pc_event->event_id];
  230. n_len = strlen(name);
  231. break;
  232. /* hash data */
  233. case POST_BIOS_ROM:
  234. case ESCD:
  235. case OPTION_ROM_MICROCODE:
  236. case S_CRTM_CONTENTS:
  237. case POST_CONTENTS:
  238. name = tcpa_pc_event_id_strings[pc_event->event_id];
  239. n_len = strlen(name);
  240. for (i = 0; i < 20; i++)
  241. d_len += sprintf(&data[2*i], "%02x",
  242. pc_event->event_data[i]);
  243. break;
  244. default:
  245. break;
  246. }
  247. default:
  248. break;
  249. }
  250. return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
  251. n_len, name, d_len, data);
  252. }
  253. static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
  254. {
  255. struct tcpa_event *event = v;
  256. char *data = v;
  257. int i;
  258. for (i = 0; i < sizeof(struct tcpa_event) + event->event_size; i++)
  259. seq_putc(m, data[i]);
  260. return 0;
  261. }
  262. static int tpm_bios_measurements_release(struct inode *inode,
  263. struct file *file)
  264. {
  265. struct seq_file *seq = file->private_data;
  266. struct tpm_bios_log *log = seq->private;
  267. if (log) {
  268. kfree(log->bios_event_log);
  269. kfree(log);
  270. }
  271. return seq_release(inode, file);
  272. }
  273. static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
  274. {
  275. int len = 0;
  276. int i;
  277. char *eventname;
  278. struct tcpa_event *event = v;
  279. unsigned char *event_entry =
  280. (unsigned char *) (v + sizeof(struct tcpa_event));
  281. eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
  282. if (!eventname) {
  283. printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
  284. __func__);
  285. return -EFAULT;
  286. }
  287. seq_printf(m, "%2d ", event->pcr_index);
  288. /* 2nd: SHA1 */
  289. for (i = 0; i < 20; i++)
  290. seq_printf(m, "%02x", event->pcr_value[i]);
  291. /* 3rd: event type identifier */
  292. seq_printf(m, " %02x", event->event_type);
  293. len += get_event_name(eventname, event, event_entry);
  294. /* 4th: eventname <= max + \'0' delimiter */
  295. seq_printf(m, " %s\n", eventname);
  296. kfree(eventname);
  297. return 0;
  298. }
  299. static struct seq_operations tpm_ascii_b_measurments_seqops = {
  300. .start = tpm_bios_measurements_start,
  301. .next = tpm_bios_measurements_next,
  302. .stop = tpm_bios_measurements_stop,
  303. .show = tpm_ascii_bios_measurements_show,
  304. };
  305. static struct seq_operations tpm_binary_b_measurments_seqops = {
  306. .start = tpm_bios_measurements_start,
  307. .next = tpm_bios_measurements_next,
  308. .stop = tpm_bios_measurements_stop,
  309. .show = tpm_binary_bios_measurements_show,
  310. };
  311. /* read binary bios log */
  312. static int read_log(struct tpm_bios_log *log)
  313. {
  314. struct acpi_tcpa *buff;
  315. acpi_status status;
  316. struct acpi_table_header *virt;
  317. u64 len, start;
  318. if (log->bios_event_log != NULL) {
  319. printk(KERN_ERR
  320. "%s: ERROR - Eventlog already initialized\n",
  321. __func__);
  322. return -EFAULT;
  323. }
  324. /* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
  325. status = acpi_get_table(ACPI_SIG_TCPA, 1,
  326. (struct acpi_table_header **)&buff);
  327. if (ACPI_FAILURE(status)) {
  328. printk(KERN_ERR "%s: ERROR - Could not get TCPA table\n",
  329. __func__);
  330. return -EIO;
  331. }
  332. switch(buff->platform_class) {
  333. case BIOS_SERVER:
  334. len = buff->server.log_max_len;
  335. start = buff->server.log_start_addr;
  336. break;
  337. case BIOS_CLIENT:
  338. default:
  339. len = buff->client.log_max_len;
  340. start = buff->client.log_start_addr;
  341. break;
  342. }
  343. if (!len) {
  344. printk(KERN_ERR "%s: ERROR - TCPA log area empty\n", __func__);
  345. return -EIO;
  346. }
  347. /* malloc EventLog space */
  348. log->bios_event_log = kmalloc(len, GFP_KERNEL);
  349. if (!log->bios_event_log) {
  350. printk("%s: ERROR - Not enough Memory for BIOS measurements\n",
  351. __func__);
  352. return -ENOMEM;
  353. }
  354. log->bios_event_log_end = log->bios_event_log + len;
  355. virt = acpi_os_map_memory(start, len);
  356. memcpy(log->bios_event_log, virt, len);
  357. acpi_os_unmap_memory(virt, len);
  358. return 0;
  359. }
  360. static int tpm_ascii_bios_measurements_open(struct inode *inode,
  361. struct file *file)
  362. {
  363. int err;
  364. struct tpm_bios_log *log;
  365. struct seq_file *seq;
  366. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  367. if (!log)
  368. return -ENOMEM;
  369. if ((err = read_log(log)))
  370. goto out_free;
  371. /* now register seq file */
  372. err = seq_open(file, &tpm_ascii_b_measurments_seqops);
  373. if (!err) {
  374. seq = file->private_data;
  375. seq->private = log;
  376. } else {
  377. goto out_free;
  378. }
  379. out:
  380. return err;
  381. out_free:
  382. kfree(log->bios_event_log);
  383. kfree(log);
  384. goto out;
  385. }
  386. static const struct file_operations tpm_ascii_bios_measurements_ops = {
  387. .open = tpm_ascii_bios_measurements_open,
  388. .read = seq_read,
  389. .llseek = seq_lseek,
  390. .release = tpm_bios_measurements_release,
  391. };
  392. static int tpm_binary_bios_measurements_open(struct inode *inode,
  393. struct file *file)
  394. {
  395. int err;
  396. struct tpm_bios_log *log;
  397. struct seq_file *seq;
  398. log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
  399. if (!log)
  400. return -ENOMEM;
  401. if ((err = read_log(log)))
  402. goto out_free;
  403. /* now register seq file */
  404. err = seq_open(file, &tpm_binary_b_measurments_seqops);
  405. if (!err) {
  406. seq = file->private_data;
  407. seq->private = log;
  408. } else {
  409. goto out_free;
  410. }
  411. out:
  412. return err;
  413. out_free:
  414. kfree(log->bios_event_log);
  415. kfree(log);
  416. goto out;
  417. }
  418. static const struct file_operations tpm_binary_bios_measurements_ops = {
  419. .open = tpm_binary_bios_measurements_open,
  420. .read = seq_read,
  421. .llseek = seq_lseek,
  422. .release = tpm_bios_measurements_release,
  423. };
  424. static int is_bad(void *p)
  425. {
  426. if (!p)
  427. return 1;
  428. if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
  429. return 1;
  430. return 0;
  431. }
  432. struct dentry **tpm_bios_log_setup(char *name)
  433. {
  434. struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
  435. tpm_dir = securityfs_create_dir(name, NULL);
  436. if (is_bad(tpm_dir))
  437. goto out;
  438. bin_file =
  439. securityfs_create_file("binary_bios_measurements",
  440. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  441. &tpm_binary_bios_measurements_ops);
  442. if (is_bad(bin_file))
  443. goto out_tpm;
  444. ascii_file =
  445. securityfs_create_file("ascii_bios_measurements",
  446. S_IRUSR | S_IRGRP, tpm_dir, NULL,
  447. &tpm_ascii_bios_measurements_ops);
  448. if (is_bad(ascii_file))
  449. goto out_bin;
  450. ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
  451. if (!ret)
  452. goto out_ascii;
  453. ret[0] = ascii_file;
  454. ret[1] = bin_file;
  455. ret[2] = tpm_dir;
  456. return ret;
  457. out_ascii:
  458. securityfs_remove(ascii_file);
  459. out_bin:
  460. securityfs_remove(bin_file);
  461. out_tpm:
  462. securityfs_remove(tpm_dir);
  463. out:
  464. return NULL;
  465. }
  466. EXPORT_SYMBOL_GPL(tpm_bios_log_setup);
  467. void tpm_bios_log_teardown(struct dentry **lst)
  468. {
  469. int i;
  470. for (i = 0; i < 3; i++)
  471. securityfs_remove(lst[i]);
  472. }
  473. EXPORT_SYMBOL_GPL(tpm_bios_log_teardown);
  474. MODULE_LICENSE("GPL");