Re: [akaros] [PATCH 1/4] add the capability device from Harvey (from Plan 9)
thanks Merged to master at 0b99538dc853..2644e0eb10fd (from, to] You can see the entire diff with 'git diff' or at https://github.com/brho/akaros/compare/0b99538dc853...2644e0eb10fd On 2016-10-14 at 13:26 Ronald G. Minnich wrote: > Change-Id: If159e72517809eedd0d1e98271e3dde57e035090 > Signed-off-by: Ronald G. Minnich -- You received this message because you are subscribed to the Google Groups "Akaros" group. To unsubscribe from this group and stop receiving emails from it, send an email to akaros+unsubscr...@googlegroups.com. To post to this group, send email to akaros@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[akaros] [PATCH 1/4] add the capability device from Harvey (from Plan 9)
Change-Id: If159e72517809eedd0d1e98271e3dde57e035090 Signed-off-by: Ronald G. Minnich --- kern/drivers/dev/capability.c | 295 ++ 1 file changed, 295 insertions(+) create mode 100644 kern/drivers/dev/capability.c diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c new file mode 100644 index 000..53a59af --- /dev/null +++ b/kern/drivers/dev/capability.c @@ -0,0 +1,295 @@ +/* + * This file is part of the UCB release of Plan 9. It is subject to the license + * terms in the LICENSE file found in the top-level directory of this + * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No + * part of the UCB release of Plan 9, including this file, may be copied, + * modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ + +#include "u.h" +#include "../port/lib.h" +#include "mem.h" +#include "dat.h" +#include "fns.h" +#include "../port/error.h" + +#include + +enum +{ + Hashlen=SHA1dlen, + Maxhash=256, +}; + +/* + * if a process knows cap->cap, it can change user + * to capabilty->user. + */ +typedef struct Caphash Caphash; +struct Caphash +{ + Caphash *next; + charhash[Hashlen]; +}; + +struct +{ + QLock QLock; + Caphash *first; + int nhash; +} capalloc; + +enum +{ + Qdir, + Qhash, + Quse, +}; + +/* caphash must be last */ +Dirtab capdir[] = +{ + ".",{Qdir,0,QTDIR}, 0, DMDIR|0500, + "capuse", {Quse}, 0, 0222, + "caphash", {Qhash},0, 0200, +}; +int ncapdir = nelem(capdir); + +static Chan* +capattach(char *spec) +{ + return devattach(L'¤', spec); +} + +static Walkqid* +capwalk(Chan *c, Chan *nc, char **name, int nname) +{ + return devwalk(c, nc, name, nname, capdir, ncapdir, devgen); +} + +static void +capremove(Chan *c) +{ + if(iseve() && c->qid.path == Qhash) + ncapdir = nelem(capdir)-1; + else + error(Eperm); +} + + +static int32_t +capstat(Chan *c, uint8_t *db, int32_t n) +{ + return devstat(c, db, n, capdir, ncapdir, devgen); +} + +/* + * if the stream doesn't exist, create it + */ +static Chan* +capopen(Chan *c, int omode) +{ + if(c->qid.type & QTDIR){ + if(omode != OREAD) + error(Ebadarg); + c->mode = omode; + c->flag |= COPEN; + c->offset = 0; + return c; + } + + switch((uint32_t)c->qid.path){ + case Qhash: + if(!iseve()) + error(Eperm); + break; + } + + c->mode = openmode(omode); + c->flag |= COPEN; + c->offset = 0; + return c; +} + +/* +static char* +hashstr(uchar *hash) +{ + static char buf[2*Hashlen+1]; + int i; + + for(i = 0; i < Hashlen; i++) + sprint(buf+2*i, "%2.2x", hash[i]); + buf[2*Hashlen] = 0; + return buf; +} + */ + +static Caphash* +remcap(uint8_t *hash) +{ + Caphash *t, **l; + + qlock(&capalloc.QLock); + + /* find the matching capability */ + for(l = &capalloc.first; *l != nil;){ + t = *l; + if(memcmp(hash, t->hash, Hashlen) == 0) + break; + l = &t->next; + } + t = *l; + if(t != nil){ + capalloc.nhash--; + *l = t->next; + } + qunlock(&capalloc.QLock); + + return t; +} + +/* add a capability, throwing out any old ones */ +static void +addcap(uint8_t *hash) +{ + Caphash *p, *t, **l; + + p = smalloc(sizeof *p); + memmove(p->hash, hash, Hashlen); + p->next = nil; + + qlock(&capalloc.QLock); + + /* trim extras */ + while(capalloc.nhash >= Maxhash){ + t = capalloc.first; + if(t == nil) + panic("addcap"); + capalloc.first = t->next; + free(t); + capalloc.nhash--; + } + + /* add new one */ + for(l = &capalloc.first; *l != nil; l = &(*l)->next) + ; + *l = p; + capalloc.nhash++; + + qunlock(&capalloc.QLock); +} + +static void +capclose(Chan* c) +{ +} + +static int32_t +capread(Chan *c, void *va, int32_t n, int64_t m) +{ + switch((uint32_t)c->qid.path){ + case Qdir: + return devdirread(c, va, n, capdir, ncapdir, devgen); + + default: + error(Eperm); + break; + } + return n; +} + +static int32_t +capwrite(Chan *c, void *va, int32_t n, int64_t m) +{ + Caphash *p; + char *cp; + uint8_t hash[Hashlen]; + char *key, *from, *to; + char err[256]; + Proc *up = externup(); + + switch((uint32_t)c->qid.path){ +
[akaros] [PATCH 1/4] add the capability device from Harvey (from Plan 9)
Change-Id: If159e72517809eedd0d1e98271e3dde57e035090 Signed-off-by: Ronald G. Minnich --- kern/drivers/dev/capability.c | 295 ++ 1 file changed, 295 insertions(+) create mode 100644 kern/drivers/dev/capability.c diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c new file mode 100644 index 000..53a59af --- /dev/null +++ b/kern/drivers/dev/capability.c @@ -0,0 +1,295 @@ +/* + * This file is part of the UCB release of Plan 9. It is subject to the license + * terms in the LICENSE file found in the top-level directory of this + * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No + * part of the UCB release of Plan 9, including this file, may be copied, + * modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ + +#include "u.h" +#include "../port/lib.h" +#include "mem.h" +#include "dat.h" +#include "fns.h" +#include "../port/error.h" + +#include + +enum +{ + Hashlen=SHA1dlen, + Maxhash=256, +}; + +/* + * if a process knows cap->cap, it can change user + * to capabilty->user. + */ +typedef struct Caphash Caphash; +struct Caphash +{ + Caphash *next; + charhash[Hashlen]; +}; + +struct +{ + QLock QLock; + Caphash *first; + int nhash; +} capalloc; + +enum +{ + Qdir, + Qhash, + Quse, +}; + +/* caphash must be last */ +Dirtab capdir[] = +{ + ".",{Qdir,0,QTDIR}, 0, DMDIR|0500, + "capuse", {Quse}, 0, 0222, + "caphash", {Qhash},0, 0200, +}; +int ncapdir = nelem(capdir); + +static Chan* +capattach(char *spec) +{ + return devattach(L'¤', spec); +} + +static Walkqid* +capwalk(Chan *c, Chan *nc, char **name, int nname) +{ + return devwalk(c, nc, name, nname, capdir, ncapdir, devgen); +} + +static void +capremove(Chan *c) +{ + if(iseve() && c->qid.path == Qhash) + ncapdir = nelem(capdir)-1; + else + error(Eperm); +} + + +static int32_t +capstat(Chan *c, uint8_t *db, int32_t n) +{ + return devstat(c, db, n, capdir, ncapdir, devgen); +} + +/* + * if the stream doesn't exist, create it + */ +static Chan* +capopen(Chan *c, int omode) +{ + if(c->qid.type & QTDIR){ + if(omode != OREAD) + error(Ebadarg); + c->mode = omode; + c->flag |= COPEN; + c->offset = 0; + return c; + } + + switch((uint32_t)c->qid.path){ + case Qhash: + if(!iseve()) + error(Eperm); + break; + } + + c->mode = openmode(omode); + c->flag |= COPEN; + c->offset = 0; + return c; +} + +/* +static char* +hashstr(uchar *hash) +{ + static char buf[2*Hashlen+1]; + int i; + + for(i = 0; i < Hashlen; i++) + sprint(buf+2*i, "%2.2x", hash[i]); + buf[2*Hashlen] = 0; + return buf; +} + */ + +static Caphash* +remcap(uint8_t *hash) +{ + Caphash *t, **l; + + qlock(&capalloc.QLock); + + /* find the matching capability */ + for(l = &capalloc.first; *l != nil;){ + t = *l; + if(memcmp(hash, t->hash, Hashlen) == 0) + break; + l = &t->next; + } + t = *l; + if(t != nil){ + capalloc.nhash--; + *l = t->next; + } + qunlock(&capalloc.QLock); + + return t; +} + +/* add a capability, throwing out any old ones */ +static void +addcap(uint8_t *hash) +{ + Caphash *p, *t, **l; + + p = smalloc(sizeof *p); + memmove(p->hash, hash, Hashlen); + p->next = nil; + + qlock(&capalloc.QLock); + + /* trim extras */ + while(capalloc.nhash >= Maxhash){ + t = capalloc.first; + if(t == nil) + panic("addcap"); + capalloc.first = t->next; + free(t); + capalloc.nhash--; + } + + /* add new one */ + for(l = &capalloc.first; *l != nil; l = &(*l)->next) + ; + *l = p; + capalloc.nhash++; + + qunlock(&capalloc.QLock); +} + +static void +capclose(Chan* c) +{ +} + +static int32_t +capread(Chan *c, void *va, int32_t n, int64_t m) +{ + switch((uint32_t)c->qid.path){ + case Qdir: + return devdirread(c, va, n, capdir, ncapdir, devgen); + + default: + error(Eperm); + break; + } + return n; +} + +static int32_t +capwrite(Chan *c, void *va, int32_t n, int64_t m) +{ + Caphash *p; + char *cp; + uint8_t hash[Hashlen]; + char *key, *from, *to; + char err[256]; + Proc *up = externup(); + + switch((uint32_t)c->qid.path){ +