1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/* $NetBSD: binpatch.c,v 1.7 2016/09/22 17:08:16 christos Exp $ */
/*-
* Copyright (c) 2009 Izumi Tsutsui. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1996 Christopher G. Demetriou
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1996\
Christopher G. Demetriou. All rights reserved.");
#endif /* not lint */
#ifndef lint
__RCSID("$NetBSD: binpatch.c,v 1.7 2016/09/22 17:08:16 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/inttypes.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include "extern.h"
static void usage(void) __dead;
bool replace, verbose;
u_long addr, offset;
char *symbol;
size_t size;
uint64_t val;
static const struct {
const char *name;
int (*check)(const char *, size_t);
int (*findoff)(const char *, size_t, u_long, size_t *, u_long);
} exec_formats[] = {
#ifdef NLIST_AOUT
{ "a.out", check_aout, findoff_aout, },
#endif
#ifdef NLIST_ECOFF
{ "ECOFF", check_ecoff, findoff_ecoff, },
#endif
#ifdef NLIST_ELF32
{ "ELF32", check_elf32, findoff_elf32, },
#endif
#ifdef NLIST_ELF64
{ "ELF64", check_elf64, findoff_elf64, },
#endif
#ifdef NLIST_COFF
{ "COFF", check_coff, findoff_coff, },
#endif
};
int
main(int argc, char *argv[])
{
const char *fname;
struct stat sb;
struct nlist nl[2];
char *mappedfile;
size_t valoff;
void *valp;
uint8_t uval8;
int8_t sval8;
uint16_t uval16;
int16_t sval16;
uint32_t uval32;
int32_t sval32;
uint64_t uval64;
int64_t sval64;
int ch, fd, rv, i, n;
u_long text_start; /* Start of kernel text (a.out) */
setprogname(argv[0]);
text_start = (unsigned long)~0;
while ((ch = getopt(argc, argv, "bwldT:a:s:o:r:v")) != -1)
switch (ch) {
case 'b':
size = sizeof(uint8_t);
break;
case 'w':
size = sizeof(uint16_t);
break;
case 'l':
size = sizeof(uint32_t);
break;
case 'd':
size = sizeof(uint64_t);
break;
case 'a':
if (addr != 0 || symbol != NULL)
errx(EXIT_FAILURE,
"only one address/symbol allowed");
addr = strtoul(optarg, NULL, 0);
break;
case 's':
if (addr != 0 || symbol != NULL)
errx(EXIT_FAILURE,
"only one address/symbol allowed");
symbol = optarg;
break;
case 'o':
if (offset != 0)
err(EXIT_FAILURE,
"only one offset allowed");
offset = strtoul(optarg, NULL, 0);
break;
case 'r':
replace = true;
val = strtoull(optarg, NULL, 0);
break;
case 'v':
verbose = true;
break;
case 'T':
text_start = strtoul(optarg, NULL, 0);
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
if (addr == 0 && symbol == NULL) {
warnx("no address or symbol specified");
usage();
}
if (size == 0)
size = sizeof(uint32_t); /* default to int */
fname = argv[0];
if ((fd = open(fname, replace ? O_RDWR : O_RDONLY, 0)) == -1)
err(EXIT_FAILURE, "open %s", fname);
if (symbol != NULL) {
nl[0].n_name = symbol;
nl[1].n_name = NULL;
if ((rv = __fdnlist(fd, nl)) != 0)
errx(EXIT_FAILURE, "could not find symbol %s in %s",
symbol, fname);
addr = nl[0].n_value;
if (verbose)
fprintf(stderr, "got symbol address 0x%lx from %s\n",
addr, fname);
}
addr += offset * size;
if (fstat(fd, &sb) == -1)
err(EXIT_FAILURE, "fstat %s", fname);
if (sb.st_size != (ssize_t)sb.st_size)
errx(EXIT_FAILURE, "%s too big to map", fname);
if ((mappedfile = mmap(NULL, sb.st_size,
replace ? PROT_READ | PROT_WRITE : PROT_READ,
MAP_FILE | MAP_SHARED, fd, 0)) == (char *)-1)
err(EXIT_FAILURE, "mmap %s", fname);
if (verbose)
fprintf(stderr, "mapped %s\n", fname);
n = __arraycount(exec_formats);
for (i = 0; i < n; i++) {
if ((*exec_formats[i].check)(mappedfile, sb.st_size) == 0)
break;
}
if (i == n)
errx(EXIT_FAILURE, "%s: unknown executable format", fname);
if (verbose) {
fprintf(stderr, "%s is an %s binary\n", fname,
exec_formats[i].name);
if (text_start != (u_long)~0)
fprintf(stderr, "kernel text loads at 0x%lx\n",
text_start);
}
if ((*exec_formats[i].findoff)(mappedfile, sb.st_size,
addr, &valoff, text_start) != 0)
errx(EXIT_FAILURE, "couldn't find file offset for %s in %s",
symbol != NULL ? nl[0].n_name : "address" , fname);
valp = mappedfile + valoff;
if (symbol)
printf("%s(0x%lx): ", symbol, addr);
else
printf("0x%lx: ", addr);
switch (size) {
case sizeof(uint8_t):
uval8 = *(uint8_t *)valp;
sval8 = *(int8_t *)valp;
printf("0x%02" PRIx8 " (%" PRIu8, uval8, uval8);
if (sval8 < 0)
printf("/%" PRId8, sval8);
printf(")");
break;
case sizeof(uint16_t):
uval16 = *(uint16_t *)valp;
sval16 = *(int16_t *)valp;
printf("0x%04" PRIx16 " (%" PRIu16, uval16, uval16);
if (sval16 < 0)
printf("/%" PRId16, sval16);
printf(")");
break;
case sizeof(uint32_t):
uval32 = *(uint32_t *)valp;
sval32 = *(int32_t *)valp;
printf("0x%08" PRIx32 " (%" PRIu32, uval32, uval32);
if (sval32 < 0)
printf("/%" PRId32, sval32);
printf(")");
break;
case sizeof(uint64_t):
uval64 = *(uint64_t *)valp;
sval64 = *(int64_t *)valp;
printf("0x%016" PRIx64 " (%" PRIu64, uval64, uval64);
if (sval64 < 0)
printf("/%" PRId64, sval64);
printf(")");
break;
}
printf(", at offset %#lx in %s\n", (unsigned long)valoff, fname);
if (!replace)
goto done;
printf("new value: ");
switch (size) {
case sizeof(uint8_t):
uval8 = (uint8_t)val;
sval8 = (int8_t)val;
printf("0x%02" PRIx8 " (%" PRIu8, uval8, uval8);
if (sval8 < 0)
printf("/%" PRId8, sval8);
printf(")");
*(uint8_t *)valp = uval8;
break;
case sizeof(uint16_t):
uval16 = (uint16_t)val;
sval16 = (int16_t)val;
printf("0x%04" PRIx16 " (%" PRIu16, uval16, uval16);
if (sval16 < 0)
printf("/%" PRId16, sval16);
printf(")");
*(uint16_t *)valp = uval16;
break;
case sizeof(uint32_t):
uval32 = (uint32_t)val;
sval32 = (int32_t)val;
printf("0x%08" PRIx32 " (%" PRIu32, uval32, uval32);
if (sval32 < 0)
printf("/%" PRId32, sval32);
printf(")");
*(uint32_t *)valp = uval32;
break;
case sizeof(uint64_t):
uval64 = (uint64_t)val;
sval64 = (int64_t)val;
printf("0x%016" PRIx64 " (%" PRIu64, uval64, uval64);
if (sval64 < 0)
printf("/%" PRId64, sval64);
printf(")");
*(uint64_t *)valp = uval64;
break;
}
printf("\n");
done:
munmap(mappedfile, sb.st_size);
close(fd);
if (verbose)
fprintf(stderr, "exiting\n");
exit(EXIT_SUCCESS);
}
static void
usage(void)
{
fprintf(stderr,
"Usage: %s [-b|-w|-l|-d] [-a address | -s symbol] [-o offset]"
" [-r value] [-T text_start] [-v] binary\n", getprogname());
exit(EXIT_FAILURE);
}
|