auth_gss.c 57 KB

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