9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] sysproc: raise limit on shebang lines, handle quoting.
@ 2022-07-10 15:38 ori
  2022-07-10 18:29 ` cinap_lenrek
  0 siblings, 1 reply; 5+ messages in thread
From: ori @ 2022-07-10 15:38 UTC (permalink / raw)
  To: 9front

when experimenting with auth/box, I realized that the size of
the shebang lines we were using was rather limited.

this raises the limit, but also switches to tokenizing the
command line arguments via tokenize(2), rather than hand
rolled whitespace splitting -- which gives us proper handling
of quotes; now I can box a shell script with:

	#!/bin/auth/box -r/bin -r/usr/git -r/lib -r/mnt rc

diff 90f47fadf8808d84d9bde8316f3945e60650093d uncommitted
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -243,35 +243,17 @@
 }
 
 static int
-shargs(char *s, int n, char **ap)
+shargs(char *s, int n, char **ap, int nap)
 {
+	char *p;
 	int i;
 
 	if(n <= 2 || s[0] != '#' || s[1] != '!')
 		return -1;
-	s += 2;
-	n -= 2;		/* skip #! */
-	for(i=0;; i++){
-		if(i >= n)
-			return 0;
-		if(s[i]=='\n')
-			break;
-	}
-	s[i] = 0;
-
-	i = 0;
-	for(;;) {
-		while(*s==' ' || *s=='\t')
-			s++;
-		if(*s == 0)
-			break;
-		ap[i++] = s++;
-		while(*s && *s!=' ' && *s!='\t')
-			s++;
-		if(*s == 0)
-			break;
-		*s++ = 0;
-	}
+	if((p = memchr(s+2, '\n', n-2)) == nil)
+		return 0;
+	*p = 0;
+	i = tokenize(s+2, ap, nap-1);
 	ap[i] = nil;
 	return i;
 }
@@ -303,8 +285,8 @@
 	struct {
 		Exec;
 		uvlong	hdr[1];
-	} ehdr;
-	char line[sizeof(ehdr)];
+	} *ehdr;
+	char line[128], buf[128];
 	char *progarg[sizeof(line)/2+1];
 	volatile char *args, *elem, *file0;
 	char **argv, **argp, **argp0;
@@ -353,22 +335,23 @@
 		if(!indir)
 			kstrdup(&elem, up->genbuf);
 
-		n = devtab[tc->type]->read(tc, &ehdr, sizeof(ehdr), 0);
+		n = devtab[tc->type]->read(tc, buf, sizeof(buf), 0);
 		if(n >= sizeof(Exec)) {
-			magic = beswal(ehdr.magic);
+			ehdr = (void*)buf;
+			magic = beswal(ehdr->magic);
 			if(magic == AOUT_MAGIC) {
 				if(magic & HDR_MAGIC) {
-					if(n < sizeof(ehdr))
+					if(n < sizeof(*ehdr))
 						error(Ebadexec);
-					entry = beswav(ehdr.hdr[0]);
-					text = UTZERO+sizeof(ehdr);
+					entry = beswav(ehdr->hdr[0]);
+					text = UTZERO+sizeof(*ehdr);
 				} else {
-					entry = beswal(ehdr.entry);
+					entry = beswal(ehdr->entry);
 					text = UTZERO+sizeof(Exec);
 				}
 				if(entry < text)
 					error(Ebadexec);
-				text += beswal(ehdr.text);
+				text += beswal(ehdr->text);
 				if(text <= entry || text >= (USTKTOP-USTKSIZE))
 					error(Ebadexec);
 
@@ -393,8 +376,8 @@
 		/*
 		 * Process #! /bin/sh args ...
 		 */
-		memmove(line, &ehdr, n);
-		n = shargs(line, n, progarg);
+		memmove(line, buf, n);
+		n = shargs(line, n, progarg, nelem(progarg));
 		if(n < 1)
 			error(Ebadexec);
 		/*
@@ -411,8 +394,8 @@
 
 	t = (text+align) & ~align;
 	text -= UTZERO;
-	data = beswal(ehdr.data);
-	bss = beswal(ehdr.bss);
+	data = beswal(ehdr->data);
+	bss = beswal(ehdr->bss);
 	align = BY2PG-1;
 	d = (t + data + align) & ~align;
 	bssend = t + data + bss;


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-16 17:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-10 15:38 [9front] sysproc: raise limit on shebang lines, handle quoting ori
2022-07-10 18:29 ` cinap_lenrek
2022-07-16 17:20   ` ori
2022-07-16 17:29     ` ori
2022-07-16 17:45       ` ori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).