auth_gss.c 53 KB

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