tpm_bios.c 11 KB

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