insert-sys-cert.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Write the contents of the <certfile> into kernel symbol system_extra_cert
  2. *
  3. * Copyright (C) IBM Corporation, 2015
  4. *
  5. * Author: Mehmet Kayaalp <mkayaalp@linux.vnet.ibm.com>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. * Usage: insert-sys-cert [-s <System.map> -b <vmlinux> -c <certfile>
  11. */
  12. #define _GNU_SOURCE
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16. #include <limits.h>
  17. #include <stdbool.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <elf.h>
  27. #define CERT_SYM "system_extra_cert"
  28. #define USED_SYM "system_extra_cert_used"
  29. #define LSIZE_SYM "system_certificate_list_size"
  30. #define info(format, args...) fprintf(stderr, "INFO: " format, ## args)
  31. #define warn(format, args...) fprintf(stdout, "WARNING: " format, ## args)
  32. #define err(format, args...) fprintf(stderr, "ERROR: " format, ## args)
  33. #if UINTPTR_MAX == 0xffffffff
  34. #define CURRENT_ELFCLASS ELFCLASS32
  35. #define Elf_Ehdr Elf32_Ehdr
  36. #define Elf_Shdr Elf32_Shdr
  37. #define Elf_Sym Elf32_Sym
  38. #else
  39. #define CURRENT_ELFCLASS ELFCLASS64
  40. #define Elf_Ehdr Elf64_Ehdr
  41. #define Elf_Shdr Elf64_Shdr
  42. #define Elf_Sym Elf64_Sym
  43. #endif
  44. static unsigned char endianness(void)
  45. {
  46. uint16_t two_byte = 0x00FF;
  47. uint8_t low_address = *((uint8_t *)&two_byte);
  48. if (low_address == 0)
  49. return ELFDATA2MSB;
  50. else
  51. return ELFDATA2LSB;
  52. }
  53. struct sym {
  54. char *name;
  55. unsigned long address;
  56. unsigned long offset;
  57. void *content;
  58. int size;
  59. };
  60. static unsigned long get_offset_from_address(Elf_Ehdr *hdr, unsigned long addr)
  61. {
  62. Elf_Shdr *x;
  63. unsigned int i, num_sections;
  64. x = (void *)hdr + hdr->e_shoff;
  65. if (hdr->e_shnum == SHN_UNDEF)
  66. num_sections = x[0].sh_size;
  67. else
  68. num_sections = hdr->e_shnum;
  69. for (i = 1; i < num_sections; i++) {
  70. unsigned long start = x[i].sh_addr;
  71. unsigned long end = start + x[i].sh_size;
  72. unsigned long offset = x[i].sh_offset;
  73. if (addr >= start && addr <= end)
  74. return addr - start + offset;
  75. }
  76. return 0;
  77. }
  78. #define LINE_SIZE 100
  79. static void get_symbol_from_map(Elf_Ehdr *hdr, FILE *f, char *name,
  80. struct sym *s)
  81. {
  82. char l[LINE_SIZE];
  83. char *w, *p, *n;
  84. s->size = 0;
  85. s->address = 0;
  86. s->offset = 0;
  87. if (fseek(f, 0, SEEK_SET) != 0) {
  88. perror("File seek failed");
  89. exit(EXIT_FAILURE);
  90. }
  91. while (fgets(l, LINE_SIZE, f)) {
  92. p = strchr(l, '\n');
  93. if (!p) {
  94. err("Missing line ending.\n");
  95. return;
  96. }
  97. n = strstr(l, name);
  98. if (n)
  99. break;
  100. }
  101. if (!n) {
  102. err("Unable to find symbol: %s\n", name);
  103. return;
  104. }
  105. w = strchr(l, ' ');
  106. if (!w)
  107. return;
  108. *w = '\0';
  109. s->address = strtoul(l, NULL, 16);
  110. if (s->address == 0)
  111. return;
  112. s->offset = get_offset_from_address(hdr, s->address);
  113. s->name = name;
  114. s->content = (void *)hdr + s->offset;
  115. }
  116. static Elf_Sym *find_elf_symbol(Elf_Ehdr *hdr, Elf_Shdr *symtab, char *name)
  117. {
  118. Elf_Sym *sym, *symtab_start;
  119. char *strtab, *symname;
  120. unsigned int link;
  121. Elf_Shdr *x;
  122. int i, n;
  123. x = (void *)hdr + hdr->e_shoff;
  124. link = symtab->sh_link;
  125. symtab_start = (void *)hdr + symtab->sh_offset;
  126. n = symtab->sh_size / symtab->sh_entsize;
  127. strtab = (void *)hdr + x[link].sh_offset;
  128. for (i = 0; i < n; i++) {
  129. sym = &symtab_start[i];
  130. symname = strtab + sym->st_name;
  131. if (strcmp(symname, name) == 0)
  132. return sym;
  133. }
  134. err("Unable to find symbol: %s\n", name);
  135. return NULL;
  136. }
  137. static void get_symbol_from_table(Elf_Ehdr *hdr, Elf_Shdr *symtab,
  138. char *name, struct sym *s)
  139. {
  140. Elf_Shdr *sec;
  141. int secndx;
  142. Elf_Sym *elf_sym;
  143. Elf_Shdr *x;
  144. x = (void *)hdr + hdr->e_shoff;
  145. s->size = 0;
  146. s->address = 0;
  147. s->offset = 0;
  148. elf_sym = find_elf_symbol(hdr, symtab, name);
  149. if (!elf_sym)
  150. return;
  151. secndx = elf_sym->st_shndx;
  152. if (!secndx)
  153. return;
  154. sec = &x[secndx];
  155. s->size = elf_sym->st_size;
  156. s->address = elf_sym->st_value;
  157. s->offset = s->address - sec->sh_addr
  158. + sec->sh_offset;
  159. s->name = name;
  160. s->content = (void *)hdr + s->offset;
  161. }
  162. static Elf_Shdr *get_symbol_table(Elf_Ehdr *hdr)
  163. {
  164. Elf_Shdr *x;
  165. unsigned int i, num_sections;
  166. x = (void *)hdr + hdr->e_shoff;
  167. if (hdr->e_shnum == SHN_UNDEF)
  168. num_sections = x[0].sh_size;
  169. else
  170. num_sections = hdr->e_shnum;
  171. for (i = 1; i < num_sections; i++)
  172. if (x[i].sh_type == SHT_SYMTAB)
  173. return &x[i];
  174. return NULL;
  175. }
  176. static void *map_file(char *file_name, int *size)
  177. {
  178. struct stat st;
  179. void *map;
  180. int fd;
  181. fd = open(file_name, O_RDWR);
  182. if (fd < 0) {
  183. perror(file_name);
  184. return NULL;
  185. }
  186. if (fstat(fd, &st)) {
  187. perror("Could not determine file size");
  188. close(fd);
  189. return NULL;
  190. }
  191. *size = st.st_size;
  192. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  193. if (map == MAP_FAILED) {
  194. perror("Mapping to memory failed");
  195. close(fd);
  196. return NULL;
  197. }
  198. close(fd);
  199. return map;
  200. }
  201. static char *read_file(char *file_name, int *size)
  202. {
  203. struct stat st;
  204. char *buf;
  205. int fd;
  206. fd = open(file_name, O_RDONLY);
  207. if (fd < 0) {
  208. perror(file_name);
  209. return NULL;
  210. }
  211. if (fstat(fd, &st)) {
  212. perror("Could not determine file size");
  213. close(fd);
  214. return NULL;
  215. }
  216. *size = st.st_size;
  217. buf = malloc(*size);
  218. if (!buf) {
  219. perror("Allocating memory failed");
  220. close(fd);
  221. return NULL;
  222. }
  223. if (read(fd, buf, *size) != *size) {
  224. perror("File read failed");
  225. close(fd);
  226. return NULL;
  227. }
  228. close(fd);
  229. return buf;
  230. }
  231. static void print_sym(Elf_Ehdr *hdr, struct sym *s)
  232. {
  233. info("sym: %s\n", s->name);
  234. info("addr: 0x%lx\n", s->address);
  235. info("size: %d\n", s->size);
  236. info("offset: 0x%lx\n", (unsigned long)s->offset);
  237. }
  238. static void print_usage(char *e)
  239. {
  240. printf("Usage %s [-s <System.map>] -b <vmlinux> -c <certfile>\n", e);
  241. }
  242. int main(int argc, char **argv)
  243. {
  244. char *system_map_file = NULL;
  245. char *vmlinux_file = NULL;
  246. char *cert_file = NULL;
  247. int vmlinux_size;
  248. int cert_size;
  249. Elf_Ehdr *hdr;
  250. char *cert;
  251. FILE *system_map;
  252. unsigned long *lsize;
  253. int *used;
  254. int opt;
  255. Elf_Shdr *symtab = NULL;
  256. struct sym cert_sym, lsize_sym, used_sym;
  257. while ((opt = getopt(argc, argv, "b:c:s:")) != -1) {
  258. switch (opt) {
  259. case 's':
  260. system_map_file = optarg;
  261. break;
  262. case 'b':
  263. vmlinux_file = optarg;
  264. break;
  265. case 'c':
  266. cert_file = optarg;
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. if (!vmlinux_file || !cert_file) {
  273. print_usage(argv[0]);
  274. exit(EXIT_FAILURE);
  275. }
  276. cert = read_file(cert_file, &cert_size);
  277. if (!cert)
  278. exit(EXIT_FAILURE);
  279. hdr = map_file(vmlinux_file, &vmlinux_size);
  280. if (!hdr)
  281. exit(EXIT_FAILURE);
  282. if (vmlinux_size < sizeof(*hdr)) {
  283. err("Invalid ELF file.\n");
  284. exit(EXIT_FAILURE);
  285. }
  286. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  287. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  288. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  289. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  290. err("Invalid ELF magic.\n");
  291. exit(EXIT_FAILURE);
  292. }
  293. if (hdr->e_ident[EI_CLASS] != CURRENT_ELFCLASS) {
  294. err("ELF class mismatch.\n");
  295. exit(EXIT_FAILURE);
  296. }
  297. if (hdr->e_ident[EI_DATA] != endianness()) {
  298. err("ELF endian mismatch.\n");
  299. exit(EXIT_FAILURE);
  300. }
  301. if (hdr->e_shoff > vmlinux_size) {
  302. err("Could not find section header.\n");
  303. exit(EXIT_FAILURE);
  304. }
  305. symtab = get_symbol_table(hdr);
  306. if (!symtab) {
  307. warn("Could not find the symbol table.\n");
  308. if (!system_map_file) {
  309. err("Please provide a System.map file.\n");
  310. print_usage(argv[0]);
  311. exit(EXIT_FAILURE);
  312. }
  313. system_map = fopen(system_map_file, "r");
  314. if (!system_map) {
  315. perror(system_map_file);
  316. exit(EXIT_FAILURE);
  317. }
  318. get_symbol_from_map(hdr, system_map, CERT_SYM, &cert_sym);
  319. get_symbol_from_map(hdr, system_map, USED_SYM, &used_sym);
  320. get_symbol_from_map(hdr, system_map, LSIZE_SYM, &lsize_sym);
  321. cert_sym.size = used_sym.address - cert_sym.address;
  322. } else {
  323. info("Symbol table found.\n");
  324. if (system_map_file)
  325. warn("System.map is ignored.\n");
  326. get_symbol_from_table(hdr, symtab, CERT_SYM, &cert_sym);
  327. get_symbol_from_table(hdr, symtab, USED_SYM, &used_sym);
  328. get_symbol_from_table(hdr, symtab, LSIZE_SYM, &lsize_sym);
  329. }
  330. if (!cert_sym.offset || !lsize_sym.offset || !used_sym.offset)
  331. exit(EXIT_FAILURE);
  332. print_sym(hdr, &cert_sym);
  333. print_sym(hdr, &used_sym);
  334. print_sym(hdr, &lsize_sym);
  335. lsize = (unsigned long *)lsize_sym.content;
  336. used = (int *)used_sym.content;
  337. if (cert_sym.size < cert_size) {
  338. err("Certificate is larger than the reserved area!\n");
  339. exit(EXIT_FAILURE);
  340. }
  341. /* If the existing cert is the same, don't overwrite */
  342. if (cert_size == *used &&
  343. strncmp(cert_sym.content, cert, cert_size) == 0) {
  344. warn("Certificate was already inserted.\n");
  345. exit(EXIT_SUCCESS);
  346. }
  347. if (*used > 0)
  348. warn("Replacing previously inserted certificate.\n");
  349. memcpy(cert_sym.content, cert, cert_size);
  350. if (cert_size < cert_sym.size)
  351. memset(cert_sym.content + cert_size,
  352. 0, cert_sym.size - cert_size);
  353. *lsize = *lsize + cert_size - *used;
  354. *used = cert_size;
  355. info("Inserted the contents of %s into %lx.\n", cert_file,
  356. cert_sym.address);
  357. info("Used %d bytes out of %d bytes reserved.\n", *used,
  358. cert_sym.size);
  359. exit(EXIT_SUCCESS);
  360. }