shithub: riscv

Download patch

ref: bedffdd8c3946a5a13a2ef7015b214ed8f8f5f66
parent: 55bf3d639938722d3da2464110cb188e87591d52
author: cinap_lenrek <[email protected]>
date: Wed Aug 13 19:09:47 EDT 2014

devenv: prevent non-hostowner from creating or removing variables in '#ec', cleanup

--- a/sys/src/9/port/devenv.c
+++ b/sys/src/9/port/devenv.c
@@ -23,7 +23,7 @@
 
 	for(i=0; i<eg->nent; i++){
 		e = eg->ent[i];
-		if(e->qid.path == qidpath || (name && e->name[0]==name[0] && strcmp(e->name, name) == 0))
+		if(e->qid.path == qidpath || (name != nil && name[0] == e->name[0] && strcmp(e->name, name) == 0))
 			return e;
 	}
 	return nil;
@@ -42,13 +42,13 @@
 
 	eg = envgrp(c);
 	rlock(eg);
-	e = 0;
-	if(name)
+	e = nil;
+	if(name != nil)
 		e = envlookup(eg, name, -1);
 	else if(s < eg->nent)
 		e = eg->ent[s];
 
-	if(e == 0 || name && (strlen(e->name) >= sizeof(up->genbuf))) {
+	if(e == nil || name != nil && (strlen(e->name) >= sizeof(up->genbuf))) {
 		runlock(eg);
 		return -1;
 	}
@@ -66,11 +66,10 @@
 	Chan *c;
 	Egrp *egrp = nil;
 
-	if(spec && *spec) {
-		if(strcmp(spec, "c") == 0)
-			egrp = &confegrp;
-		if(egrp == nil)
+	if(spec != nil && *spec != '\0') {
+		if(strcmp(spec, "c") != 0)
 			error(Ebadarg);
+		egrp = &confegrp;
 	}
 
 	c = devattach('e', spec);
@@ -113,7 +112,7 @@
 		else
 			rlock(eg);
 		e = envlookup(eg, nil, c->qid.path);
-		if(e == 0) {
+		if(e == nil) {
 			if(trunc)
 				wunlock(eg);
 			else
@@ -120,10 +119,10 @@
 				runlock(eg);
 			error(Enonexist);
 		}
-		if(trunc && e->value) {
+		if(trunc && e->value != nil) {
 			e->qid.vers++;
 			free(e->value);
-			e->value = 0;
+			e->value = nil;
 			e->len = 0;
 		}
 		if(trunc)
@@ -144,7 +143,7 @@
 	Evalue *e;
 	Evalue **ent;
 
-	if(c->qid.type != QTDIR)
+	if(c->qid.type != QTDIR || !envwriteable(c))
 		error(Eperm);
 
 	if(strlen(name) >= sizeof(up->genbuf))
@@ -159,7 +158,7 @@
 		nexterror();
 	}
 
-	if(envlookup(eg, name, -1))
+	if(envlookup(eg, name, -1) != nil)
 		error(Eexist);
 
 	e = smalloc(sizeof(Evalue));
@@ -196,12 +195,12 @@
 	Egrp *eg;
 	Evalue *e;
 
-	if(c->qid.type & QTDIR)
+	if(c->qid.type & QTDIR || !envwriteable(c))
 		error(Eperm);
 
 	eg = envgrp(c);
 	wlock(eg);
-	e = 0;
+	e = nil;
 	for(i=0; i<eg->nent; i++){
 		if(eg->ent[i]->qid.path == c->qid.path){
 			e = eg->ent[i];
@@ -212,11 +211,10 @@
 		}
 	}
 	wunlock(eg);
-	if(e == 0)
+	if(e == nil)
 		error(Enonexist);
 	free(e->name);
-	if(e->value)
-		free(e->value);
+	free(e->value);
 	free(e);
 }
 
@@ -245,12 +243,12 @@
 	eg = envgrp(c);
 	rlock(eg);
 	e = envlookup(eg, nil, c->qid.path);
-	if(e == 0) {
+	if(e == nil) {
 		runlock(eg);
 		error(Enonexist);
 	}
 
-	if(offset > e->len)	/* protects against overflow converting vlong to ulong */
+	if(offset >= e->len || e->value == nil)
 		n = 0;
 	else if(offset + n > e->len)
 		n = e->len - offset;
@@ -279,7 +277,7 @@
 	eg = envgrp(c);
 	wlock(eg);
 	e = envlookup(eg, nil, c->qid.path);
-	if(e == 0) {
+	if(e == nil) {
 		wunlock(eg);
 		error(Enonexist);
 	}
@@ -291,7 +289,7 @@
 			wunlock(eg);
 			error(Enomem);
 		}
-		if(e->value){
+		if(e->value != nil){
 			memmove(s, e->value, e->len);
 			free(e->value);
 		}
@@ -340,7 +338,7 @@
 		ne = smalloc(sizeof(Evalue));
 		ne->name = smalloc(strlen(e->name)+1);
 		strcpy(ne->name, e->name);
-		if(e->value){
+		if(e->value != nil){
 			ne->value = smalloc(e->len);
 			memmove(ne->value, e->value, e->len);
 			ne->len = e->len;
@@ -362,8 +360,7 @@
 		for(i=0; i<eg->nent; i++){
 			e = eg->ent[i];
 			free(e->name);
-			if(e->value)
-				free(e->value);
+			free(e->value);
 			free(e);
 		}
 		free(eg->ent);
@@ -382,7 +379,7 @@
 static int
 envwriteable(Chan *c)
 {
-	return iseve() || c->aux == nil;
+	return c->aux == nil || c->aux == up->egrp || iseve();
 }
 
 /*
@@ -438,7 +435,7 @@
 		/* move up to the first null */
 		q += strlen(q) + 1;
 	}
-	*q = 0;
+	*q = '\0';
 	
 	poperror();
 	runlock(eg);