auth_gss.c 51 KB

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