From eb9de925c63990f6b19494698e4db1eb9682e46d Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Mon, 3 Dec 2012 05:35:33 +0100 Subject: ape: fix more bugs, use /env and /proc instead of #e and #p, cleanup remove envname length limitation in _envsetup() by using allocated buffer and use /env instead of #e use /proc and getpid() instead of #p and #c in readprocfdinit() fix buffer overflow in execlp(), check if name of failed exec starts with / . or is \0 make sure not to close our own filedescriptors for FD_CLOEXEC in execve(), fix wrong length check for flushing buffer to /env/_fdinfo. fix error handling cases. copy the enviroment before decoding \1 to \0 because the strings in environ[] array might not be writable. remove bogus close if we fail to open ppid file in getppid() and use /dev/ppid instead of #c/ppid --- sys/src/ape/lib/ap/plan9/_envsetup.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'sys/src/ape/lib/ap/plan9/_envsetup.c') diff --git a/sys/src/ape/lib/ap/plan9/_envsetup.c b/sys/src/ape/lib/ap/plan9/_envsetup.c index c83d45008..6a4f4204e 100644 --- a/sys/src/ape/lib/ap/plan9/_envsetup.c +++ b/sys/src/ape/lib/ap/plan9/_envsetup.c @@ -21,7 +21,6 @@ char **environ; int errno; unsigned long _clock; -static char name[NAME_MAX+5] = "#e"; static void fdsetup(char *, char *); static void sigsetup(char *, char *); @@ -45,33 +44,32 @@ _envsetup(void) nohandle = 0; fdinited = 0; cnt = 0; - dfd = _OPEN(name, 0); + dfd = _OPEN("/env", 0); if(dfd < 0) { static char **emptyenvp = 0; environ = emptyenvp; return; } - name[2] = '/'; - ps = p = malloc(Envhunk); psize = Envhunk; + ps = p = malloc(psize); nd = _dirreadall(dfd, &d9a); _CLOSE(dfd); for(j=0; jname); - if(n >= sizeof(name)-4) - continue; m = d9->length; i = p - ps; - if(i+n+1+m+1 > psize) { - psize += (n+m+2 < Envhunk)? Envhunk : n+m+2; + if(i+n+5+m+1 > psize) { + psize += (n+m+6 < Envhunk)? Envhunk : n+m+6; ps = realloc(ps, psize); p = ps + i; } + strcpy(p, "/env/"); + memcpy(p+5, d9->name, n+1); + f = _OPEN(p, 0); + memset(p, 0, n+6); memcpy(p, d9->name, n); p[n] = '='; - strcpy(name+3, d9->name); - f = _OPEN(name, O_RDONLY); if(f < 0 || _READ(f, p+n+1, m) != m) m = 0; _CLOSE(f); -- cgit v1.2.3