1ba10db99SColin Percival /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
31de7b4b8SPedro F. Giffuni  *
4ba10db99SColin Percival  * Copyright 2003-2005 Colin Percival
5ba10db99SColin Percival  * All rights reserved
6ba10db99SColin Percival  *
7ba10db99SColin Percival  * Redistribution and use in source and binary forms, with or without
8ba10db99SColin Percival  * modification, are permitted providing that the following conditions
9ba10db99SColin Percival  * are met:
10ba10db99SColin Percival  * 1. Redistributions of source code must retain the above copyright
11ba10db99SColin Percival  *    notice, this list of conditions and the following disclaimer.
12ba10db99SColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
13ba10db99SColin Percival  *    notice, this list of conditions and the following disclaimer in the
14ba10db99SColin Percival  *    documentation and/or other materials provided with the distribution.
15ba10db99SColin Percival  *
16ba10db99SColin Percival  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ba10db99SColin Percival  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18ba10db99SColin Percival  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba10db99SColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20ba10db99SColin Percival  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ba10db99SColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ba10db99SColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ba10db99SColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24ba10db99SColin Percival  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25ba10db99SColin Percival  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26ba10db99SColin Percival  * POSSIBILITY OF SUCH DAMAGE.
27ba10db99SColin Percival  */
28ba10db99SColin Percival 
29ba10db99SColin Percival #include <sys/cdefs.h>
30ba10db99SColin Percival __FBSDID("$FreeBSD$");
31ba10db99SColin Percival 
32c93b6e5fSXin LI #ifndef WITHOUT_CAPSICUM
3376723b39SAllan Jude #include <sys/capsicum.h>
3476723b39SAllan Jude #endif
3576723b39SAllan Jude 
36ba10db99SColin Percival #include <bzlib.h>
37ba10db99SColin Percival #include <err.h>
38ba10db99SColin Percival #include <fcntl.h>
3906ce2764SEd Maste #include <libgen.h>
40e3d9ae4cSEd Maste #include <limits.h>
416d9f0e4dSEd Maste #include <stdint.h>
42ce437befSEd Maste #include <stdio.h>
43ce437befSEd Maste #include <stdlib.h>
44ce437befSEd Maste #include <string.h>
45ce437befSEd Maste #include <unistd.h>
46ba10db99SColin Percival 
478904d5ecSColin Percival #ifndef O_BINARY
488904d5ecSColin Percival #define O_BINARY 0
498904d5ecSColin Percival #endif
506d9f0e4dSEd Maste #define HEADER_SIZE 32
518904d5ecSColin Percival 
5206ce2764SEd Maste static char *newfile;
5306ce2764SEd Maste static int dirfd = -1;
5406ce2764SEd Maste 
5506ce2764SEd Maste static void
exit_cleanup(void)5606ce2764SEd Maste exit_cleanup(void)
5706ce2764SEd Maste {
5806ce2764SEd Maste 
5906ce2764SEd Maste 	if (dirfd != -1 && newfile != NULL)
6006ce2764SEd Maste 		if (unlinkat(dirfd, newfile, 0))
6106ce2764SEd Maste 			warn("unlinkat");
6206ce2764SEd Maste }
6306ce2764SEd Maste 
64*20bd5941SEd Maste static inline off_t
add_off_t(off_t a,off_t b)65*20bd5941SEd Maste add_off_t(off_t a, off_t b)
66*20bd5941SEd Maste {
67*20bd5941SEd Maste 	off_t result;
68*20bd5941SEd Maste 
69*20bd5941SEd Maste #if __GNUC__ >= 5 || \
70*20bd5941SEd Maste     (defined(__has_builtin) && __has_builtin(__builtin_add_overflow))
71*20bd5941SEd Maste 	if (__builtin_add_overflow(a, b, &result))
72*20bd5941SEd Maste 		errx(1, "Corrupt patch");
73*20bd5941SEd Maste #else
74*20bd5941SEd Maste 	if ((b > 0 && a > OFF_MAX - b) || (b < 0 && a < OFF_MIN - b))
75*20bd5941SEd Maste 		errx(1, "Corrupt patch");
76*20bd5941SEd Maste 	result = a + b;
77*20bd5941SEd Maste #endif
78*20bd5941SEd Maste 	return result;
79*20bd5941SEd Maste }
80*20bd5941SEd Maste 
offtin(u_char * buf)81ba10db99SColin Percival static off_t offtin(u_char *buf)
82ba10db99SColin Percival {
83ba10db99SColin Percival 	off_t y;
84ba10db99SColin Percival 
85ba10db99SColin Percival 	y = buf[7] & 0x7F;
86ba10db99SColin Percival 	y = y * 256; y += buf[6];
87ba10db99SColin Percival 	y = y * 256; y += buf[5];
88ba10db99SColin Percival 	y = y * 256; y += buf[4];
89ba10db99SColin Percival 	y = y * 256; y += buf[3];
90ba10db99SColin Percival 	y = y * 256; y += buf[2];
91ba10db99SColin Percival 	y = y * 256; y += buf[1];
92ba10db99SColin Percival 	y = y * 256; y += buf[0];
93ba10db99SColin Percival 
94ce437befSEd Maste 	if (buf[7] & 0x80)
95ce437befSEd Maste 		y = -y;
96ba10db99SColin Percival 
97ce437befSEd Maste 	return (y);
98ba10db99SColin Percival }
99ba10db99SColin Percival 
10043e0d7bfSEd Schouten static void
usage(void)10143e0d7bfSEd Schouten usage(void)
10243e0d7bfSEd Schouten {
10343e0d7bfSEd Schouten 
10443e0d7bfSEd Schouten 	fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n");
10543e0d7bfSEd Schouten 	exit(1);
10643e0d7bfSEd Schouten }
10743e0d7bfSEd Schouten 
main(int argc,char * argv[])108ba10db99SColin Percival int main(int argc, char *argv[])
109ba10db99SColin Percival {
110ba10db99SColin Percival 	FILE *f, *cpf, *dpf, *epf;
111ba10db99SColin Percival 	BZFILE *cpfbz2, *dpfbz2, *epfbz2;
11206ce2764SEd Maste 	char *directory, *namebuf;
113ba10db99SColin Percival 	int cbz2err, dbz2err, ebz2err;
114ce437befSEd Maste 	int newfd, oldfd;
115e3d9ae4cSEd Maste 	off_t oldsize, newsize;
116e3d9ae4cSEd Maste 	off_t bzctrllen, bzdatalen;
1176d9f0e4dSEd Maste 	u_char header[HEADER_SIZE], buf[8];
118ba10db99SColin Percival 	u_char *old, *new;
119ba10db99SColin Percival 	off_t oldpos, newpos;
120ba10db99SColin Percival 	off_t ctrl[3];
1216d9f0e4dSEd Maste 	off_t i, lenread, offset;
122c93b6e5fSXin LI #ifndef WITHOUT_CAPSICUM
12306ce2764SEd Maste 	cap_rights_t rights_dir, rights_ro, rights_wr;
12476723b39SAllan Jude #endif
125ba10db99SColin Percival 
12643e0d7bfSEd Schouten 	if (argc != 4)
12743e0d7bfSEd Schouten 		usage();
128ba10db99SColin Percival 
129ba10db99SColin Percival 	/* Open patch file */
1308904d5ecSColin Percival 	if ((f = fopen(argv[3], "rb")) == NULL)
131ba10db99SColin Percival 		err(1, "fopen(%s)", argv[3]);
13276723b39SAllan Jude 	/* Open patch file for control block */
13376723b39SAllan Jude 	if ((cpf = fopen(argv[3], "rb")) == NULL)
13476723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
13576723b39SAllan Jude 	/* open patch file for diff block */
13676723b39SAllan Jude 	if ((dpf = fopen(argv[3], "rb")) == NULL)
13776723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
13876723b39SAllan Jude 	/* open patch file for extra block */
13976723b39SAllan Jude 	if ((epf = fopen(argv[3], "rb")) == NULL)
14076723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
14176723b39SAllan Jude 	/* open oldfile */
14276723b39SAllan Jude 	if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
14376723b39SAllan Jude 		err(1, "open(%s)", argv[1]);
14406ce2764SEd Maste 	/* open directory where we'll write newfile */
14506ce2764SEd Maste 	if ((namebuf = strdup(argv[2])) == NULL ||
14606ce2764SEd Maste 	    (directory = dirname(namebuf)) == NULL ||
14706ce2764SEd Maste 	    (dirfd = open(directory, O_DIRECTORY)) < 0)
14806ce2764SEd Maste 		err(1, "open %s", argv[2]);
14906ce2764SEd Maste 	free(namebuf);
15006ce2764SEd Maste 	if ((newfile = basename(argv[2])) == NULL)
15106ce2764SEd Maste 		err(1, "basename");
15276723b39SAllan Jude 	/* open newfile */
15306ce2764SEd Maste 	if ((newfd = openat(dirfd, newfile,
15406ce2764SEd Maste 	    O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0)
15576723b39SAllan Jude 		err(1, "open(%s)", argv[2]);
15606ce2764SEd Maste 	atexit(exit_cleanup);
15776723b39SAllan Jude 
158c93b6e5fSXin LI #ifndef WITHOUT_CAPSICUM
159a25896caSMariusz Zaborski 	if (cap_enter() < 0)
16076723b39SAllan Jude 		err(1, "failed to enter security sandbox");
161a25896caSMariusz Zaborski 
16276723b39SAllan Jude 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
16376723b39SAllan Jude 	cap_rights_init(&rights_wr, CAP_WRITE);
16406ce2764SEd Maste 	cap_rights_init(&rights_dir, CAP_UNLINKAT);
16576723b39SAllan Jude 
16676723b39SAllan Jude 	if (cap_rights_limit(fileno(f), &rights_ro) < 0 ||
16776723b39SAllan Jude 	    cap_rights_limit(fileno(cpf), &rights_ro) < 0 ||
16876723b39SAllan Jude 	    cap_rights_limit(fileno(dpf), &rights_ro) < 0 ||
16976723b39SAllan Jude 	    cap_rights_limit(fileno(epf), &rights_ro) < 0 ||
17076723b39SAllan Jude 	    cap_rights_limit(oldfd, &rights_ro) < 0 ||
17106ce2764SEd Maste 	    cap_rights_limit(newfd, &rights_wr) < 0 ||
17206ce2764SEd Maste 	    cap_rights_limit(dirfd, &rights_dir) < 0)
17376723b39SAllan Jude 		err(1, "cap_rights_limit() failed, could not restrict"
17476723b39SAllan Jude 		    " capabilities");
17576723b39SAllan Jude #endif
176ba10db99SColin Percival 
177ba10db99SColin Percival 	/*
178ba10db99SColin Percival 	File format:
179ba10db99SColin Percival 		0	8	"BSDIFF40"
180ba10db99SColin Percival 		8	8	X
181ba10db99SColin Percival 		16	8	Y
182ba10db99SColin Percival 		24	8	sizeof(newfile)
183ba10db99SColin Percival 		32	X	bzip2(control block)
184ba10db99SColin Percival 		32+X	Y	bzip2(diff block)
185ba10db99SColin Percival 		32+X+Y	???	bzip2(extra block)
186ba10db99SColin Percival 	with control block a set of triples (x,y,z) meaning "add x bytes
187ba10db99SColin Percival 	from oldfile to x bytes from the diff block; copy y bytes from the
188ba10db99SColin Percival 	extra block; seek forwards in oldfile by z bytes".
189ba10db99SColin Percival 	*/
190ba10db99SColin Percival 
191ba10db99SColin Percival 	/* Read header */
1926d9f0e4dSEd Maste 	if (fread(header, 1, HEADER_SIZE, f) < HEADER_SIZE) {
193ba10db99SColin Percival 		if (feof(f))
19404708d25SEd Maste 			errx(1, "Corrupt patch");
195ba10db99SColin Percival 		err(1, "fread(%s)", argv[3]);
196ba10db99SColin Percival 	}
197ba10db99SColin Percival 
198ba10db99SColin Percival 	/* Check for appropriate magic */
199ba10db99SColin Percival 	if (memcmp(header, "BSDIFF40", 8) != 0)
20004708d25SEd Maste 		errx(1, "Corrupt patch");
201ba10db99SColin Percival 
202ba10db99SColin Percival 	/* Read lengths from header */
203ba10db99SColin Percival 	bzctrllen = offtin(header + 8);
204ba10db99SColin Percival 	bzdatalen = offtin(header + 16);
205ba10db99SColin Percival 	newsize = offtin(header + 24);
2066d9f0e4dSEd Maste 	if (bzctrllen < 0 || bzctrllen > OFF_MAX - HEADER_SIZE ||
2076d9f0e4dSEd Maste 	    bzdatalen < 0 || bzctrllen + HEADER_SIZE > OFF_MAX - bzdatalen ||
208e3d9ae4cSEd Maste 	    newsize < 0 || newsize > SSIZE_MAX)
20904708d25SEd Maste 		errx(1, "Corrupt patch");
210ba10db99SColin Percival 
211ba10db99SColin Percival 	/* Close patch file and re-open it via libbzip2 at the right places */
212ba10db99SColin Percival 	if (fclose(f))
213ba10db99SColin Percival 		err(1, "fclose(%s)", argv[3]);
2146d9f0e4dSEd Maste 	offset = HEADER_SIZE;
2156d9f0e4dSEd Maste 	if (fseeko(cpf, offset, SEEK_SET))
2166d9f0e4dSEd Maste 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
217ba10db99SColin Percival 	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
218ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
219*20bd5941SEd Maste 	offset = add_off_t(offset, bzctrllen);
2206d9f0e4dSEd Maste 	if (fseeko(dpf, offset, SEEK_SET))
2216d9f0e4dSEd Maste 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
222ba10db99SColin Percival 	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
223ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
224*20bd5941SEd Maste 	offset = add_off_t(offset, bzdatalen);
2256d9f0e4dSEd Maste 	if (fseeko(epf, offset, SEEK_SET))
2266d9f0e4dSEd Maste 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
227ba10db99SColin Percival 	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
228ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
229ba10db99SColin Percival 
230ce437befSEd Maste 	if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
231e3d9ae4cSEd Maste 	    oldsize > SSIZE_MAX ||
232e3d9ae4cSEd Maste 	    (old = malloc(oldsize)) == NULL ||
233ce437befSEd Maste 	    lseek(oldfd, 0, SEEK_SET) != 0 ||
234ce437befSEd Maste 	    read(oldfd, old, oldsize) != oldsize ||
235ce437befSEd Maste 	    close(oldfd) == -1)
236ce437befSEd Maste 		err(1, "%s", argv[1]);
237e3d9ae4cSEd Maste 	if ((new = malloc(newsize)) == NULL)
238ce437befSEd Maste 		err(1, NULL);
239ba10db99SColin Percival 
240ce437befSEd Maste 	oldpos = 0;
241ce437befSEd Maste 	newpos = 0;
242ba10db99SColin Percival 	while (newpos < newsize) {
243ba10db99SColin Percival 		/* Read control data */
244ba10db99SColin Percival 		for (i = 0; i <= 2; i++) {
245ba10db99SColin Percival 			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
246ba10db99SColin Percival 			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
247ba10db99SColin Percival 			    (cbz2err != BZ_STREAM_END)))
24804708d25SEd Maste 				errx(1, "Corrupt patch");
249ba10db99SColin Percival 			ctrl[i] = offtin(buf);
25080c7cc1cSPedro F. Giffuni 		}
251ba10db99SColin Percival 
252ba10db99SColin Percival 		/* Sanity-check */
253e3d9ae4cSEd Maste 		if (ctrl[0] < 0 || ctrl[0] > INT_MAX ||
254e3d9ae4cSEd Maste 		    ctrl[1] < 0 || ctrl[1] > INT_MAX)
25504708d25SEd Maste 			errx(1, "Corrupt patch");
2562c8d04d0SXin LI 
2572c8d04d0SXin LI 		/* Sanity-check */
258*20bd5941SEd Maste 		if (add_off_t(newpos, ctrl[0]) > newsize)
25904708d25SEd Maste 			errx(1, "Corrupt patch");
260ba10db99SColin Percival 
261ba10db99SColin Percival 		/* Read diff string */
262ba10db99SColin Percival 		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
263ba10db99SColin Percival 		if ((lenread < ctrl[0]) ||
264ba10db99SColin Percival 		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
26504708d25SEd Maste 			errx(1, "Corrupt patch");
266ba10db99SColin Percival 
267ba10db99SColin Percival 		/* Add old data to diff string */
268ba10db99SColin Percival 		for (i = 0; i < ctrl[0]; i++)
269*20bd5941SEd Maste 			if (add_off_t(oldpos, i) < oldsize)
270ba10db99SColin Percival 				new[newpos + i] += old[oldpos + i];
271ba10db99SColin Percival 
272ba10db99SColin Percival 		/* Adjust pointers */
273*20bd5941SEd Maste 		newpos = add_off_t(newpos, ctrl[0]);
274*20bd5941SEd Maste 		oldpos = add_off_t(oldpos, ctrl[0]);
275ba10db99SColin Percival 
276ba10db99SColin Percival 		/* Sanity-check */
277*20bd5941SEd Maste 		if (add_off_t(newpos, ctrl[1]) > newsize)
27804708d25SEd Maste 			errx(1, "Corrupt patch");
279ba10db99SColin Percival 
280ba10db99SColin Percival 		/* Read extra string */
281ba10db99SColin Percival 		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
282ba10db99SColin Percival 		if ((lenread < ctrl[1]) ||
283ba10db99SColin Percival 		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
28404708d25SEd Maste 			errx(1, "Corrupt patch");
285ba10db99SColin Percival 
286ba10db99SColin Percival 		/* Adjust pointers */
287*20bd5941SEd Maste 		newpos = add_off_t(newpos, ctrl[1]);
288*20bd5941SEd Maste 		oldpos = add_off_t(oldpos, ctrl[2]);
28980c7cc1cSPedro F. Giffuni 	}
290ba10db99SColin Percival 
291ba10db99SColin Percival 	/* Clean up the bzip2 reads */
292ba10db99SColin Percival 	BZ2_bzReadClose(&cbz2err, cpfbz2);
293ba10db99SColin Percival 	BZ2_bzReadClose(&dbz2err, dpfbz2);
294ba10db99SColin Percival 	BZ2_bzReadClose(&ebz2err, epfbz2);
295ba10db99SColin Percival 	if (fclose(cpf) || fclose(dpf) || fclose(epf))
296ba10db99SColin Percival 		err(1, "fclose(%s)", argv[3]);
297ba10db99SColin Percival 
298ba10db99SColin Percival 	/* Write the new file */
299ce437befSEd Maste 	if (write(newfd, new, newsize) != newsize || close(newfd) == -1)
300ba10db99SColin Percival 		err(1, "%s", argv[2]);
30106ce2764SEd Maste 	/* Disable atexit cleanup */
30206ce2764SEd Maste 	newfile = NULL;
303ba10db99SColin Percival 
304ba10db99SColin Percival 	free(new);
305ba10db99SColin Percival 	free(old);
306ba10db99SColin Percival 
307ce437befSEd Maste 	return (0);
308ba10db99SColin Percival }
309