auth_gss.c 53 KB

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