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
|
/* $OpenBSD: prebind_delete.c,v 1.4 2006/05/13 16:33:40 deraadt Exp $ */
/*
* Copyright (c) 2006 Dale Rahn <drahn@dalerahn.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/exec_elf.h>
#include <elf_abi.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "prebind.h"
#define BUFSZ (256 * 1024)
int strip_prebind(char *file);
int prebind_remove_load_section(int fd, char *name);
int prebind_newfile(int fd, char *name, struct stat *st,
struct prebind_footer *footer);
extern int verbose;
int
prebind_delete(char **argv)
{
while (*argv) {
if (strip_prebind(*argv) == -1)
return (1);
argv++;
}
return (0);
}
int
strip_prebind(char *file)
{
struct prebind_footer footer;
extern char *__progname;
int fd, rdonly = 0;
struct stat st;
ssize_t bytes;
fd = open(file, O_RDWR);
if (fd == -1 && errno == ETXTBSY) {
fd = open(file, O_RDONLY);
rdonly = 1;
}
if (fd == -1) {
warn("%s", file);
return (-1);
}
if (fstat(fd, &st) == -1)
return (-1);
lseek(fd, -((off_t)sizeof(struct prebind_footer)), SEEK_END);
bytes = read(fd, &footer, sizeof(struct prebind_footer));
if (bytes != sizeof(struct prebind_footer))
goto done;
if (footer.bind_id[0] != BIND_ID0 || footer.bind_id[1] != BIND_ID1 ||
footer.bind_id[2] != BIND_ID2 || footer.bind_id[3] != BIND_ID3) {
if (verbose)
fprintf(stderr, "%s: no prebind header\n", file);
goto done;
}
if (rdonly) {
fd = prebind_newfile(fd, file, &st, &footer);
} else {
prebind_remove_load_section(fd, file);
ftruncate(fd, footer.orig_size);
}
if (verbose)
fprintf(stderr, "%s: stripped %lld bytes from %s\n",
__progname, st.st_size - footer.orig_size, file);
done:
if (fd != -1)
close(fd);
return (0);
}
int
prebind_remove_load_section(int fd, char *name)
{
Elf_Ehdr *ehdr;
Elf_Phdr *phdr;
int loadsection;
char *buf;
buf = mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_FILE | MAP_SHARED,
fd, 0);
if (buf == MAP_FAILED) {
if (verbose)
warn("%s: cannot mmap for for write", name);
return (-1);
}
ehdr = (Elf_Ehdr *)buf;
phdr = (Elf_Phdr *)(buf + ehdr->e_phoff);
loadsection = ehdr->e_phnum - 1;
if (ehdr->e_type != ET_EXEC ||
(phdr[loadsection].p_flags & 0x08000000) == 0)
goto done;
if (phdr[loadsection].p_type != PT_LOAD ||
((phdr[loadsection].p_flags & 0x08000000) == 0)) {
/* doesn't look like ours */
if (verbose)
fprintf(stderr, "mapped, %s id doesn't match %lx\n", name,
(long)(phdr[loadsection].p_vaddr));
goto done;
}
bzero(&phdr[loadsection], sizeof(Elf_Phdr));
ehdr->e_phnum--;
done:
munmap(buf, 8192);
return (0);
}
int
prebind_newfile(int infd, char *name, struct stat *st,
struct prebind_footer *footer)
{
struct timeval tv[2];
char *newname, *buf;
ssize_t len, wlen;
int outfd;
if (asprintf(&newname, "%s.XXXXXXXXXX", name) == -1) {
if (verbose)
warn("asprintf");
return (-1);
}
outfd = open(newname, O_CREAT|O_RDWR|O_TRUNC, 0600);
if (outfd == -1) {
warn("%s", newname);
free(newname);
return (-1);
}
buf = malloc(BUFSZ);
if (buf == NULL) {
if (verbose)
warn("malloc");
goto fail;
}
/* copy old file to new file */
lseek(infd, (off_t)0, SEEK_SET);
while (1) {
len = read(infd, buf, BUFSIZ);
if (len == -1) {
if (verbose)
warn("read");
free(buf);
goto fail;
}
if (len == 0)
break;
wlen = write(outfd, buf, len);
if (wlen != len) {
free(buf);
goto fail;
}
}
free(buf);
/* now back track, and delete the header */
if (prebind_remove_load_section(outfd, newname) == -1)
goto fail;
if (ftruncate(outfd, footer->orig_size) == -1)
goto fail;
/* move new file into place */
TIMESPEC_TO_TIMEVAL(&tv[0], &st->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tv[1], &st->st_mtimespec);
if (futimes(outfd, tv) == -1)
goto fail;
if (fchown(outfd, st->st_uid, st->st_gid) == -1)
goto fail;
if (fchmod(outfd, st->st_mode) == -1)
goto fail;
if (fchflags(outfd, st->st_flags) == -1)
goto fail;
if (rename(newname, name) == -1)
goto fail;
return (outfd);
fail:
free(newname);
unlink(newname);
close(outfd);
return (-1);
}
|