xref: /openbsd-src/usr.sbin/config/exec_elf.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: exec_elf.c,v 1.13 2011/10/02 22:20:49 edd 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 <sys/types.h>
28 #include <sys/exec.h>
29 #include <sys/exec_elf.h>
30 
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <nlist.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include "ukc.h"
40 #include "config.h"
41 
42 caddr_t		ptr, rest, pre;
43 Elf_Ehdr	elf_ex;
44 Elf_Phdr	*elf_phdr;
45 Elf_Shdr	*elf_shdr;
46 char		*elf_total;
47 char		*elf_shstrtab;
48 off_t		elf_size;
49 
50 caddr_t		elf_adjust(caddr_t);
51 caddr_t		elf_readjust(caddr_t);
52 int		elf_check(char *);
53 void		elf_loadkernel(char *);
54 void		elf_savekernel(char *);
55 
56 caddr_t
57 elf_adjust(caddr_t x)
58 {
59 	int i;
60 	Elf_Shdr *s;
61 	unsigned long y = 0;
62 
63 	s = elf_shdr;
64 
65 	for (i = 0; i < elf_ex.e_shnum; i++) {
66 		if (s[i].sh_addr == 0)
67 			continue;
68 		if (((unsigned long)x >= s[i].sh_addr) &&
69 		    ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) {
70 			y = (unsigned long)&elf_total[(unsigned long)x -
71 			    s[i].sh_addr + s[i].sh_offset];
72 			break;
73 		}
74 	}
75 
76 	return((caddr_t)y);
77 }
78 
79 caddr_t
80 elf_readjust(caddr_t x)
81 {
82 	int i;
83 	Elf_Shdr *s;
84 	unsigned long y = 0;
85 
86 	s = elf_shdr;
87 
88 	for (i = 0; i < elf_ex.e_shnum; i++) {
89 		if (s[i].sh_addr == 0)
90 			continue;
91 		if (((x - elf_total) >= s[i].sh_offset) &&
92 		    ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size)))
93 			y = (unsigned long)x - (unsigned long)elf_total +
94 			    (unsigned long)s[i].sh_addr - s[i].sh_offset;
95 	}
96 
97 	return((caddr_t)y);
98 }
99 
100 int
101 elf_check(char *file)
102 {
103 	int fd, ret = 1;
104 
105 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
106 		return (0);
107 
108 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
109 		ret = 0;
110 
111 	if (ret) {
112 		if (!IS_ELF(elf_ex))
113 			ret = 0;
114 	}
115 
116 	close(fd);
117 	return (ret);
118 }
119 
120 void
121 elf_loadkernel(char *file)
122 {
123 	int fd;
124 
125 	if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
126 		err(1, "%s", file);
127 
128 	if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex))
129 		errx(1, "can't read elf header");
130 
131 	if (!IS_ELF(elf_ex))
132 		errx(1, "bad elf magic");
133 
134 	elf_size = lseek(fd, (off_t)0, SEEK_END);
135 	(void)lseek(fd, (off_t)0, SEEK_SET);
136 	elf_total = emalloc((size_t)elf_size);
137 
138 	if (read(fd, elf_total, (size_t)elf_size) != elf_size)
139 		errx(1, "can't read elf kernel");
140 
141 	if (elf_ex.e_phoff > (size_t)elf_size)
142 		errx(1, "incorrect ELF header or truncated file");
143 	if (elf_ex.e_shoff > (size_t)elf_size)
144 		errx(1, "incorrect ELF header or truncated file");
145 
146 	elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff];
147 	elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff];
148 
149 	if ((char *)&elf_shdr[elf_ex.e_shstrndx] +
150 	    sizeof(elf_shdr[elf_ex.e_shstrndx]) >= elf_total + (size_t)elf_size)
151 		errx(1, "incorrect ELF header or truncated file");
152 
153 	if ((char *)&elf_shdr[elf_ex.e_shstrndx].sh_offset +
154 	    sizeof(elf_shdr[elf_ex.e_shstrndx].sh_offset) >=
155 	    elf_total + (size_t)elf_size)
156 		errx(1, "incorrect ELF header or truncated file");
157 
158 	elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset];
159 
160 	close(fd);
161 }
162 
163 void
164 elf_savekernel(char *outfile)
165 {
166 	int fd;
167 
168 	if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0)
169 		err(1, "%s", outfile);
170 
171 	if (write(fd, elf_total, (size_t)elf_size) != elf_size)
172 		errx(1, "can't write file %s", outfile);
173 
174 	close(fd);
175 }
176