xref: /freebsd/usr.bin/bsdiff/bspatch/bspatch.c (revision e3d9ae4c56e15404846e4cb3360394a0a36cec23)
1ba10db99SColin Percival /*-
2ba10db99SColin Percival  * Copyright 2003-2005 Colin Percival
3ba10db99SColin Percival  * All rights reserved
4ba10db99SColin Percival  *
5ba10db99SColin Percival  * Redistribution and use in source and binary forms, with or without
6ba10db99SColin Percival  * modification, are permitted providing that the following conditions
7ba10db99SColin Percival  * are met:
8ba10db99SColin Percival  * 1. Redistributions of source code must retain the above copyright
9ba10db99SColin Percival  *    notice, this list of conditions and the following disclaimer.
10ba10db99SColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
11ba10db99SColin Percival  *    notice, this list of conditions and the following disclaimer in the
12ba10db99SColin Percival  *    documentation and/or other materials provided with the distribution.
13ba10db99SColin Percival  *
14ba10db99SColin Percival  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15ba10db99SColin Percival  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16ba10db99SColin Percival  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ba10db99SColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18ba10db99SColin Percival  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ba10db99SColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ba10db99SColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ba10db99SColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22ba10db99SColin Percival  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23ba10db99SColin Percival  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24ba10db99SColin Percival  * POSSIBILITY OF SUCH DAMAGE.
25ba10db99SColin Percival  */
26ba10db99SColin Percival 
27ba10db99SColin Percival #include <sys/cdefs.h>
28ba10db99SColin Percival __FBSDID("$FreeBSD$");
29ba10db99SColin Percival 
3076723b39SAllan Jude #if defined(__FreeBSD__)
3176723b39SAllan Jude #include <sys/param.h>
3276723b39SAllan Jude #if __FreeBSD_version >= 1100014
3376723b39SAllan Jude #include <sys/capsicum.h>
3476723b39SAllan Jude #define HAVE_CAPSICUM
3576723b39SAllan Jude #elif __FreeBSD_version >= 1000000
3676723b39SAllan Jude #include <sys/capability.h>
3776723b39SAllan Jude #define HAVE_CAPSICUM
3876723b39SAllan Jude #endif
3976723b39SAllan Jude #endif
4076723b39SAllan Jude 
41ba10db99SColin Percival #include <bzlib.h>
42ba10db99SColin Percival #include <err.h>
4376723b39SAllan Jude #include <errno.h>
44ba10db99SColin Percival #include <fcntl.h>
4506ce2764SEd Maste #include <libgen.h>
46*e3d9ae4cSEd Maste #include <limits.h>
47ce437befSEd Maste #include <stdio.h>
48ce437befSEd Maste #include <stdlib.h>
49ce437befSEd Maste #include <string.h>
50ce437befSEd Maste #include <unistd.h>
51ba10db99SColin Percival 
528904d5ecSColin Percival #ifndef O_BINARY
538904d5ecSColin Percival #define O_BINARY 0
548904d5ecSColin Percival #endif
558904d5ecSColin Percival 
5606ce2764SEd Maste static char *newfile;
5706ce2764SEd Maste static int dirfd = -1;
5806ce2764SEd Maste 
5906ce2764SEd Maste static void
6006ce2764SEd Maste exit_cleanup(void)
6106ce2764SEd Maste {
6206ce2764SEd Maste 
6306ce2764SEd Maste 	if (dirfd != -1 && newfile != NULL)
6406ce2764SEd Maste 		if (unlinkat(dirfd, newfile, 0))
6506ce2764SEd Maste 			warn("unlinkat");
6606ce2764SEd Maste }
6706ce2764SEd Maste 
68ba10db99SColin Percival static off_t offtin(u_char *buf)
69ba10db99SColin Percival {
70ba10db99SColin Percival 	off_t y;
71ba10db99SColin Percival 
72ba10db99SColin Percival 	y = buf[7] & 0x7F;
73ba10db99SColin Percival 	y = y * 256; y += buf[6];
74ba10db99SColin Percival 	y = y * 256; y += buf[5];
75ba10db99SColin Percival 	y = y * 256; y += buf[4];
76ba10db99SColin Percival 	y = y * 256; y += buf[3];
77ba10db99SColin Percival 	y = y * 256; y += buf[2];
78ba10db99SColin Percival 	y = y * 256; y += buf[1];
79ba10db99SColin Percival 	y = y * 256; y += buf[0];
80ba10db99SColin Percival 
81ce437befSEd Maste 	if (buf[7] & 0x80)
82ce437befSEd Maste 		y = -y;
83ba10db99SColin Percival 
84ce437befSEd Maste 	return (y);
85ba10db99SColin Percival }
86ba10db99SColin Percival 
8743e0d7bfSEd Schouten static void
8843e0d7bfSEd Schouten usage(void)
8943e0d7bfSEd Schouten {
9043e0d7bfSEd Schouten 
9143e0d7bfSEd Schouten 	fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n");
9243e0d7bfSEd Schouten 	exit(1);
9343e0d7bfSEd Schouten }
9443e0d7bfSEd Schouten 
95ba10db99SColin Percival int main(int argc, char *argv[])
96ba10db99SColin Percival {
97ba10db99SColin Percival 	FILE *f, *cpf, *dpf, *epf;
98ba10db99SColin Percival 	BZFILE *cpfbz2, *dpfbz2, *epfbz2;
9906ce2764SEd Maste 	char *directory, *namebuf;
100ba10db99SColin Percival 	int cbz2err, dbz2err, ebz2err;
101ce437befSEd Maste 	int newfd, oldfd;
102*e3d9ae4cSEd Maste 	off_t oldsize, newsize;
103*e3d9ae4cSEd Maste 	off_t bzctrllen, bzdatalen;
104ba10db99SColin Percival 	u_char header[32], buf[8];
105ba10db99SColin Percival 	u_char *old, *new;
106ba10db99SColin Percival 	off_t oldpos, newpos;
107ba10db99SColin Percival 	off_t ctrl[3];
108ba10db99SColin Percival 	off_t lenread;
109ba10db99SColin Percival 	off_t i;
11076723b39SAllan Jude #ifdef HAVE_CAPSICUM
11106ce2764SEd Maste 	cap_rights_t rights_dir, rights_ro, rights_wr;
11276723b39SAllan Jude #endif
113ba10db99SColin Percival 
11443e0d7bfSEd Schouten 	if (argc != 4)
11543e0d7bfSEd Schouten 		usage();
116ba10db99SColin Percival 
117ba10db99SColin Percival 	/* Open patch file */
1188904d5ecSColin Percival 	if ((f = fopen(argv[3], "rb")) == NULL)
119ba10db99SColin Percival 		err(1, "fopen(%s)", argv[3]);
12076723b39SAllan Jude 	/* Open patch file for control block */
12176723b39SAllan Jude 	if ((cpf = fopen(argv[3], "rb")) == NULL)
12276723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
12376723b39SAllan Jude 	/* open patch file for diff block */
12476723b39SAllan Jude 	if ((dpf = fopen(argv[3], "rb")) == NULL)
12576723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
12676723b39SAllan Jude 	/* open patch file for extra block */
12776723b39SAllan Jude 	if ((epf = fopen(argv[3], "rb")) == NULL)
12876723b39SAllan Jude 		err(1, "fopen(%s)", argv[3]);
12976723b39SAllan Jude 	/* open oldfile */
13076723b39SAllan Jude 	if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
13176723b39SAllan Jude 		err(1, "open(%s)", argv[1]);
13206ce2764SEd Maste 	/* open directory where we'll write newfile */
13306ce2764SEd Maste 	if ((namebuf = strdup(argv[2])) == NULL ||
13406ce2764SEd Maste 	    (directory = dirname(namebuf)) == NULL ||
13506ce2764SEd Maste 	    (dirfd = open(directory, O_DIRECTORY)) < 0)
13606ce2764SEd Maste 		err(1, "open %s", argv[2]);
13706ce2764SEd Maste 	free(namebuf);
13806ce2764SEd Maste 	if ((newfile = basename(argv[2])) == NULL)
13906ce2764SEd Maste 		err(1, "basename");
14076723b39SAllan Jude 	/* open newfile */
14106ce2764SEd Maste 	if ((newfd = openat(dirfd, newfile,
14206ce2764SEd Maste 	    O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0)
14376723b39SAllan Jude 		err(1, "open(%s)", argv[2]);
14406ce2764SEd Maste 	atexit(exit_cleanup);
14576723b39SAllan Jude 
14676723b39SAllan Jude #ifdef HAVE_CAPSICUM
14776723b39SAllan Jude 	if (cap_enter() < 0) {
14876723b39SAllan Jude 		/* Failed to sandbox, fatal if CAPABILITY_MODE enabled */
14976723b39SAllan Jude 		if (errno != ENOSYS)
15076723b39SAllan Jude 			err(1, "failed to enter security sandbox");
15176723b39SAllan Jude 	} else {
15276723b39SAllan Jude 		/* Capsicum Available */
15376723b39SAllan Jude 		cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
15476723b39SAllan Jude 		cap_rights_init(&rights_wr, CAP_WRITE);
15506ce2764SEd Maste 		cap_rights_init(&rights_dir, CAP_UNLINKAT);
15676723b39SAllan Jude 
15776723b39SAllan Jude 		if (cap_rights_limit(fileno(f), &rights_ro) < 0 ||
15876723b39SAllan Jude 		    cap_rights_limit(fileno(cpf), &rights_ro) < 0 ||
15976723b39SAllan Jude 		    cap_rights_limit(fileno(dpf), &rights_ro) < 0 ||
16076723b39SAllan Jude 		    cap_rights_limit(fileno(epf), &rights_ro) < 0 ||
16176723b39SAllan Jude 		    cap_rights_limit(oldfd, &rights_ro) < 0 ||
16206ce2764SEd Maste 		    cap_rights_limit(newfd, &rights_wr) < 0 ||
16306ce2764SEd Maste 		    cap_rights_limit(dirfd, &rights_dir) < 0)
16476723b39SAllan Jude 			err(1, "cap_rights_limit() failed, could not restrict"
16576723b39SAllan Jude 			    " capabilities");
16676723b39SAllan Jude 	}
16776723b39SAllan Jude #endif
168ba10db99SColin Percival 
169ba10db99SColin Percival 	/*
170ba10db99SColin Percival 	File format:
171ba10db99SColin Percival 		0	8	"BSDIFF40"
172ba10db99SColin Percival 		8	8	X
173ba10db99SColin Percival 		16	8	Y
174ba10db99SColin Percival 		24	8	sizeof(newfile)
175ba10db99SColin Percival 		32	X	bzip2(control block)
176ba10db99SColin Percival 		32+X	Y	bzip2(diff block)
177ba10db99SColin Percival 		32+X+Y	???	bzip2(extra block)
178ba10db99SColin Percival 	with control block a set of triples (x,y,z) meaning "add x bytes
179ba10db99SColin Percival 	from oldfile to x bytes from the diff block; copy y bytes from the
180ba10db99SColin Percival 	extra block; seek forwards in oldfile by z bytes".
181ba10db99SColin Percival 	*/
182ba10db99SColin Percival 
183ba10db99SColin Percival 	/* Read header */
184ba10db99SColin Percival 	if (fread(header, 1, 32, f) < 32) {
185ba10db99SColin Percival 		if (feof(f))
186ba10db99SColin Percival 			errx(1, "Corrupt patch\n");
187ba10db99SColin Percival 		err(1, "fread(%s)", argv[3]);
188ba10db99SColin Percival 	}
189ba10db99SColin Percival 
190ba10db99SColin Percival 	/* Check for appropriate magic */
191ba10db99SColin Percival 	if (memcmp(header, "BSDIFF40", 8) != 0)
192ba10db99SColin Percival 		errx(1, "Corrupt patch\n");
193ba10db99SColin Percival 
194ba10db99SColin Percival 	/* Read lengths from header */
195ba10db99SColin Percival 	bzctrllen = offtin(header + 8);
196ba10db99SColin Percival 	bzdatalen = offtin(header + 16);
197ba10db99SColin Percival 	newsize = offtin(header + 24);
198*e3d9ae4cSEd Maste 	if (bzctrllen < 0 || bzctrllen > OFF_MAX - 32 ||
199*e3d9ae4cSEd Maste 	    bzdatalen < 0 || bzctrllen + 32 > OFF_MAX - bzdatalen ||
200*e3d9ae4cSEd Maste 	    newsize < 0 || newsize > SSIZE_MAX)
201ba10db99SColin Percival 		errx(1, "Corrupt patch\n");
202ba10db99SColin Percival 
203ba10db99SColin Percival 	/* Close patch file and re-open it via libbzip2 at the right places */
204ba10db99SColin Percival 	if (fclose(f))
205ba10db99SColin Percival 		err(1, "fclose(%s)", argv[3]);
206ba10db99SColin Percival 	if (fseeko(cpf, 32, SEEK_SET))
207ba10db99SColin Percival 		err(1, "fseeko(%s, %lld)", argv[3],
208ba10db99SColin Percival 		    (long long)32);
209ba10db99SColin Percival 	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
210ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
211ba10db99SColin Percival 	if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
212ba10db99SColin Percival 		err(1, "fseeko(%s, %lld)", argv[3],
213ba10db99SColin Percival 		    (long long)(32 + bzctrllen));
214ba10db99SColin Percival 	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
215ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
216ba10db99SColin Percival 	if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
217ba10db99SColin Percival 		err(1, "fseeko(%s, %lld)", argv[3],
218ba10db99SColin Percival 		    (long long)(32 + bzctrllen + bzdatalen));
219ba10db99SColin Percival 	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
220ba10db99SColin Percival 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
221ba10db99SColin Percival 
222ce437befSEd Maste 	if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
223*e3d9ae4cSEd Maste 	    oldsize > SSIZE_MAX ||
224*e3d9ae4cSEd Maste 	    (old = malloc(oldsize)) == NULL ||
225ce437befSEd Maste 	    lseek(oldfd, 0, SEEK_SET) != 0 ||
226ce437befSEd Maste 	    read(oldfd, old, oldsize) != oldsize ||
227ce437befSEd Maste 	    close(oldfd) == -1)
228ce437befSEd Maste 		err(1, "%s", argv[1]);
229*e3d9ae4cSEd Maste 	if ((new = malloc(newsize)) == NULL)
230ce437befSEd Maste 		err(1, NULL);
231ba10db99SColin Percival 
232ce437befSEd Maste 	oldpos = 0;
233ce437befSEd Maste 	newpos = 0;
234ba10db99SColin Percival 	while (newpos < newsize) {
235ba10db99SColin Percival 		/* Read control data */
236ba10db99SColin Percival 		for (i = 0; i <= 2; i++) {
237ba10db99SColin Percival 			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
238ba10db99SColin Percival 			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
239ba10db99SColin Percival 			    (cbz2err != BZ_STREAM_END)))
240ba10db99SColin Percival 				errx(1, "Corrupt patch\n");
241ba10db99SColin Percival 			ctrl[i] = offtin(buf);
24280c7cc1cSPedro F. Giffuni 		}
243ba10db99SColin Percival 
244ba10db99SColin Percival 		/* Sanity-check */
245*e3d9ae4cSEd Maste 		if (ctrl[0] < 0 || ctrl[0] > INT_MAX ||
246*e3d9ae4cSEd Maste 		    ctrl[1] < 0 || ctrl[1] > INT_MAX)
2472c8d04d0SXin LI 			errx(1, "Corrupt patch\n");
2482c8d04d0SXin LI 
2492c8d04d0SXin LI 		/* Sanity-check */
250ba10db99SColin Percival 		if (newpos + ctrl[0] > newsize)
251ba10db99SColin Percival 			errx(1, "Corrupt patch\n");
252ba10db99SColin Percival 
253ba10db99SColin Percival 		/* Read diff string */
254ba10db99SColin Percival 		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
255ba10db99SColin Percival 		if ((lenread < ctrl[0]) ||
256ba10db99SColin Percival 		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
257ba10db99SColin Percival 			errx(1, "Corrupt patch\n");
258ba10db99SColin Percival 
259ba10db99SColin Percival 		/* Add old data to diff string */
260ba10db99SColin Percival 		for (i = 0; i < ctrl[0]; i++)
261ba10db99SColin Percival 			if ((oldpos + i >= 0) && (oldpos + i < oldsize))
262ba10db99SColin Percival 				new[newpos + i] += old[oldpos + i];
263ba10db99SColin Percival 
264ba10db99SColin Percival 		/* Adjust pointers */
265ba10db99SColin Percival 		newpos += ctrl[0];
266ba10db99SColin Percival 		oldpos += ctrl[0];
267ba10db99SColin Percival 
268ba10db99SColin Percival 		/* Sanity-check */
269ba10db99SColin Percival 		if (newpos + ctrl[1] > newsize)
270ba10db99SColin Percival 			errx(1, "Corrupt patch\n");
271ba10db99SColin Percival 
272ba10db99SColin Percival 		/* Read extra string */
273ba10db99SColin Percival 		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
274ba10db99SColin Percival 		if ((lenread < ctrl[1]) ||
275ba10db99SColin Percival 		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
276ba10db99SColin Percival 			errx(1, "Corrupt patch\n");
277ba10db99SColin Percival 
278ba10db99SColin Percival 		/* Adjust pointers */
279ba10db99SColin Percival 		newpos+=ctrl[1];
280ba10db99SColin Percival 		oldpos+=ctrl[2];
28180c7cc1cSPedro F. Giffuni 	}
282ba10db99SColin Percival 
283ba10db99SColin Percival 	/* Clean up the bzip2 reads */
284ba10db99SColin Percival 	BZ2_bzReadClose(&cbz2err, cpfbz2);
285ba10db99SColin Percival 	BZ2_bzReadClose(&dbz2err, dpfbz2);
286ba10db99SColin Percival 	BZ2_bzReadClose(&ebz2err, epfbz2);
287ba10db99SColin Percival 	if (fclose(cpf) || fclose(dpf) || fclose(epf))
288ba10db99SColin Percival 		err(1, "fclose(%s)", argv[3]);
289ba10db99SColin Percival 
290ba10db99SColin Percival 	/* Write the new file */
291ce437befSEd Maste 	if (write(newfd, new, newsize) != newsize || close(newfd) == -1)
292ba10db99SColin Percival 		err(1, "%s", argv[2]);
29306ce2764SEd Maste 	/* Disable atexit cleanup */
29406ce2764SEd Maste 	newfile = NULL;
295ba10db99SColin Percival 
296ba10db99SColin Percival 	free(new);
297ba10db99SColin Percival 	free(old);
298ba10db99SColin Percival 
299ce437befSEd Maste 	return (0);
300ba10db99SColin Percival }
301