auth_gss.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. * $Id$
  38. */
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/types.h>
  42. #include <linux/slab.h>
  43. #include <linux/sched.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/sunrpc/clnt.h>
  46. #include <linux/sunrpc/auth.h>
  47. #include <linux/sunrpc/auth_gss.h>
  48. #include <linux/sunrpc/svcauth_gss.h>
  49. #include <linux/sunrpc/gss_err.h>
  50. #include <linux/workqueue.h>
  51. #include <linux/sunrpc/rpc_pipe_fs.h>
  52. #include <linux/sunrpc/gss_api.h>
  53. #include <asm/uaccess.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. #ifdef RPC_DEBUG
  58. # define RPCDBG_FACILITY RPCDBG_AUTH
  59. #endif
  60. #define NFS_NGROUPS 16
  61. #define GSS_CRED_SLACK 1024 /* XXX: unused */
  62. /* length of a krb5 verifier (48), plus data added before arguments when
  63. * using integrity (two 4-byte integers): */
  64. #define GSS_VERF_SLACK 100
  65. /* XXX this define must match the gssd define
  66. * as it is passed to gssd to signal the use of
  67. * machine creds should be part of the shared rpc interface */
  68. #define CA_RUN_AS_MACHINE 0x00000200
  69. /* dump the buffer in `emacs-hexl' style */
  70. #define isprint(c) ((c > 0x1f) && (c < 0x7f))
  71. struct gss_auth {
  72. struct kref kref;
  73. struct rpc_auth rpc_auth;
  74. struct gss_api_mech *mech;
  75. enum rpc_gss_svc service;
  76. struct rpc_clnt *client;
  77. struct dentry *dentry;
  78. };
  79. static void gss_free_ctx(struct gss_cl_ctx *);
  80. static struct rpc_pipe_ops gss_upcall_ops;
  81. static inline struct gss_cl_ctx *
  82. gss_get_ctx(struct gss_cl_ctx *ctx)
  83. {
  84. atomic_inc(&ctx->count);
  85. return ctx;
  86. }
  87. static inline void
  88. gss_put_ctx(struct gss_cl_ctx *ctx)
  89. {
  90. if (atomic_dec_and_test(&ctx->count))
  91. gss_free_ctx(ctx);
  92. }
  93. /* gss_cred_set_ctx:
  94. * called by gss_upcall_callback and gss_create_upcall in order
  95. * to set the gss context. The actual exchange of an old context
  96. * and a new one is protected by the inode->i_lock.
  97. */
  98. static void
  99. gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
  100. {
  101. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  102. struct gss_cl_ctx *old;
  103. old = gss_cred->gc_ctx;
  104. rcu_assign_pointer(gss_cred->gc_ctx, ctx);
  105. set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  106. clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
  107. if (old)
  108. gss_put_ctx(old);
  109. }
  110. static int
  111. gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
  112. {
  113. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  114. int res = 0;
  115. rcu_read_lock();
  116. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) && gss_cred->gc_ctx)
  117. res = 1;
  118. rcu_read_unlock();
  119. return res;
  120. }
  121. static const void *
  122. simple_get_bytes(const void *p, const void *end, void *res, size_t len)
  123. {
  124. const void *q = (const void *)((const char *)p + len);
  125. if (unlikely(q > end || q < p))
  126. return ERR_PTR(-EFAULT);
  127. memcpy(res, p, len);
  128. return q;
  129. }
  130. static inline const void *
  131. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
  132. {
  133. const void *q;
  134. unsigned int len;
  135. p = simple_get_bytes(p, end, &len, sizeof(len));
  136. if (IS_ERR(p))
  137. return p;
  138. q = (const void *)((const char *)p + len);
  139. if (unlikely(q > end || q < p))
  140. return ERR_PTR(-EFAULT);
  141. dest->data = kmemdup(p, len, GFP_KERNEL);
  142. if (unlikely(dest->data == NULL))
  143. return ERR_PTR(-ENOMEM);
  144. dest->len = len;
  145. return q;
  146. }
  147. static struct gss_cl_ctx *
  148. gss_cred_get_ctx(struct rpc_cred *cred)
  149. {
  150. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  151. struct gss_cl_ctx *ctx = NULL;
  152. rcu_read_lock();
  153. if (gss_cred->gc_ctx)
  154. ctx = gss_get_ctx(gss_cred->gc_ctx);
  155. rcu_read_unlock();
  156. return ctx;
  157. }
  158. static struct gss_cl_ctx *
  159. gss_alloc_context(void)
  160. {
  161. struct gss_cl_ctx *ctx;
  162. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  163. if (ctx != NULL) {
  164. ctx->gc_proc = RPC_GSS_PROC_DATA;
  165. ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
  166. spin_lock_init(&ctx->gc_seq_lock);
  167. atomic_set(&ctx->count,1);
  168. }
  169. return ctx;
  170. }
  171. #define GSSD_MIN_TIMEOUT (60 * 60)
  172. static const void *
  173. gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
  174. {
  175. const void *q;
  176. unsigned int seclen;
  177. unsigned int timeout;
  178. u32 window_size;
  179. int ret;
  180. /* First unsigned int gives the lifetime (in seconds) of the cred */
  181. p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
  182. if (IS_ERR(p))
  183. goto err;
  184. if (timeout == 0)
  185. timeout = GSSD_MIN_TIMEOUT;
  186. ctx->gc_expiry = jiffies + (unsigned long)timeout * HZ * 3 / 4;
  187. /* Sequence number window. Determines the maximum number of simultaneous requests */
  188. p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
  189. if (IS_ERR(p))
  190. goto err;
  191. ctx->gc_win = window_size;
  192. /* gssd signals an error by passing ctx->gc_win = 0: */
  193. if (ctx->gc_win == 0) {
  194. /* in which case, p points to an error code which we ignore */
  195. p = ERR_PTR(-EACCES);
  196. goto err;
  197. }
  198. /* copy the opaque wire context */
  199. p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
  200. if (IS_ERR(p))
  201. goto err;
  202. /* import the opaque security context */
  203. p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
  204. if (IS_ERR(p))
  205. goto err;
  206. q = (const void *)((const char *)p + seclen);
  207. if (unlikely(q > end || q < p)) {
  208. p = ERR_PTR(-EFAULT);
  209. goto err;
  210. }
  211. ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx);
  212. if (ret < 0) {
  213. p = ERR_PTR(ret);
  214. goto err;
  215. }
  216. return q;
  217. err:
  218. dprintk("RPC: gss_fill_context returning %ld\n", -PTR_ERR(p));
  219. return p;
  220. }
  221. struct gss_upcall_msg {
  222. atomic_t count;
  223. uid_t uid;
  224. struct rpc_pipe_msg msg;
  225. struct list_head list;
  226. struct gss_auth *auth;
  227. struct rpc_wait_queue rpc_waitqueue;
  228. wait_queue_head_t waitqueue;
  229. struct gss_cl_ctx *ctx;
  230. };
  231. static void
  232. gss_release_msg(struct gss_upcall_msg *gss_msg)
  233. {
  234. if (!atomic_dec_and_test(&gss_msg->count))
  235. return;
  236. BUG_ON(!list_empty(&gss_msg->list));
  237. if (gss_msg->ctx != NULL)
  238. gss_put_ctx(gss_msg->ctx);
  239. kfree(gss_msg);
  240. }
  241. static struct gss_upcall_msg *
  242. __gss_find_upcall(struct rpc_inode *rpci, uid_t uid)
  243. {
  244. struct gss_upcall_msg *pos;
  245. list_for_each_entry(pos, &rpci->in_downcall, list) {
  246. if (pos->uid != uid)
  247. continue;
  248. atomic_inc(&pos->count);
  249. dprintk("RPC: gss_find_upcall found msg %p\n", pos);
  250. return pos;
  251. }
  252. dprintk("RPC: gss_find_upcall found nothing\n");
  253. return NULL;
  254. }
  255. /* Try to add a upcall to the pipefs queue.
  256. * If an upcall owned by our uid already exists, then we return a reference
  257. * to that upcall instead of adding the new upcall.
  258. */
  259. static inline struct gss_upcall_msg *
  260. gss_add_msg(struct gss_auth *gss_auth, struct gss_upcall_msg *gss_msg)
  261. {
  262. struct inode *inode = gss_auth->dentry->d_inode;
  263. struct rpc_inode *rpci = RPC_I(inode);
  264. struct gss_upcall_msg *old;
  265. spin_lock(&inode->i_lock);
  266. old = __gss_find_upcall(rpci, gss_msg->uid);
  267. if (old == NULL) {
  268. atomic_inc(&gss_msg->count);
  269. list_add(&gss_msg->list, &rpci->in_downcall);
  270. } else
  271. gss_msg = old;
  272. spin_unlock(&inode->i_lock);
  273. return gss_msg;
  274. }
  275. static void
  276. __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  277. {
  278. list_del_init(&gss_msg->list);
  279. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  280. wake_up_all(&gss_msg->waitqueue);
  281. atomic_dec(&gss_msg->count);
  282. }
  283. static void
  284. gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  285. {
  286. struct gss_auth *gss_auth = gss_msg->auth;
  287. struct inode *inode = gss_auth->dentry->d_inode;
  288. if (list_empty(&gss_msg->list))
  289. return;
  290. spin_lock(&inode->i_lock);
  291. if (!list_empty(&gss_msg->list))
  292. __gss_unhash_msg(gss_msg);
  293. spin_unlock(&inode->i_lock);
  294. }
  295. static void
  296. gss_upcall_callback(struct rpc_task *task)
  297. {
  298. struct gss_cred *gss_cred = container_of(task->tk_msg.rpc_cred,
  299. struct gss_cred, gc_base);
  300. struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
  301. struct inode *inode = gss_msg->auth->dentry->d_inode;
  302. spin_lock(&inode->i_lock);
  303. if (gss_msg->ctx)
  304. gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_get_ctx(gss_msg->ctx));
  305. else
  306. task->tk_status = gss_msg->msg.errno;
  307. gss_cred->gc_upcall = NULL;
  308. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  309. spin_unlock(&inode->i_lock);
  310. gss_release_msg(gss_msg);
  311. }
  312. static inline struct gss_upcall_msg *
  313. gss_alloc_msg(struct gss_auth *gss_auth, uid_t uid)
  314. {
  315. struct gss_upcall_msg *gss_msg;
  316. gss_msg = kzalloc(sizeof(*gss_msg), GFP_KERNEL);
  317. if (gss_msg != NULL) {
  318. INIT_LIST_HEAD(&gss_msg->list);
  319. rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
  320. init_waitqueue_head(&gss_msg->waitqueue);
  321. atomic_set(&gss_msg->count, 1);
  322. gss_msg->msg.data = &gss_msg->uid;
  323. gss_msg->msg.len = sizeof(gss_msg->uid);
  324. gss_msg->uid = uid;
  325. gss_msg->auth = gss_auth;
  326. }
  327. return gss_msg;
  328. }
  329. static struct gss_upcall_msg *
  330. gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
  331. {
  332. struct gss_upcall_msg *gss_new, *gss_msg;
  333. gss_new = gss_alloc_msg(gss_auth, cred->cr_uid);
  334. if (gss_new == NULL)
  335. return ERR_PTR(-ENOMEM);
  336. gss_msg = gss_add_msg(gss_auth, gss_new);
  337. if (gss_msg == gss_new) {
  338. int res = rpc_queue_upcall(gss_auth->dentry->d_inode, &gss_new->msg);
  339. if (res) {
  340. gss_unhash_msg(gss_new);
  341. gss_msg = ERR_PTR(res);
  342. }
  343. } else
  344. gss_release_msg(gss_new);
  345. return gss_msg;
  346. }
  347. static inline int
  348. gss_refresh_upcall(struct rpc_task *task)
  349. {
  350. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  351. struct gss_auth *gss_auth = container_of(cred->cr_auth,
  352. struct gss_auth, rpc_auth);
  353. struct gss_cred *gss_cred = container_of(cred,
  354. struct gss_cred, gc_base);
  355. struct gss_upcall_msg *gss_msg;
  356. struct inode *inode = gss_auth->dentry->d_inode;
  357. int err = 0;
  358. dprintk("RPC: %5u gss_refresh_upcall for uid %u\n", task->tk_pid,
  359. cred->cr_uid);
  360. gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
  361. if (IS_ERR(gss_msg)) {
  362. err = PTR_ERR(gss_msg);
  363. goto out;
  364. }
  365. spin_lock(&inode->i_lock);
  366. if (gss_cred->gc_upcall != NULL)
  367. rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL, NULL);
  368. else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
  369. task->tk_timeout = 0;
  370. gss_cred->gc_upcall = gss_msg;
  371. /* gss_upcall_callback will release the reference to gss_upcall_msg */
  372. atomic_inc(&gss_msg->count);
  373. rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback, NULL);
  374. } else
  375. err = gss_msg->msg.errno;
  376. spin_unlock(&inode->i_lock);
  377. gss_release_msg(gss_msg);
  378. out:
  379. dprintk("RPC: %5u gss_refresh_upcall for uid %u result %d\n",
  380. task->tk_pid, cred->cr_uid, err);
  381. return err;
  382. }
  383. static inline int
  384. gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
  385. {
  386. struct inode *inode = gss_auth->dentry->d_inode;
  387. struct rpc_cred *cred = &gss_cred->gc_base;
  388. struct gss_upcall_msg *gss_msg;
  389. DEFINE_WAIT(wait);
  390. int err = 0;
  391. dprintk("RPC: gss_upcall for uid %u\n", cred->cr_uid);
  392. gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
  393. if (IS_ERR(gss_msg)) {
  394. err = PTR_ERR(gss_msg);
  395. goto out;
  396. }
  397. for (;;) {
  398. prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_INTERRUPTIBLE);
  399. spin_lock(&inode->i_lock);
  400. if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
  401. break;
  402. }
  403. spin_unlock(&inode->i_lock);
  404. if (signalled()) {
  405. err = -ERESTARTSYS;
  406. goto out_intr;
  407. }
  408. schedule();
  409. }
  410. if (gss_msg->ctx)
  411. gss_cred_set_ctx(cred, gss_get_ctx(gss_msg->ctx));
  412. else
  413. err = gss_msg->msg.errno;
  414. spin_unlock(&inode->i_lock);
  415. out_intr:
  416. finish_wait(&gss_msg->waitqueue, &wait);
  417. gss_release_msg(gss_msg);
  418. out:
  419. dprintk("RPC: gss_create_upcall for uid %u result %d\n",
  420. cred->cr_uid, err);
  421. return err;
  422. }
  423. static ssize_t
  424. gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  425. char __user *dst, size_t buflen)
  426. {
  427. char *data = (char *)msg->data + msg->copied;
  428. ssize_t mlen = msg->len;
  429. ssize_t left;
  430. if (mlen > buflen)
  431. mlen = buflen;
  432. left = copy_to_user(dst, data, mlen);
  433. if (left < 0) {
  434. msg->errno = left;
  435. return left;
  436. }
  437. mlen -= left;
  438. msg->copied += mlen;
  439. msg->errno = 0;
  440. return mlen;
  441. }
  442. #define MSG_BUF_MAXSIZE 1024
  443. static ssize_t
  444. gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  445. {
  446. const void *p, *end;
  447. void *buf;
  448. struct rpc_clnt *clnt;
  449. struct gss_upcall_msg *gss_msg;
  450. struct inode *inode = filp->f_path.dentry->d_inode;
  451. struct gss_cl_ctx *ctx;
  452. uid_t uid;
  453. ssize_t err = -EFBIG;
  454. if (mlen > MSG_BUF_MAXSIZE)
  455. goto out;
  456. err = -ENOMEM;
  457. buf = kmalloc(mlen, GFP_KERNEL);
  458. if (!buf)
  459. goto out;
  460. clnt = RPC_I(inode)->private;
  461. err = -EFAULT;
  462. if (copy_from_user(buf, src, mlen))
  463. goto err;
  464. end = (const void *)((char *)buf + mlen);
  465. p = simple_get_bytes(buf, end, &uid, sizeof(uid));
  466. if (IS_ERR(p)) {
  467. err = PTR_ERR(p);
  468. goto err;
  469. }
  470. err = -ENOMEM;
  471. ctx = gss_alloc_context();
  472. if (ctx == NULL)
  473. goto err;
  474. err = -ENOENT;
  475. /* Find a matching upcall */
  476. spin_lock(&inode->i_lock);
  477. gss_msg = __gss_find_upcall(RPC_I(inode), uid);
  478. if (gss_msg == NULL) {
  479. spin_unlock(&inode->i_lock);
  480. goto err_put_ctx;
  481. }
  482. list_del_init(&gss_msg->list);
  483. spin_unlock(&inode->i_lock);
  484. p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
  485. if (IS_ERR(p)) {
  486. err = PTR_ERR(p);
  487. gss_msg->msg.errno = (err == -EACCES) ? -EACCES : -EAGAIN;
  488. goto err_release_msg;
  489. }
  490. gss_msg->ctx = gss_get_ctx(ctx);
  491. err = mlen;
  492. err_release_msg:
  493. spin_lock(&inode->i_lock);
  494. __gss_unhash_msg(gss_msg);
  495. spin_unlock(&inode->i_lock);
  496. gss_release_msg(gss_msg);
  497. err_put_ctx:
  498. gss_put_ctx(ctx);
  499. err:
  500. kfree(buf);
  501. out:
  502. dprintk("RPC: gss_pipe_downcall returning %Zd\n", err);
  503. return err;
  504. }
  505. static void
  506. gss_pipe_release(struct inode *inode)
  507. {
  508. struct rpc_inode *rpci = RPC_I(inode);
  509. struct gss_upcall_msg *gss_msg;
  510. spin_lock(&inode->i_lock);
  511. while (!list_empty(&rpci->in_downcall)) {
  512. gss_msg = list_entry(rpci->in_downcall.next,
  513. struct gss_upcall_msg, list);
  514. gss_msg->msg.errno = -EPIPE;
  515. atomic_inc(&gss_msg->count);
  516. __gss_unhash_msg(gss_msg);
  517. spin_unlock(&inode->i_lock);
  518. gss_release_msg(gss_msg);
  519. spin_lock(&inode->i_lock);
  520. }
  521. spin_unlock(&inode->i_lock);
  522. }
  523. static void
  524. gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  525. {
  526. struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
  527. static unsigned long ratelimit;
  528. if (msg->errno < 0) {
  529. dprintk("RPC: gss_pipe_destroy_msg releasing msg %p\n",
  530. gss_msg);
  531. atomic_inc(&gss_msg->count);
  532. gss_unhash_msg(gss_msg);
  533. if (msg->errno == -ETIMEDOUT) {
  534. unsigned long now = jiffies;
  535. if (time_after(now, ratelimit)) {
  536. printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
  537. "Please check user daemon is running!\n");
  538. ratelimit = now + 15*HZ;
  539. }
  540. }
  541. gss_release_msg(gss_msg);
  542. }
  543. }
  544. /*
  545. * NOTE: we have the opportunity to use different
  546. * parameters based on the input flavor (which must be a pseudoflavor)
  547. */
  548. static struct rpc_auth *
  549. gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  550. {
  551. struct gss_auth *gss_auth;
  552. struct rpc_auth * auth;
  553. int err = -ENOMEM; /* XXX? */
  554. dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
  555. if (!try_module_get(THIS_MODULE))
  556. return ERR_PTR(err);
  557. if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
  558. goto out_dec;
  559. gss_auth->client = clnt;
  560. err = -EINVAL;
  561. gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
  562. if (!gss_auth->mech) {
  563. printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
  564. __FUNCTION__, flavor);
  565. goto err_free;
  566. }
  567. gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
  568. if (gss_auth->service == 0)
  569. goto err_put_mech;
  570. auth = &gss_auth->rpc_auth;
  571. auth->au_cslack = GSS_CRED_SLACK >> 2;
  572. auth->au_rslack = GSS_VERF_SLACK >> 2;
  573. auth->au_ops = &authgss_ops;
  574. auth->au_flavor = flavor;
  575. atomic_set(&auth->au_count, 1);
  576. kref_init(&gss_auth->kref);
  577. gss_auth->dentry = rpc_mkpipe(clnt->cl_dentry, gss_auth->mech->gm_name,
  578. clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  579. if (IS_ERR(gss_auth->dentry)) {
  580. err = PTR_ERR(gss_auth->dentry);
  581. goto err_put_mech;
  582. }
  583. err = rpcauth_init_credcache(auth);
  584. if (err)
  585. goto err_unlink_pipe;
  586. return auth;
  587. err_unlink_pipe:
  588. rpc_unlink(gss_auth->dentry);
  589. err_put_mech:
  590. gss_mech_put(gss_auth->mech);
  591. err_free:
  592. kfree(gss_auth);
  593. out_dec:
  594. module_put(THIS_MODULE);
  595. return ERR_PTR(err);
  596. }
  597. static void
  598. gss_free(struct gss_auth *gss_auth)
  599. {
  600. rpc_unlink(gss_auth->dentry);
  601. gss_auth->dentry = NULL;
  602. gss_mech_put(gss_auth->mech);
  603. kfree(gss_auth);
  604. module_put(THIS_MODULE);
  605. }
  606. static void
  607. gss_free_callback(struct kref *kref)
  608. {
  609. struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
  610. gss_free(gss_auth);
  611. }
  612. static void
  613. gss_destroy(struct rpc_auth *auth)
  614. {
  615. struct gss_auth *gss_auth;
  616. dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
  617. auth, auth->au_flavor);
  618. rpcauth_destroy_credcache(auth);
  619. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  620. kref_put(&gss_auth->kref, gss_free_callback);
  621. }
  622. /*
  623. * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
  624. * to the server with the GSS control procedure field set to
  625. * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
  626. * all RPCSEC_GSS state associated with that context.
  627. */
  628. static int
  629. gss_destroying_context(struct rpc_cred *cred)
  630. {
  631. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  632. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  633. struct rpc_task *task;
  634. if (gss_cred->gc_ctx == NULL ||
  635. gss_cred->gc_ctx->gc_proc == RPC_GSS_PROC_DESTROY)
  636. return 0;
  637. gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
  638. cred->cr_ops = &gss_nullops;
  639. /* Take a reference to ensure the cred will be destroyed either
  640. * by the RPC call or by the put_rpccred() below */
  641. get_rpccred(cred);
  642. task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC);
  643. if (!IS_ERR(task))
  644. rpc_put_task(task);
  645. put_rpccred(cred);
  646. return 1;
  647. }
  648. /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
  649. * to create a new cred or context, so they check that things have been
  650. * allocated before freeing them. */
  651. static void
  652. gss_do_free_ctx(struct gss_cl_ctx *ctx)
  653. {
  654. dprintk("RPC: gss_free_ctx\n");
  655. if (ctx->gc_gss_ctx)
  656. gss_delete_sec_context(&ctx->gc_gss_ctx);
  657. kfree(ctx->gc_wire_ctx.data);
  658. kfree(ctx);
  659. }
  660. static void
  661. gss_free_ctx_callback(struct rcu_head *head)
  662. {
  663. struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
  664. gss_do_free_ctx(ctx);
  665. }
  666. static void
  667. gss_free_ctx(struct gss_cl_ctx *ctx)
  668. {
  669. call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
  670. }
  671. static void
  672. gss_free_cred(struct gss_cred *gss_cred)
  673. {
  674. dprintk("RPC: gss_free_cred %p\n", gss_cred);
  675. kfree(gss_cred);
  676. }
  677. static void
  678. gss_free_cred_callback(struct rcu_head *head)
  679. {
  680. struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
  681. gss_free_cred(gss_cred);
  682. }
  683. static void
  684. gss_destroy_cred(struct rpc_cred *cred)
  685. {
  686. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  687. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  688. struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
  689. if (gss_destroying_context(cred))
  690. return;
  691. rcu_assign_pointer(gss_cred->gc_ctx, NULL);
  692. call_rcu(&cred->cr_rcu, gss_free_cred_callback);
  693. if (ctx)
  694. gss_put_ctx(ctx);
  695. kref_put(&gss_auth->kref, gss_free_callback);
  696. }
  697. /*
  698. * Lookup RPCSEC_GSS cred for the current process
  699. */
  700. static struct rpc_cred *
  701. gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  702. {
  703. return rpcauth_lookup_credcache(auth, acred, flags);
  704. }
  705. static struct rpc_cred *
  706. gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  707. {
  708. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  709. struct gss_cred *cred = NULL;
  710. int err = -ENOMEM;
  711. dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
  712. acred->uid, auth->au_flavor);
  713. if (!(cred = kzalloc(sizeof(*cred), GFP_KERNEL)))
  714. goto out_err;
  715. rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
  716. /*
  717. * Note: in order to force a call to call_refresh(), we deliberately
  718. * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
  719. */
  720. cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
  721. cred->gc_service = gss_auth->service;
  722. kref_get(&gss_auth->kref);
  723. return &cred->gc_base;
  724. out_err:
  725. dprintk("RPC: gss_create_cred failed with error %d\n", err);
  726. return ERR_PTR(err);
  727. }
  728. static int
  729. gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
  730. {
  731. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  732. struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
  733. int err;
  734. do {
  735. err = gss_create_upcall(gss_auth, gss_cred);
  736. } while (err == -EAGAIN);
  737. return err;
  738. }
  739. static int
  740. gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
  741. {
  742. struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  743. /*
  744. * If the searchflags have set RPCAUTH_LOOKUP_NEW, then
  745. * we don't really care if the credential has expired or not,
  746. * since the caller should be prepared to reinitialise it.
  747. */
  748. if ((flags & RPCAUTH_LOOKUP_NEW) && test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
  749. goto out;
  750. /* Don't match with creds that have expired. */
  751. if (gss_cred->gc_ctx && time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
  752. return 0;
  753. out:
  754. return (rc->cr_uid == acred->uid);
  755. }
  756. /*
  757. * Marshal credentials.
  758. * Maybe we should keep a cached credential for performance reasons.
  759. */
  760. static __be32 *
  761. gss_marshal(struct rpc_task *task, __be32 *p)
  762. {
  763. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  764. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  765. gc_base);
  766. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  767. __be32 *cred_len;
  768. struct rpc_rqst *req = task->tk_rqstp;
  769. u32 maj_stat = 0;
  770. struct xdr_netobj mic;
  771. struct kvec iov;
  772. struct xdr_buf verf_buf;
  773. dprintk("RPC: %5u gss_marshal\n", task->tk_pid);
  774. *p++ = htonl(RPC_AUTH_GSS);
  775. cred_len = p++;
  776. spin_lock(&ctx->gc_seq_lock);
  777. req->rq_seqno = ctx->gc_seq++;
  778. spin_unlock(&ctx->gc_seq_lock);
  779. *p++ = htonl((u32) RPC_GSS_VERSION);
  780. *p++ = htonl((u32) ctx->gc_proc);
  781. *p++ = htonl((u32) req->rq_seqno);
  782. *p++ = htonl((u32) gss_cred->gc_service);
  783. p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
  784. *cred_len = htonl((p - (cred_len + 1)) << 2);
  785. /* We compute the checksum for the verifier over the xdr-encoded bytes
  786. * starting with the xid and ending at the end of the credential: */
  787. iov.iov_base = xprt_skip_transport_header(task->tk_xprt,
  788. req->rq_snd_buf.head[0].iov_base);
  789. iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
  790. xdr_buf_from_iov(&iov, &verf_buf);
  791. /* set verifier flavor*/
  792. *p++ = htonl(RPC_AUTH_GSS);
  793. mic.data = (u8 *)(p + 1);
  794. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  795. if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
  796. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  797. } else if (maj_stat != 0) {
  798. printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
  799. goto out_put_ctx;
  800. }
  801. p = xdr_encode_opaque(p, NULL, mic.len);
  802. gss_put_ctx(ctx);
  803. return p;
  804. out_put_ctx:
  805. gss_put_ctx(ctx);
  806. return NULL;
  807. }
  808. /*
  809. * Refresh credentials. XXX - finish
  810. */
  811. static int
  812. gss_refresh(struct rpc_task *task)
  813. {
  814. if (!gss_cred_is_uptodate_ctx(task->tk_msg.rpc_cred))
  815. return gss_refresh_upcall(task);
  816. return 0;
  817. }
  818. /* Dummy refresh routine: used only when destroying the context */
  819. static int
  820. gss_refresh_null(struct rpc_task *task)
  821. {
  822. return -EACCES;
  823. }
  824. static __be32 *
  825. gss_validate(struct rpc_task *task, __be32 *p)
  826. {
  827. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  828. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  829. __be32 seq;
  830. struct kvec iov;
  831. struct xdr_buf verf_buf;
  832. struct xdr_netobj mic;
  833. u32 flav,len;
  834. u32 maj_stat;
  835. dprintk("RPC: %5u gss_validate\n", task->tk_pid);
  836. flav = ntohl(*p++);
  837. if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
  838. goto out_bad;
  839. if (flav != RPC_AUTH_GSS)
  840. goto out_bad;
  841. seq = htonl(task->tk_rqstp->rq_seqno);
  842. iov.iov_base = &seq;
  843. iov.iov_len = sizeof(seq);
  844. xdr_buf_from_iov(&iov, &verf_buf);
  845. mic.data = (u8 *)p;
  846. mic.len = len;
  847. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  848. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  849. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  850. if (maj_stat) {
  851. dprintk("RPC: %5u gss_validate: gss_verify_mic returned"
  852. "error 0x%08x\n", task->tk_pid, maj_stat);
  853. goto out_bad;
  854. }
  855. /* We leave it to unwrap to calculate au_rslack. For now we just
  856. * calculate the length of the verifier: */
  857. cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
  858. gss_put_ctx(ctx);
  859. dprintk("RPC: %5u gss_validate: gss_verify_mic succeeded.\n",
  860. task->tk_pid);
  861. return p + XDR_QUADLEN(len);
  862. out_bad:
  863. gss_put_ctx(ctx);
  864. dprintk("RPC: %5u gss_validate failed.\n", task->tk_pid);
  865. return NULL;
  866. }
  867. static inline int
  868. gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  869. kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
  870. {
  871. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  872. struct xdr_buf integ_buf;
  873. __be32 *integ_len = NULL;
  874. struct xdr_netobj mic;
  875. u32 offset;
  876. __be32 *q;
  877. struct kvec *iov;
  878. u32 maj_stat = 0;
  879. int status = -EIO;
  880. integ_len = p++;
  881. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  882. *p++ = htonl(rqstp->rq_seqno);
  883. lock_kernel();
  884. status = encode(rqstp, p, obj);
  885. unlock_kernel();
  886. if (status)
  887. return status;
  888. if (xdr_buf_subsegment(snd_buf, &integ_buf,
  889. offset, snd_buf->len - offset))
  890. return status;
  891. *integ_len = htonl(integ_buf.len);
  892. /* guess whether we're in the head or the tail: */
  893. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  894. iov = snd_buf->tail;
  895. else
  896. iov = snd_buf->head;
  897. p = iov->iov_base + iov->iov_len;
  898. mic.data = (u8 *)(p + 1);
  899. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  900. status = -EIO; /* XXX? */
  901. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  902. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  903. else if (maj_stat)
  904. return status;
  905. q = xdr_encode_opaque(p, NULL, mic.len);
  906. offset = (u8 *)q - (u8 *)p;
  907. iov->iov_len += offset;
  908. snd_buf->len += offset;
  909. return 0;
  910. }
  911. static void
  912. priv_release_snd_buf(struct rpc_rqst *rqstp)
  913. {
  914. int i;
  915. for (i=0; i < rqstp->rq_enc_pages_num; i++)
  916. __free_page(rqstp->rq_enc_pages[i]);
  917. kfree(rqstp->rq_enc_pages);
  918. }
  919. static int
  920. alloc_enc_pages(struct rpc_rqst *rqstp)
  921. {
  922. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  923. int first, last, i;
  924. if (snd_buf->page_len == 0) {
  925. rqstp->rq_enc_pages_num = 0;
  926. return 0;
  927. }
  928. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  929. last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
  930. rqstp->rq_enc_pages_num = last - first + 1 + 1;
  931. rqstp->rq_enc_pages
  932. = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
  933. GFP_NOFS);
  934. if (!rqstp->rq_enc_pages)
  935. goto out;
  936. for (i=0; i < rqstp->rq_enc_pages_num; i++) {
  937. rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
  938. if (rqstp->rq_enc_pages[i] == NULL)
  939. goto out_free;
  940. }
  941. rqstp->rq_release_snd_buf = priv_release_snd_buf;
  942. return 0;
  943. out_free:
  944. for (i--; i >= 0; i--) {
  945. __free_page(rqstp->rq_enc_pages[i]);
  946. }
  947. out:
  948. return -EAGAIN;
  949. }
  950. static inline int
  951. gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  952. kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
  953. {
  954. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  955. u32 offset;
  956. u32 maj_stat;
  957. int status;
  958. __be32 *opaque_len;
  959. struct page **inpages;
  960. int first;
  961. int pad;
  962. struct kvec *iov;
  963. char *tmp;
  964. opaque_len = p++;
  965. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  966. *p++ = htonl(rqstp->rq_seqno);
  967. lock_kernel();
  968. status = encode(rqstp, p, obj);
  969. unlock_kernel();
  970. if (status)
  971. return status;
  972. status = alloc_enc_pages(rqstp);
  973. if (status)
  974. return status;
  975. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  976. inpages = snd_buf->pages + first;
  977. snd_buf->pages = rqstp->rq_enc_pages;
  978. snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
  979. /* Give the tail its own page, in case we need extra space in the
  980. * head when wrapping: */
  981. if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
  982. tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
  983. memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
  984. snd_buf->tail[0].iov_base = tmp;
  985. }
  986. maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
  987. /* RPC_SLACK_SPACE should prevent this ever happening: */
  988. BUG_ON(snd_buf->len > snd_buf->buflen);
  989. status = -EIO;
  990. /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
  991. * done anyway, so it's safe to put the request on the wire: */
  992. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  993. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  994. else if (maj_stat)
  995. return status;
  996. *opaque_len = htonl(snd_buf->len - offset);
  997. /* guess whether we're in the head or the tail: */
  998. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  999. iov = snd_buf->tail;
  1000. else
  1001. iov = snd_buf->head;
  1002. p = iov->iov_base + iov->iov_len;
  1003. pad = 3 - ((snd_buf->len - offset - 1) & 3);
  1004. memset(p, 0, pad);
  1005. iov->iov_len += pad;
  1006. snd_buf->len += pad;
  1007. return 0;
  1008. }
  1009. static int
  1010. gss_wrap_req(struct rpc_task *task,
  1011. kxdrproc_t encode, void *rqstp, __be32 *p, void *obj)
  1012. {
  1013. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  1014. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1015. gc_base);
  1016. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1017. int status = -EIO;
  1018. dprintk("RPC: %5u gss_wrap_req\n", task->tk_pid);
  1019. if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
  1020. /* The spec seems a little ambiguous here, but I think that not
  1021. * wrapping context destruction requests makes the most sense.
  1022. */
  1023. lock_kernel();
  1024. status = encode(rqstp, p, obj);
  1025. unlock_kernel();
  1026. goto out;
  1027. }
  1028. switch (gss_cred->gc_service) {
  1029. case RPC_GSS_SVC_NONE:
  1030. lock_kernel();
  1031. status = encode(rqstp, p, obj);
  1032. unlock_kernel();
  1033. break;
  1034. case RPC_GSS_SVC_INTEGRITY:
  1035. status = gss_wrap_req_integ(cred, ctx, encode,
  1036. rqstp, p, obj);
  1037. break;
  1038. case RPC_GSS_SVC_PRIVACY:
  1039. status = gss_wrap_req_priv(cred, ctx, encode,
  1040. rqstp, p, obj);
  1041. break;
  1042. }
  1043. out:
  1044. gss_put_ctx(ctx);
  1045. dprintk("RPC: %5u gss_wrap_req returning %d\n", task->tk_pid, status);
  1046. return status;
  1047. }
  1048. static inline int
  1049. gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1050. struct rpc_rqst *rqstp, __be32 **p)
  1051. {
  1052. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1053. struct xdr_buf integ_buf;
  1054. struct xdr_netobj mic;
  1055. u32 data_offset, mic_offset;
  1056. u32 integ_len;
  1057. u32 maj_stat;
  1058. int status = -EIO;
  1059. integ_len = ntohl(*(*p)++);
  1060. if (integ_len & 3)
  1061. return status;
  1062. data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1063. mic_offset = integ_len + data_offset;
  1064. if (mic_offset > rcv_buf->len)
  1065. return status;
  1066. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1067. return status;
  1068. if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
  1069. mic_offset - data_offset))
  1070. return status;
  1071. if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
  1072. return status;
  1073. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1074. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1075. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1076. if (maj_stat != GSS_S_COMPLETE)
  1077. return status;
  1078. return 0;
  1079. }
  1080. static inline int
  1081. gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1082. struct rpc_rqst *rqstp, __be32 **p)
  1083. {
  1084. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1085. u32 offset;
  1086. u32 opaque_len;
  1087. u32 maj_stat;
  1088. int status = -EIO;
  1089. opaque_len = ntohl(*(*p)++);
  1090. offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1091. if (offset + opaque_len > rcv_buf->len)
  1092. return status;
  1093. /* remove padding: */
  1094. rcv_buf->len = offset + opaque_len;
  1095. maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
  1096. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1097. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1098. if (maj_stat != GSS_S_COMPLETE)
  1099. return status;
  1100. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1101. return status;
  1102. return 0;
  1103. }
  1104. static int
  1105. gss_unwrap_resp(struct rpc_task *task,
  1106. kxdrproc_t decode, void *rqstp, __be32 *p, void *obj)
  1107. {
  1108. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  1109. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1110. gc_base);
  1111. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1112. __be32 *savedp = p;
  1113. struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
  1114. int savedlen = head->iov_len;
  1115. int status = -EIO;
  1116. if (ctx->gc_proc != RPC_GSS_PROC_DATA)
  1117. goto out_decode;
  1118. switch (gss_cred->gc_service) {
  1119. case RPC_GSS_SVC_NONE:
  1120. break;
  1121. case RPC_GSS_SVC_INTEGRITY:
  1122. status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
  1123. if (status)
  1124. goto out;
  1125. break;
  1126. case RPC_GSS_SVC_PRIVACY:
  1127. status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
  1128. if (status)
  1129. goto out;
  1130. break;
  1131. }
  1132. /* take into account extra slack for integrity and privacy cases: */
  1133. cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
  1134. + (savedlen - head->iov_len);
  1135. out_decode:
  1136. lock_kernel();
  1137. status = decode(rqstp, p, obj);
  1138. unlock_kernel();
  1139. out:
  1140. gss_put_ctx(ctx);
  1141. dprintk("RPC: %5u gss_unwrap_resp returning %d\n", task->tk_pid,
  1142. status);
  1143. return status;
  1144. }
  1145. static const struct rpc_authops authgss_ops = {
  1146. .owner = THIS_MODULE,
  1147. .au_flavor = RPC_AUTH_GSS,
  1148. #ifdef RPC_DEBUG
  1149. .au_name = "RPCSEC_GSS",
  1150. #endif
  1151. .create = gss_create,
  1152. .destroy = gss_destroy,
  1153. .lookup_cred = gss_lookup_cred,
  1154. .crcreate = gss_create_cred
  1155. };
  1156. static const struct rpc_credops gss_credops = {
  1157. .cr_name = "AUTH_GSS",
  1158. .crdestroy = gss_destroy_cred,
  1159. .cr_init = gss_cred_init,
  1160. .crmatch = gss_match,
  1161. .crmarshal = gss_marshal,
  1162. .crrefresh = gss_refresh,
  1163. .crvalidate = gss_validate,
  1164. .crwrap_req = gss_wrap_req,
  1165. .crunwrap_resp = gss_unwrap_resp,
  1166. };
  1167. static const struct rpc_credops gss_nullops = {
  1168. .cr_name = "AUTH_GSS",
  1169. .crdestroy = gss_destroy_cred,
  1170. .crmatch = gss_match,
  1171. .crmarshal = gss_marshal,
  1172. .crrefresh = gss_refresh_null,
  1173. .crvalidate = gss_validate,
  1174. .crwrap_req = gss_wrap_req,
  1175. .crunwrap_resp = gss_unwrap_resp,
  1176. };
  1177. static struct rpc_pipe_ops gss_upcall_ops = {
  1178. .upcall = gss_pipe_upcall,
  1179. .downcall = gss_pipe_downcall,
  1180. .destroy_msg = gss_pipe_destroy_msg,
  1181. .release_pipe = gss_pipe_release,
  1182. };
  1183. /*
  1184. * Initialize RPCSEC_GSS module
  1185. */
  1186. static int __init init_rpcsec_gss(void)
  1187. {
  1188. int err = 0;
  1189. err = rpcauth_register(&authgss_ops);
  1190. if (err)
  1191. goto out;
  1192. err = gss_svc_init();
  1193. if (err)
  1194. goto out_unregister;
  1195. return 0;
  1196. out_unregister:
  1197. rpcauth_unregister(&authgss_ops);
  1198. out:
  1199. return err;
  1200. }
  1201. static void __exit exit_rpcsec_gss(void)
  1202. {
  1203. gss_svc_shutdown();
  1204. rpcauth_unregister(&authgss_ops);
  1205. }
  1206. MODULE_LICENSE("GPL");
  1207. module_init(init_rpcsec_gss)
  1208. module_exit(exit_rpcsec_gss)