xref: /freebsd/usr.bin/bsdiff/bspatch/bspatch.c (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2003-2005 Colin Percival
5  * All rights reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted providing that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #if defined(__FreeBSD__)
33 #include <sys/param.h>
34 #if __FreeBSD_version >= 1001511
35 #include <sys/capsicum.h>
36 #define HAVE_CAPSICUM
37 #endif
38 #endif
39 
40 #include <bzlib.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <libgen.h>
45 #include <limits.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #ifndef O_BINARY
53 #define O_BINARY 0
54 #endif
55 #define HEADER_SIZE 32
56 
57 static char *newfile;
58 static int dirfd = -1;
59 
60 static void
61 exit_cleanup(void)
62 {
63 
64 	if (dirfd != -1 && newfile != NULL)
65 		if (unlinkat(dirfd, newfile, 0))
66 			warn("unlinkat");
67 }
68 
69 static off_t offtin(u_char *buf)
70 {
71 	off_t y;
72 
73 	y = buf[7] & 0x7F;
74 	y = y * 256; y += buf[6];
75 	y = y * 256; y += buf[5];
76 	y = y * 256; y += buf[4];
77 	y = y * 256; y += buf[3];
78 	y = y * 256; y += buf[2];
79 	y = y * 256; y += buf[1];
80 	y = y * 256; y += buf[0];
81 
82 	if (buf[7] & 0x80)
83 		y = -y;
84 
85 	return (y);
86 }
87 
88 static void
89 usage(void)
90 {
91 
92 	fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n");
93 	exit(1);
94 }
95 
96 int main(int argc, char *argv[])
97 {
98 	FILE *f, *cpf, *dpf, *epf;
99 	BZFILE *cpfbz2, *dpfbz2, *epfbz2;
100 	char *directory, *namebuf;
101 	int cbz2err, dbz2err, ebz2err;
102 	int newfd, oldfd;
103 	off_t oldsize, newsize;
104 	off_t bzctrllen, bzdatalen;
105 	u_char header[HEADER_SIZE], buf[8];
106 	u_char *old, *new;
107 	off_t oldpos, newpos;
108 	off_t ctrl[3];
109 	off_t i, lenread, offset;
110 #ifdef HAVE_CAPSICUM
111 	cap_rights_t rights_dir, rights_ro, rights_wr;
112 #endif
113 
114 	if (argc != 4)
115 		usage();
116 
117 	/* Open patch file */
118 	if ((f = fopen(argv[3], "rb")) == NULL)
119 		err(1, "fopen(%s)", argv[3]);
120 	/* Open patch file for control block */
121 	if ((cpf = fopen(argv[3], "rb")) == NULL)
122 		err(1, "fopen(%s)", argv[3]);
123 	/* open patch file for diff block */
124 	if ((dpf = fopen(argv[3], "rb")) == NULL)
125 		err(1, "fopen(%s)", argv[3]);
126 	/* open patch file for extra block */
127 	if ((epf = fopen(argv[3], "rb")) == NULL)
128 		err(1, "fopen(%s)", argv[3]);
129 	/* open oldfile */
130 	if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
131 		err(1, "open(%s)", argv[1]);
132 	/* open directory where we'll write newfile */
133 	if ((namebuf = strdup(argv[2])) == NULL ||
134 	    (directory = dirname(namebuf)) == NULL ||
135 	    (dirfd = open(directory, O_DIRECTORY)) < 0)
136 		err(1, "open %s", argv[2]);
137 	free(namebuf);
138 	if ((newfile = basename(argv[2])) == NULL)
139 		err(1, "basename");
140 	/* open newfile */
141 	if ((newfd = openat(dirfd, newfile,
142 	    O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0)
143 		err(1, "open(%s)", argv[2]);
144 	atexit(exit_cleanup);
145 
146 #ifdef HAVE_CAPSICUM
147 	if (cap_enter() < 0)
148 		err(1, "failed to enter security sandbox");
149 
150 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
151 	cap_rights_init(&rights_wr, CAP_WRITE);
152 	cap_rights_init(&rights_dir, CAP_UNLINKAT);
153 
154 	if (cap_rights_limit(fileno(f), &rights_ro) < 0 ||
155 	    cap_rights_limit(fileno(cpf), &rights_ro) < 0 ||
156 	    cap_rights_limit(fileno(dpf), &rights_ro) < 0 ||
157 	    cap_rights_limit(fileno(epf), &rights_ro) < 0 ||
158 	    cap_rights_limit(oldfd, &rights_ro) < 0 ||
159 	    cap_rights_limit(newfd, &rights_wr) < 0 ||
160 	    cap_rights_limit(dirfd, &rights_dir) < 0)
161 		err(1, "cap_rights_limit() failed, could not restrict"
162 		    " capabilities");
163 #endif
164 
165 	/*
166 	File format:
167 		0	8	"BSDIFF40"
168 		8	8	X
169 		16	8	Y
170 		24	8	sizeof(newfile)
171 		32	X	bzip2(control block)
172 		32+X	Y	bzip2(diff block)
173 		32+X+Y	???	bzip2(extra block)
174 	with control block a set of triples (x,y,z) meaning "add x bytes
175 	from oldfile to x bytes from the diff block; copy y bytes from the
176 	extra block; seek forwards in oldfile by z bytes".
177 	*/
178 
179 	/* Read header */
180 	if (fread(header, 1, HEADER_SIZE, f) < HEADER_SIZE) {
181 		if (feof(f))
182 			errx(1, "Corrupt patch");
183 		err(1, "fread(%s)", argv[3]);
184 	}
185 
186 	/* Check for appropriate magic */
187 	if (memcmp(header, "BSDIFF40", 8) != 0)
188 		errx(1, "Corrupt patch");
189 
190 	/* Read lengths from header */
191 	bzctrllen = offtin(header + 8);
192 	bzdatalen = offtin(header + 16);
193 	newsize = offtin(header + 24);
194 	if (bzctrllen < 0 || bzctrllen > OFF_MAX - HEADER_SIZE ||
195 	    bzdatalen < 0 || bzctrllen + HEADER_SIZE > OFF_MAX - bzdatalen ||
196 	    newsize < 0 || newsize > SSIZE_MAX)
197 		errx(1, "Corrupt patch");
198 
199 	/* Close patch file and re-open it via libbzip2 at the right places */
200 	if (fclose(f))
201 		err(1, "fclose(%s)", argv[3]);
202 	offset = HEADER_SIZE;
203 	if (fseeko(cpf, offset, SEEK_SET))
204 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
205 	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
206 		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
207 	offset += bzctrllen;
208 	if (fseeko(dpf, offset, SEEK_SET))
209 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
210 	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
211 		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
212 	offset += bzdatalen;
213 	if (fseeko(epf, offset, SEEK_SET))
214 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
215 	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
216 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
217 
218 	if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
219 	    oldsize > SSIZE_MAX ||
220 	    (old = malloc(oldsize)) == NULL ||
221 	    lseek(oldfd, 0, SEEK_SET) != 0 ||
222 	    read(oldfd, old, oldsize) != oldsize ||
223 	    close(oldfd) == -1)
224 		err(1, "%s", argv[1]);
225 	if ((new = malloc(newsize)) == NULL)
226 		err(1, NULL);
227 
228 	oldpos = 0;
229 	newpos = 0;
230 	while (newpos < newsize) {
231 		/* Read control data */
232 		for (i = 0; i <= 2; i++) {
233 			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
234 			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
235 			    (cbz2err != BZ_STREAM_END)))
236 				errx(1, "Corrupt patch");
237 			ctrl[i] = offtin(buf);
238 		}
239 
240 		/* Sanity-check */
241 		if (ctrl[0] < 0 || ctrl[0] > INT_MAX ||
242 		    ctrl[1] < 0 || ctrl[1] > INT_MAX)
243 			errx(1, "Corrupt patch");
244 
245 		/* Sanity-check */
246 		if (newpos + ctrl[0] > newsize)
247 			errx(1, "Corrupt patch");
248 
249 		/* Read diff string */
250 		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
251 		if ((lenread < ctrl[0]) ||
252 		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
253 			errx(1, "Corrupt patch");
254 
255 		/* Add old data to diff string */
256 		for (i = 0; i < ctrl[0]; i++)
257 			if ((oldpos + i >= 0) && (oldpos + i < oldsize))
258 				new[newpos + i] += old[oldpos + i];
259 
260 		/* Adjust pointers */
261 		newpos += ctrl[0];
262 		oldpos += ctrl[0];
263 
264 		/* Sanity-check */
265 		if (newpos + ctrl[1] > newsize)
266 			errx(1, "Corrupt patch");
267 
268 		/* Read extra string */
269 		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
270 		if ((lenread < ctrl[1]) ||
271 		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
272 			errx(1, "Corrupt patch");
273 
274 		/* Adjust pointers */
275 		newpos+=ctrl[1];
276 		oldpos+=ctrl[2];
277 	}
278 
279 	/* Clean up the bzip2 reads */
280 	BZ2_bzReadClose(&cbz2err, cpfbz2);
281 	BZ2_bzReadClose(&dbz2err, dpfbz2);
282 	BZ2_bzReadClose(&ebz2err, epfbz2);
283 	if (fclose(cpf) || fclose(dpf) || fclose(epf))
284 		err(1, "fclose(%s)", argv[3]);
285 
286 	/* Write the new file */
287 	if (write(newfd, new, newsize) != newsize || close(newfd) == -1)
288 		err(1, "%s", argv[2]);
289 	/* Disable atexit cleanup */
290 	newfile = NULL;
291 
292 	free(new);
293 	free(old);
294 
295 	return (0);
296 }
297