9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] Permissions for child boards in /srv
@ 2022-06-24 14:34 Jacob Moody
  2022-06-25  3:49 ` Jacob Moody
  0 siblings, 1 reply; 10+ messages in thread
From: Jacob Moody @ 2022-06-24 14:34 UTC (permalink / raw)
  To: 9front

This patch allows board stats to be mutated. It also changes
the default child board permissions to 0550.

This results in child service directories being
"private" to just the user who created them by
default.


Thanks,
moody

diff 5ee86cf824c5591aa92118c0cd9d71b005e789d0 uncommitted
--- a/sys/src/9/port/devsrv.c
+++ b/sys/src/9/port/devsrv.c
@@ -13,6 +13,8 @@
 	void 	*link;
 	char 	*name;
 	ulong 	path;
+	char	*owner;
+	ulong	perm;
 };

 typedef struct Srv Srv;
@@ -19,9 +21,6 @@
 struct Srv
 {
 	Link;
-
-	char	*owner;
-	ulong	perm;
 	Chan	*chan;
 };

@@ -140,6 +139,7 @@

 		b = ch->parent;
 		free(ch->name);
+		free(ch->owner);
 		wunlock(ch);
 		free(ch);
 	}
@@ -162,14 +162,17 @@
 	rlock(b);
 	if(waserror()){
 		runlock(b);
-		return -1;
+		nexterror();
 	}
-	if(s == DEVDOTDOT){
+	switch(s){
+	case -2: /* dot */
+		ch = b;
+		goto Child;
+	case DEVDOTDOT:
 		ch = b->parent;
 		if(ch == nil)
 			ch = &root;
 		goto Child;
-		
 	}
 	if(name != nil){
 		if(strcmp("clone", name) == 0)
@@ -194,21 +197,25 @@
 		devdir(c, q, up->genbuf, 0, sp->owner, sp->perm, dp);
 	} else if(ch != nil){
 Child:
+		if(name != nil || s == DEVDOTDOT){
+			devpermcheck(ch->owner, ch->perm, OEXEC);
+			c->aux = ch;
+		}
 		kstrcpy(up->genbuf, ch->name, sizeof up->genbuf);
 		q.vers = ch->id;
 		q.path = NETQID(q.vers, ch->path);
 		q.type = QTDIR;
-		devdir(c, q, up->genbuf, 0, eve, 0555|DMDIR, dp);
-		/* dirread's and stats shouldn't alter c->aux */
-		if(name != nil)
-			c->aux = ch;
+		devdir(c, q, up->genbuf, 0, ch->owner, ch->perm|DMDIR, dp);
 	} else if(0){
 Clone:
 		q.vers = NETID(c->qid.path);
 		q.path = NETQID(q.vers, Qclone);
 		devdir(c, q, "clone", 0, eve, 0444, dp);
-	} else
-		error(Enonexist);
+	} else {
+		runlock(b);
+		poperror();
+		return -1;
+	}

 	runlock(b);
 	poperror();
@@ -220,6 +227,8 @@
 {
 	root.qidpath = Qend;
 	root.name = "#s";
+	root.owner = eve;
+	root.perm = 0555;
 }

 static Chan*
@@ -253,6 +262,17 @@
 static int
 srvstat(Chan *c, uchar *db, int n)
 {
+	Dir d;
+
+	/* devstat cheats for dir stats, we care about our dir perms */
+	if(c->qid.type == QTDIR){
+		srvgen(c, nil, nil, 0, -2, &d);
+		n = convD2M(&d, db, n);
+		if(n == 0)
+			error(Ebadarg);
+		return n;
+	}
+
 	return devstat(c, db, n, 0, 0, srvgen);
 }

@@ -286,16 +306,6 @@
 	Chan *nc;
 	char buf[64];

-	if(c->qid.type == QTDIR){
-		if(omode & ORCLOSE)
-			error(Eperm);
-		if(omode != OREAD)
-			error(Eisdir);
-		c->mode = omode;
-		c->flag |= COPEN;
-		c->offset = 0;
-		return c;
-	}
 	if(omode&OTRUNC)
 		error(Eexist);
 	if(omode&ORCLOSE)
@@ -311,6 +321,8 @@
 		ch = smalloc(sizeof *ch);
 		ch->qidpath = Qend;
 		ch->ref = 1;
+		ch->perm = 0550;
+		kstrdup(&ch->owner, up->user);
 		do {
 			qlock(&boards);
 			ch->id = ++boards.path;
@@ -336,6 +348,19 @@
 		runlock(b);
 		nexterror();
 	}
+	if(c->qid.type == QTDIR){
+		if(omode & ORCLOSE)
+			error(Eperm);
+		if(omode != OREAD)
+			error(Eisdir);
+		devpermcheck(b->owner, b->perm, omode);
+		c->mode = omode;
+		c->flag |= COPEN;
+		c->offset = 0;
+		runlock(b);
+		poperror();
+		return c;
+	}
 	if(b->closed)
 		error(Eexpired);

@@ -459,18 +484,18 @@
 static int
 srvwstat(Chan *c, uchar *dp, int n)
 {
-	Board *b;
+	Board *b, *s;
 	char *strs;
-	Srv *sp;
 	Dir d;
+	Link *lp;

-	if(c->qid.type & QTDIR)
-		error(Eperm);
 	switch(NETTYPE(c->qid.path)){
 	case Qlease:
 	case Qclone:
 		error(Eperm);
 	}
+	if(c->qid.type == QTDIR && c->aux == &root)
+		error(Eperm);

 	strs = smalloc(n);
 	if(waserror()){
@@ -490,28 +515,49 @@
 	if(b->closed)
 		error(Eexpired);

-	sp = lookup(b->srv, nil, c->qid.path);
-	if(sp == nil)
+	if(c->qid.type == QTDIR){
+		lp = b;
+		/* we share ownership of our stats with our parent */
+		assert(b->parent != nil);
+		wlock(b->parent);
+		if(waserror()){
+			wunlock(b->parent);
+			nexterror();
+		}
+	} else
+		lp = lookup(b->srv, nil, c->qid.path);
+	if(lp == nil)
 		error(Enonexist);

-	if(strcmp(sp->owner, up->user) != 0 && !iseve())
+	if(strcmp(lp->owner, up->user) != 0 && !iseve())
 		error(Eperm);

-	if(d.name != nil && *d.name && strcmp(sp->name, d.name) != 0) {
+	if(d.name != nil && *d.name && strcmp(lp->name, d.name) != 0) {
 		if(strchr(d.name, '/') != nil)
 			error(Ebadchar);
 		if(strlen(d.name) >= sizeof(up->genbuf))
 			error(Etoolong);
-		if(lookup(b->srv, d.name, ~0UL) != nil)
+
+		//Ensure new name doesn't conflict with old names
+		if(c->qid.type == QTDIR)
+			s = b->parent;
+		else
+			s = b;
+		if(lookup(s->srv, d.name, ~0UL) != nil)
 			error(Eexist);
-		if(lookup(b->child, d.name, ~0UL) != nil)
+		if(lookup(s->child, d.name, ~0UL) != nil)
 			error(Eexist);
-		kstrdup(&sp->name, d.name);
+		kstrdup(&lp->name, d.name);
 	}
 	if(d.uid != nil && *d.uid)
-		kstrdup(&sp->owner, d.uid);
+		kstrdup(&lp->owner, d.uid);
 	if(d.mode != ~0UL)
-		sp->perm = d.mode & 0777;
+		lp->perm = d.mode & 0777;
+
+	if(c->qid.type == QTDIR){
+		wunlock(b->parent);
+		poperror();
+	}

 	wunlock(b);
 	poperror();


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

end of thread, other threads:[~2022-07-02 21:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-24 14:34 [9front] [PATCH] Permissions for child boards in /srv Jacob Moody
2022-06-25  3:49 ` Jacob Moody
2022-06-29 14:53   ` Jacob Moody
2022-06-29 16:35     ` cinap_lenrek
2022-06-29 16:42       ` Jacob Moody
2022-06-29 16:46         ` ori
2022-06-29 17:12           ` hiro
2022-06-29 17:20         ` cinap_lenrek
2022-06-29 17:50           ` Jacob Moody
2022-07-02 21:25             ` 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).