auth_gss.c 51 KB

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