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