changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: snap: check for overflows, cleanup error handling

changeset 6910: 85ef38b2272f
parent 6909: d72ccdb6abdd
child 6911: 337e03870053
author: cinap_lenrek@felloff.net
date: Thu, 22 Nov 2018 12:44:15 +0100
files: sys/src/cmd/snap/read.c sys/src/cmd/snap/snap.c sys/src/cmd/snap/snapfs.c sys/src/cmd/snap/take.c sys/src/cmd/snap/util.c
description: snap: check for overflows, cleanup error handling
     1.1--- a/sys/src/cmd/snap/read.c
     1.2+++ b/sys/src/cmd/snap/read.c
     1.3@@ -3,14 +3,6 @@
     1.4 #include <bio.h>
     1.5 #include "snap.h"
     1.6 
     1.7-void
     1.8-panic(char *s)
     1.9-{
    1.10-	fprint(2, "%s\n", s);
    1.11-	abort();
    1.12-	exits(s);
    1.13-}
    1.14-
    1.15 static Proc*
    1.16 findpid(Proc *plist, long pid)
    1.17 {
    1.18@@ -30,11 +22,11 @@ findpage(Proc *plist, long pid, int type
    1.19 
    1.20 	plist = findpid(plist, pid);
    1.21 	if(plist == nil)
    1.22-		panic("can't find referenced pid");
    1.23+		sysfatal("can't find referenced pid");
    1.24 
    1.25 	if(type == 't') {
    1.26 		if(off%Pagesize)
    1.27-			panic("bad text offset alignment");
    1.28+			sysfatal("bad text offset alignment");
    1.29 		s = plist->text;
    1.30 		if(off >= s->len)
    1.31 			return nil;
    1.32@@ -53,7 +45,7 @@ findpage(Proc *plist, long pid, int type
    1.33 
    1.34 	off -= s->offset;
    1.35 	if(off%Pagesize)
    1.36-		panic("bad mem offset alignment");
    1.37+		sysfatal("bad mem offset alignment");
    1.38 
    1.39 	return s->pg[off/Pagesize];
    1.40 }
    1.41@@ -116,15 +108,17 @@ readdata(Biobuf *b)
    1.42 {
    1.43 	Data *d;
    1.44 	char str[32];
    1.45-	long len;
    1.46+	ulong len;
    1.47 
    1.48 	if(Bread(b, str, 12) != 12)
    1.49-		panic("can't read data hdr\n");
    1.50-
    1.51-	len = atoi(str);
    1.52+		sysfatal("can't read data hdr: %r");
    1.53+	str[12] = 0;
    1.54+	len = strtoul(str, 0, 0);
    1.55+	if(len + sizeof(*d) < sizeof(*d))
    1.56+		sysfatal("data len too large");
    1.57 	d = emalloc(sizeof(*d) + len);
    1.58-	if(Bread(b, d->data, len) != len)
    1.59-		panic("can't read data body\n");
    1.60+	if(len && Bread(b, d->data, len) != len)
    1.61+		sysfatal("can't read data body");
    1.62 	d->len = len;
    1.63 	return d;
    1.64 }
    1.65@@ -140,12 +134,12 @@ readseg(Seg **ps, Biobuf *b, Proc *plist
    1.66 	ulong pid;
    1.67 	uvlong off;
    1.68 	char buf[Pagesize];
    1.69-	static char zero[Pagesize];
    1.70+	extern char zeros[];
    1.71 
    1.72 	s = emalloc(sizeof *s);
    1.73 	if(Breaduvlong(b, &s->offset) < 0
    1.74 	|| Breaduvlong(b, &s->len) < 0)
    1.75-		panic("error reading segment");
    1.76+		sysfatal("error reading segment: %r");
    1.77 
    1.78 	npg = (s->len + Pagesize-1)/Pagesize;
    1.79 	s->npg = npg;
    1.80@@ -164,7 +158,7 @@ readseg(Seg **ps, Biobuf *b, Proc *plist
    1.81 
    1.82 		switch(t = Bgetc(b)) {
    1.83 		case 'z':
    1.84-			pp[i] = datapage(zero, len);
    1.85+			pp[i] = datapage(zeros, len);
    1.86 			if(debug)
    1.87 				fprint(2, "0x%.8llux all zeros\n", s->offset+(uvlong)i*Pagesize);
    1.88 			break;
    1.89@@ -172,10 +166,10 @@ readseg(Seg **ps, Biobuf *b, Proc *plist
    1.90 		case 't':
    1.91 			if(Breadulong(b, &pid) < 0 
    1.92 			|| Breaduvlong(b, &off) < 0)
    1.93-				panic("error reading segment x");
    1.94+				sysfatal("error reading segment x: %r");
    1.95 			pp[i] = findpage(plist, pid, t, off);
    1.96 			if(pp[i] == nil)
    1.97-				panic("bad page reference in snapshot");
    1.98+				sysfatal("bad page reference in snapshot");
    1.99 			if(debug)
   1.100 				fprint(2, "0x%.8llux same as %s pid %lud 0x%.8llux\n",
   1.101 					s->offset+(uvlong)i*Pagesize, t=='m'?"mem":"text", pid, off);
   1.102@@ -189,7 +183,7 @@ readseg(Seg **ps, Biobuf *b, Proc *plist
   1.103 			break;
   1.104 		default:
   1.105 			fprint(2, "bad type char %#.2ux\n", t);
   1.106-			panic("error reading segment");
   1.107+			sysfatal("error reading segment");
   1.108 		}
   1.109 	}
   1.110 	return s;
   1.111@@ -205,9 +199,9 @@ readsnap(Biobuf *b)
   1.112 	int i, n;
   1.113 
   1.114 	if((q = Brdline(b, '\n')) == nil)
   1.115-		panic("error reading snapshot file");
   1.116+		sysfatal("error reading snapshot file");
   1.117 	if(strncmp(q, "process snapshot", strlen("process snapshot")) != 0)
   1.118-		panic("bad snapshot file format");
   1.119+		sysfatal("bad snapshot file format");
   1.120 
   1.121 	plist = nil;
   1.122 	while(q = Brdline(b, '\n')) {
   1.123@@ -232,8 +226,11 @@ readsnap(Biobuf *b)
   1.124 			continue;
   1.125 		if(strcmp(q, "mem") == 0) {
   1.126 			if(Bread(b, buf, 12) != 12) 
   1.127-				panic("can't read memory section");
   1.128+				sysfatal("can't read memory section: %r");
   1.129+			buf[12] = 0;
   1.130 			n = atoi(buf);
   1.131+			if(n <= 0 || n > 16)
   1.132+				sysfatal("bad segment count: %d", n);
   1.133 			p->nseg = n;
   1.134 			p->seg = emalloc(n*sizeof(*p->seg));
   1.135 			for(i=0; i<n; i++)
   1.136@@ -241,7 +238,7 @@ readsnap(Biobuf *b)
   1.137 		} else if(strcmp(q, "text") == 0)
   1.138 			readseg(&p->text, b, plist);
   1.139 		else
   1.140-			panic("unknown section");
   1.141+			sysfatal("unknown section");
   1.142 	}
   1.143 	return plist;
   1.144 }
     2.1--- a/sys/src/cmd/snap/snap.c
     2.2+++ b/sys/src/cmd/snap/snap.c
     2.3@@ -33,15 +33,11 @@ main(int argc, char **argv)
     2.4 		usage();
     2.5 
     2.6 	/* get kernel compilation time */
     2.7-	if((d = dirstat("#/")) == nil) {
     2.8-		fprint(2, "cannot stat #/ ???\n");
     2.9-		exits("stat");
    2.10-	}
    2.11+	if((d = dirstat("#/")) == nil)
    2.12+		sysfatal("cannot stat #/: %r");
    2.13 
    2.14-	if((b = Bopen(ofile, OWRITE)) == nil) {
    2.15-		fprint(2, "cannot write to \"%s\"\n", ofile);
    2.16-		exits("Bopen");
    2.17-	}
    2.18+	if((b = Bopen(ofile, OWRITE)) == nil)
    2.19+		sysfatal("cannot write to \"%s\": %r", ofile);
    2.20 
    2.21 	if((user = getuser()) == nil)
    2.22 		user = "gre";
     3.1--- a/sys/src/cmd/snap/snapfs.c
     3.2+++ b/sys/src/cmd/snap/snapfs.c
     3.3@@ -154,16 +154,11 @@ main(int argc, char **argv)
     3.4 	if(argc != 1)
     3.5 		usage();
     3.6 
     3.7-	b = Bopen(argv[0], OREAD);
     3.8-	if(b == nil) {
     3.9-		fprint(2, "cannot open \"%s\": %r\n", argv[0]);
    3.10-		exits("Bopen");
    3.11-	}
    3.12+	if((b = Bopen(argv[0], OREAD)) == nil)
    3.13+		sysfatal("cannot open \"%s\": %r", argv[0]);
    3.14 
    3.15-	if((plist = readsnap(b)) == nil) {
    3.16-		fprint(2, "readsnap fails\n");
    3.17-		exits("readsnap");
    3.18-	}
    3.19+	if((plist = readsnap(b)) == nil)
    3.20+		sysfatal("readsnap fails");
    3.21 
    3.22 	tree = alloctree(nil, nil, DMDIR|0555, nil);
    3.23 	fs.tree = tree;
     4.1--- a/sys/src/cmd/snap/take.c
     4.2+++ b/sys/src/cmd/snap/take.c
     4.3@@ -20,6 +20,7 @@ sumr(ulong sum, void *buf, int n)
     4.4 	return sum;
     4.5 }
     4.6 
     4.7+char zeros[Pagesize];
     4.8 static ulong npage;
     4.9 static Page *pgtab[1<<10];
    4.10 
    4.11@@ -27,43 +28,27 @@ Page*
    4.12 datapage(char *p, long len)
    4.13 {
    4.14 	Page *pg;
    4.15-	char *q, *ep;
    4.16-	long	sum;
    4.17-	int iszero;
    4.18+	ulong sum;
    4.19 
    4.20-	if(len > Pagesize) {
    4.21-		fprint(2, "datapage cannot handle pages > %d\n", Pagesize);
    4.22-		exits("datapage");
    4.23-	}
    4.24+	if(len > Pagesize)
    4.25+		sysfatal("datapage cannot handle pages > %d", Pagesize);
    4.26 
    4.27 	sum = sumr(0, p, len) & (nelem(pgtab)-1);
    4.28-	if(sum == 0) {
    4.29-		iszero = 1;
    4.30-		for(q=p, ep=p+len; q<ep; q++)
    4.31-			if(*q != 0) {
    4.32-				iszero = 0;
    4.33-				break;
    4.34-			}
    4.35-	} else
    4.36-		iszero = 0;
    4.37-
    4.38-	for(pg = pgtab[sum]; pg; pg=pg->link)
    4.39+	for(pg = pgtab[sum]; pg != nil; pg=pg->link)
    4.40 		if(pg->len == len && memcmp(pg->data, p, len) == 0)
    4.41-			break;
    4.42-	if(pg)
    4.43-		return pg;
    4.44+			return pg;
    4.45 
    4.46 	pg = emalloc(sizeof(*pg)+len);
    4.47 	pg->data = (char*)&pg[1];
    4.48 	pg->type = 0;
    4.49 	pg->len = len;
    4.50 	memmove(pg->data, p, len);
    4.51-	pg->link = pgtab[sum];
    4.52-	pgtab[sum] = pg;
    4.53-	if(iszero) {
    4.54+	if(sum == 0 && memcmp(zeros, p, len) == 0) {
    4.55 		pg->type = 'z';
    4.56 		pg->written = 1;
    4.57 	}
    4.58+	pg->link = pgtab[sum];
    4.59+	pgtab[sum] = pg;
    4.60 
    4.61 	++npage;
    4.62 	return pg;
    4.63@@ -148,7 +133,7 @@ stackptr(Proc *proc, int fd)
    4.64 	char *q;
    4.65 	Fhdr f;
    4.66 	Reglist *r;
    4.67-	long textoff;
    4.68+	vlong textoff;
    4.69 	int i;
    4.70 	Data *dreg;
    4.71 
    4.72@@ -160,7 +145,9 @@ stackptr(Proc *proc, int fd)
    4.73 	if(textoff == -1)
    4.74 		return 0;
    4.75 
    4.76-	seek(fd, textoff, 0);
    4.77+	if(seek(fd, textoff, 0) < 0)
    4.78+		return 0;
    4.79+
    4.80 	if(crackhdr(fd, &f) == 0)
    4.81 		return 0;
    4.82 
     5.1--- a/sys/src/cmd/snap/util.c
     5.2+++ b/sys/src/cmd/snap/util.c
     5.3@@ -8,10 +8,8 @@ emalloc(ulong n)
     5.4 {
     5.5 	void *v;
     5.6 	v = malloc(n);
     5.7-	if(v == nil){
     5.8-		fprint(2, "out of memory\n");
     5.9-		exits("memory");
    5.10-	}
    5.11+	if(v == nil)
    5.12+		sysfatal("out of memory");
    5.13 	memset(v, 0, n);
    5.14 	return v;
    5.15 }
    5.16@@ -20,10 +18,8 @@ void*
    5.17 erealloc(void *v, ulong n)
    5.18 {
    5.19 	v = realloc(v, n);
    5.20-	if(v == nil) {
    5.21-		fprint(2, "out of memory\n");
    5.22-		exits("memory");
    5.23-	}
    5.24+	if(v == nil && n != 0)
    5.25+		sysfatal("out of memory");
    5.26 	return v;
    5.27 }
    5.28 
    5.29@@ -31,9 +27,7 @@ char*
    5.30 estrdup(char *s)
    5.31 {
    5.32 	s = strdup(s);
    5.33-	if(s == nil) {
    5.34-		fprint(2, "out of memory\n");
    5.35-		exits("memory");
    5.36-	}
    5.37+	if(s == nil)
    5.38+		sysfatal("out of memory");
    5.39 	return s;
    5.40 }