auth_gss.c 50 KB

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