1 /* Write the contents of the <certfile> into kernel symbol system_extra_cert
3 * Copyright (C) IBM Corporation, 2015
5 * Author: Mehmet Kayaalp <mkayaalp@linux.vnet.ibm.com>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
10 * Usage: insert-sys-cert [-s <System.map> -b <vmlinux> -c <certfile>
22 #include <sys/types.h>
29 #define CERT_SYM "system_extra_cert"
30 #define USED_SYM "system_extra_cert_used"
31 #define LSIZE_SYM "system_certificate_list_size"
33 #define info(format, args...) fprintf(stderr, "INFO: " format, ## args)
34 #define warn(format, args...) fprintf(stdout, "WARNING: " format, ## args)
35 #define err(format, args...) fprintf(stderr, "ERROR: " format, ## args)
37 #if UINTPTR_MAX == 0xffffffff
38 #define CURRENT_ELFCLASS ELFCLASS32
39 #define Elf_Ehdr Elf32_Ehdr
40 #define Elf_Shdr Elf32_Shdr
41 #define Elf_Sym Elf32_Sym
43 #define CURRENT_ELFCLASS ELFCLASS64
44 #define Elf_Ehdr Elf64_Ehdr
45 #define Elf_Shdr Elf64_Shdr
46 #define Elf_Sym Elf64_Sym
49 static unsigned char endianness(void)
51 uint16_t two_byte
= 0x00FF;
52 uint8_t low_address
= *((uint8_t *)&two_byte
);
62 unsigned long address
;
68 static unsigned long get_offset_from_address(Elf_Ehdr
*hdr
, unsigned long addr
)
71 unsigned int i
, num_sections
;
73 x
= (void *)hdr
+ hdr
->e_shoff
;
74 if (hdr
->e_shnum
== SHN_UNDEF
)
75 num_sections
= x
[0].sh_size
;
77 num_sections
= hdr
->e_shnum
;
79 for (i
= 1; i
< num_sections
; i
++) {
80 unsigned long start
= x
[i
].sh_addr
;
81 unsigned long end
= start
+ x
[i
].sh_size
;
82 unsigned long offset
= x
[i
].sh_offset
;
84 if (addr
>= start
&& addr
<= end
)
85 return addr
- start
+ offset
;
93 static void get_symbol_from_map(Elf_Ehdr
*hdr
, FILE *f
, char *name
,
102 if (fseek(f
, 0, SEEK_SET
) != 0) {
103 perror("File seek failed");
106 while (fgets(l
, LINE_SIZE
, f
)) {
109 err("Missing line ending.\n");
117 err("Unable to find symbol: %s\n", name
);
125 s
->address
= strtoul(l
, NULL
, 16);
128 s
->offset
= get_offset_from_address(hdr
, s
->address
);
130 s
->content
= (void *)hdr
+ s
->offset
;
133 static Elf_Sym
*find_elf_symbol(Elf_Ehdr
*hdr
, Elf_Shdr
*symtab
, char *name
)
135 Elf_Sym
*sym
, *symtab_start
;
136 char *strtab
, *symname
;
141 x
= (void *)hdr
+ hdr
->e_shoff
;
142 link
= symtab
->sh_link
;
143 symtab_start
= (void *)hdr
+ symtab
->sh_offset
;
144 n
= symtab
->sh_size
/ symtab
->sh_entsize
;
145 strtab
= (void *)hdr
+ x
[link
].sh_offset
;
147 for (i
= 0; i
< n
; i
++) {
148 sym
= &symtab_start
[i
];
149 symname
= strtab
+ sym
->st_name
;
150 if (strcmp(symname
, name
) == 0)
153 err("Unable to find symbol: %s\n", name
);
157 static void get_symbol_from_table(Elf_Ehdr
*hdr
, Elf_Shdr
*symtab
,
158 char *name
, struct sym
*s
)
165 x
= (void *)hdr
+ hdr
->e_shoff
;
169 elf_sym
= find_elf_symbol(hdr
, symtab
, name
);
172 secndx
= elf_sym
->st_shndx
;
176 s
->size
= elf_sym
->st_size
;
177 s
->address
= elf_sym
->st_value
;
178 s
->offset
= s
->address
- sec
->sh_addr
181 s
->content
= (void *)hdr
+ s
->offset
;
184 static Elf_Shdr
*get_symbol_table(Elf_Ehdr
*hdr
)
187 unsigned int i
, num_sections
;
189 x
= (void *)hdr
+ hdr
->e_shoff
;
190 if (hdr
->e_shnum
== SHN_UNDEF
)
191 num_sections
= x
[0].sh_size
;
193 num_sections
= hdr
->e_shnum
;
195 for (i
= 1; i
< num_sections
; i
++)
196 if (x
[i
].sh_type
== SHT_SYMTAB
)
201 static void *map_file(char *file_name
, int *size
)
207 fd
= open(file_name
, O_RDWR
);
212 if (fstat(fd
, &st
)) {
213 perror("Could not determine file size");
218 map
= mmap(NULL
, *size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
219 if (map
== MAP_FAILED
) {
220 perror("Mapping to memory failed");
228 static char *read_file(char *file_name
, int *size
)
234 fd
= open(file_name
, O_RDONLY
);
239 if (fstat(fd
, &st
)) {
240 perror("Could not determine file size");
247 perror("Allocating memory failed");
251 if (read(fd
, buf
, *size
) != *size
) {
252 perror("File read failed");
260 static void print_sym(Elf_Ehdr
*hdr
, struct sym
*s
)
262 info("sym: %s\n", s
->name
);
263 info("addr: 0x%lx\n", s
->address
);
264 info("size: %d\n", s
->size
);
265 info("offset: 0x%lx\n", (unsigned long)s
->offset
);
268 static void print_usage(char *e
)
270 printf("Usage %s [-s <System.map>] -b <vmlinux> -c <certfile>\n", e
);
273 int main(int argc
, char **argv
)
275 char *system_map_file
= NULL
;
276 char *vmlinux_file
= NULL
;
277 char *cert_file
= NULL
;
283 unsigned long *lsize
;
286 Elf_Shdr
*symtab
= NULL
;
287 struct sym cert_sym
, lsize_sym
, used_sym
;
289 while ((opt
= getopt(argc
, argv
, "b:c:s:")) != -1) {
292 system_map_file
= optarg
;
295 vmlinux_file
= optarg
;
305 if (!vmlinux_file
|| !cert_file
) {
306 print_usage(argv
[0]);
310 cert
= read_file(cert_file
, &cert_size
);
314 hdr
= map_file(vmlinux_file
, &vmlinux_size
);
318 if (vmlinux_size
< sizeof(*hdr
)) {
319 err("Invalid ELF file.\n");
323 if ((hdr
->e_ident
[EI_MAG0
] != ELFMAG0
) ||
324 (hdr
->e_ident
[EI_MAG1
] != ELFMAG1
) ||
325 (hdr
->e_ident
[EI_MAG2
] != ELFMAG2
) ||
326 (hdr
->e_ident
[EI_MAG3
] != ELFMAG3
)) {
327 err("Invalid ELF magic.\n");
331 if (hdr
->e_ident
[EI_CLASS
] != CURRENT_ELFCLASS
) {
332 err("ELF class mismatch.\n");
336 if (hdr
->e_ident
[EI_DATA
] != endianness()) {
337 err("ELF endian mismatch.\n");
341 if (hdr
->e_shoff
> vmlinux_size
) {
342 err("Could not find section header.\n");
346 symtab
= get_symbol_table(hdr
);
348 warn("Could not find the symbol table.\n");
349 if (!system_map_file
) {
350 err("Please provide a System.map file.\n");
351 print_usage(argv
[0]);
355 system_map
= fopen(system_map_file
, "r");
357 perror(system_map_file
);
360 get_symbol_from_map(hdr
, system_map
, CERT_SYM
, &cert_sym
);
361 get_symbol_from_map(hdr
, system_map
, USED_SYM
, &used_sym
);
362 get_symbol_from_map(hdr
, system_map
, LSIZE_SYM
, &lsize_sym
);
363 cert_sym
.size
= used_sym
.address
- cert_sym
.address
;
365 info("Symbol table found.\n");
367 warn("System.map is ignored.\n");
368 get_symbol_from_table(hdr
, symtab
, CERT_SYM
, &cert_sym
);
369 get_symbol_from_table(hdr
, symtab
, USED_SYM
, &used_sym
);
370 get_symbol_from_table(hdr
, symtab
, LSIZE_SYM
, &lsize_sym
);
373 if (!cert_sym
.offset
|| !lsize_sym
.offset
|| !used_sym
.offset
)
376 print_sym(hdr
, &cert_sym
);
377 print_sym(hdr
, &used_sym
);
378 print_sym(hdr
, &lsize_sym
);
380 lsize
= (unsigned long *)lsize_sym
.content
;
381 used
= (int *)used_sym
.content
;
383 if (cert_sym
.size
< cert_size
) {
384 err("Certificate is larger than the reserved area!\n");
388 /* If the existing cert is the same, don't overwrite */
389 if (cert_size
== *used
&&
390 strncmp(cert_sym
.content
, cert
, cert_size
) == 0) {
391 warn("Certificate was already inserted.\n");
396 warn("Replacing previously inserted certificate.\n");
398 memcpy(cert_sym
.content
, cert
, cert_size
);
399 if (cert_size
< cert_sym
.size
)
400 memset(cert_sym
.content
+ cert_size
,
401 0, cert_sym
.size
- cert_size
);
403 *lsize
= *lsize
+ cert_size
- *used
;
405 info("Inserted the contents of %s into %lx.\n", cert_file
,
407 info("Used %d bytes out of %d bytes reserved.\n", *used
,