~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/insert-sys-cert.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/insert-sys-cert.c (Version linux-6.12-rc7) and /scripts/insert-sys-cert.c (Version linux-6.8.12)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php