auth_gss.c 54 KB

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