auth_gss.c 53 KB

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