1 /* $OpenBSD: exec_elf.c,v 1.12 2009/10/27 23:59:51 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Mats O Jansson. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <err.h> 28 #include <errno.h> 29 #include <fcntl.h> 30 #include <nlist.h> 31 #include <stdlib.h> 32 #include <stdio.h> 33 #include <unistd.h> 34 #include <sys/exec.h> 35 #include <sys/exec_elf.h> 36 #include <sys/types.h> 37 38 #include "ukc.h" 39 #include "config.h" 40 41 caddr_t ptr, rest, pre; 42 Elf_Ehdr elf_ex; 43 Elf_Phdr *elf_phdr; 44 Elf_Shdr *elf_shdr; 45 char *elf_total; 46 char *elf_shstrtab; 47 off_t elf_size; 48 49 caddr_t elf_adjust(caddr_t); 50 caddr_t elf_readjust(caddr_t); 51 int elf_check(char *); 52 void elf_loadkernel(char *); 53 void elf_savekernel(char *); 54 55 caddr_t 56 elf_adjust(caddr_t x) 57 { 58 int i; 59 Elf_Shdr *s; 60 unsigned long y = 0; 61 62 s = elf_shdr; 63 64 for (i = 0; i < elf_ex.e_shnum; i++) { 65 if (s[i].sh_addr == 0) 66 continue; 67 if (((unsigned long)x >= s[i].sh_addr) && 68 ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) { 69 y = (unsigned long)&elf_total[(unsigned long)x - 70 s[i].sh_addr + s[i].sh_offset]; 71 break; 72 } 73 } 74 75 return((caddr_t)y); 76 } 77 78 caddr_t 79 elf_readjust(caddr_t x) 80 { 81 int i; 82 Elf_Shdr *s; 83 unsigned long y = 0; 84 85 s = elf_shdr; 86 87 for (i = 0; i < elf_ex.e_shnum; i++) { 88 if (s[i].sh_addr == 0) 89 continue; 90 if (((x - elf_total) >= s[i].sh_offset) && 91 ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size))) 92 y = (unsigned long)x - (unsigned long)elf_total + 93 (unsigned long)s[i].sh_addr - s[i].sh_offset; 94 } 95 96 return((caddr_t)y); 97 } 98 99 int 100 elf_check(char *file) 101 { 102 int fd, ret = 1; 103 104 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 105 return (0); 106 107 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 108 ret = 0; 109 110 if (ret) { 111 if (!IS_ELF(elf_ex)) 112 ret = 0; 113 } 114 115 close(fd); 116 return (ret); 117 } 118 119 void 120 elf_loadkernel(char *file) 121 { 122 int fd; 123 124 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 125 err(1, "%s", file); 126 127 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 128 errx(1, "can't read elf header"); 129 130 if (!IS_ELF(elf_ex)) 131 errx(1, "bad elf magic"); 132 133 elf_size = lseek(fd, (off_t)0, SEEK_END); 134 (void)lseek(fd, (off_t)0, SEEK_SET); 135 elf_total = emalloc((size_t)elf_size); 136 137 if (read(fd, elf_total, (size_t)elf_size) != elf_size) 138 errx(1, "can't read elf kernel"); 139 140 if (elf_ex.e_phoff > (size_t)elf_size) 141 errx(1, "incorrect ELF header or truncated file"); 142 if (elf_ex.e_shoff > (size_t)elf_size) 143 errx(1, "incorrect ELF header or truncated file"); 144 145 elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff]; 146 elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff]; 147 148 if ((char *)&elf_shdr[elf_ex.e_shstrndx] + 149 sizeof(elf_shdr[elf_ex.e_shstrndx]) >= elf_total + (size_t)elf_size) 150 errx(1, "incorrect ELF header or truncated file"); 151 152 if ((char *)&elf_shdr[elf_ex.e_shstrndx].sh_offset + 153 sizeof(elf_shdr[elf_ex.e_shstrndx].sh_offset) >= 154 elf_total + (size_t)elf_size) 155 errx(1, "incorrect ELF header or truncated file"); 156 157 elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset]; 158 159 close(fd); 160 } 161 162 void 163 elf_savekernel(char *outfile) 164 { 165 int fd; 166 167 if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) 168 err(1, "%s", outfile); 169 170 if (write(fd, elf_total, (size_t)elf_size) != elf_size) 171 errx(1, "can't write file %s", outfile); 172 173 close(fd); 174 } 175