auth_gss.c 55 KB

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