1
0

033-CVE-2020-14315.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. Description: patch for CVE-2020-14315
  2. A memory corruption vulnerability is present in bspatch as shipped in
  3. Colin Percival’s bsdiff tools version 4.3. Insufficient checks when
  4. handling external inputs allows an attacker to bypass the sanity checks
  5. in place and write out of a dynamically allocated buffer boundaries.
  6. Source: https://svnweb.freebsd.org/base/head/usr.bin/bsdiff/bspatch/bspatch.c?revision=352742&view=co
  7. Author: tony mancill <tmancill@debian.org>
  8. Comment: The patch was created by comparing the Debian sources to the
  9. "Confirmed Patched Version" [1] documented in the
  10. X41 D-SEC GmbH Security Advisory: X41-2020-006 [2].
  11. References to FreeBSD capsicum have been dropped. Definitions for
  12. TYPE_MINIMUM and TYPE_MAXIMUM have been borrowed from the Debian
  13. coreutils package sources but originate in gnulib [3] and are used to
  14. define OFF_MIN and OFF_MAX (limits of off_t). Whitespace changes from
  15. the confirmed patched version are also included and keep the difference
  16. between the Debian sources and the confirmed patched version minimal.
  17. .
  18. [1] https://svnweb.freebsd.org/base/head/usr.bin/bsdiff/bspatch/bspatch.c?revision=352742&view=co
  19. [2] https://www.openwall.com/lists/oss-security/2020/07/09/2
  20. [3] https://www.gnu.org/software/gnulib/
  21. Last-Update: 2021-04-03
  22. Forwarded: not-needed
  23. Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=964796
  24. --- a/bspatch.c
  25. +++ b/bspatch.c
  26. @@ -1,4 +1,6 @@
  27. /*-
  28. + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  29. + *
  30. * Copyright 2003-2005 Colin Percival
  31. * All rights reserved
  32. *
  33. @@ -25,55 +27,147 @@
  34. */
  35. #if 0
  36. -__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $");
  37. +__FBSDID("$FreeBSD$");
  38. #endif
  39. #include <bzlib.h>
  40. -#include <stdlib.h>
  41. +#include <err.h>
  42. +#include <fcntl.h>
  43. +#include <libgen.h>
  44. +#include <limits.h>
  45. +#include <stdint.h>
  46. #include <stdio.h>
  47. +#include <stdlib.h>
  48. #include <string.h>
  49. -#include <err.h>
  50. #include <unistd.h>
  51. -#include <fcntl.h>
  52. +
  53. +#ifndef O_BINARY
  54. +#define O_BINARY 0
  55. +#endif
  56. +#define HEADER_SIZE 32
  57. +
  58. +/* TYPE_MINIMUM and TYPE_MAXIMUM taken from coreutils */
  59. +#ifndef TYPE_MINIMUM
  60. +#define TYPE_MINIMUM(t) \
  61. + ((t) ((t) 0 < (t) -1 ? (t) 0 : ~ TYPE_MAXIMUM (t)))
  62. +#endif
  63. +#ifndef TYPE_MAXIMUM
  64. +#define TYPE_MAXIMUM(t) \
  65. + ((t) ((t) 0 < (t) -1 \
  66. + ? (t) -1 \
  67. + : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  68. +#endif
  69. +
  70. +#ifndef OFF_MAX
  71. +#define OFF_MAX TYPE_MAXIMUM(off_t)
  72. +#endif
  73. +
  74. +#ifndef OFF_MIN
  75. +#define OFF_MIN TYPE_MINIMUM(off_t)
  76. +#endif
  77. +
  78. +static char *newfile;
  79. +static int dirfd = -1;
  80. +
  81. +static void
  82. +exit_cleanup(void)
  83. +{
  84. +
  85. + if (dirfd != -1 && newfile != NULL)
  86. + if (unlinkat(dirfd, newfile, 0))
  87. + warn("unlinkat");
  88. +}
  89. +
  90. +static inline off_t
  91. +add_off_t(off_t a, off_t b)
  92. +{
  93. + off_t result;
  94. +
  95. +#if __GNUC__ >= 5 || \
  96. + (defined(__has_builtin) && __has_builtin(__builtin_add_overflow))
  97. + if (__builtin_add_overflow(a, b, &result))
  98. + errx(1, "Corrupt patch");
  99. +#else
  100. + if ((b > 0 && a > OFF_MAX - b) || (b < 0 && a < OFF_MIN - b))
  101. + errx(1, "Corrupt patch");
  102. + result = a + b;
  103. +#endif
  104. + return result;
  105. +}
  106. static off_t offtin(unsigned char *buf)
  107. {
  108. off_t y;
  109. - y=buf[7]&0x7F;
  110. - y=y*256;y+=buf[6];
  111. - y=y*256;y+=buf[5];
  112. - y=y*256;y+=buf[4];
  113. - y=y*256;y+=buf[3];
  114. - y=y*256;y+=buf[2];
  115. - y=y*256;y+=buf[1];
  116. - y=y*256;y+=buf[0];
  117. + y = buf[7] & 0x7F;
  118. + y = y * 256; y += buf[6];
  119. + y = y * 256; y += buf[5];
  120. + y = y * 256; y += buf[4];
  121. + y = y * 256; y += buf[3];
  122. + y = y * 256; y += buf[2];
  123. + y = y * 256; y += buf[1];
  124. + y = y * 256; y += buf[0];
  125. - if(buf[7]&0x80) y=-y;
  126. + if (buf[7] & 0x80)
  127. + y = -y;
  128. - return y;
  129. + return (y);
  130. }
  131. -int main(int argc,char * argv[])
  132. +static void
  133. +usage(void)
  134. {
  135. - FILE * f, * cpf, * dpf, * epf;
  136. - BZFILE * cpfbz2, * dpfbz2, * epfbz2;
  137. +
  138. + fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n");
  139. + exit(1);
  140. +}
  141. +
  142. +int main(int argc, char *argv[])
  143. +{
  144. + FILE *f, *cpf, *dpf, *epf;
  145. + BZFILE *cpfbz2, *dpfbz2, *epfbz2;
  146. + char *directory, *namebuf;
  147. int cbz2err, dbz2err, ebz2err;
  148. - int fd;
  149. - ssize_t oldsize,newsize;
  150. - ssize_t bzctrllen,bzdatalen;
  151. - unsigned char header[32],buf[8];
  152. + int newfd, oldfd;
  153. + off_t oldsize, newsize;
  154. + off_t bzctrllen, bzdatalen;
  155. + unsigned char header[HEADER_SIZE], buf[8];
  156. unsigned char *old, *new;
  157. - off_t oldpos,newpos;
  158. + off_t oldpos, newpos;
  159. off_t ctrl[3];
  160. - off_t lenread;
  161. - off_t i;
  162. + off_t i, lenread, offset;
  163. - if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  164. + if (argc != 4)
  165. + usage();
  166. /* Open patch file */
  167. - if ((f = fopen(argv[3], "r")) == NULL)
  168. + if ((f = fopen(argv[3], "rb")) == NULL)
  169. + err(1, "fopen(%s)", argv[3]);
  170. + /* Open patch file for control block */
  171. + if ((cpf = fopen(argv[3], "rb")) == NULL)
  172. + err(1, "fopen(%s)", argv[3]);
  173. + /* open patch file for diff block */
  174. + if ((dpf = fopen(argv[3], "rb")) == NULL)
  175. err(1, "fopen(%s)", argv[3]);
  176. + /* open patch file for extra block */
  177. + if ((epf = fopen(argv[3], "rb")) == NULL)
  178. + err(1, "fopen(%s)", argv[3]);
  179. + /* open oldfile */
  180. + if ((oldfd = open(argv[1], O_RDONLY | O_BINARY, 0)) < 0)
  181. + err(1, "open(%s)", argv[1]);
  182. + /* open directory where we'll write newfile */
  183. + if ((namebuf = strdup(argv[2])) == NULL ||
  184. + (directory = dirname(namebuf)) == NULL ||
  185. + (dirfd = open(directory, O_DIRECTORY)) < 0)
  186. + err(1, "open %s", argv[2]);
  187. + free(namebuf);
  188. + if ((newfile = basename(argv[2])) == NULL)
  189. + err(1, "basename");
  190. + /* open newfile */
  191. + if ((newfd = openat(dirfd, newfile,
  192. + O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) < 0)
  193. + err(1, "open(%s)", argv[2]);
  194. + atexit(exit_cleanup);
  195. /*
  196. File format:
  197. @@ -90,104 +184,104 @@ int main(int argc,char * argv[])
  198. */
  199. /* Read header */
  200. - if (fread(header, 1, 32, f) < 32) {
  201. + if (fread(header, 1, HEADER_SIZE, f) < HEADER_SIZE) {
  202. if (feof(f))
  203. - errx(1, "Corrupt patch\n");
  204. + errx(1, "Corrupt patch");
  205. err(1, "fread(%s)", argv[3]);
  206. }
  207. /* Check for appropriate magic */
  208. if (memcmp(header, "BSDIFF40", 8) != 0)
  209. - errx(1, "Corrupt patch\n");
  210. + errx(1, "Corrupt patch");
  211. /* Read lengths from header */
  212. - bzctrllen=offtin(header+8);
  213. - bzdatalen=offtin(header+16);
  214. - newsize=offtin(header+24);
  215. - if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
  216. - errx(1,"Corrupt patch\n");
  217. + bzctrllen = offtin(header + 8);
  218. + bzdatalen = offtin(header + 16);
  219. + newsize = offtin(header + 24);
  220. + if (bzctrllen < 0 || bzctrllen > OFF_MAX - HEADER_SIZE ||
  221. + bzdatalen < 0 || bzctrllen + HEADER_SIZE > OFF_MAX - bzdatalen ||
  222. + newsize < 0 || newsize > SSIZE_MAX)
  223. + errx(1, "Corrupt patch");
  224. /* Close patch file and re-open it via libbzip2 at the right places */
  225. if (fclose(f))
  226. err(1, "fclose(%s)", argv[3]);
  227. - if ((cpf = fopen(argv[3], "r")) == NULL)
  228. - err(1, "fopen(%s)", argv[3]);
  229. - if (fseeko(cpf, 32, SEEK_SET))
  230. - err(1, "fseeko(%s, %lld)", argv[3],
  231. - (long long)32);
  232. + offset = HEADER_SIZE;
  233. + if (fseeko(cpf, offset, SEEK_SET))
  234. + err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
  235. if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
  236. errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
  237. - if ((dpf = fopen(argv[3], "r")) == NULL)
  238. - err(1, "fopen(%s)", argv[3]);
  239. - if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
  240. - err(1, "fseeko(%s, %lld)", argv[3],
  241. - (long long)(32 + bzctrllen));
  242. + offset = add_off_t(offset, bzctrllen);
  243. + if (fseeko(dpf, offset, SEEK_SET))
  244. + err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
  245. if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
  246. errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
  247. - if ((epf = fopen(argv[3], "r")) == NULL)
  248. - err(1, "fopen(%s)", argv[3]);
  249. - if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
  250. - err(1, "fseeko(%s, %lld)", argv[3],
  251. - (long long)(32 + bzctrllen + bzdatalen));
  252. + offset = add_off_t(offset, bzdatalen);
  253. + if (fseeko(epf, offset, SEEK_SET))
  254. + err(1, "fseeko(%s, %jd)", argv[3], (intmax_t)offset);
  255. if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
  256. errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
  257. - if(((fd=open(argv[1],O_RDONLY,0))<0) ||
  258. - ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  259. - ((old=malloc(oldsize+1))==NULL) ||
  260. - (lseek(fd,0,SEEK_SET)!=0) ||
  261. - (read(fd,old,oldsize)!=oldsize) ||
  262. - (close(fd)==-1)) err(1,"%s",argv[1]);
  263. - if((new=malloc(newsize+1))==NULL) err(1,NULL);
  264. -
  265. - oldpos=0;newpos=0;
  266. - while(newpos<newsize) {
  267. + if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
  268. + oldsize > SSIZE_MAX ||
  269. + (old = malloc(oldsize)) == NULL ||
  270. + lseek(oldfd, 0, SEEK_SET) != 0 ||
  271. + read(oldfd, old, oldsize) != oldsize ||
  272. + close(oldfd) == -1)
  273. + err(1, "%s", argv[1]);
  274. + if ((new = malloc(newsize)) == NULL)
  275. + err(1, NULL);
  276. +
  277. + oldpos = 0;
  278. + newpos = 0;
  279. + while (newpos < newsize) {
  280. /* Read control data */
  281. - for(i=0;i<=2;i++) {
  282. + for (i = 0; i <= 2; i++) {
  283. lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
  284. if ((lenread < 8) || ((cbz2err != BZ_OK) &&
  285. (cbz2err != BZ_STREAM_END)))
  286. - errx(1, "Corrupt patch\n");
  287. - ctrl[i]=offtin(buf);
  288. - };
  289. + errx(1, "Corrupt patch");
  290. + ctrl[i] = offtin(buf);
  291. + }
  292. /* Sanity-check */
  293. - if ((ctrl[0] < 0) || (ctrl[1] < 0))
  294. - errx(1,"Corrupt patch\n");
  295. + if (ctrl[0] < 0 || ctrl[0] > INT_MAX ||
  296. + ctrl[1] < 0 || ctrl[1] > INT_MAX)
  297. + errx(1, "Corrupt patch");
  298. /* Sanity-check */
  299. - if(newpos+ctrl[0]>newsize)
  300. - errx(1,"Corrupt patch\n");
  301. + if (add_off_t(newpos, ctrl[0]) > newsize)
  302. + errx(1, "Corrupt patch");
  303. /* Read diff string */
  304. lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
  305. if ((lenread < ctrl[0]) ||
  306. ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
  307. - errx(1, "Corrupt patch\n");
  308. + errx(1, "Corrupt patch");
  309. /* Add old data to diff string */
  310. - for(i=0;i<ctrl[0];i++)
  311. - if((oldpos+i>=0) && (oldpos+i<oldsize))
  312. - new[newpos+i]+=old[oldpos+i];
  313. + for (i = 0; i < ctrl[0]; i++)
  314. + if (add_off_t(oldpos, i) < oldsize)
  315. + new[newpos + i] += old[oldpos + i];
  316. /* Adjust pointers */
  317. - newpos+=ctrl[0];
  318. - oldpos+=ctrl[0];
  319. + newpos = add_off_t(newpos, ctrl[0]);
  320. + oldpos = add_off_t(oldpos, ctrl[0]);
  321. /* Sanity-check */
  322. - if(newpos+ctrl[1]>newsize)
  323. - errx(1,"Corrupt patch\n");
  324. + if (add_off_t(newpos, ctrl[1]) > newsize)
  325. + errx(1, "Corrupt patch");
  326. /* Read extra string */
  327. lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
  328. if ((lenread < ctrl[1]) ||
  329. ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
  330. - errx(1, "Corrupt patch\n");
  331. + errx(1, "Corrupt patch");
  332. /* Adjust pointers */
  333. - newpos+=ctrl[1];
  334. - oldpos+=ctrl[2];
  335. - };
  336. + newpos = add_off_t(newpos, ctrl[1]);
  337. + oldpos = add_off_t(oldpos, ctrl[2]);
  338. + }
  339. /* Clean up the bzip2 reads */
  340. BZ2_bzReadClose(&cbz2err, cpfbz2);
  341. @@ -197,12 +291,13 @@ int main(int argc,char * argv[])
  342. err(1, "fclose(%s)", argv[3]);
  343. /* Write the new file */
  344. - if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
  345. - (write(fd,new,newsize)!=newsize) || (close(fd)==-1))
  346. - err(1,"%s",argv[2]);
  347. + if (write(newfd, new, newsize) != newsize || close(newfd) == -1)
  348. + err(1, "%s", argv[2]);
  349. + /* Disable atexit cleanup */
  350. + newfile = NULL;
  351. free(new);
  352. free(old);
  353. - return 0;
  354. + return (0);
  355. }