9front - general discussion about 9front
 help / color / mirror / Atom feed
From: ori@eigenstate.org
To: 9front@9front.org
Subject: [9front] sysproc: raise limit on shebang lines, handle quoting.
Date: Sun, 10 Jul 2022 11:38:26 -0400	[thread overview]
Message-ID: <80149CA0DE680A311CCA7F4AE4F4167B@eigenstate.org> (raw)

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;


             reply	other threads:[~2022-07-10 15:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-10 15:38 ori [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=80149CA0DE680A311CCA7F4AE4F4167B@eigenstate.org \
    --to=ori@eigenstate.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).