Path: blob/master/net/sunrpc/auth_gss/svcauth_gss.c
15112 views
/*1* Neil Brown <[email protected]>2* J. Bruce Fields <[email protected]>3* Andy Adamson <[email protected]>4* Dug Song <[email protected]>5*6* RPCSEC_GSS server authentication.7* This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc20788* (gssapi)9*10* The RPCSEC_GSS involves three stages:11* 1/ context creation12* 2/ data exchange13* 3/ context destruction14*15* Context creation is handled largely by upcalls to user-space.16* In particular, GSS_Accept_sec_context is handled by an upcall17* Data exchange is handled entirely within the kernel18* In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.19* Context destruction is handled in-kernel20* GSS_Delete_sec_context is in-kernel21*22* Context creation is initiated by a RPCSEC_GSS_INIT request arriving.23* The context handle and gss_token are used as a key into the rpcsec_init cache.24* The content of this cache includes some of the outputs of GSS_Accept_sec_context,25* being major_status, minor_status, context_handle, reply_token.26* These are sent back to the client.27* Sequence window management is handled by the kernel. The window size if currently28* a compile time constant.29*30* When user-space is happy that a context is established, it places an entry31* in the rpcsec_context cache. The key for this cache is the context_handle.32* The content includes:33* uid/gidlist - for determining access rights34* mechanism type35* mechanism specific information, such as a key36*37*/3839#include <linux/slab.h>40#include <linux/types.h>41#include <linux/module.h>42#include <linux/pagemap.h>4344#include <linux/sunrpc/auth_gss.h>45#include <linux/sunrpc/gss_err.h>46#include <linux/sunrpc/svcauth.h>47#include <linux/sunrpc/svcauth_gss.h>48#include <linux/sunrpc/cache.h>4950#ifdef RPC_DEBUG51# define RPCDBG_FACILITY RPCDBG_AUTH52#endif5354/* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests55* into replies.56*57* Key is context handle (\x if empty) and gss_token.58* Content is major_status minor_status (integers) context_handle, reply_token.59*60*/6162static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)63{64return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);65}6667#define RSI_HASHBITS 668#define RSI_HASHMAX (1<<RSI_HASHBITS)6970struct rsi {71struct cache_head h;72struct xdr_netobj in_handle, in_token;73struct xdr_netobj out_handle, out_token;74int major_status, minor_status;75};7677static struct cache_head *rsi_table[RSI_HASHMAX];78static struct cache_detail rsi_cache;79static struct rsi *rsi_update(struct rsi *new, struct rsi *old);80static struct rsi *rsi_lookup(struct rsi *item);8182static void rsi_free(struct rsi *rsii)83{84kfree(rsii->in_handle.data);85kfree(rsii->in_token.data);86kfree(rsii->out_handle.data);87kfree(rsii->out_token.data);88}8990static void rsi_put(struct kref *ref)91{92struct rsi *rsii = container_of(ref, struct rsi, h.ref);93rsi_free(rsii);94kfree(rsii);95}9697static inline int rsi_hash(struct rsi *item)98{99return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)100^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);101}102103static int rsi_match(struct cache_head *a, struct cache_head *b)104{105struct rsi *item = container_of(a, struct rsi, h);106struct rsi *tmp = container_of(b, struct rsi, h);107return netobj_equal(&item->in_handle, &tmp->in_handle) &&108netobj_equal(&item->in_token, &tmp->in_token);109}110111static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)112{113dst->len = len;114dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);115if (len && !dst->data)116return -ENOMEM;117return 0;118}119120static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)121{122return dup_to_netobj(dst, src->data, src->len);123}124125static void rsi_init(struct cache_head *cnew, struct cache_head *citem)126{127struct rsi *new = container_of(cnew, struct rsi, h);128struct rsi *item = container_of(citem, struct rsi, h);129130new->out_handle.data = NULL;131new->out_handle.len = 0;132new->out_token.data = NULL;133new->out_token.len = 0;134new->in_handle.len = item->in_handle.len;135item->in_handle.len = 0;136new->in_token.len = item->in_token.len;137item->in_token.len = 0;138new->in_handle.data = item->in_handle.data;139item->in_handle.data = NULL;140new->in_token.data = item->in_token.data;141item->in_token.data = NULL;142}143144static void update_rsi(struct cache_head *cnew, struct cache_head *citem)145{146struct rsi *new = container_of(cnew, struct rsi, h);147struct rsi *item = container_of(citem, struct rsi, h);148149BUG_ON(new->out_handle.data || new->out_token.data);150new->out_handle.len = item->out_handle.len;151item->out_handle.len = 0;152new->out_token.len = item->out_token.len;153item->out_token.len = 0;154new->out_handle.data = item->out_handle.data;155item->out_handle.data = NULL;156new->out_token.data = item->out_token.data;157item->out_token.data = NULL;158159new->major_status = item->major_status;160new->minor_status = item->minor_status;161}162163static struct cache_head *rsi_alloc(void)164{165struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);166if (rsii)167return &rsii->h;168else169return NULL;170}171172static void rsi_request(struct cache_detail *cd,173struct cache_head *h,174char **bpp, int *blen)175{176struct rsi *rsii = container_of(h, struct rsi, h);177178qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);179qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);180(*bpp)[-1] = '\n';181}182183static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)184{185return sunrpc_cache_pipe_upcall(cd, h, rsi_request);186}187188189static int rsi_parse(struct cache_detail *cd,190char *mesg, int mlen)191{192/* context token expiry major minor context token */193char *buf = mesg;194char *ep;195int len;196struct rsi rsii, *rsip = NULL;197time_t expiry;198int status = -EINVAL;199200memset(&rsii, 0, sizeof(rsii));201/* handle */202len = qword_get(&mesg, buf, mlen);203if (len < 0)204goto out;205status = -ENOMEM;206if (dup_to_netobj(&rsii.in_handle, buf, len))207goto out;208209/* token */210len = qword_get(&mesg, buf, mlen);211status = -EINVAL;212if (len < 0)213goto out;214status = -ENOMEM;215if (dup_to_netobj(&rsii.in_token, buf, len))216goto out;217218rsip = rsi_lookup(&rsii);219if (!rsip)220goto out;221222rsii.h.flags = 0;223/* expiry */224expiry = get_expiry(&mesg);225status = -EINVAL;226if (expiry == 0)227goto out;228229/* major/minor */230len = qword_get(&mesg, buf, mlen);231if (len <= 0)232goto out;233rsii.major_status = simple_strtoul(buf, &ep, 10);234if (*ep)235goto out;236len = qword_get(&mesg, buf, mlen);237if (len <= 0)238goto out;239rsii.minor_status = simple_strtoul(buf, &ep, 10);240if (*ep)241goto out;242243/* out_handle */244len = qword_get(&mesg, buf, mlen);245if (len < 0)246goto out;247status = -ENOMEM;248if (dup_to_netobj(&rsii.out_handle, buf, len))249goto out;250251/* out_token */252len = qword_get(&mesg, buf, mlen);253status = -EINVAL;254if (len < 0)255goto out;256status = -ENOMEM;257if (dup_to_netobj(&rsii.out_token, buf, len))258goto out;259rsii.h.expiry_time = expiry;260rsip = rsi_update(&rsii, rsip);261status = 0;262out:263rsi_free(&rsii);264if (rsip)265cache_put(&rsip->h, &rsi_cache);266else267status = -ENOMEM;268return status;269}270271static struct cache_detail rsi_cache = {272.owner = THIS_MODULE,273.hash_size = RSI_HASHMAX,274.hash_table = rsi_table,275.name = "auth.rpcsec.init",276.cache_put = rsi_put,277.cache_upcall = rsi_upcall,278.cache_parse = rsi_parse,279.match = rsi_match,280.init = rsi_init,281.update = update_rsi,282.alloc = rsi_alloc,283};284285static struct rsi *rsi_lookup(struct rsi *item)286{287struct cache_head *ch;288int hash = rsi_hash(item);289290ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);291if (ch)292return container_of(ch, struct rsi, h);293else294return NULL;295}296297static struct rsi *rsi_update(struct rsi *new, struct rsi *old)298{299struct cache_head *ch;300int hash = rsi_hash(new);301302ch = sunrpc_cache_update(&rsi_cache, &new->h,303&old->h, hash);304if (ch)305return container_of(ch, struct rsi, h);306else307return NULL;308}309310311/*312* The rpcsec_context cache is used to store a context that is313* used in data exchange.314* The key is a context handle. The content is:315* uid, gidlist, mechanism, service-set, mech-specific-data316*/317318#define RSC_HASHBITS 10319#define RSC_HASHMAX (1<<RSC_HASHBITS)320321#define GSS_SEQ_WIN 128322323struct gss_svc_seq_data {324/* highest seq number seen so far: */325int sd_max;326/* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of327* sd_win is nonzero iff sequence number i has been seen already: */328unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];329spinlock_t sd_lock;330};331332struct rsc {333struct cache_head h;334struct xdr_netobj handle;335struct svc_cred cred;336struct gss_svc_seq_data seqdata;337struct gss_ctx *mechctx;338char *client_name;339};340341static struct cache_head *rsc_table[RSC_HASHMAX];342static struct cache_detail rsc_cache;343static struct rsc *rsc_update(struct rsc *new, struct rsc *old);344static struct rsc *rsc_lookup(struct rsc *item);345346static void rsc_free(struct rsc *rsci)347{348kfree(rsci->handle.data);349if (rsci->mechctx)350gss_delete_sec_context(&rsci->mechctx);351if (rsci->cred.cr_group_info)352put_group_info(rsci->cred.cr_group_info);353kfree(rsci->client_name);354}355356static void rsc_put(struct kref *ref)357{358struct rsc *rsci = container_of(ref, struct rsc, h.ref);359360rsc_free(rsci);361kfree(rsci);362}363364static inline int365rsc_hash(struct rsc *rsci)366{367return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);368}369370static int371rsc_match(struct cache_head *a, struct cache_head *b)372{373struct rsc *new = container_of(a, struct rsc, h);374struct rsc *tmp = container_of(b, struct rsc, h);375376return netobj_equal(&new->handle, &tmp->handle);377}378379static void380rsc_init(struct cache_head *cnew, struct cache_head *ctmp)381{382struct rsc *new = container_of(cnew, struct rsc, h);383struct rsc *tmp = container_of(ctmp, struct rsc, h);384385new->handle.len = tmp->handle.len;386tmp->handle.len = 0;387new->handle.data = tmp->handle.data;388tmp->handle.data = NULL;389new->mechctx = NULL;390new->cred.cr_group_info = NULL;391new->client_name = NULL;392}393394static void395update_rsc(struct cache_head *cnew, struct cache_head *ctmp)396{397struct rsc *new = container_of(cnew, struct rsc, h);398struct rsc *tmp = container_of(ctmp, struct rsc, h);399400new->mechctx = tmp->mechctx;401tmp->mechctx = NULL;402memset(&new->seqdata, 0, sizeof(new->seqdata));403spin_lock_init(&new->seqdata.sd_lock);404new->cred = tmp->cred;405tmp->cred.cr_group_info = NULL;406new->client_name = tmp->client_name;407tmp->client_name = NULL;408}409410static struct cache_head *411rsc_alloc(void)412{413struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);414if (rsci)415return &rsci->h;416else417return NULL;418}419420static int rsc_parse(struct cache_detail *cd,421char *mesg, int mlen)422{423/* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */424char *buf = mesg;425int len, rv;426struct rsc rsci, *rscp = NULL;427time_t expiry;428int status = -EINVAL;429struct gss_api_mech *gm = NULL;430431memset(&rsci, 0, sizeof(rsci));432/* context handle */433len = qword_get(&mesg, buf, mlen);434if (len < 0) goto out;435status = -ENOMEM;436if (dup_to_netobj(&rsci.handle, buf, len))437goto out;438439rsci.h.flags = 0;440/* expiry */441expiry = get_expiry(&mesg);442status = -EINVAL;443if (expiry == 0)444goto out;445446rscp = rsc_lookup(&rsci);447if (!rscp)448goto out;449450/* uid, or NEGATIVE */451rv = get_int(&mesg, &rsci.cred.cr_uid);452if (rv == -EINVAL)453goto out;454if (rv == -ENOENT)455set_bit(CACHE_NEGATIVE, &rsci.h.flags);456else {457int N, i;458459/* gid */460if (get_int(&mesg, &rsci.cred.cr_gid))461goto out;462463/* number of additional gid's */464if (get_int(&mesg, &N))465goto out;466status = -ENOMEM;467rsci.cred.cr_group_info = groups_alloc(N);468if (rsci.cred.cr_group_info == NULL)469goto out;470471/* gid's */472status = -EINVAL;473for (i=0; i<N; i++) {474gid_t gid;475if (get_int(&mesg, &gid))476goto out;477GROUP_AT(rsci.cred.cr_group_info, i) = gid;478}479480/* mech name */481len = qword_get(&mesg, buf, mlen);482if (len < 0)483goto out;484gm = gss_mech_get_by_name(buf);485status = -EOPNOTSUPP;486if (!gm)487goto out;488489status = -EINVAL;490/* mech-specific data: */491len = qword_get(&mesg, buf, mlen);492if (len < 0)493goto out;494status = gss_import_sec_context(buf, len, gm, &rsci.mechctx, GFP_KERNEL);495if (status)496goto out;497498/* get client name */499len = qword_get(&mesg, buf, mlen);500if (len > 0) {501rsci.client_name = kstrdup(buf, GFP_KERNEL);502if (!rsci.client_name)503goto out;504}505506}507rsci.h.expiry_time = expiry;508rscp = rsc_update(&rsci, rscp);509status = 0;510out:511gss_mech_put(gm);512rsc_free(&rsci);513if (rscp)514cache_put(&rscp->h, &rsc_cache);515else516status = -ENOMEM;517return status;518}519520static struct cache_detail rsc_cache = {521.owner = THIS_MODULE,522.hash_size = RSC_HASHMAX,523.hash_table = rsc_table,524.name = "auth.rpcsec.context",525.cache_put = rsc_put,526.cache_parse = rsc_parse,527.match = rsc_match,528.init = rsc_init,529.update = update_rsc,530.alloc = rsc_alloc,531};532533static struct rsc *rsc_lookup(struct rsc *item)534{535struct cache_head *ch;536int hash = rsc_hash(item);537538ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);539if (ch)540return container_of(ch, struct rsc, h);541else542return NULL;543}544545static struct rsc *rsc_update(struct rsc *new, struct rsc *old)546{547struct cache_head *ch;548int hash = rsc_hash(new);549550ch = sunrpc_cache_update(&rsc_cache, &new->h,551&old->h, hash);552if (ch)553return container_of(ch, struct rsc, h);554else555return NULL;556}557558559static struct rsc *560gss_svc_searchbyctx(struct xdr_netobj *handle)561{562struct rsc rsci;563struct rsc *found;564565memset(&rsci, 0, sizeof(rsci));566if (dup_to_netobj(&rsci.handle, handle->data, handle->len))567return NULL;568found = rsc_lookup(&rsci);569rsc_free(&rsci);570if (!found)571return NULL;572if (cache_check(&rsc_cache, &found->h, NULL))573return NULL;574return found;575}576577/* Implements sequence number algorithm as specified in RFC 2203. */578static int579gss_check_seq_num(struct rsc *rsci, int seq_num)580{581struct gss_svc_seq_data *sd = &rsci->seqdata;582583spin_lock(&sd->sd_lock);584if (seq_num > sd->sd_max) {585if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {586memset(sd->sd_win,0,sizeof(sd->sd_win));587sd->sd_max = seq_num;588} else while (sd->sd_max < seq_num) {589sd->sd_max++;590__clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);591}592__set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);593goto ok;594} else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {595goto drop;596}597/* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */598if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))599goto drop;600ok:601spin_unlock(&sd->sd_lock);602return 1;603drop:604spin_unlock(&sd->sd_lock);605return 0;606}607608static inline u32 round_up_to_quad(u32 i)609{610return (i + 3 ) & ~3;611}612613static inline int614svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)615{616int l;617618if (argv->iov_len < 4)619return -1;620o->len = svc_getnl(argv);621l = round_up_to_quad(o->len);622if (argv->iov_len < l)623return -1;624o->data = argv->iov_base;625argv->iov_base += l;626argv->iov_len -= l;627return 0;628}629630static inline int631svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)632{633u8 *p;634635if (resv->iov_len + 4 > PAGE_SIZE)636return -1;637svc_putnl(resv, o->len);638p = resv->iov_base + resv->iov_len;639resv->iov_len += round_up_to_quad(o->len);640if (resv->iov_len > PAGE_SIZE)641return -1;642memcpy(p, o->data, o->len);643memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);644return 0;645}646647/*648* Verify the checksum on the header and return SVC_OK on success.649* Otherwise, return SVC_DROP (in the case of a bad sequence number)650* or return SVC_DENIED and indicate error in authp.651*/652static int653gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,654__be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)655{656struct gss_ctx *ctx_id = rsci->mechctx;657struct xdr_buf rpchdr;658struct xdr_netobj checksum;659u32 flavor = 0;660struct kvec *argv = &rqstp->rq_arg.head[0];661struct kvec iov;662663/* data to compute the checksum over: */664iov.iov_base = rpcstart;665iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;666xdr_buf_from_iov(&iov, &rpchdr);667668*authp = rpc_autherr_badverf;669if (argv->iov_len < 4)670return SVC_DENIED;671flavor = svc_getnl(argv);672if (flavor != RPC_AUTH_GSS)673return SVC_DENIED;674if (svc_safe_getnetobj(argv, &checksum))675return SVC_DENIED;676677if (rqstp->rq_deferred) /* skip verification of revisited request */678return SVC_OK;679if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {680*authp = rpcsec_gsserr_credproblem;681return SVC_DENIED;682}683684if (gc->gc_seq > MAXSEQ) {685dprintk("RPC: svcauth_gss: discarding request with "686"large sequence number %d\n", gc->gc_seq);687*authp = rpcsec_gsserr_ctxproblem;688return SVC_DENIED;689}690if (!gss_check_seq_num(rsci, gc->gc_seq)) {691dprintk("RPC: svcauth_gss: discarding request with "692"old sequence number %d\n", gc->gc_seq);693return SVC_DROP;694}695return SVC_OK;696}697698static int699gss_write_null_verf(struct svc_rqst *rqstp)700{701__be32 *p;702703svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);704p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;705/* don't really need to check if head->iov_len > PAGE_SIZE ... */706*p++ = 0;707if (!xdr_ressize_check(rqstp, p))708return -1;709return 0;710}711712static int713gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)714{715__be32 xdr_seq;716u32 maj_stat;717struct xdr_buf verf_data;718struct xdr_netobj mic;719__be32 *p;720struct kvec iov;721722svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);723xdr_seq = htonl(seq);724725iov.iov_base = &xdr_seq;726iov.iov_len = sizeof(xdr_seq);727xdr_buf_from_iov(&iov, &verf_data);728p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;729mic.data = (u8 *)(p + 1);730maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);731if (maj_stat != GSS_S_COMPLETE)732return -1;733*p++ = htonl(mic.len);734memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);735p += XDR_QUADLEN(mic.len);736if (!xdr_ressize_check(rqstp, p))737return -1;738return 0;739}740741struct gss_domain {742struct auth_domain h;743u32 pseudoflavor;744};745746static struct auth_domain *747find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)748{749char *name;750751name = gss_service_to_auth_domain_name(ctx->mech_type, svc);752if (!name)753return NULL;754return auth_domain_find(name);755}756757static struct auth_ops svcauthops_gss;758759u32 svcauth_gss_flavor(struct auth_domain *dom)760{761struct gss_domain *gd = container_of(dom, struct gss_domain, h);762763return gd->pseudoflavor;764}765766EXPORT_SYMBOL_GPL(svcauth_gss_flavor);767768int769svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)770{771struct gss_domain *new;772struct auth_domain *test;773int stat = -ENOMEM;774775new = kmalloc(sizeof(*new), GFP_KERNEL);776if (!new)777goto out;778kref_init(&new->h.ref);779new->h.name = kstrdup(name, GFP_KERNEL);780if (!new->h.name)781goto out_free_dom;782new->h.flavour = &svcauthops_gss;783new->pseudoflavor = pseudoflavor;784785stat = 0;786test = auth_domain_lookup(name, &new->h);787if (test != &new->h) { /* Duplicate registration */788auth_domain_put(test);789kfree(new->h.name);790goto out_free_dom;791}792return 0;793794out_free_dom:795kfree(new);796out:797return stat;798}799800EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);801802static inline int803read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)804{805__be32 raw;806int status;807808status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));809if (status)810return status;811*obj = ntohl(raw);812return 0;813}814815/* It would be nice if this bit of code could be shared with the client.816* Obstacles:817* The client shouldn't malloc(), would have to pass in own memory.818* The server uses base of head iovec as read pointer, while the819* client uses separate pointer. */820static int821unwrap_integ_data(struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)822{823int stat = -EINVAL;824u32 integ_len, maj_stat;825struct xdr_netobj mic;826struct xdr_buf integ_buf;827828integ_len = svc_getnl(&buf->head[0]);829if (integ_len & 3)830return stat;831if (integ_len > buf->len)832return stat;833if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))834BUG();835/* copy out mic... */836if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))837BUG();838if (mic.len > RPC_MAX_AUTH_SIZE)839return stat;840mic.data = kmalloc(mic.len, GFP_KERNEL);841if (!mic.data)842return stat;843if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))844goto out;845maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);846if (maj_stat != GSS_S_COMPLETE)847goto out;848if (svc_getnl(&buf->head[0]) != seq)849goto out;850stat = 0;851out:852kfree(mic.data);853return stat;854}855856static inline int857total_buf_len(struct xdr_buf *buf)858{859return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;860}861862static void863fix_priv_head(struct xdr_buf *buf, int pad)864{865if (buf->page_len == 0) {866/* We need to adjust head and buf->len in tandem in this867* case to make svc_defer() work--it finds the original868* buffer start using buf->len - buf->head[0].iov_len. */869buf->head[0].iov_len -= pad;870}871}872873static int874unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)875{876u32 priv_len, maj_stat;877int pad, saved_len, remaining_len, offset;878879rqstp->rq_splice_ok = 0;880881priv_len = svc_getnl(&buf->head[0]);882if (rqstp->rq_deferred) {883/* Already decrypted last time through! The sequence number884* check at out_seq is unnecessary but harmless: */885goto out_seq;886}887/* buf->len is the number of bytes from the original start of the888* request to the end, where head[0].iov_len is just the bytes889* not yet read from the head, so these two values are different: */890remaining_len = total_buf_len(buf);891if (priv_len > remaining_len)892return -EINVAL;893pad = remaining_len - priv_len;894buf->len -= pad;895fix_priv_head(buf, pad);896897/* Maybe it would be better to give gss_unwrap a length parameter: */898saved_len = buf->len;899buf->len = priv_len;900maj_stat = gss_unwrap(ctx, 0, buf);901pad = priv_len - buf->len;902buf->len = saved_len;903buf->len -= pad;904/* The upper layers assume the buffer is aligned on 4-byte boundaries.905* In the krb5p case, at least, the data ends up offset, so we need to906* move it around. */907/* XXX: This is very inefficient. It would be better to either do908* this while we encrypt, or maybe in the receive code, if we can peak909* ahead and work out the service and mechanism there. */910offset = buf->head[0].iov_len % 4;911if (offset) {912buf->buflen = RPCSVC_MAXPAYLOAD;913xdr_shift_buf(buf, offset);914fix_priv_head(buf, pad);915}916if (maj_stat != GSS_S_COMPLETE)917return -EINVAL;918out_seq:919if (svc_getnl(&buf->head[0]) != seq)920return -EINVAL;921return 0;922}923924struct gss_svc_data {925/* decoded gss client cred: */926struct rpc_gss_wire_cred clcred;927/* save a pointer to the beginning of the encoded verifier,928* for use in encryption/checksumming in svcauth_gss_release: */929__be32 *verf_start;930struct rsc *rsci;931};932933char *svc_gss_principal(struct svc_rqst *rqstp)934{935struct gss_svc_data *gd = (struct gss_svc_data *)rqstp->rq_auth_data;936937if (gd && gd->rsci)938return gd->rsci->client_name;939return NULL;940}941EXPORT_SYMBOL_GPL(svc_gss_principal);942943static int944svcauth_gss_set_client(struct svc_rqst *rqstp)945{946struct gss_svc_data *svcdata = rqstp->rq_auth_data;947struct rsc *rsci = svcdata->rsci;948struct rpc_gss_wire_cred *gc = &svcdata->clcred;949int stat;950951/*952* A gss export can be specified either by:953* export *(sec=krb5,rw)954* or by955* export gss/krb5(rw)956* The latter is deprecated; but for backwards compatibility reasons957* the nfsd code will still fall back on trying it if the former958* doesn't work; so we try to make both available to nfsd, below.959*/960rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);961if (rqstp->rq_gssclient == NULL)962return SVC_DENIED;963stat = svcauth_unix_set_client(rqstp);964if (stat == SVC_DROP || stat == SVC_CLOSE)965return stat;966return SVC_OK;967}968969static inline int970gss_write_init_verf(struct svc_rqst *rqstp, struct rsi *rsip)971{972struct rsc *rsci;973int rc;974975if (rsip->major_status != GSS_S_COMPLETE)976return gss_write_null_verf(rqstp);977rsci = gss_svc_searchbyctx(&rsip->out_handle);978if (rsci == NULL) {979rsip->major_status = GSS_S_NO_CONTEXT;980return gss_write_null_verf(rqstp);981}982rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);983cache_put(&rsci->h, &rsc_cache);984return rc;985}986987/*988* Having read the cred already and found we're in the context989* initiation case, read the verifier and initiate (or check the results990* of) upcalls to userspace for help with context initiation. If991* the upcall results are available, write the verifier and result.992* Otherwise, drop the request pending an answer to the upcall.993*/994static int svcauth_gss_handle_init(struct svc_rqst *rqstp,995struct rpc_gss_wire_cred *gc, __be32 *authp)996{997struct kvec *argv = &rqstp->rq_arg.head[0];998struct kvec *resv = &rqstp->rq_res.head[0];999struct xdr_netobj tmpobj;1000struct rsi *rsip, rsikey;1001int ret;10021003/* Read the verifier; should be NULL: */1004*authp = rpc_autherr_badverf;1005if (argv->iov_len < 2 * 4)1006return SVC_DENIED;1007if (svc_getnl(argv) != RPC_AUTH_NULL)1008return SVC_DENIED;1009if (svc_getnl(argv) != 0)1010return SVC_DENIED;10111012/* Martial context handle and token for upcall: */1013*authp = rpc_autherr_badcred;1014if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)1015return SVC_DENIED;1016memset(&rsikey, 0, sizeof(rsikey));1017if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))1018return SVC_CLOSE;1019*authp = rpc_autherr_badverf;1020if (svc_safe_getnetobj(argv, &tmpobj)) {1021kfree(rsikey.in_handle.data);1022return SVC_DENIED;1023}1024if (dup_netobj(&rsikey.in_token, &tmpobj)) {1025kfree(rsikey.in_handle.data);1026return SVC_CLOSE;1027}10281029/* Perform upcall, or find upcall result: */1030rsip = rsi_lookup(&rsikey);1031rsi_free(&rsikey);1032if (!rsip)1033return SVC_CLOSE;1034if (cache_check(&rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)1035/* No upcall result: */1036return SVC_CLOSE;10371038ret = SVC_CLOSE;1039/* Got an answer to the upcall; use it: */1040if (gss_write_init_verf(rqstp, rsip))1041goto out;1042if (resv->iov_len + 4 > PAGE_SIZE)1043goto out;1044svc_putnl(resv, RPC_SUCCESS);1045if (svc_safe_putnetobj(resv, &rsip->out_handle))1046goto out;1047if (resv->iov_len + 3 * 4 > PAGE_SIZE)1048goto out;1049svc_putnl(resv, rsip->major_status);1050svc_putnl(resv, rsip->minor_status);1051svc_putnl(resv, GSS_SEQ_WIN);1052if (svc_safe_putnetobj(resv, &rsip->out_token))1053goto out;10541055ret = SVC_COMPLETE;1056out:1057cache_put(&rsip->h, &rsi_cache);1058return ret;1059}10601061/*1062* Accept an rpcsec packet.1063* If context establishment, punt to user space1064* If data exchange, verify/decrypt1065* If context destruction, handle here1066* In the context establishment and destruction case we encode1067* response here and return SVC_COMPLETE.1068*/1069static int1070svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)1071{1072struct kvec *argv = &rqstp->rq_arg.head[0];1073struct kvec *resv = &rqstp->rq_res.head[0];1074u32 crlen;1075struct gss_svc_data *svcdata = rqstp->rq_auth_data;1076struct rpc_gss_wire_cred *gc;1077struct rsc *rsci = NULL;1078__be32 *rpcstart;1079__be32 *reject_stat = resv->iov_base + resv->iov_len;1080int ret;10811082dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",1083argv->iov_len);10841085*authp = rpc_autherr_badcred;1086if (!svcdata)1087svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);1088if (!svcdata)1089goto auth_err;1090rqstp->rq_auth_data = svcdata;1091svcdata->verf_start = NULL;1092svcdata->rsci = NULL;1093gc = &svcdata->clcred;10941095/* start of rpc packet is 7 u32's back from here:1096* xid direction rpcversion prog vers proc flavour1097*/1098rpcstart = argv->iov_base;1099rpcstart -= 7;11001101/* credential is:1102* version(==1), proc(0,1,2,3), seq, service (1,2,3), handle1103* at least 5 u32s, and is preceded by length, so that makes 6.1104*/11051106if (argv->iov_len < 5 * 4)1107goto auth_err;1108crlen = svc_getnl(argv);1109if (svc_getnl(argv) != RPC_GSS_VERSION)1110goto auth_err;1111gc->gc_proc = svc_getnl(argv);1112gc->gc_seq = svc_getnl(argv);1113gc->gc_svc = svc_getnl(argv);1114if (svc_safe_getnetobj(argv, &gc->gc_ctx))1115goto auth_err;1116if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)1117goto auth_err;11181119if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))1120goto auth_err;11211122*authp = rpc_autherr_badverf;1123switch (gc->gc_proc) {1124case RPC_GSS_PROC_INIT:1125case RPC_GSS_PROC_CONTINUE_INIT:1126return svcauth_gss_handle_init(rqstp, gc, authp);1127case RPC_GSS_PROC_DATA:1128case RPC_GSS_PROC_DESTROY:1129/* Look up the context, and check the verifier: */1130*authp = rpcsec_gsserr_credproblem;1131rsci = gss_svc_searchbyctx(&gc->gc_ctx);1132if (!rsci)1133goto auth_err;1134switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {1135case SVC_OK:1136break;1137case SVC_DENIED:1138goto auth_err;1139case SVC_DROP:1140goto drop;1141}1142break;1143default:1144*authp = rpc_autherr_rejectedcred;1145goto auth_err;1146}11471148/* now act upon the command: */1149switch (gc->gc_proc) {1150case RPC_GSS_PROC_DESTROY:1151if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))1152goto auth_err;1153rsci->h.expiry_time = get_seconds();1154set_bit(CACHE_NEGATIVE, &rsci->h.flags);1155if (resv->iov_len + 4 > PAGE_SIZE)1156goto drop;1157svc_putnl(resv, RPC_SUCCESS);1158goto complete;1159case RPC_GSS_PROC_DATA:1160*authp = rpcsec_gsserr_ctxproblem;1161svcdata->verf_start = resv->iov_base + resv->iov_len;1162if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))1163goto auth_err;1164rqstp->rq_cred = rsci->cred;1165get_group_info(rsci->cred.cr_group_info);1166*authp = rpc_autherr_badcred;1167switch (gc->gc_svc) {1168case RPC_GSS_SVC_NONE:1169break;1170case RPC_GSS_SVC_INTEGRITY:1171/* placeholders for length and seq. number: */1172svc_putnl(resv, 0);1173svc_putnl(resv, 0);1174if (unwrap_integ_data(&rqstp->rq_arg,1175gc->gc_seq, rsci->mechctx))1176goto garbage_args;1177break;1178case RPC_GSS_SVC_PRIVACY:1179/* placeholders for length and seq. number: */1180svc_putnl(resv, 0);1181svc_putnl(resv, 0);1182if (unwrap_priv_data(rqstp, &rqstp->rq_arg,1183gc->gc_seq, rsci->mechctx))1184goto garbage_args;1185break;1186default:1187goto auth_err;1188}1189svcdata->rsci = rsci;1190cache_get(&rsci->h);1191rqstp->rq_flavor = gss_svc_to_pseudoflavor(1192rsci->mechctx->mech_type, gc->gc_svc);1193ret = SVC_OK;1194goto out;1195}1196garbage_args:1197ret = SVC_GARBAGE;1198goto out;1199auth_err:1200/* Restore write pointer to its original value: */1201xdr_ressize_check(rqstp, reject_stat);1202ret = SVC_DENIED;1203goto out;1204complete:1205ret = SVC_COMPLETE;1206goto out;1207drop:1208ret = SVC_DROP;1209out:1210if (rsci)1211cache_put(&rsci->h, &rsc_cache);1212return ret;1213}12141215static __be32 *1216svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)1217{1218__be32 *p;1219u32 verf_len;12201221p = gsd->verf_start;1222gsd->verf_start = NULL;12231224/* If the reply stat is nonzero, don't wrap: */1225if (*(p-1) != rpc_success)1226return NULL;1227/* Skip the verifier: */1228p += 1;1229verf_len = ntohl(*p++);1230p += XDR_QUADLEN(verf_len);1231/* move accept_stat to right place: */1232memcpy(p, p + 2, 4);1233/* Also don't wrap if the accept stat is nonzero: */1234if (*p != rpc_success) {1235resbuf->head[0].iov_len -= 2 * 4;1236return NULL;1237}1238p++;1239return p;1240}12411242static inline int1243svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)1244{1245struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;1246struct rpc_gss_wire_cred *gc = &gsd->clcred;1247struct xdr_buf *resbuf = &rqstp->rq_res;1248struct xdr_buf integ_buf;1249struct xdr_netobj mic;1250struct kvec *resv;1251__be32 *p;1252int integ_offset, integ_len;1253int stat = -EINVAL;12541255p = svcauth_gss_prepare_to_wrap(resbuf, gsd);1256if (p == NULL)1257goto out;1258integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;1259integ_len = resbuf->len - integ_offset;1260BUG_ON(integ_len % 4);1261*p++ = htonl(integ_len);1262*p++ = htonl(gc->gc_seq);1263if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset,1264integ_len))1265BUG();1266if (resbuf->tail[0].iov_base == NULL) {1267if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)1268goto out_err;1269resbuf->tail[0].iov_base = resbuf->head[0].iov_base1270+ resbuf->head[0].iov_len;1271resbuf->tail[0].iov_len = 0;1272resv = &resbuf->tail[0];1273} else {1274resv = &resbuf->tail[0];1275}1276mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;1277if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))1278goto out_err;1279svc_putnl(resv, mic.len);1280memset(mic.data + mic.len, 0,1281round_up_to_quad(mic.len) - mic.len);1282resv->iov_len += XDR_QUADLEN(mic.len) << 2;1283/* not strictly required: */1284resbuf->len += XDR_QUADLEN(mic.len) << 2;1285BUG_ON(resv->iov_len > PAGE_SIZE);1286out:1287stat = 0;1288out_err:1289return stat;1290}12911292static inline int1293svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)1294{1295struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;1296struct rpc_gss_wire_cred *gc = &gsd->clcred;1297struct xdr_buf *resbuf = &rqstp->rq_res;1298struct page **inpages = NULL;1299__be32 *p, *len;1300int offset;1301int pad;13021303p = svcauth_gss_prepare_to_wrap(resbuf, gsd);1304if (p == NULL)1305return 0;1306len = p++;1307offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;1308*p++ = htonl(gc->gc_seq);1309inpages = resbuf->pages;1310/* XXX: Would be better to write some xdr helper functions for1311* nfs{2,3,4}xdr.c that place the data right, instead of copying: */13121313/*1314* If there is currently tail data, make sure there is1315* room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in1316* the page, and move the current tail data such that1317* there is RPC_MAX_AUTH_SIZE slack space available in1318* both the head and tail.1319*/1320if (resbuf->tail[0].iov_base) {1321BUG_ON(resbuf->tail[0].iov_base >= resbuf->head[0].iov_base1322+ PAGE_SIZE);1323BUG_ON(resbuf->tail[0].iov_base < resbuf->head[0].iov_base);1324if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len1325+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)1326return -ENOMEM;1327memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,1328resbuf->tail[0].iov_base,1329resbuf->tail[0].iov_len);1330resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;1331}1332/*1333* If there is no current tail data, make sure there is1334* room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the1335* allotted page, and set up tail information such that there1336* is RPC_MAX_AUTH_SIZE slack space available in both the1337* head and tail.1338*/1339if (resbuf->tail[0].iov_base == NULL) {1340if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)1341return -ENOMEM;1342resbuf->tail[0].iov_base = resbuf->head[0].iov_base1343+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;1344resbuf->tail[0].iov_len = 0;1345}1346if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))1347return -ENOMEM;1348*len = htonl(resbuf->len - offset);1349pad = 3 - ((resbuf->len - offset - 1)&3);1350p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);1351memset(p, 0, pad);1352resbuf->tail[0].iov_len += pad;1353resbuf->len += pad;1354return 0;1355}13561357static int1358svcauth_gss_release(struct svc_rqst *rqstp)1359{1360struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;1361struct rpc_gss_wire_cred *gc = &gsd->clcred;1362struct xdr_buf *resbuf = &rqstp->rq_res;1363int stat = -EINVAL;13641365if (gc->gc_proc != RPC_GSS_PROC_DATA)1366goto out;1367/* Release can be called twice, but we only wrap once. */1368if (gsd->verf_start == NULL)1369goto out;1370/* normally not set till svc_send, but we need it here: */1371/* XXX: what for? Do we mess it up the moment we call svc_putu321372* or whatever? */1373resbuf->len = total_buf_len(resbuf);1374switch (gc->gc_svc) {1375case RPC_GSS_SVC_NONE:1376break;1377case RPC_GSS_SVC_INTEGRITY:1378stat = svcauth_gss_wrap_resp_integ(rqstp);1379if (stat)1380goto out_err;1381break;1382case RPC_GSS_SVC_PRIVACY:1383stat = svcauth_gss_wrap_resp_priv(rqstp);1384if (stat)1385goto out_err;1386break;1387/*1388* For any other gc_svc value, svcauth_gss_accept() already set1389* the auth_error appropriately; just fall through:1390*/1391}13921393out:1394stat = 0;1395out_err:1396if (rqstp->rq_client)1397auth_domain_put(rqstp->rq_client);1398rqstp->rq_client = NULL;1399if (rqstp->rq_gssclient)1400auth_domain_put(rqstp->rq_gssclient);1401rqstp->rq_gssclient = NULL;1402if (rqstp->rq_cred.cr_group_info)1403put_group_info(rqstp->rq_cred.cr_group_info);1404rqstp->rq_cred.cr_group_info = NULL;1405if (gsd->rsci)1406cache_put(&gsd->rsci->h, &rsc_cache);1407gsd->rsci = NULL;14081409return stat;1410}14111412static void1413svcauth_gss_domain_release(struct auth_domain *dom)1414{1415struct gss_domain *gd = container_of(dom, struct gss_domain, h);14161417kfree(dom->name);1418kfree(gd);1419}14201421static struct auth_ops svcauthops_gss = {1422.name = "rpcsec_gss",1423.owner = THIS_MODULE,1424.flavour = RPC_AUTH_GSS,1425.accept = svcauth_gss_accept,1426.release = svcauth_gss_release,1427.domain_release = svcauth_gss_domain_release,1428.set_client = svcauth_gss_set_client,1429};14301431int1432gss_svc_init(void)1433{1434int rv = svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);1435if (rv)1436return rv;1437rv = cache_register(&rsc_cache);1438if (rv)1439goto out1;1440rv = cache_register(&rsi_cache);1441if (rv)1442goto out2;1443return 0;1444out2:1445cache_unregister(&rsc_cache);1446out1:1447svc_auth_unregister(RPC_AUTH_GSS);1448return rv;1449}14501451void1452gss_svc_shutdown(void)1453{1454cache_unregister(&rsc_cache);1455cache_unregister(&rsi_cache);1456svc_auth_unregister(RPC_AUTH_GSS);1457}145814591460