xref: /freebsd/usr.bin/bsdiff/bspatch/bspatch.c (revision 36d7818975359fd2aacb19e4f9442a841dc954bb)
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 #ifndef WITHOUT_CAPSICUM
33 #include <sys/capsicum.h>
34 #endif
35 
36 #include <bzlib.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <limits.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
50 #define HEADER_SIZE 32
51 
52 static char *newfile;
53 static int dirfd = -1;
54 
55 static void
56 exit_cleanup(void)
57 {
58 
59 	if (dirfd != -1 && newfile != NULL)
60 		if (unlinkat(dirfd, newfile, 0))
61 			warn("unlinkat");
62 }
63 
64 static off_t offtin(u_char *buf)
65 {
66 	off_t y;
67 
68 	y = buf[7] & 0x7F;
69 	y = y * 256; y += buf[6];
70 	y = y * 256; y += buf[5];
71 	y = y * 256; y += buf[4];
72 	y = y * 256; y += buf[3];
73 	y = y * 256; y += buf[2];
74 	y = y * 256; y += buf[1];
75 	y = y * 256; y += buf[0];
76 
77 	if (buf[7] & 0x80)
78 		y = -y;
79 
80 	return (y);
81 }
82 
83 static void
84 usage(void)
85 {
86 
87 	fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n");
88 	exit(1);
89 }
90 
91 int main(int argc, char *argv[])
92 {
93 	FILE *f, *cpf, *dpf, *epf;
94 	BZFILE *cpfbz2, *dpfbz2, *epfbz2;
95 	char *directory, *namebuf;
96 	int cbz2err, dbz2err, ebz2err;
97 	int newfd, oldfd;
98 	off_t oldsize, newsize;
99 	off_t bzctrllen, bzdatalen;
100 	u_char header[HEADER_SIZE], buf[8];
101 	u_char *old, *new;
102 	off_t oldpos, newpos;
103 	off_t ctrl[3];
104 	off_t i, lenread, offset;
105 #ifndef WITHOUT_CAPSICUM
106 	cap_rights_t rights_dir, rights_ro, rights_wr;
107 #endif
108 
109 	if (argc != 4)
110 		usage();
111 
112 	/* Open patch file */
113 	if ((f = fopen(argv[3], "rb")) == NULL)
114 		err(1, "fopen(%s)", argv[3]);
115 	/* Open patch file for control block */
116 	if ((cpf = fopen(argv[3], "rb")) == NULL)
117 		err(1, "fopen(%s)", argv[3]);
118 	/* open patch file for diff block */
119 	if ((dpf = fopen(argv[3], "rb")) == NULL)
120 		err(1, "fopen(%s)", argv[3]);
121 	/* open patch file for extra block */
122 	if ((epf = fopen(argv[3], "rb")) == NULL)
123 		err(1, "fopen(%s)", argv[3]);
124 	/* open oldfile */
125 	if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
126 		err(1, "open(%s)", argv[1]);
127 	/* open directory where we'll write newfile */
128 	if ((namebuf = strdup(argv[2])) == NULL ||
129 	    (directory = dirname(namebuf)) == NULL ||
130 	    (dirfd = open(directory, O_DIRECTORY)) < 0)
131 		err(1, "open %s", argv[2]);
132 	free(namebuf);
133 	if ((newfile = basename(argv[2])) == NULL)
134 		err(1, "basename");
135 	/* open newfile */
136 	if ((newfd = openat(dirfd, newfile,
137 	    O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0)
138 		err(1, "open(%s)", argv[2]);
139 	atexit(exit_cleanup);
140 
141 #ifndef WITHOUT_CAPSICUM
142 	if (cap_enter() < 0)
143 		err(1, "failed to enter security sandbox");
144 
145 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
146 	cap_rights_init(&rights_wr, CAP_WRITE);
147 	cap_rights_init(&rights_dir, CAP_UNLINKAT);
148 
149 	if (cap_rights_limit(fileno(f), &rights_ro) < 0 ||
150 	    cap_rights_limit(fileno(cpf), &rights_ro) < 0 ||
151 	    cap_rights_limit(fileno(dpf), &rights_ro) < 0 ||
152 	    cap_rights_limit(fileno(epf), &rights_ro) < 0 ||
153 	    cap_rights_limit(oldfd, &rights_ro) < 0 ||
154 	    cap_rights_limit(newfd, &rights_wr) < 0 ||
155 	    cap_rights_limit(dirfd, &rights_dir) < 0)
156 		err(1, "cap_rights_limit() failed, could not restrict"
157 		    " capabilities");
158 #endif
159 
160 	/*
161 	File format:
162 		0	8	"BSDIFF40"
163 		8	8	X
164 		16	8	Y
165 		24	8	sizeof(newfile)
166 		32	X	bzip2(control block)
167 		32+X	Y	bzip2(diff block)
168 		32+X+Y	???	bzip2(extra block)
169 	with control block a set of triples (x,y,z) meaning "add x bytes
170 	from oldfile to x bytes from the diff block; copy y bytes from the
171 	extra block; seek forwards in oldfile by z bytes".
172 	*/
173 
174 	/* Read header */
175 	if (fread(header, 1, HEADER_SIZE, f) < HEADER_SIZE) {
176 		if (feof(f))
177 			errx(1, "Corrupt patch");
178 		err(1, "fread(%s)", argv[3]);
179 	}
180 
181 	/* Check for appropriate magic */
182 	if (memcmp(header, "BSDIFF40", 8) != 0)
183 		errx(1, "Corrupt patch");
184 
185 	/* Read lengths from header */
186 	bzctrllen = offtin(header + 8);
187 	bzdatalen = offtin(header + 16);
188 	newsize = offtin(header + 24);
189 	if (bzctrllen < 0 || bzctrllen > OFF_MAX - HEADER_SIZE ||
190 	    bzdatalen < 0 || bzctrllen + HEADER_SIZE > OFF_MAX - bzdatalen ||
191 	    newsize < 0 || newsize > SSIZE_MAX)
192 		errx(1, "Corrupt patch");
193 
194 	/* Close patch file and re-open it via libbzip2 at the right places */
195 	if (fclose(f))
196 		err(1, "fclose(%s)", argv[3]);
197 	offset = HEADER_SIZE;
198 	if (fseeko(cpf, offset, SEEK_SET))
199 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
200 	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
201 		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
202 	offset += bzctrllen;
203 	if (fseeko(dpf, offset, SEEK_SET))
204 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
205 	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
206 		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
207 	offset += bzdatalen;
208 	if (fseeko(epf, offset, SEEK_SET))
209 		err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
210 	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
211 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
212 
213 	if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
214 	    oldsize > SSIZE_MAX ||
215 	    (old = malloc(oldsize)) == NULL ||
216 	    lseek(oldfd, 0, SEEK_SET) != 0 ||
217 	    read(oldfd, old, oldsize) != oldsize ||
218 	    close(oldfd) == -1)
219 		err(1, "%s", argv[1]);
220 	if ((new = malloc(newsize)) == NULL)
221 		err(1, NULL);
222 
223 	oldpos = 0;
224 	newpos = 0;
225 	while (newpos < newsize) {
226 		/* Read control data */
227 		for (i = 0; i <= 2; i++) {
228 			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
229 			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
230 			    (cbz2err != BZ_STREAM_END)))
231 				errx(1, "Corrupt patch");
232 			ctrl[i] = offtin(buf);
233 		}
234 
235 		/* Sanity-check */
236 		if (ctrl[0] < 0 || ctrl[0] > INT_MAX ||
237 		    ctrl[1] < 0 || ctrl[1] > INT_MAX)
238 			errx(1, "Corrupt patch");
239 
240 		/* Sanity-check */
241 		if (newpos + ctrl[0] > newsize)
242 			errx(1, "Corrupt patch");
243 
244 		/* Read diff string */
245 		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
246 		if ((lenread < ctrl[0]) ||
247 		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
248 			errx(1, "Corrupt patch");
249 
250 		/* Add old data to diff string */
251 		for (i = 0; i < ctrl[0]; i++)
252 			if ((oldpos + i >= 0) && (oldpos + i < oldsize))
253 				new[newpos + i] += old[oldpos + i];
254 
255 		/* Adjust pointers */
256 		newpos += ctrl[0];
257 		oldpos += ctrl[0];
258 
259 		/* Sanity-check */
260 		if (newpos + ctrl[1] > newsize)
261 			errx(1, "Corrupt patch");
262 
263 		/* Read extra string */
264 		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
265 		if ((lenread < ctrl[1]) ||
266 		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
267 			errx(1, "Corrupt patch");
268 
269 		/* Adjust pointers */
270 		newpos+=ctrl[1];
271 		oldpos+=ctrl[2];
272 	}
273 
274 	/* Clean up the bzip2 reads */
275 	BZ2_bzReadClose(&cbz2err, cpfbz2);
276 	BZ2_bzReadClose(&dbz2err, dpfbz2);
277 	BZ2_bzReadClose(&ebz2err, epfbz2);
278 	if (fclose(cpf) || fclose(dpf) || fclose(epf))
279 		err(1, "fclose(%s)", argv[3]);
280 
281 	/* Write the new file */
282 	if (write(newfd, new, newsize) != newsize || close(newfd) == -1)
283 		err(1, "%s", argv[2]);
284 	/* Disable atexit cleanup */
285 	newfile = NULL;
286 
287 	free(new);
288 	free(old);
289 
290 	return (0);
291 }
292