Date: Wed, 18 Sep 2013 22:03:20 GMT From: dpl@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r257471 - in soc2013/dpl/head/usr.bin/bsdiff: bsdiff bspatch Message-ID: <201309182203.r8IM3KQu090008@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dpl Date: Wed Sep 18 22:03:20 2013 New Revision: 257471 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=257471 Log: Ported bsdiff and bspatch to Capsicum. Hope that it is useful. Modified: soc2013/dpl/head/usr.bin/bsdiff/bsdiff/bsdiff.c soc2013/dpl/head/usr.bin/bsdiff/bspatch/bspatch.c Modified: soc2013/dpl/head/usr.bin/bsdiff/bsdiff/bsdiff.c ============================================================================== --- soc2013/dpl/head/usr.bin/bsdiff/bsdiff/bsdiff.c Wed Sep 18 21:15:21 2013 (r257470) +++ soc2013/dpl/head/usr.bin/bsdiff/bsdiff/bsdiff.c Wed Sep 18 22:03:20 2013 (r257471) @@ -27,6 +27,7 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <sys/capability.h> #include <sys/types.h> #include <bzlib.h> @@ -41,7 +42,7 @@ #define O_BINARY 0 #endif -#define MIN(x,y) (((x)<(y)) ? (x) : (y)) +#define DMIN(x,y) (((x)<(y)) ? (x) : (y)) static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h) { @@ -171,7 +172,7 @@ }; x=st+(en-st)/2; - if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { + if(memcmp(old+I[x],new,DMIN(oldsize-I[x],newsize))<0) { return search(I,old,oldsize,new,newsize,x,en,pos); } else { return search(I,old,oldsize,new,newsize,st,x,pos); @@ -198,7 +199,7 @@ int main(int argc,char *argv[]) { - int fd; + int first, second; u_char *old,*new; off_t oldsize,newsize; off_t *I,*V; @@ -215,17 +216,39 @@ FILE * pf; BZFILE * pfbz2; int bz2err; + cap_rights_t filerights, pathrights; if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); + /* Capsicum */ + if ((first = open(argv[1],O_RDONLY|O_BINARY,0)) < 0) + err(1,"%s",argv[1]); + if ((second = open(argv[2], O_RDONLY|O_BINARY, 0)) < 0) + err(1,"%s",argv[1]); + /* Create the patch file */ + if ((pf = fopen(argv[3], "wb")) == NULL) + err(1, "%s", argv[3]); + + cap_rights_init(&filerights, CAP_READ, CAP_SEEK); + cap_rights_init(&pathrights, CAP_WRITE, CAP_SEEK); + + if (cap_rights_limit(first, &filerights) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(second, &filerights) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(fileno(pf), &pathrights) < 0) + err(1, "Couldn't limit fd"); + + if (cap_enter() < 0) + err(1, "Couldn't enter capability mode"); + /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ - if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) || - ((oldsize=lseek(fd,0,SEEK_END))==-1) || + if(((oldsize=lseek(first,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || - (close(fd)==-1)) err(1,"%s",argv[1]); + (lseek(first,0,SEEK_SET)!=0) || + (read(first,old,oldsize)!=oldsize) || + (close(first)==-1)) err(1,"%s",argv[1]); if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) || ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL); @@ -236,22 +259,17 @@ /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ - if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) || - ((newsize=lseek(fd,0,SEEK_END))==-1) || + if(((newsize=lseek(second,0,SEEK_END))==-1) || ((new=malloc(newsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,new,newsize)!=newsize) || - (close(fd)==-1)) err(1,"%s",argv[2]); + (lseek(second,0,SEEK_SET)!=0) || + (read(second,new,newsize)!=newsize) || + (close(second)==-1)) err(1,"%s",argv[2]); if(((db=malloc(newsize+1))==NULL) || ((eb=malloc(newsize+1))==NULL)) err(1,NULL); dblen=0; eblen=0; - /* Create the patch file */ - if ((pf = fopen(argv[3], "wb")) == NULL) - err(1, "%s", argv[3]); - /* Header is 0 8 "BSDIFF40" 8 8 length of bzip2ed ctrl block Modified: soc2013/dpl/head/usr.bin/bsdiff/bspatch/bspatch.c ============================================================================== --- soc2013/dpl/head/usr.bin/bsdiff/bspatch/bspatch.c Wed Sep 18 21:15:21 2013 (r257470) +++ soc2013/dpl/head/usr.bin/bsdiff/bspatch/bspatch.c Wed Sep 18 22:03:20 2013 (r257471) @@ -24,6 +24,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include <sys/capability.h> #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); @@ -62,7 +63,6 @@ FILE * f, * cpf, * dpf, * epf; BZFILE * cpfbz2, * dpfbz2, * epfbz2; int cbz2err, dbz2err, ebz2err; - int fd; ssize_t oldsize,newsize; ssize_t bzctrllen,bzdatalen; u_char header[32],buf[8]; @@ -71,12 +71,46 @@ off_t ctrl[3]; off_t lenread; off_t i; + int first, second; + cap_rights_t firstr, secondr, patchr, bzfiler; if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); + /* Capsicum */ + if ((first = open(argv[1],O_RDONLY|O_BINARY,0)) < 0) + err(1,"%s",argv[1]); + if ((second = open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666)) < 0) + err(1,"%s",argv[1]); /* Open patch file */ if ((f = fopen(argv[3], "rb")) == NULL) err(1, "fopen(%s)", argv[3]); + if ((cpf = fopen(argv[3], "rb")) == NULL) + err(1, "fopen(%s)", argv[3]); + if ((dpf = fopen(argv[3], "rb")) == NULL) + err(1, "fopen(%s)", argv[3]); + if ((epf = fopen(argv[3], "rb")) == NULL) + err(1, "fopen(%s)", argv[3]); + + cap_rights_init(&firstr, CAP_READ, CAP_SEEK); + cap_rights_init(&secondr, CAP_WRITE); + cap_rights_init(&patchr, CAP_READ, CAP_SEEK); + cap_rights_init(&bzfiler, CAP_READ, CAP_SEEK); + + if (cap_rights_limit(first, &firstr) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(second, &secondr) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(fileno(f), &patchr) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(fileno(cpf), &bzfiler) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(fileno(dpf), &bzfiler) < 0) + err(1, "Couldn't limit fd"); + if (cap_rights_limit(fileno(epf), &bzfiler) < 0) + err(1, "Couldn't limit fd"); + + if (cap_enter() < 0) + err(1, "Couldn't enter capability mode"); /* File format: @@ -113,34 +147,27 @@ /* Close patch file and re-open it via libbzip2 at the right places */ if (fclose(f)) err(1, "fclose(%s)", argv[3]); - if ((cpf = fopen(argv[3], "rb")) == NULL) - err(1, "fopen(%s)", argv[3]); if (fseeko(cpf, 32, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)32); if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); - if ((dpf = fopen(argv[3], "rb")) == NULL) - err(1, "fopen(%s)", argv[3]); if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen)); if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err); - if ((epf = fopen(argv[3], "rb")) == NULL) - err(1, "fopen(%s)", argv[3]); if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen + bzdatalen)); if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); - if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) || - ((oldsize=lseek(fd,0,SEEK_END))==-1) || + if(((oldsize=lseek(first,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || - (close(fd)==-1)) err(1,"%s",argv[1]); + (lseek(first,0,SEEK_SET)!=0) || + (read(first,old,oldsize)!=oldsize) || + (close(first)==-1)) err(1,"%s",argv[1]); if((new=malloc(newsize+1))==NULL) err(1,NULL); oldpos=0;newpos=0; @@ -196,8 +223,7 @@ err(1, "fclose(%s)", argv[3]); /* Write the new file */ - if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))<0) || - (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) + if((write(second,new,newsize)!=newsize) || (close(second)==-1)) err(1,"%s",argv[2]); free(new);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201309182203.r8IM3KQu090008>