auth_gss.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /*
  2. * linux/net/sunrpc/auth_gss/auth_gss.c
  3. *
  4. * RPCSEC_GSS client authentication.
  5. *
  6. * Copyright (c) 2000 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Dug Song <dugsong@monkey.org>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/sched.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/sunrpc/auth.h>
  45. #include <linux/sunrpc/auth_gss.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include <linux/sunrpc/gss_err.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/sunrpc/rpc_pipe_fs.h>
  50. #include <linux/sunrpc/gss_api.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/hashtable.h>
  53. #include "../netns.h"
  54. static const struct rpc_authops authgss_ops;
  55. static const struct rpc_credops gss_credops;
  56. static const struct rpc_credops gss_nullops;
  57. #define GSS_RETRY_EXPIRED 5
  58. static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
  59. #define GSS_KEY_EXPIRE_TIMEO 240
  60. static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
  61. #ifdef RPC_DEBUG
  62. # define RPCDBG_FACILITY RPCDBG_AUTH
  63. #endif
  64. #define GSS_CRED_SLACK (RPC_MAX_AUTH_SIZE * 2)
  65. /* length of a krb5 verifier (48), plus data added before arguments when
  66. * using integrity (two 4-byte integers): */
  67. #define GSS_VERF_SLACK 100
  68. static DEFINE_HASHTABLE(gss_auth_hash_table, 4);
  69. static DEFINE_SPINLOCK(gss_auth_hash_lock);
  70. struct gss_pipe {
  71. struct rpc_pipe_dir_object pdo;
  72. struct rpc_pipe *pipe;
  73. struct rpc_clnt *clnt;
  74. const char *name;
  75. struct kref kref;
  76. };
  77. struct gss_auth {
  78. struct kref kref;
  79. struct hlist_node hash;
  80. struct rpc_auth rpc_auth;
  81. struct gss_api_mech *mech;
  82. enum rpc_gss_svc service;
  83. struct rpc_clnt *client;
  84. struct net *net;
  85. /*
  86. * There are two upcall pipes; dentry[1], named "gssd", is used
  87. * for the new text-based upcall; dentry[0] is named after the
  88. * mechanism (for example, "krb5") and exists for
  89. * backwards-compatibility with older gssd's.
  90. */
  91. struct gss_pipe *gss_pipe[2];
  92. const char *target_name;
  93. };
  94. /* pipe_version >= 0 if and only if someone has a pipe open. */
  95. static DEFINE_SPINLOCK(pipe_version_lock);
  96. static struct rpc_wait_queue pipe_version_rpc_waitqueue;
  97. static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
  98. static void gss_free_ctx(struct gss_cl_ctx *);
  99. static const struct rpc_pipe_ops gss_upcall_ops_v0;
  100. static const struct rpc_pipe_ops gss_upcall_ops_v1;
  101. static inline struct gss_cl_ctx *
  102. gss_get_ctx(struct gss_cl_ctx *ctx)
  103. {
  104. atomic_inc(&ctx->count);
  105. return ctx;
  106. }
  107. static inline void
  108. gss_put_ctx(struct gss_cl_ctx *ctx)
  109. {
  110. if (atomic_dec_and_test(&ctx->count))
  111. gss_free_ctx(ctx);
  112. }
  113. /* gss_cred_set_ctx:
  114. * called by gss_upcall_callback and gss_create_upcall in order
  115. * to set the gss context. The actual exchange of an old context
  116. * and a new one is protected by the pipe->lock.
  117. */
  118. static void
  119. gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
  120. {
  121. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  122. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  123. return;
  124. gss_get_ctx(ctx);
  125. rcu_assign_pointer(gss_cred->gc_ctx, ctx);
  126. set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  127. smp_mb__before_clear_bit();
  128. clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
  129. }
  130. static const void *
  131. simple_get_bytes(const void *p, const void *end, void *res, size_t len)
  132. {
  133. const void *q = (const void *)((const char *)p + len);
  134. if (unlikely(q > end || q < p))
  135. return ERR_PTR(-EFAULT);
  136. memcpy(res, p, len);
  137. return q;
  138. }
  139. static inline const void *
  140. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
  141. {
  142. const void *q;
  143. unsigned int len;
  144. p = simple_get_bytes(p, end, &len, sizeof(len));
  145. if (IS_ERR(p))
  146. return p;
  147. q = (const void *)((const char *)p + len);
  148. if (unlikely(q > end || q < p))
  149. return ERR_PTR(-EFAULT);
  150. dest->data = kmemdup(p, len, GFP_NOFS);
  151. if (unlikely(dest->data == NULL))
  152. return ERR_PTR(-ENOMEM);
  153. dest->len = len;
  154. return q;
  155. }
  156. static struct gss_cl_ctx *
  157. gss_cred_get_ctx(struct rpc_cred *cred)
  158. {
  159. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  160. struct gss_cl_ctx *ctx = NULL;
  161. rcu_read_lock();
  162. if (gss_cred->gc_ctx)
  163. ctx = gss_get_ctx(gss_cred->gc_ctx);
  164. rcu_read_unlock();
  165. return ctx;
  166. }
  167. static struct gss_cl_ctx *
  168. gss_alloc_context(void)
  169. {
  170. struct gss_cl_ctx *ctx;
  171. ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
  172. if (ctx != NULL) {
  173. ctx->gc_proc = RPC_GSS_PROC_DATA;
  174. ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
  175. spin_lock_init(&ctx->gc_seq_lock);
  176. atomic_set(&ctx->count,1);
  177. }
  178. return ctx;
  179. }
  180. #define GSSD_MIN_TIMEOUT (60 * 60)
  181. static const void *
  182. gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
  183. {
  184. const void *q;
  185. unsigned int seclen;
  186. unsigned int timeout;
  187. unsigned long now = jiffies;
  188. u32 window_size;
  189. int ret;
  190. /* First unsigned int gives the remaining lifetime in seconds of the
  191. * credential - e.g. the remaining TGT lifetime for Kerberos or
  192. * the -t value passed to GSSD.
  193. */
  194. p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
  195. if (IS_ERR(p))
  196. goto err;
  197. if (timeout == 0)
  198. timeout = GSSD_MIN_TIMEOUT;
  199. ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
  200. /* Sequence number window. Determines the maximum number of
  201. * simultaneous requests
  202. */
  203. p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
  204. if (IS_ERR(p))
  205. goto err;
  206. ctx->gc_win = window_size;
  207. /* gssd signals an error by passing ctx->gc_win = 0: */
  208. if (ctx->gc_win == 0) {
  209. /*
  210. * in which case, p points to an error code. Anything other
  211. * than -EKEYEXPIRED gets converted to -EACCES.
  212. */
  213. p = simple_get_bytes(p, end, &ret, sizeof(ret));
  214. if (!IS_ERR(p))
  215. p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
  216. ERR_PTR(-EACCES);
  217. goto err;
  218. }
  219. /* copy the opaque wire context */
  220. p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
  221. if (IS_ERR(p))
  222. goto err;
  223. /* import the opaque security context */
  224. p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
  225. if (IS_ERR(p))
  226. goto err;
  227. q = (const void *)((const char *)p + seclen);
  228. if (unlikely(q > end || q < p)) {
  229. p = ERR_PTR(-EFAULT);
  230. goto err;
  231. }
  232. ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
  233. if (ret < 0) {
  234. p = ERR_PTR(ret);
  235. goto err;
  236. }
  237. dprintk("RPC: %s Success. gc_expiry %lu now %lu timeout %u\n",
  238. __func__, ctx->gc_expiry, now, timeout);
  239. return q;
  240. err:
  241. dprintk("RPC: %s returns error %ld\n", __func__, -PTR_ERR(p));
  242. return p;
  243. }
  244. #define UPCALL_BUF_LEN 128
  245. struct gss_upcall_msg {
  246. atomic_t count;
  247. kuid_t uid;
  248. struct rpc_pipe_msg msg;
  249. struct list_head list;
  250. struct gss_auth *auth;
  251. struct rpc_pipe *pipe;
  252. struct rpc_wait_queue rpc_waitqueue;
  253. wait_queue_head_t waitqueue;
  254. struct gss_cl_ctx *ctx;
  255. char databuf[UPCALL_BUF_LEN];
  256. };
  257. static int get_pipe_version(struct net *net)
  258. {
  259. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  260. int ret;
  261. spin_lock(&pipe_version_lock);
  262. if (sn->pipe_version >= 0) {
  263. atomic_inc(&sn->pipe_users);
  264. ret = sn->pipe_version;
  265. } else
  266. ret = -EAGAIN;
  267. spin_unlock(&pipe_version_lock);
  268. return ret;
  269. }
  270. static void put_pipe_version(struct net *net)
  271. {
  272. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  273. if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
  274. sn->pipe_version = -1;
  275. spin_unlock(&pipe_version_lock);
  276. }
  277. }
  278. static void
  279. gss_release_msg(struct gss_upcall_msg *gss_msg)
  280. {
  281. struct net *net = gss_msg->auth->net;
  282. if (!atomic_dec_and_test(&gss_msg->count))
  283. return;
  284. put_pipe_version(net);
  285. BUG_ON(!list_empty(&gss_msg->list));
  286. if (gss_msg->ctx != NULL)
  287. gss_put_ctx(gss_msg->ctx);
  288. rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
  289. kfree(gss_msg);
  290. }
  291. static struct gss_upcall_msg *
  292. __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
  293. {
  294. struct gss_upcall_msg *pos;
  295. list_for_each_entry(pos, &pipe->in_downcall, list) {
  296. if (!uid_eq(pos->uid, uid))
  297. continue;
  298. atomic_inc(&pos->count);
  299. dprintk("RPC: %s found msg %p\n", __func__, pos);
  300. return pos;
  301. }
  302. dprintk("RPC: %s found nothing\n", __func__);
  303. return NULL;
  304. }
  305. /* Try to add an upcall to the pipefs queue.
  306. * If an upcall owned by our uid already exists, then we return a reference
  307. * to that upcall instead of adding the new upcall.
  308. */
  309. static inline struct gss_upcall_msg *
  310. gss_add_msg(struct gss_upcall_msg *gss_msg)
  311. {
  312. struct rpc_pipe *pipe = gss_msg->pipe;
  313. struct gss_upcall_msg *old;
  314. spin_lock(&pipe->lock);
  315. old = __gss_find_upcall(pipe, gss_msg->uid);
  316. if (old == NULL) {
  317. atomic_inc(&gss_msg->count);
  318. list_add(&gss_msg->list, &pipe->in_downcall);
  319. } else
  320. gss_msg = old;
  321. spin_unlock(&pipe->lock);
  322. return gss_msg;
  323. }
  324. static void
  325. __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  326. {
  327. list_del_init(&gss_msg->list);
  328. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  329. wake_up_all(&gss_msg->waitqueue);
  330. atomic_dec(&gss_msg->count);
  331. }
  332. static void
  333. gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  334. {
  335. struct rpc_pipe *pipe = gss_msg->pipe;
  336. if (list_empty(&gss_msg->list))
  337. return;
  338. spin_lock(&pipe->lock);
  339. if (!list_empty(&gss_msg->list))
  340. __gss_unhash_msg(gss_msg);
  341. spin_unlock(&pipe->lock);
  342. }
  343. static void
  344. gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
  345. {
  346. switch (gss_msg->msg.errno) {
  347. case 0:
  348. if (gss_msg->ctx == NULL)
  349. break;
  350. clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  351. gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
  352. break;
  353. case -EKEYEXPIRED:
  354. set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  355. }
  356. gss_cred->gc_upcall_timestamp = jiffies;
  357. gss_cred->gc_upcall = NULL;
  358. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  359. }
  360. static void
  361. gss_upcall_callback(struct rpc_task *task)
  362. {
  363. struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
  364. struct gss_cred, gc_base);
  365. struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
  366. struct rpc_pipe *pipe = gss_msg->pipe;
  367. spin_lock(&pipe->lock);
  368. gss_handle_downcall_result(gss_cred, gss_msg);
  369. spin_unlock(&pipe->lock);
  370. task->tk_status = gss_msg->msg.errno;
  371. gss_release_msg(gss_msg);
  372. }
  373. static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
  374. {
  375. uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
  376. memcpy(gss_msg->databuf, &uid, sizeof(uid));
  377. gss_msg->msg.data = gss_msg->databuf;
  378. gss_msg->msg.len = sizeof(uid);
  379. BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
  380. }
  381. static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
  382. const char *service_name,
  383. const char *target_name)
  384. {
  385. struct gss_api_mech *mech = gss_msg->auth->mech;
  386. char *p = gss_msg->databuf;
  387. int len = 0;
  388. gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
  389. mech->gm_name,
  390. from_kuid(&init_user_ns, gss_msg->uid));
  391. p += gss_msg->msg.len;
  392. if (target_name) {
  393. len = sprintf(p, "target=%s ", target_name);
  394. p += len;
  395. gss_msg->msg.len += len;
  396. }
  397. if (service_name != NULL) {
  398. len = sprintf(p, "service=%s ", service_name);
  399. p += len;
  400. gss_msg->msg.len += len;
  401. }
  402. if (mech->gm_upcall_enctypes) {
  403. len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
  404. p += len;
  405. gss_msg->msg.len += len;
  406. }
  407. len = sprintf(p, "\n");
  408. gss_msg->msg.len += len;
  409. gss_msg->msg.data = gss_msg->databuf;
  410. BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
  411. }
  412. static struct gss_upcall_msg *
  413. gss_alloc_msg(struct gss_auth *gss_auth,
  414. kuid_t uid, const char *service_name)
  415. {
  416. struct gss_upcall_msg *gss_msg;
  417. int vers;
  418. gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
  419. if (gss_msg == NULL)
  420. return ERR_PTR(-ENOMEM);
  421. vers = get_pipe_version(gss_auth->net);
  422. if (vers < 0) {
  423. kfree(gss_msg);
  424. return ERR_PTR(vers);
  425. }
  426. gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
  427. INIT_LIST_HEAD(&gss_msg->list);
  428. rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
  429. init_waitqueue_head(&gss_msg->waitqueue);
  430. atomic_set(&gss_msg->count, 1);
  431. gss_msg->uid = uid;
  432. gss_msg->auth = gss_auth;
  433. switch (vers) {
  434. case 0:
  435. gss_encode_v0_msg(gss_msg);
  436. default:
  437. gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
  438. };
  439. return gss_msg;
  440. }
  441. static struct gss_upcall_msg *
  442. gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
  443. {
  444. struct gss_cred *gss_cred = container_of(cred,
  445. struct gss_cred, gc_base);
  446. struct gss_upcall_msg *gss_new, *gss_msg;
  447. kuid_t uid = cred->cr_uid;
  448. gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
  449. if (IS_ERR(gss_new))
  450. return gss_new;
  451. gss_msg = gss_add_msg(gss_new);
  452. if (gss_msg == gss_new) {
  453. int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
  454. if (res) {
  455. gss_unhash_msg(gss_new);
  456. gss_msg = ERR_PTR(res);
  457. }
  458. } else
  459. gss_release_msg(gss_new);
  460. return gss_msg;
  461. }
  462. static void warn_gssd(void)
  463. {
  464. static unsigned long ratelimit;
  465. unsigned long now = jiffies;
  466. if (time_after(now, ratelimit)) {
  467. printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
  468. "Please check user daemon is running.\n");
  469. ratelimit = now + 15*HZ;
  470. }
  471. }
  472. static inline int
  473. gss_refresh_upcall(struct rpc_task *task)
  474. {
  475. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  476. struct gss_auth *gss_auth = container_of(cred->cr_auth,
  477. struct gss_auth, rpc_auth);
  478. struct gss_cred *gss_cred = container_of(cred,
  479. struct gss_cred, gc_base);
  480. struct gss_upcall_msg *gss_msg;
  481. struct rpc_pipe *pipe;
  482. int err = 0;
  483. dprintk("RPC: %5u %s for uid %u\n",
  484. task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
  485. gss_msg = gss_setup_upcall(gss_auth, cred);
  486. if (PTR_ERR(gss_msg) == -EAGAIN) {
  487. /* XXX: warning on the first, under the assumption we
  488. * shouldn't normally hit this case on a refresh. */
  489. warn_gssd();
  490. task->tk_timeout = 15*HZ;
  491. rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
  492. return -EAGAIN;
  493. }
  494. if (IS_ERR(gss_msg)) {
  495. err = PTR_ERR(gss_msg);
  496. goto out;
  497. }
  498. pipe = gss_msg->pipe;
  499. spin_lock(&pipe->lock);
  500. if (gss_cred->gc_upcall != NULL)
  501. rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
  502. else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
  503. task->tk_timeout = 0;
  504. gss_cred->gc_upcall = gss_msg;
  505. /* gss_upcall_callback will release the reference to gss_upcall_msg */
  506. atomic_inc(&gss_msg->count);
  507. rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
  508. } else {
  509. gss_handle_downcall_result(gss_cred, gss_msg);
  510. err = gss_msg->msg.errno;
  511. }
  512. spin_unlock(&pipe->lock);
  513. gss_release_msg(gss_msg);
  514. out:
  515. dprintk("RPC: %5u %s for uid %u result %d\n",
  516. task->tk_pid, __func__,
  517. from_kuid(&init_user_ns, cred->cr_uid), err);
  518. return err;
  519. }
  520. static inline int
  521. gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
  522. {
  523. struct net *net = gss_auth->net;
  524. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  525. struct rpc_pipe *pipe;
  526. struct rpc_cred *cred = &gss_cred->gc_base;
  527. struct gss_upcall_msg *gss_msg;
  528. unsigned long timeout;
  529. DEFINE_WAIT(wait);
  530. int err;
  531. dprintk("RPC: %s for uid %u\n",
  532. __func__, from_kuid(&init_user_ns, cred->cr_uid));
  533. retry:
  534. err = 0;
  535. /* Default timeout is 15s unless we know that gssd is not running */
  536. timeout = 15 * HZ;
  537. if (!sn->gssd_running)
  538. timeout = HZ >> 2;
  539. gss_msg = gss_setup_upcall(gss_auth, cred);
  540. if (PTR_ERR(gss_msg) == -EAGAIN) {
  541. err = wait_event_interruptible_timeout(pipe_version_waitqueue,
  542. sn->pipe_version >= 0, timeout);
  543. if (sn->pipe_version < 0) {
  544. if (err == 0)
  545. sn->gssd_running = 0;
  546. warn_gssd();
  547. err = -EACCES;
  548. }
  549. if (err < 0)
  550. goto out;
  551. goto retry;
  552. }
  553. if (IS_ERR(gss_msg)) {
  554. err = PTR_ERR(gss_msg);
  555. goto out;
  556. }
  557. pipe = gss_msg->pipe;
  558. for (;;) {
  559. prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
  560. spin_lock(&pipe->lock);
  561. if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
  562. break;
  563. }
  564. spin_unlock(&pipe->lock);
  565. if (fatal_signal_pending(current)) {
  566. err = -ERESTARTSYS;
  567. goto out_intr;
  568. }
  569. schedule();
  570. }
  571. if (gss_msg->ctx)
  572. gss_cred_set_ctx(cred, gss_msg->ctx);
  573. else
  574. err = gss_msg->msg.errno;
  575. spin_unlock(&pipe->lock);
  576. out_intr:
  577. finish_wait(&gss_msg->waitqueue, &wait);
  578. gss_release_msg(gss_msg);
  579. out:
  580. dprintk("RPC: %s for uid %u result %d\n",
  581. __func__, from_kuid(&init_user_ns, cred->cr_uid), err);
  582. return err;
  583. }
  584. #define MSG_BUF_MAXSIZE 1024
  585. static ssize_t
  586. gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  587. {
  588. const void *p, *end;
  589. void *buf;
  590. struct gss_upcall_msg *gss_msg;
  591. struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
  592. struct gss_cl_ctx *ctx;
  593. uid_t id;
  594. kuid_t uid;
  595. ssize_t err = -EFBIG;
  596. if (mlen > MSG_BUF_MAXSIZE)
  597. goto out;
  598. err = -ENOMEM;
  599. buf = kmalloc(mlen, GFP_NOFS);
  600. if (!buf)
  601. goto out;
  602. err = -EFAULT;
  603. if (copy_from_user(buf, src, mlen))
  604. goto err;
  605. end = (const void *)((char *)buf + mlen);
  606. p = simple_get_bytes(buf, end, &id, sizeof(id));
  607. if (IS_ERR(p)) {
  608. err = PTR_ERR(p);
  609. goto err;
  610. }
  611. uid = make_kuid(&init_user_ns, id);
  612. if (!uid_valid(uid)) {
  613. err = -EINVAL;
  614. goto err;
  615. }
  616. err = -ENOMEM;
  617. ctx = gss_alloc_context();
  618. if (ctx == NULL)
  619. goto err;
  620. err = -ENOENT;
  621. /* Find a matching upcall */
  622. spin_lock(&pipe->lock);
  623. gss_msg = __gss_find_upcall(pipe, uid);
  624. if (gss_msg == NULL) {
  625. spin_unlock(&pipe->lock);
  626. goto err_put_ctx;
  627. }
  628. list_del_init(&gss_msg->list);
  629. spin_unlock(&pipe->lock);
  630. p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
  631. if (IS_ERR(p)) {
  632. err = PTR_ERR(p);
  633. switch (err) {
  634. case -EACCES:
  635. case -EKEYEXPIRED:
  636. gss_msg->msg.errno = err;
  637. err = mlen;
  638. break;
  639. case -EFAULT:
  640. case -ENOMEM:
  641. case -EINVAL:
  642. case -ENOSYS:
  643. gss_msg->msg.errno = -EAGAIN;
  644. break;
  645. default:
  646. printk(KERN_CRIT "%s: bad return from "
  647. "gss_fill_context: %zd\n", __func__, err);
  648. BUG();
  649. }
  650. goto err_release_msg;
  651. }
  652. gss_msg->ctx = gss_get_ctx(ctx);
  653. err = mlen;
  654. err_release_msg:
  655. spin_lock(&pipe->lock);
  656. __gss_unhash_msg(gss_msg);
  657. spin_unlock(&pipe->lock);
  658. gss_release_msg(gss_msg);
  659. err_put_ctx:
  660. gss_put_ctx(ctx);
  661. err:
  662. kfree(buf);
  663. out:
  664. dprintk("RPC: %s returning %Zd\n", __func__, err);
  665. return err;
  666. }
  667. static int gss_pipe_open(struct inode *inode, int new_version)
  668. {
  669. struct net *net = inode->i_sb->s_fs_info;
  670. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  671. int ret = 0;
  672. spin_lock(&pipe_version_lock);
  673. if (sn->pipe_version < 0) {
  674. /* First open of any gss pipe determines the version: */
  675. sn->pipe_version = new_version;
  676. rpc_wake_up(&pipe_version_rpc_waitqueue);
  677. wake_up(&pipe_version_waitqueue);
  678. } else if (sn->pipe_version != new_version) {
  679. /* Trying to open a pipe of a different version */
  680. ret = -EBUSY;
  681. goto out;
  682. }
  683. atomic_inc(&sn->pipe_users);
  684. out:
  685. spin_unlock(&pipe_version_lock);
  686. return ret;
  687. }
  688. static int gss_pipe_open_v0(struct inode *inode)
  689. {
  690. return gss_pipe_open(inode, 0);
  691. }
  692. static int gss_pipe_open_v1(struct inode *inode)
  693. {
  694. return gss_pipe_open(inode, 1);
  695. }
  696. static void
  697. gss_pipe_release(struct inode *inode)
  698. {
  699. struct net *net = inode->i_sb->s_fs_info;
  700. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  701. struct gss_upcall_msg *gss_msg;
  702. restart:
  703. spin_lock(&pipe->lock);
  704. list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
  705. if (!list_empty(&gss_msg->msg.list))
  706. continue;
  707. gss_msg->msg.errno = -EPIPE;
  708. atomic_inc(&gss_msg->count);
  709. __gss_unhash_msg(gss_msg);
  710. spin_unlock(&pipe->lock);
  711. gss_release_msg(gss_msg);
  712. goto restart;
  713. }
  714. spin_unlock(&pipe->lock);
  715. put_pipe_version(net);
  716. }
  717. static void
  718. gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  719. {
  720. struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
  721. if (msg->errno < 0) {
  722. dprintk("RPC: %s releasing msg %p\n",
  723. __func__, gss_msg);
  724. atomic_inc(&gss_msg->count);
  725. gss_unhash_msg(gss_msg);
  726. if (msg->errno == -ETIMEDOUT)
  727. warn_gssd();
  728. gss_release_msg(gss_msg);
  729. }
  730. }
  731. static void gss_pipe_dentry_destroy(struct dentry *dir,
  732. struct rpc_pipe_dir_object *pdo)
  733. {
  734. struct gss_pipe *gss_pipe = pdo->pdo_data;
  735. struct rpc_pipe *pipe = gss_pipe->pipe;
  736. if (pipe->dentry != NULL) {
  737. rpc_unlink(pipe->dentry);
  738. pipe->dentry = NULL;
  739. }
  740. }
  741. static int gss_pipe_dentry_create(struct dentry *dir,
  742. struct rpc_pipe_dir_object *pdo)
  743. {
  744. struct gss_pipe *p = pdo->pdo_data;
  745. struct dentry *dentry;
  746. dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
  747. if (IS_ERR(dentry))
  748. return PTR_ERR(dentry);
  749. p->pipe->dentry = dentry;
  750. return 0;
  751. }
  752. static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
  753. .create = gss_pipe_dentry_create,
  754. .destroy = gss_pipe_dentry_destroy,
  755. };
  756. static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
  757. const char *name,
  758. const struct rpc_pipe_ops *upcall_ops)
  759. {
  760. struct gss_pipe *p;
  761. int err = -ENOMEM;
  762. p = kmalloc(sizeof(*p), GFP_KERNEL);
  763. if (p == NULL)
  764. goto err;
  765. p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  766. if (IS_ERR(p->pipe)) {
  767. err = PTR_ERR(p->pipe);
  768. goto err_free_gss_pipe;
  769. }
  770. p->name = name;
  771. p->clnt = clnt;
  772. kref_init(&p->kref);
  773. rpc_init_pipe_dir_object(&p->pdo,
  774. &gss_pipe_dir_object_ops,
  775. p);
  776. return p;
  777. err_free_gss_pipe:
  778. kfree(p);
  779. err:
  780. return ERR_PTR(err);
  781. }
  782. struct gss_alloc_pdo {
  783. struct rpc_clnt *clnt;
  784. const char *name;
  785. const struct rpc_pipe_ops *upcall_ops;
  786. };
  787. static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
  788. {
  789. struct gss_pipe *gss_pipe;
  790. struct gss_alloc_pdo *args = data;
  791. if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
  792. return 0;
  793. gss_pipe = container_of(pdo, struct gss_pipe, pdo);
  794. if (strcmp(gss_pipe->name, args->name) != 0)
  795. return 0;
  796. if (!kref_get_unless_zero(&gss_pipe->kref))
  797. return 0;
  798. return 1;
  799. }
  800. static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
  801. {
  802. struct gss_pipe *gss_pipe;
  803. struct gss_alloc_pdo *args = data;
  804. gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
  805. if (!IS_ERR(gss_pipe))
  806. return &gss_pipe->pdo;
  807. return NULL;
  808. }
  809. static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
  810. const char *name,
  811. const struct rpc_pipe_ops *upcall_ops)
  812. {
  813. struct net *net = rpc_net_ns(clnt);
  814. struct rpc_pipe_dir_object *pdo;
  815. struct gss_alloc_pdo args = {
  816. .clnt = clnt,
  817. .name = name,
  818. .upcall_ops = upcall_ops,
  819. };
  820. pdo = rpc_find_or_alloc_pipe_dir_object(net,
  821. &clnt->cl_pipedir_objects,
  822. gss_pipe_match_pdo,
  823. gss_pipe_alloc_pdo,
  824. &args);
  825. if (pdo != NULL)
  826. return container_of(pdo, struct gss_pipe, pdo);
  827. return ERR_PTR(-ENOMEM);
  828. }
  829. static void __gss_pipe_free(struct gss_pipe *p)
  830. {
  831. struct rpc_clnt *clnt = p->clnt;
  832. struct net *net = rpc_net_ns(clnt);
  833. rpc_remove_pipe_dir_object(net,
  834. &clnt->cl_pipedir_objects,
  835. &p->pdo);
  836. rpc_destroy_pipe_data(p->pipe);
  837. kfree(p);
  838. }
  839. static void __gss_pipe_release(struct kref *kref)
  840. {
  841. struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
  842. __gss_pipe_free(p);
  843. }
  844. static void gss_pipe_free(struct gss_pipe *p)
  845. {
  846. if (p != NULL)
  847. kref_put(&p->kref, __gss_pipe_release);
  848. }
  849. /*
  850. * NOTE: we have the opportunity to use different
  851. * parameters based on the input flavor (which must be a pseudoflavor)
  852. */
  853. static struct gss_auth *
  854. gss_create_new(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  855. {
  856. rpc_authflavor_t flavor = args->pseudoflavor;
  857. struct gss_auth *gss_auth;
  858. struct gss_pipe *gss_pipe;
  859. struct rpc_auth * auth;
  860. int err = -ENOMEM; /* XXX? */
  861. dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
  862. if (!try_module_get(THIS_MODULE))
  863. return ERR_PTR(err);
  864. if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
  865. goto out_dec;
  866. INIT_HLIST_NODE(&gss_auth->hash);
  867. gss_auth->target_name = NULL;
  868. if (args->target_name) {
  869. gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
  870. if (gss_auth->target_name == NULL)
  871. goto err_free;
  872. }
  873. gss_auth->client = clnt;
  874. gss_auth->net = get_net(rpc_net_ns(clnt));
  875. err = -EINVAL;
  876. gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
  877. if (!gss_auth->mech) {
  878. dprintk("RPC: Pseudoflavor %d not found!\n", flavor);
  879. goto err_put_net;
  880. }
  881. gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
  882. if (gss_auth->service == 0)
  883. goto err_put_mech;
  884. auth = &gss_auth->rpc_auth;
  885. auth->au_cslack = GSS_CRED_SLACK >> 2;
  886. auth->au_rslack = GSS_VERF_SLACK >> 2;
  887. auth->au_ops = &authgss_ops;
  888. auth->au_flavor = flavor;
  889. atomic_set(&auth->au_count, 1);
  890. kref_init(&gss_auth->kref);
  891. err = rpcauth_init_credcache(auth);
  892. if (err)
  893. goto err_put_mech;
  894. /*
  895. * Note: if we created the old pipe first, then someone who
  896. * examined the directory at the right moment might conclude
  897. * that we supported only the old pipe. So we instead create
  898. * the new pipe first.
  899. */
  900. gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
  901. if (IS_ERR(gss_pipe)) {
  902. err = PTR_ERR(gss_pipe);
  903. goto err_destroy_credcache;
  904. }
  905. gss_auth->gss_pipe[1] = gss_pipe;
  906. gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
  907. &gss_upcall_ops_v0);
  908. if (IS_ERR(gss_pipe)) {
  909. err = PTR_ERR(gss_pipe);
  910. goto err_destroy_pipe_1;
  911. }
  912. gss_auth->gss_pipe[0] = gss_pipe;
  913. return gss_auth;
  914. err_destroy_pipe_1:
  915. gss_pipe_free(gss_auth->gss_pipe[1]);
  916. err_destroy_credcache:
  917. rpcauth_destroy_credcache(auth);
  918. err_put_mech:
  919. gss_mech_put(gss_auth->mech);
  920. err_put_net:
  921. put_net(gss_auth->net);
  922. err_free:
  923. kfree(gss_auth->target_name);
  924. kfree(gss_auth);
  925. out_dec:
  926. module_put(THIS_MODULE);
  927. return ERR_PTR(err);
  928. }
  929. static void
  930. gss_free(struct gss_auth *gss_auth)
  931. {
  932. gss_pipe_free(gss_auth->gss_pipe[0]);
  933. gss_pipe_free(gss_auth->gss_pipe[1]);
  934. gss_mech_put(gss_auth->mech);
  935. put_net(gss_auth->net);
  936. kfree(gss_auth->target_name);
  937. kfree(gss_auth);
  938. module_put(THIS_MODULE);
  939. }
  940. static void
  941. gss_free_callback(struct kref *kref)
  942. {
  943. struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
  944. gss_free(gss_auth);
  945. }
  946. static void
  947. gss_destroy(struct rpc_auth *auth)
  948. {
  949. struct gss_auth *gss_auth = container_of(auth,
  950. struct gss_auth, rpc_auth);
  951. dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
  952. auth, auth->au_flavor);
  953. if (hash_hashed(&gss_auth->hash)) {
  954. spin_lock(&gss_auth_hash_lock);
  955. hash_del(&gss_auth->hash);
  956. spin_unlock(&gss_auth_hash_lock);
  957. }
  958. gss_pipe_free(gss_auth->gss_pipe[0]);
  959. gss_auth->gss_pipe[0] = NULL;
  960. gss_pipe_free(gss_auth->gss_pipe[1]);
  961. gss_auth->gss_pipe[1] = NULL;
  962. rpcauth_destroy_credcache(auth);
  963. kref_put(&gss_auth->kref, gss_free_callback);
  964. }
  965. /*
  966. * Auths may be shared between rpc clients that were cloned from a
  967. * common client with the same xprt, if they also share the flavor and
  968. * target_name.
  969. *
  970. * The auth is looked up from the oldest parent sharing the same
  971. * cl_xprt, and the auth itself references only that common parent
  972. * (which is guaranteed to last as long as any of its descendants).
  973. */
  974. static struct gss_auth *
  975. gss_auth_find_or_add_hashed(struct rpc_auth_create_args *args,
  976. struct rpc_clnt *clnt,
  977. struct gss_auth *new)
  978. {
  979. struct gss_auth *gss_auth;
  980. unsigned long hashval = (unsigned long)clnt;
  981. spin_lock(&gss_auth_hash_lock);
  982. hash_for_each_possible(gss_auth_hash_table,
  983. gss_auth,
  984. hash,
  985. hashval) {
  986. if (gss_auth->client != clnt)
  987. continue;
  988. if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
  989. continue;
  990. if (gss_auth->target_name != args->target_name) {
  991. if (gss_auth->target_name == NULL)
  992. continue;
  993. if (args->target_name == NULL)
  994. continue;
  995. if (strcmp(gss_auth->target_name, args->target_name))
  996. continue;
  997. }
  998. if (!atomic_inc_not_zero(&gss_auth->rpc_auth.au_count))
  999. continue;
  1000. goto out;
  1001. }
  1002. if (new)
  1003. hash_add(gss_auth_hash_table, &new->hash, hashval);
  1004. gss_auth = new;
  1005. out:
  1006. spin_unlock(&gss_auth_hash_lock);
  1007. return gss_auth;
  1008. }
  1009. static struct gss_auth *
  1010. gss_create_hashed(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  1011. {
  1012. struct gss_auth *gss_auth;
  1013. struct gss_auth *new;
  1014. gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
  1015. if (gss_auth != NULL)
  1016. goto out;
  1017. new = gss_create_new(args, clnt);
  1018. if (IS_ERR(new))
  1019. return new;
  1020. gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
  1021. if (gss_auth != new)
  1022. gss_destroy(&new->rpc_auth);
  1023. out:
  1024. return gss_auth;
  1025. }
  1026. static struct rpc_auth *
  1027. gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  1028. {
  1029. struct gss_auth *gss_auth;
  1030. struct rpc_xprt *xprt = rcu_access_pointer(clnt->cl_xprt);
  1031. while (clnt != clnt->cl_parent) {
  1032. struct rpc_clnt *parent = clnt->cl_parent;
  1033. /* Find the original parent for this transport */
  1034. if (rcu_access_pointer(parent->cl_xprt) != xprt)
  1035. break;
  1036. clnt = parent;
  1037. }
  1038. gss_auth = gss_create_hashed(args, clnt);
  1039. if (IS_ERR(gss_auth))
  1040. return ERR_CAST(gss_auth);
  1041. return &gss_auth->rpc_auth;
  1042. }
  1043. /*
  1044. * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
  1045. * to the server with the GSS control procedure field set to
  1046. * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
  1047. * all RPCSEC_GSS state associated with that context.
  1048. */
  1049. static int
  1050. gss_destroying_context(struct rpc_cred *cred)
  1051. {
  1052. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  1053. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  1054. struct rpc_task *task;
  1055. if (gss_cred->gc_ctx == NULL ||
  1056. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
  1057. return 0;
  1058. gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
  1059. cred->cr_ops = &gss_nullops;
  1060. /* Take a reference to ensure the cred will be destroyed either
  1061. * by the RPC call or by the put_rpccred() below */
  1062. get_rpccred(cred);
  1063. task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
  1064. if (!IS_ERR(task))
  1065. rpc_put_task(task);
  1066. put_rpccred(cred);
  1067. return 1;
  1068. }
  1069. /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
  1070. * to create a new cred or context, so they check that things have been
  1071. * allocated before freeing them. */
  1072. static void
  1073. gss_do_free_ctx(struct gss_cl_ctx *ctx)
  1074. {
  1075. dprintk("RPC: %s\n", __func__);
  1076. gss_delete_sec_context(&ctx->gc_gss_ctx);
  1077. kfree(ctx->gc_wire_ctx.data);
  1078. kfree(ctx);
  1079. }
  1080. static void
  1081. gss_free_ctx_callback(struct rcu_head *head)
  1082. {
  1083. struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
  1084. gss_do_free_ctx(ctx);
  1085. }
  1086. static void
  1087. gss_free_ctx(struct gss_cl_ctx *ctx)
  1088. {
  1089. call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
  1090. }
  1091. static void
  1092. gss_free_cred(struct gss_cred *gss_cred)
  1093. {
  1094. dprintk("RPC: %s cred=%p\n", __func__, gss_cred);
  1095. kfree(gss_cred);
  1096. }
  1097. static void
  1098. gss_free_cred_callback(struct rcu_head *head)
  1099. {
  1100. struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
  1101. gss_free_cred(gss_cred);
  1102. }
  1103. static void
  1104. gss_destroy_nullcred(struct rpc_cred *cred)
  1105. {
  1106. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  1107. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  1108. struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
  1109. RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
  1110. call_rcu(&cred->cr_rcu, gss_free_cred_callback);
  1111. if (ctx)
  1112. gss_put_ctx(ctx);
  1113. kref_put(&gss_auth->kref, gss_free_callback);
  1114. }
  1115. static void
  1116. gss_destroy_cred(struct rpc_cred *cred)
  1117. {
  1118. if (gss_destroying_context(cred))
  1119. return;
  1120. gss_destroy_nullcred(cred);
  1121. }
  1122. /*
  1123. * Lookup RPCSEC_GSS cred for the current process
  1124. */
  1125. static struct rpc_cred *
  1126. gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  1127. {
  1128. return rpcauth_lookup_credcache(auth, acred, flags);
  1129. }
  1130. static struct rpc_cred *
  1131. gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  1132. {
  1133. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  1134. struct gss_cred *cred = NULL;
  1135. int err = -ENOMEM;
  1136. dprintk("RPC: %s for uid %d, flavor %d\n",
  1137. __func__, from_kuid(&init_user_ns, acred->uid),
  1138. auth->au_flavor);
  1139. if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
  1140. goto out_err;
  1141. rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
  1142. /*
  1143. * Note: in order to force a call to call_refresh(), we deliberately
  1144. * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
  1145. */
  1146. cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
  1147. cred->gc_service = gss_auth->service;
  1148. cred->gc_principal = NULL;
  1149. if (acred->machine_cred)
  1150. cred->gc_principal = acred->principal;
  1151. kref_get(&gss_auth->kref);
  1152. return &cred->gc_base;
  1153. out_err:
  1154. dprintk("RPC: %s failed with error %d\n", __func__, err);
  1155. return ERR_PTR(err);
  1156. }
  1157. static int
  1158. gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
  1159. {
  1160. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  1161. struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
  1162. int err;
  1163. do {
  1164. err = gss_create_upcall(gss_auth, gss_cred);
  1165. } while (err == -EAGAIN);
  1166. return err;
  1167. }
  1168. /*
  1169. * Returns -EACCES if GSS context is NULL or will expire within the
  1170. * timeout (miliseconds)
  1171. */
  1172. static int
  1173. gss_key_timeout(struct rpc_cred *rc)
  1174. {
  1175. struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  1176. unsigned long now = jiffies;
  1177. unsigned long expire;
  1178. if (gss_cred->gc_ctx == NULL)
  1179. return -EACCES;
  1180. expire = gss_cred->gc_ctx->gc_expiry - (gss_key_expire_timeo * HZ);
  1181. if (time_after(now, expire))
  1182. return -EACCES;
  1183. return 0;
  1184. }
  1185. static int
  1186. gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
  1187. {
  1188. struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  1189. int ret;
  1190. if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
  1191. goto out;
  1192. /* Don't match with creds that have expired. */
  1193. if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
  1194. return 0;
  1195. if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
  1196. return 0;
  1197. out:
  1198. if (acred->principal != NULL) {
  1199. if (gss_cred->gc_principal == NULL)
  1200. return 0;
  1201. ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
  1202. goto check_expire;
  1203. }
  1204. if (gss_cred->gc_principal != NULL)
  1205. return 0;
  1206. ret = uid_eq(rc->cr_uid, acred->uid);
  1207. check_expire:
  1208. if (ret == 0)
  1209. return ret;
  1210. /* Notify acred users of GSS context expiration timeout */
  1211. if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags) &&
  1212. (gss_key_timeout(rc) != 0)) {
  1213. /* test will now be done from generic cred */
  1214. test_and_clear_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
  1215. /* tell NFS layer that key will expire soon */
  1216. set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  1217. }
  1218. return ret;
  1219. }
  1220. /*
  1221. * Marshal credentials.
  1222. * Maybe we should keep a cached credential for performance reasons.
  1223. */
  1224. static __be32 *
  1225. gss_marshal(struct rpc_task *task, __be32 *p)
  1226. {
  1227. struct rpc_rqst *req = task->tk_rqstp;
  1228. struct rpc_cred *cred = req->rq_cred;
  1229. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1230. gc_base);
  1231. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1232. __be32 *cred_len;
  1233. u32 maj_stat = 0;
  1234. struct xdr_netobj mic;
  1235. struct kvec iov;
  1236. struct xdr_buf verf_buf;
  1237. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1238. *p++ = htonl(RPC_AUTH_GSS);
  1239. cred_len = p++;
  1240. spin_lock(&ctx->gc_seq_lock);
  1241. req->rq_seqno = ctx->gc_seq++;
  1242. spin_unlock(&ctx->gc_seq_lock);
  1243. *p++ = htonl((u32) RPC_GSS_VERSION);
  1244. *p++ = htonl((u32) ctx->gc_proc);
  1245. *p++ = htonl((u32) req->rq_seqno);
  1246. *p++ = htonl((u32) gss_cred->gc_service);
  1247. p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
  1248. *cred_len = htonl((p - (cred_len + 1)) << 2);
  1249. /* We compute the checksum for the verifier over the xdr-encoded bytes
  1250. * starting with the xid and ending at the end of the credential: */
  1251. iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
  1252. req->rq_snd_buf.head[0].iov_base);
  1253. iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
  1254. xdr_buf_from_iov(&iov, &verf_buf);
  1255. /* set verifier flavor*/
  1256. *p++ = htonl(RPC_AUTH_GSS);
  1257. mic.data = (u8 *)(p + 1);
  1258. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1259. if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
  1260. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1261. } else if (maj_stat != 0) {
  1262. printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
  1263. goto out_put_ctx;
  1264. }
  1265. p = xdr_encode_opaque(p, NULL, mic.len);
  1266. gss_put_ctx(ctx);
  1267. return p;
  1268. out_put_ctx:
  1269. gss_put_ctx(ctx);
  1270. return NULL;
  1271. }
  1272. static int gss_renew_cred(struct rpc_task *task)
  1273. {
  1274. struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
  1275. struct gss_cred *gss_cred = container_of(oldcred,
  1276. struct gss_cred,
  1277. gc_base);
  1278. struct rpc_auth *auth = oldcred->cr_auth;
  1279. struct auth_cred acred = {
  1280. .uid = oldcred->cr_uid,
  1281. .principal = gss_cred->gc_principal,
  1282. .machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
  1283. };
  1284. struct rpc_cred *new;
  1285. new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
  1286. if (IS_ERR(new))
  1287. return PTR_ERR(new);
  1288. task->tk_rqstp->rq_cred = new;
  1289. put_rpccred(oldcred);
  1290. return 0;
  1291. }
  1292. static int gss_cred_is_negative_entry(struct rpc_cred *cred)
  1293. {
  1294. if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
  1295. unsigned long now = jiffies;
  1296. unsigned long begin, expire;
  1297. struct gss_cred *gss_cred;
  1298. gss_cred = container_of(cred, struct gss_cred, gc_base);
  1299. begin = gss_cred->gc_upcall_timestamp;
  1300. expire = begin + gss_expired_cred_retry_delay * HZ;
  1301. if (time_in_range_open(now, begin, expire))
  1302. return 1;
  1303. }
  1304. return 0;
  1305. }
  1306. /*
  1307. * Refresh credentials. XXX - finish
  1308. */
  1309. static int
  1310. gss_refresh(struct rpc_task *task)
  1311. {
  1312. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1313. int ret = 0;
  1314. if (gss_cred_is_negative_entry(cred))
  1315. return -EKEYEXPIRED;
  1316. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  1317. !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
  1318. ret = gss_renew_cred(task);
  1319. if (ret < 0)
  1320. goto out;
  1321. cred = task->tk_rqstp->rq_cred;
  1322. }
  1323. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  1324. ret = gss_refresh_upcall(task);
  1325. out:
  1326. return ret;
  1327. }
  1328. /* Dummy refresh routine: used only when destroying the context */
  1329. static int
  1330. gss_refresh_null(struct rpc_task *task)
  1331. {
  1332. return -EACCES;
  1333. }
  1334. static __be32 *
  1335. gss_validate(struct rpc_task *task, __be32 *p)
  1336. {
  1337. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1338. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1339. __be32 seq;
  1340. struct kvec iov;
  1341. struct xdr_buf verf_buf;
  1342. struct xdr_netobj mic;
  1343. u32 flav,len;
  1344. u32 maj_stat;
  1345. __be32 *ret = ERR_PTR(-EIO);
  1346. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1347. flav = ntohl(*p++);
  1348. if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
  1349. goto out_bad;
  1350. if (flav != RPC_AUTH_GSS)
  1351. goto out_bad;
  1352. seq = htonl(task->tk_rqstp->rq_seqno);
  1353. iov.iov_base = &seq;
  1354. iov.iov_len = sizeof(seq);
  1355. xdr_buf_from_iov(&iov, &verf_buf);
  1356. mic.data = (u8 *)p;
  1357. mic.len = len;
  1358. ret = ERR_PTR(-EACCES);
  1359. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1360. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1361. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1362. if (maj_stat) {
  1363. dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
  1364. task->tk_pid, __func__, maj_stat);
  1365. goto out_bad;
  1366. }
  1367. /* We leave it to unwrap to calculate au_rslack. For now we just
  1368. * calculate the length of the verifier: */
  1369. cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
  1370. gss_put_ctx(ctx);
  1371. dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
  1372. task->tk_pid, __func__);
  1373. return p + XDR_QUADLEN(len);
  1374. out_bad:
  1375. gss_put_ctx(ctx);
  1376. dprintk("RPC: %5u %s failed ret %ld.\n", task->tk_pid, __func__,
  1377. PTR_ERR(ret));
  1378. return ret;
  1379. }
  1380. static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  1381. __be32 *p, void *obj)
  1382. {
  1383. struct xdr_stream xdr;
  1384. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
  1385. encode(rqstp, &xdr, obj);
  1386. }
  1387. static inline int
  1388. gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1389. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1390. __be32 *p, void *obj)
  1391. {
  1392. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1393. struct xdr_buf integ_buf;
  1394. __be32 *integ_len = NULL;
  1395. struct xdr_netobj mic;
  1396. u32 offset;
  1397. __be32 *q;
  1398. struct kvec *iov;
  1399. u32 maj_stat = 0;
  1400. int status = -EIO;
  1401. integ_len = p++;
  1402. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1403. *p++ = htonl(rqstp->rq_seqno);
  1404. gss_wrap_req_encode(encode, rqstp, p, obj);
  1405. if (xdr_buf_subsegment(snd_buf, &integ_buf,
  1406. offset, snd_buf->len - offset))
  1407. return status;
  1408. *integ_len = htonl(integ_buf.len);
  1409. /* guess whether we're in the head or the tail: */
  1410. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1411. iov = snd_buf->tail;
  1412. else
  1413. iov = snd_buf->head;
  1414. p = iov->iov_base + iov->iov_len;
  1415. mic.data = (u8 *)(p + 1);
  1416. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1417. status = -EIO; /* XXX? */
  1418. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1419. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1420. else if (maj_stat)
  1421. return status;
  1422. q = xdr_encode_opaque(p, NULL, mic.len);
  1423. offset = (u8 *)q - (u8 *)p;
  1424. iov->iov_len += offset;
  1425. snd_buf->len += offset;
  1426. return 0;
  1427. }
  1428. static void
  1429. priv_release_snd_buf(struct rpc_rqst *rqstp)
  1430. {
  1431. int i;
  1432. for (i=0; i < rqstp->rq_enc_pages_num; i++)
  1433. __free_page(rqstp->rq_enc_pages[i]);
  1434. kfree(rqstp->rq_enc_pages);
  1435. }
  1436. static int
  1437. alloc_enc_pages(struct rpc_rqst *rqstp)
  1438. {
  1439. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1440. int first, last, i;
  1441. if (snd_buf->page_len == 0) {
  1442. rqstp->rq_enc_pages_num = 0;
  1443. return 0;
  1444. }
  1445. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1446. last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
  1447. rqstp->rq_enc_pages_num = last - first + 1 + 1;
  1448. rqstp->rq_enc_pages
  1449. = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
  1450. GFP_NOFS);
  1451. if (!rqstp->rq_enc_pages)
  1452. goto out;
  1453. for (i=0; i < rqstp->rq_enc_pages_num; i++) {
  1454. rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
  1455. if (rqstp->rq_enc_pages[i] == NULL)
  1456. goto out_free;
  1457. }
  1458. rqstp->rq_release_snd_buf = priv_release_snd_buf;
  1459. return 0;
  1460. out_free:
  1461. rqstp->rq_enc_pages_num = i;
  1462. priv_release_snd_buf(rqstp);
  1463. out:
  1464. return -EAGAIN;
  1465. }
  1466. static inline int
  1467. gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1468. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1469. __be32 *p, void *obj)
  1470. {
  1471. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1472. u32 offset;
  1473. u32 maj_stat;
  1474. int status;
  1475. __be32 *opaque_len;
  1476. struct page **inpages;
  1477. int first;
  1478. int pad;
  1479. struct kvec *iov;
  1480. char *tmp;
  1481. opaque_len = p++;
  1482. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1483. *p++ = htonl(rqstp->rq_seqno);
  1484. gss_wrap_req_encode(encode, rqstp, p, obj);
  1485. status = alloc_enc_pages(rqstp);
  1486. if (status)
  1487. return status;
  1488. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1489. inpages = snd_buf->pages + first;
  1490. snd_buf->pages = rqstp->rq_enc_pages;
  1491. snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
  1492. /*
  1493. * Give the tail its own page, in case we need extra space in the
  1494. * head when wrapping:
  1495. *
  1496. * call_allocate() allocates twice the slack space required
  1497. * by the authentication flavor to rq_callsize.
  1498. * For GSS, slack is GSS_CRED_SLACK.
  1499. */
  1500. if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
  1501. tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
  1502. memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
  1503. snd_buf->tail[0].iov_base = tmp;
  1504. }
  1505. maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
  1506. /* slack space should prevent this ever happening: */
  1507. BUG_ON(snd_buf->len > snd_buf->buflen);
  1508. status = -EIO;
  1509. /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
  1510. * done anyway, so it's safe to put the request on the wire: */
  1511. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1512. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1513. else if (maj_stat)
  1514. return status;
  1515. *opaque_len = htonl(snd_buf->len - offset);
  1516. /* guess whether we're in the head or the tail: */
  1517. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1518. iov = snd_buf->tail;
  1519. else
  1520. iov = snd_buf->head;
  1521. p = iov->iov_base + iov->iov_len;
  1522. pad = 3 - ((snd_buf->len - offset - 1) & 3);
  1523. memset(p, 0, pad);
  1524. iov->iov_len += pad;
  1525. snd_buf->len += pad;
  1526. return 0;
  1527. }
  1528. static int
  1529. gss_wrap_req(struct rpc_task *task,
  1530. kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
  1531. {
  1532. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1533. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1534. gc_base);
  1535. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1536. int status = -EIO;
  1537. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1538. if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
  1539. /* The spec seems a little ambiguous here, but I think that not
  1540. * wrapping context destruction requests makes the most sense.
  1541. */
  1542. gss_wrap_req_encode(encode, rqstp, p, obj);
  1543. status = 0;
  1544. goto out;
  1545. }
  1546. switch (gss_cred->gc_service) {
  1547. case RPC_GSS_SVC_NONE:
  1548. gss_wrap_req_encode(encode, rqstp, p, obj);
  1549. status = 0;
  1550. break;
  1551. case RPC_GSS_SVC_INTEGRITY:
  1552. status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
  1553. break;
  1554. case RPC_GSS_SVC_PRIVACY:
  1555. status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
  1556. break;
  1557. }
  1558. out:
  1559. gss_put_ctx(ctx);
  1560. dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
  1561. return status;
  1562. }
  1563. static inline int
  1564. gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1565. struct rpc_rqst *rqstp, __be32 **p)
  1566. {
  1567. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1568. struct xdr_buf integ_buf;
  1569. struct xdr_netobj mic;
  1570. u32 data_offset, mic_offset;
  1571. u32 integ_len;
  1572. u32 maj_stat;
  1573. int status = -EIO;
  1574. integ_len = ntohl(*(*p)++);
  1575. if (integ_len & 3)
  1576. return status;
  1577. data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1578. mic_offset = integ_len + data_offset;
  1579. if (mic_offset > rcv_buf->len)
  1580. return status;
  1581. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1582. return status;
  1583. if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
  1584. mic_offset - data_offset))
  1585. return status;
  1586. if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
  1587. return status;
  1588. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1589. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1590. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1591. if (maj_stat != GSS_S_COMPLETE)
  1592. return status;
  1593. return 0;
  1594. }
  1595. static inline int
  1596. gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1597. struct rpc_rqst *rqstp, __be32 **p)
  1598. {
  1599. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1600. u32 offset;
  1601. u32 opaque_len;
  1602. u32 maj_stat;
  1603. int status = -EIO;
  1604. opaque_len = ntohl(*(*p)++);
  1605. offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1606. if (offset + opaque_len > rcv_buf->len)
  1607. return status;
  1608. /* remove padding: */
  1609. rcv_buf->len = offset + opaque_len;
  1610. maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
  1611. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1612. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1613. if (maj_stat != GSS_S_COMPLETE)
  1614. return status;
  1615. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1616. return status;
  1617. return 0;
  1618. }
  1619. static int
  1620. gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  1621. __be32 *p, void *obj)
  1622. {
  1623. struct xdr_stream xdr;
  1624. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  1625. return decode(rqstp, &xdr, obj);
  1626. }
  1627. static int
  1628. gss_unwrap_resp(struct rpc_task *task,
  1629. kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
  1630. {
  1631. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1632. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1633. gc_base);
  1634. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1635. __be32 *savedp = p;
  1636. struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
  1637. int savedlen = head->iov_len;
  1638. int status = -EIO;
  1639. if (ctx->gc_proc != RPC_GSS_PROC_DATA)
  1640. goto out_decode;
  1641. switch (gss_cred->gc_service) {
  1642. case RPC_GSS_SVC_NONE:
  1643. break;
  1644. case RPC_GSS_SVC_INTEGRITY:
  1645. status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
  1646. if (status)
  1647. goto out;
  1648. break;
  1649. case RPC_GSS_SVC_PRIVACY:
  1650. status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
  1651. if (status)
  1652. goto out;
  1653. break;
  1654. }
  1655. /* take into account extra slack for integrity and privacy cases: */
  1656. cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
  1657. + (savedlen - head->iov_len);
  1658. out_decode:
  1659. status = gss_unwrap_req_decode(decode, rqstp, p, obj);
  1660. out:
  1661. gss_put_ctx(ctx);
  1662. dprintk("RPC: %5u %s returning %d\n",
  1663. task->tk_pid, __func__, status);
  1664. return status;
  1665. }
  1666. static const struct rpc_authops authgss_ops = {
  1667. .owner = THIS_MODULE,
  1668. .au_flavor = RPC_AUTH_GSS,
  1669. .au_name = "RPCSEC_GSS",
  1670. .create = gss_create,
  1671. .destroy = gss_destroy,
  1672. .lookup_cred = gss_lookup_cred,
  1673. .crcreate = gss_create_cred,
  1674. .list_pseudoflavors = gss_mech_list_pseudoflavors,
  1675. .info2flavor = gss_mech_info2flavor,
  1676. .flavor2info = gss_mech_flavor2info,
  1677. };
  1678. static const struct rpc_credops gss_credops = {
  1679. .cr_name = "AUTH_GSS",
  1680. .crdestroy = gss_destroy_cred,
  1681. .cr_init = gss_cred_init,
  1682. .crbind = rpcauth_generic_bind_cred,
  1683. .crmatch = gss_match,
  1684. .crmarshal = gss_marshal,
  1685. .crrefresh = gss_refresh,
  1686. .crvalidate = gss_validate,
  1687. .crwrap_req = gss_wrap_req,
  1688. .crunwrap_resp = gss_unwrap_resp,
  1689. .crkey_timeout = gss_key_timeout,
  1690. };
  1691. static const struct rpc_credops gss_nullops = {
  1692. .cr_name = "AUTH_GSS",
  1693. .crdestroy = gss_destroy_nullcred,
  1694. .crbind = rpcauth_generic_bind_cred,
  1695. .crmatch = gss_match,
  1696. .crmarshal = gss_marshal,
  1697. .crrefresh = gss_refresh_null,
  1698. .crvalidate = gss_validate,
  1699. .crwrap_req = gss_wrap_req,
  1700. .crunwrap_resp = gss_unwrap_resp,
  1701. };
  1702. static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
  1703. .upcall = rpc_pipe_generic_upcall,
  1704. .downcall = gss_pipe_downcall,
  1705. .destroy_msg = gss_pipe_destroy_msg,
  1706. .open_pipe = gss_pipe_open_v0,
  1707. .release_pipe = gss_pipe_release,
  1708. };
  1709. static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
  1710. .upcall = rpc_pipe_generic_upcall,
  1711. .downcall = gss_pipe_downcall,
  1712. .destroy_msg = gss_pipe_destroy_msg,
  1713. .open_pipe = gss_pipe_open_v1,
  1714. .release_pipe = gss_pipe_release,
  1715. };
  1716. static __net_init int rpcsec_gss_init_net(struct net *net)
  1717. {
  1718. return gss_svc_init_net(net);
  1719. }
  1720. static __net_exit void rpcsec_gss_exit_net(struct net *net)
  1721. {
  1722. gss_svc_shutdown_net(net);
  1723. }
  1724. static struct pernet_operations rpcsec_gss_net_ops = {
  1725. .init = rpcsec_gss_init_net,
  1726. .exit = rpcsec_gss_exit_net,
  1727. };
  1728. /*
  1729. * Initialize RPCSEC_GSS module
  1730. */
  1731. static int __init init_rpcsec_gss(void)
  1732. {
  1733. int err = 0;
  1734. err = rpcauth_register(&authgss_ops);
  1735. if (err)
  1736. goto out;
  1737. err = gss_svc_init();
  1738. if (err)
  1739. goto out_unregister;
  1740. err = register_pernet_subsys(&rpcsec_gss_net_ops);
  1741. if (err)
  1742. goto out_svc_exit;
  1743. rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
  1744. return 0;
  1745. out_svc_exit:
  1746. gss_svc_shutdown();
  1747. out_unregister:
  1748. rpcauth_unregister(&authgss_ops);
  1749. out:
  1750. return err;
  1751. }
  1752. static void __exit exit_rpcsec_gss(void)
  1753. {
  1754. unregister_pernet_subsys(&rpcsec_gss_net_ops);
  1755. gss_svc_shutdown();
  1756. rpcauth_unregister(&authgss_ops);
  1757. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1758. }
  1759. MODULE_ALIAS("rpc-auth-6");
  1760. MODULE_LICENSE("GPL");
  1761. module_param_named(expired_cred_retry_delay,
  1762. gss_expired_cred_retry_delay,
  1763. uint, 0644);
  1764. MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
  1765. "the RPC engine retries an expired credential");
  1766. module_param_named(key_expire_timeo,
  1767. gss_key_expire_timeo,
  1768. uint, 0644);
  1769. MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
  1770. "credential keys lifetime where the NFS layer cleans up "
  1771. "prior to key expiration");
  1772. module_init(init_rpcsec_gss)
  1773. module_exit(exit_rpcsec_gss)