auth_gss.c 54 KB

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