blob: c0d36bb87d3e109e8b05a67cacbf348b3c834a58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/net/sunrpc/auth_gss/auth_gss.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * RPCSEC_GSS client authentication.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sched.h>
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -040044#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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
Trond Myklebustabfdbd52013-05-15 11:28:54 -070055#include "../netns.h"
56
Trond Myklebustf1c0a862007-06-23 20:17:58 -040057static const struct rpc_authops authgss_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebustf1c0a862007-06-23 20:17:58 -040059static const struct rpc_credops gss_credops;
Trond Myklebust0df7fb72007-06-26 17:04:57 -040060static const struct rpc_credops gss_nullops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Trond Myklebust126e2162010-05-13 12:55:38 -040062#define GSS_RETRY_EXPIRED 5
63static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef RPC_DEBUG
66# define RPCDBG_FACILITY RPCDBG_AUTH
67#endif
68
Kevin Coffman725f2862010-03-17 13:02:46 -040069#define GSS_CRED_SLACK (RPC_MAX_AUTH_SIZE * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* length of a krb5 verifier (48), plus data added before arguments when
71 * using integrity (two 4-byte integers): */
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050072#define GSS_VERF_SLACK 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct gss_auth {
Trond Myklebust0285ed12007-06-27 14:29:12 -040075 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct rpc_auth rpc_auth;
77 struct gss_api_mech *mech;
78 enum rpc_gss_svc service;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct rpc_clnt *client;
Trond Myklebuste726340a2013-08-23 14:42:29 -040080 struct net *net;
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -050081 /*
82 * There are two upcall pipes; dentry[1], named "gssd", is used
83 * for the new text-based upcall; dentry[0] is named after the
84 * mechanism (for example, "krb5") and exists for
85 * backwards-compatibility with older gssd's.
86 */
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +030087 struct rpc_pipe *pipe[2];
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -040088 const char *target_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -050091/* pipe_version >= 0 if and only if someone has a pipe open. */
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -050092static DEFINE_SPINLOCK(pipe_version_lock);
93static struct rpc_wait_queue pipe_version_rpc_waitqueue;
94static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -050095
Trond Myklebust5d28dc82007-06-26 19:18:38 -040096static void gss_free_ctx(struct gss_cl_ctx *);
Trond Myklebustb693ba42009-08-09 15:14:15 -040097static const struct rpc_pipe_ops gss_upcall_ops_v0;
98static const struct rpc_pipe_ops gss_upcall_ops_v1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static inline struct gss_cl_ctx *
101gss_get_ctx(struct gss_cl_ctx *ctx)
102{
103 atomic_inc(&ctx->count);
104 return ctx;
105}
106
107static inline void
108gss_put_ctx(struct gss_cl_ctx *ctx)
109{
110 if (atomic_dec_and_test(&ctx->count))
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400111 gss_free_ctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400114/* 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
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300117 * and a new one is protected by the pipe->lock.
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void
120gss_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);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400123
Trond Myklebustcd019f72008-04-17 17:03:58 -0400124 if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
125 return;
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400126 gss_get_ctx(ctx);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000127 rcu_assign_pointer(gss_cred->gc_ctx, ctx);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400128 set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Trond Myklebustcd019f72008-04-17 17:03:58 -0400129 smp_mb__before_clear_bit();
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400130 clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static const void *
134simple_get_bytes(const void *p, const void *end, void *res, size_t len)
135{
136 const void *q = (const void *)((const char *)p + len);
137 if (unlikely(q > end || q < p))
138 return ERR_PTR(-EFAULT);
139 memcpy(res, p, len);
140 return q;
141}
142
143static inline const void *
144simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
145{
146 const void *q;
147 unsigned int len;
148
149 p = simple_get_bytes(p, end, &len, sizeof(len));
150 if (IS_ERR(p))
151 return p;
152 q = (const void *)((const char *)p + len);
153 if (unlikely(q > end || q < p))
154 return ERR_PTR(-EFAULT);
Trond Myklebust0f38b872008-06-10 18:31:01 -0400155 dest->data = kmemdup(p, len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (unlikely(dest->data == NULL))
157 return ERR_PTR(-ENOMEM);
158 dest->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return q;
160}
161
162static struct gss_cl_ctx *
163gss_cred_get_ctx(struct rpc_cred *cred)
164{
165 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
166 struct gss_cl_ctx *ctx = NULL;
167
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400168 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (gss_cred->gc_ctx)
170 ctx = gss_get_ctx(gss_cred->gc_ctx);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400171 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return ctx;
173}
174
175static struct gss_cl_ctx *
176gss_alloc_context(void)
177{
178 struct gss_cl_ctx *ctx;
179
Trond Myklebust0f38b872008-06-10 18:31:01 -0400180 ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (ctx != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ctx->gc_proc = RPC_GSS_PROC_DATA;
183 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
184 spin_lock_init(&ctx->gc_seq_lock);
185 atomic_set(&ctx->count,1);
186 }
187 return ctx;
188}
189
190#define GSSD_MIN_TIMEOUT (60 * 60)
191static const void *
192gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
193{
194 const void *q;
195 unsigned int seclen;
196 unsigned int timeout;
Andy Adamson620038f2012-11-27 10:34:20 -0500197 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 u32 window_size;
199 int ret;
200
Andy Adamson620038f2012-11-27 10:34:20 -0500201 /* First unsigned int gives the remaining lifetime in seconds of the
202 * credential - e.g. the remaining TGT lifetime for Kerberos or
203 * the -t value passed to GSSD.
204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
206 if (IS_ERR(p))
207 goto err;
208 if (timeout == 0)
209 timeout = GSSD_MIN_TIMEOUT;
Andy Adamson620038f2012-11-27 10:34:20 -0500210 ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
211 /* Sequence number window. Determines the maximum number of
212 * simultaneous requests
213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
215 if (IS_ERR(p))
216 goto err;
217 ctx->gc_win = window_size;
218 /* gssd signals an error by passing ctx->gc_win = 0: */
219 if (ctx->gc_win == 0) {
Jeff Laytondc5ddce2010-01-07 09:42:02 -0500220 /*
221 * in which case, p points to an error code. Anything other
222 * than -EKEYEXPIRED gets converted to -EACCES.
223 */
224 p = simple_get_bytes(p, end, &ret, sizeof(ret));
225 if (!IS_ERR(p))
226 p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
227 ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto err;
229 }
230 /* copy the opaque wire context */
231 p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
232 if (IS_ERR(p))
233 goto err;
234 /* import the opaque security context */
235 p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
236 if (IS_ERR(p))
237 goto err;
238 q = (const void *)((const char *)p + seclen);
239 if (unlikely(q > end || q < p)) {
240 p = ERR_PTR(-EFAULT);
241 goto err;
242 }
Simo Sorce400f26b2012-05-25 18:09:53 -0400243 ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (ret < 0) {
245 p = ERR_PTR(ret);
246 goto err;
247 }
Andy Adamson620038f2012-11-27 10:34:20 -0500248 dprintk("RPC: %s Success. gc_expiry %lu now %lu timeout %u\n",
249 __func__, ctx->gc_expiry, now, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return q;
251err:
Jeff Layton173db302013-02-07 10:29:06 -0500252 dprintk("RPC: %s returns error %ld\n", __func__, -PTR_ERR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return p;
254}
255
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500256#define UPCALL_BUF_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258struct gss_upcall_msg {
259 atomic_t count;
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800260 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct rpc_pipe_msg msg;
262 struct list_head list;
263 struct gss_auth *auth;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300264 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct rpc_wait_queue rpc_waitqueue;
266 wait_queue_head_t waitqueue;
267 struct gss_cl_ctx *ctx;
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500268 char databuf[UPCALL_BUF_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700271static int get_pipe_version(struct net *net)
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500272{
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700273 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500274 int ret;
275
276 spin_lock(&pipe_version_lock);
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700277 if (sn->pipe_version >= 0) {
278 atomic_inc(&sn->pipe_users);
279 ret = sn->pipe_version;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500280 } else
281 ret = -EAGAIN;
282 spin_unlock(&pipe_version_lock);
283 return ret;
284}
285
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700286static void put_pipe_version(struct net *net)
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500287{
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700288 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
289
290 if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
291 sn->pipe_version = -1;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500292 spin_unlock(&pipe_version_lock);
293 }
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void
297gss_release_msg(struct gss_upcall_msg *gss_msg)
298{
Trond Myklebuste726340a2013-08-23 14:42:29 -0400299 struct net *net = gss_msg->auth->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!atomic_dec_and_test(&gss_msg->count))
301 return;
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700302 put_pipe_version(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 BUG_ON(!list_empty(&gss_msg->list));
304 if (gss_msg->ctx != NULL)
305 gss_put_ctx(gss_msg->ctx);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500306 rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 kfree(gss_msg);
308}
309
310static struct gss_upcall_msg *
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800311__gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct gss_upcall_msg *pos;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300314 list_for_each_entry(pos, &pipe->in_downcall, list) {
Eric W. Biederman0b4d51b2013-02-01 16:39:32 -0800315 if (!uid_eq(pos->uid, uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 continue;
317 atomic_inc(&pos->count);
Chuck Lever632f0d02012-09-14 17:23:43 -0400318 dprintk("RPC: %s found msg %p\n", __func__, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return pos;
320 }
Chuck Lever632f0d02012-09-14 17:23:43 -0400321 dprintk("RPC: %s found nothing\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return NULL;
323}
324
\\\"J. Bruce Fields\\\720b8f22008-06-09 16:51:33 -0400325/* Try to add an upcall to the pipefs queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * If an upcall owned by our uid already exists, then we return a reference
327 * to that upcall instead of adding the new upcall.
328 */
329static inline struct gss_upcall_msg *
Suresh Jayaraman053e3242009-12-09 23:15:22 +0530330gss_add_msg(struct gss_upcall_msg *gss_msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300332 struct rpc_pipe *pipe = gss_msg->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct gss_upcall_msg *old;
334
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300335 spin_lock(&pipe->lock);
336 old = __gss_find_upcall(pipe, gss_msg->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (old == NULL) {
338 atomic_inc(&gss_msg->count);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300339 list_add(&gss_msg->list, &pipe->in_downcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } else
341 gss_msg = old;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300342 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return gss_msg;
344}
345
346static void
347__gss_unhash_msg(struct gss_upcall_msg *gss_msg)
348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 list_del_init(&gss_msg->list);
350 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
351 wake_up_all(&gss_msg->waitqueue);
352 atomic_dec(&gss_msg->count);
353}
354
355static void
356gss_unhash_msg(struct gss_upcall_msg *gss_msg)
357{
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300358 struct rpc_pipe *pipe = gss_msg->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400360 if (list_empty(&gss_msg->list))
361 return;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300362 spin_lock(&pipe->lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400363 if (!list_empty(&gss_msg->list))
364 __gss_unhash_msg(gss_msg);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300365 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368static void
Trond Myklebust126e2162010-05-13 12:55:38 -0400369gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
370{
371 switch (gss_msg->msg.errno) {
372 case 0:
373 if (gss_msg->ctx == NULL)
374 break;
375 clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
376 gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
377 break;
378 case -EKEYEXPIRED:
379 set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
380 }
381 gss_cred->gc_upcall_timestamp = jiffies;
382 gss_cred->gc_upcall = NULL;
383 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
384}
385
386static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387gss_upcall_callback(struct rpc_task *task)
388{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400389 struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct gss_cred, gc_base);
391 struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300392 struct rpc_pipe *pipe = gss_msg->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300394 spin_lock(&pipe->lock);
Trond Myklebust126e2162010-05-13 12:55:38 -0400395 gss_handle_downcall_result(gss_cred, gss_msg);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300396 spin_unlock(&pipe->lock);
Trond Myklebust126e2162010-05-13 12:55:38 -0400397 task->tk_status = gss_msg->msg.errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 gss_release_msg(gss_msg);
399}
400
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500401static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
402{
Eric W. Biederman90602c72013-02-02 00:25:43 -0800403 uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
404 memcpy(gss_msg->databuf, &uid, sizeof(uid));
405 gss_msg->msg.data = gss_msg->databuf;
406 gss_msg->msg.len = sizeof(uid);
407 BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500408}
409
Olga Kornievskaia8b1c7bf2008-12-23 16:19:26 -0500410static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400411 const char *service_name,
412 const char *target_name)
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500413{
Trond Myklebust683ac662010-04-08 14:09:58 -0400414 struct gss_api_mech *mech = gss_msg->auth->mech;
Olga Kornievskaia8b1c7bf2008-12-23 16:19:26 -0500415 char *p = gss_msg->databuf;
416 int len = 0;
417
418 gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
Trond Myklebust683ac662010-04-08 14:09:58 -0400419 mech->gm_name,
Eric W. Biederman90602c72013-02-02 00:25:43 -0800420 from_kuid(&init_user_ns, gss_msg->uid));
Olga Kornievskaia8b1c7bf2008-12-23 16:19:26 -0500421 p += gss_msg->msg.len;
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400422 if (target_name) {
423 len = sprintf(p, "target=%s ", target_name);
Olga Kornievskaia8b1c7bf2008-12-23 16:19:26 -0500424 p += len;
425 gss_msg->msg.len += len;
426 }
Trond Myklebust68c97152012-01-03 13:22:46 -0500427 if (service_name != NULL) {
428 len = sprintf(p, "service=%s ", service_name);
Olga Kornievskaia2efef702008-12-23 16:19:56 -0500429 p += len;
430 gss_msg->msg.len += len;
431 }
Trond Myklebust683ac662010-04-08 14:09:58 -0400432 if (mech->gm_upcall_enctypes) {
Kevin Coffmanf8628222011-03-03 00:51:41 +0000433 len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
Trond Myklebust683ac662010-04-08 14:09:58 -0400434 p += len;
435 gss_msg->msg.len += len;
436 }
Olga Kornievskaia8b1c7bf2008-12-23 16:19:26 -0500437 len = sprintf(p, "\n");
438 gss_msg->msg.len += len;
439
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500440 gss_msg->msg.data = gss_msg->databuf;
441 BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
442}
443
Trond Myklebust68c97152012-01-03 13:22:46 -0500444static struct gss_upcall_msg *
Trond Myklebuste726340a2013-08-23 14:42:29 -0400445gss_alloc_msg(struct gss_auth *gss_auth,
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800446 kuid_t uid, const char *service_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct gss_upcall_msg *gss_msg;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500449 int vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Trond Myklebust0f38b872008-06-10 18:31:01 -0400451 gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500452 if (gss_msg == NULL)
453 return ERR_PTR(-ENOMEM);
Trond Myklebuste726340a2013-08-23 14:42:29 -0400454 vers = get_pipe_version(gss_auth->net);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500455 if (vers < 0) {
456 kfree(gss_msg);
457 return ERR_PTR(vers);
458 }
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300459 gss_msg->pipe = gss_auth->pipe[vers];
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500460 INIT_LIST_HEAD(&gss_msg->list);
461 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
462 init_waitqueue_head(&gss_msg->waitqueue);
463 atomic_set(&gss_msg->count, 1);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500464 gss_msg->uid = uid;
465 gss_msg->auth = gss_auth;
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400466 switch (vers) {
467 case 0:
468 gss_encode_v0_msg(gss_msg);
469 default:
470 gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return gss_msg;
473}
474
475static struct gss_upcall_msg *
Trond Myklebuste726340a2013-08-23 14:42:29 -0400476gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Trond Myklebust7c67db32008-04-07 20:50:11 -0400478 struct gss_cred *gss_cred = container_of(cred,
479 struct gss_cred, gc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct gss_upcall_msg *gss_new, *gss_msg;
Eric W. Biederman7eaf0402013-02-01 16:31:17 -0800481 kuid_t uid = cred->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Trond Myklebuste726340a2013-08-23 14:42:29 -0400483 gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500484 if (IS_ERR(gss_new))
485 return gss_new;
Suresh Jayaraman053e3242009-12-09 23:15:22 +0530486 gss_msg = gss_add_msg(gss_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (gss_msg == gss_new) {
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300488 int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (res) {
490 gss_unhash_msg(gss_new);
491 gss_msg = ERR_PTR(res);
492 }
493 } else
494 gss_release_msg(gss_new);
495 return gss_msg;
496}
497
\"J. Bruce Fields\b03568c2008-12-23 16:06:55 -0500498static void warn_gssd(void)
499{
500 static unsigned long ratelimit;
501 unsigned long now = jiffies;
502
503 if (time_after(now, ratelimit)) {
504 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
505 "Please check user daemon is running.\n");
506 ratelimit = now + 15*HZ;
507 }
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static inline int
511gss_refresh_upcall(struct rpc_task *task)
512{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400513 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebust4a8c1342007-06-07 10:14:14 -0400514 struct gss_auth *gss_auth = container_of(cred->cr_auth,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct gss_auth, rpc_auth);
516 struct gss_cred *gss_cred = container_of(cred,
517 struct gss_cred, gc_base);
518 struct gss_upcall_msg *gss_msg;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300519 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int err = 0;
521
Chuck Lever632f0d02012-09-14 17:23:43 -0400522 dprintk("RPC: %5u %s for uid %u\n",
Eric W. Biedermancdba3212013-02-01 17:10:52 -0800523 task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
Trond Myklebuste726340a2013-08-23 14:42:29 -0400524 gss_msg = gss_setup_upcall(gss_auth, cred);
Roel Kluin480e3242009-12-08 13:13:03 -0500525 if (PTR_ERR(gss_msg) == -EAGAIN) {
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500526 /* XXX: warning on the first, under the assumption we
527 * shouldn't normally hit this case on a refresh. */
528 warn_gssd();
529 task->tk_timeout = 15*HZ;
530 rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
Bryan Schumakerd1a80162011-04-13 14:31:28 -0400531 return -EAGAIN;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (IS_ERR(gss_msg)) {
534 err = PTR_ERR(gss_msg);
535 goto out;
536 }
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300537 pipe = gss_msg->pipe;
538 spin_lock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (gss_cred->gc_upcall != NULL)
Trond Myklebust5d008372008-02-22 16:34:17 -0500540 rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
Trond Myklebust126e2162010-05-13 12:55:38 -0400541 else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 task->tk_timeout = 0;
543 gss_cred->gc_upcall = gss_msg;
544 /* gss_upcall_callback will release the reference to gss_upcall_msg */
545 atomic_inc(&gss_msg->count);
Trond Myklebust5d008372008-02-22 16:34:17 -0500546 rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
Trond Myklebust126e2162010-05-13 12:55:38 -0400547 } else {
548 gss_handle_downcall_result(gss_cred, gss_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 err = gss_msg->msg.errno;
Trond Myklebust126e2162010-05-13 12:55:38 -0400550 }
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300551 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 gss_release_msg(gss_msg);
553out:
Chuck Lever632f0d02012-09-14 17:23:43 -0400554 dprintk("RPC: %5u %s for uid %u result %d\n",
Eric W. Biedermancdba3212013-02-01 17:10:52 -0800555 task->tk_pid, __func__,
556 from_kuid(&init_user_ns, cred->cr_uid), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return err;
558}
559
560static inline int
561gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
562{
Trond Myklebuste726340a2013-08-23 14:42:29 -0400563 struct net *net = gss_auth->net;
Trond Myklebustabfdbd52013-05-15 11:28:54 -0700564 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300565 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct rpc_cred *cred = &gss_cred->gc_base;
567 struct gss_upcall_msg *gss_msg;
Trond Myklebustabfdbd52013-05-15 11:28:54 -0700568 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 DEFINE_WAIT(wait);
Trond Myklebustd36ccb92013-05-15 10:46:33 -0700570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Eric W. Biedermancdba3212013-02-01 17:10:52 -0800572 dprintk("RPC: %s for uid %u\n",
573 __func__, from_kuid(&init_user_ns, cred->cr_uid));
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500574retry:
Trond Myklebustd36ccb92013-05-15 10:46:33 -0700575 err = 0;
Trond Myklebustabfdbd52013-05-15 11:28:54 -0700576 /* Default timeout is 15s unless we know that gssd is not running */
577 timeout = 15 * HZ;
578 if (!sn->gssd_running)
579 timeout = HZ >> 2;
Trond Myklebuste726340a2013-08-23 14:42:29 -0400580 gss_msg = gss_setup_upcall(gss_auth, cred);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500581 if (PTR_ERR(gss_msg) == -EAGAIN) {
582 err = wait_event_interruptible_timeout(pipe_version_waitqueue,
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700583 sn->pipe_version >= 0, timeout);
584 if (sn->pipe_version < 0) {
Trond Myklebustabfdbd52013-05-15 11:28:54 -0700585 if (err == 0)
586 sn->gssd_running = 0;
Bryan Schumakerd1a80162011-04-13 14:31:28 -0400587 warn_gssd();
588 err = -EACCES;
589 }
Trond Myklebustd36ccb92013-05-15 10:46:33 -0700590 if (err < 0)
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500591 goto out;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500592 goto retry;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (IS_ERR(gss_msg)) {
595 err = PTR_ERR(gss_msg);
596 goto out;
597 }
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300598 pipe = gss_msg->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 for (;;) {
Trond Myklebust5afa91332011-06-17 10:14:59 -0400600 prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300601 spin_lock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 }
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300605 spin_unlock(&pipe->lock);
Trond Myklebust5afa91332011-06-17 10:14:59 -0400606 if (fatal_signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 err = -ERESTARTSYS;
608 goto out_intr;
609 }
610 schedule();
611 }
612 if (gss_msg->ctx)
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400613 gss_cred_set_ctx(cred, gss_msg->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 else
615 err = gss_msg->msg.errno;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300616 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617out_intr:
618 finish_wait(&gss_msg->waitqueue, &wait);
619 gss_release_msg(gss_msg);
620out:
Chuck Lever632f0d02012-09-14 17:23:43 -0400621 dprintk("RPC: %s for uid %u result %d\n",
Eric W. Biedermancdba3212013-02-01 17:10:52 -0800622 __func__, from_kuid(&init_user_ns, cred->cr_uid), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return err;
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#define MSG_BUF_MAXSIZE 1024
627
628static ssize_t
629gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
630{
631 const void *p, *end;
632 void *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct gss_upcall_msg *gss_msg;
Al Viro496ad9a2013-01-23 17:07:38 -0500634 struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct gss_cl_ctx *ctx;
Eric W. Biederman90602c72013-02-02 00:25:43 -0800636 uid_t id;
637 kuid_t uid;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400638 ssize_t err = -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (mlen > MSG_BUF_MAXSIZE)
641 goto out;
642 err = -ENOMEM;
Trond Myklebust0f38b872008-06-10 18:31:01 -0400643 buf = kmalloc(mlen, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!buf)
645 goto out;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 err = -EFAULT;
648 if (copy_from_user(buf, src, mlen))
649 goto err;
650
651 end = (const void *)((char *)buf + mlen);
Eric W. Biederman90602c72013-02-02 00:25:43 -0800652 p = simple_get_bytes(buf, end, &id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (IS_ERR(p)) {
654 err = PTR_ERR(p);
655 goto err;
656 }
657
Eric W. Biederman90602c72013-02-02 00:25:43 -0800658 uid = make_kuid(&init_user_ns, id);
659 if (!uid_valid(uid)) {
660 err = -EINVAL;
661 goto err;
662 }
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 err = -ENOMEM;
665 ctx = gss_alloc_context();
666 if (ctx == NULL)
667 goto err;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400668
669 err = -ENOENT;
670 /* Find a matching upcall */
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300671 spin_lock(&pipe->lock);
672 gss_msg = __gss_find_upcall(pipe, uid);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400673 if (gss_msg == NULL) {
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300674 spin_unlock(&pipe->lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400675 goto err_put_ctx;
676 }
677 list_del_init(&gss_msg->list);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300678 spin_unlock(&pipe->lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400679
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400680 p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (IS_ERR(p)) {
682 err = PTR_ERR(p);
Jeff Layton486bad22009-12-18 16:28:20 -0500683 switch (err) {
684 case -EACCES:
Jeff Laytondc5ddce2010-01-07 09:42:02 -0500685 case -EKEYEXPIRED:
Jeff Layton486bad22009-12-18 16:28:20 -0500686 gss_msg->msg.errno = err;
687 err = mlen;
688 break;
689 case -EFAULT:
690 case -ENOMEM:
691 case -EINVAL:
692 case -ENOSYS:
693 gss_msg->msg.errno = -EAGAIN;
694 break;
695 default:
696 printk(KERN_CRIT "%s: bad return from "
Randy Dunlap6c853092010-01-06 17:26:27 -0500697 "gss_fill_context: %zd\n", __func__, err);
Jeff Layton486bad22009-12-18 16:28:20 -0500698 BUG();
699 }
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400700 goto err_release_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400702 gss_msg->ctx = gss_get_ctx(ctx);
703 err = mlen;
704
705err_release_msg:
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300706 spin_lock(&pipe->lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400707 __gss_unhash_msg(gss_msg);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300708 spin_unlock(&pipe->lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400709 gss_release_msg(gss_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710err_put_ctx:
711 gss_put_ctx(ctx);
712err:
713 kfree(buf);
714out:
Chuck Lever632f0d02012-09-14 17:23:43 -0400715 dprintk("RPC: %s returning %Zd\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return err;
717}
718
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500719static int gss_pipe_open(struct inode *inode, int new_version)
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500720{
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700721 struct net *net = inode->i_sb->s_fs_info;
722 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500723 int ret = 0;
724
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500725 spin_lock(&pipe_version_lock);
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700726 if (sn->pipe_version < 0) {
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500727 /* First open of any gss pipe determines the version: */
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700728 sn->pipe_version = new_version;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500729 rpc_wake_up(&pipe_version_rpc_waitqueue);
730 wake_up(&pipe_version_waitqueue);
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700731 } else if (sn->pipe_version != new_version) {
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500732 /* Trying to open a pipe of a different version */
733 ret = -EBUSY;
734 goto out;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500735 }
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700736 atomic_inc(&sn->pipe_users);
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500737out:
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500738 spin_unlock(&pipe_version_lock);
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500739 return ret;
740
741}
742
743static int gss_pipe_open_v0(struct inode *inode)
744{
745 return gss_pipe_open(inode, 0);
746}
747
748static int gss_pipe_open_v1(struct inode *inode)
749{
750 return gss_pipe_open(inode, 1);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static void
754gss_pipe_release(struct inode *inode)
755{
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700756 struct net *net = inode->i_sb->s_fs_info;
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300757 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400758 struct gss_upcall_msg *gss_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Trond Myklebust5a676572010-09-12 19:55:25 -0400760restart:
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300761 spin_lock(&pipe->lock);
762 list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Trond Myklebust5a676572010-09-12 19:55:25 -0400764 if (!list_empty(&gss_msg->msg.list))
765 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 gss_msg->msg.errno = -EPIPE;
767 atomic_inc(&gss_msg->count);
768 __gss_unhash_msg(gss_msg);
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300769 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 gss_release_msg(gss_msg);
Trond Myklebust5a676572010-09-12 19:55:25 -0400771 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Stanislav Kinsbursky9beae462011-12-26 15:43:57 +0300773 spin_unlock(&pipe->lock);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500774
Trond Myklebust2aed8b42013-05-15 10:27:32 -0700775 put_pipe_version(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778static void
779gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
780{
781 struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (msg->errno < 0) {
Chuck Lever632f0d02012-09-14 17:23:43 -0400784 dprintk("RPC: %s releasing msg %p\n",
785 __func__, gss_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 atomic_inc(&gss_msg->count);
787 gss_unhash_msg(gss_msg);
\"J. Bruce Fields\b03568c2008-12-23 16:06:55 -0500788 if (msg->errno == -ETIMEDOUT)
789 warn_gssd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 gss_release_msg(gss_msg);
791 }
792}
793
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400794static void gss_pipes_dentries_destroy(struct rpc_auth *auth)
795{
796 struct gss_auth *gss_auth;
797
798 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400799 if (gss_auth->pipe[0]->dentry) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400800 rpc_unlink(gss_auth->pipe[0]->dentry);
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400801 gss_auth->pipe[0]->dentry = NULL;
802 }
803 if (gss_auth->pipe[1]->dentry) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400804 rpc_unlink(gss_auth->pipe[1]->dentry);
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400805 gss_auth->pipe[1]->dentry = NULL;
806 }
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400807}
808
809static int gss_pipes_dentries_create(struct rpc_auth *auth)
810{
811 int err;
812 struct gss_auth *gss_auth;
813 struct rpc_clnt *clnt;
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400814 struct dentry *dentry;
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400815
816 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
817 clnt = gss_auth->client;
818
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400819 dentry = rpc_mkpipe_dentry(clnt->cl_dentry, "gssd",
820 clnt, gss_auth->pipe[1]);
821 if (IS_ERR(dentry)) {
822 err = PTR_ERR(dentry);
823 goto err;
824 }
825 gss_auth->pipe[1]->dentry = dentry;
826 dentry = rpc_mkpipe_dentry(clnt->cl_dentry, gss_auth->mech->gm_name,
827 clnt, gss_auth->pipe[0]);
828 if (IS_ERR(dentry)) {
829 err = PTR_ERR(dentry);
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400830 goto err_unlink_pipe_1;
831 }
832 return 0;
833
834err_unlink_pipe_1:
835 rpc_unlink(gss_auth->pipe[1]->dentry);
Trond Myklebust6b2fddd2013-08-26 16:05:11 -0400836 gss_auth->pipe[1]->dentry = NULL;
837err:
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400838 return err;
839}
840
841static void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt,
842 struct rpc_auth *auth)
843{
Trond Myklebuste726340a2013-08-23 14:42:29 -0400844 struct gss_auth *gss_auth = container_of(auth, struct gss_auth,
845 rpc_auth);
846 struct net *net = gss_auth->net;
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400847 struct super_block *sb;
848
849 sb = rpc_get_sb_net(net);
850 if (sb) {
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400851 if (clnt->cl_dentry)
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400852 gss_pipes_dentries_destroy(auth);
853 rpc_put_sb_net(net);
854 }
855}
856
857static int gss_pipes_dentries_create_net(struct rpc_clnt *clnt,
858 struct rpc_auth *auth)
859{
Trond Myklebuste726340a2013-08-23 14:42:29 -0400860 struct gss_auth *gss_auth = container_of(auth, struct gss_auth,
861 rpc_auth);
862 struct net *net = gss_auth->net;
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400863 struct super_block *sb;
864 int err = 0;
865
866 sb = rpc_get_sb_net(net);
867 if (sb) {
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400868 if (clnt->cl_dentry)
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400869 err = gss_pipes_dentries_create(auth);
870 rpc_put_sb_net(net);
871 }
872 return err;
873}
874
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800875/*
876 * NOTE: we have the opportunity to use different
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * parameters based on the input flavor (which must be a pseudoflavor)
878 */
879static struct rpc_auth *
Trond Myklebustc2190662013-08-26 19:23:04 -0400880gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Trond Myklebustc2190662013-08-26 19:23:04 -0400882 rpc_authflavor_t flavor = args->pseudoflavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct gss_auth *gss_auth;
884 struct rpc_auth * auth;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000885 int err = -ENOMEM; /* XXX? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Chuck Lever8885cb32007-01-31 12:14:05 -0500887 dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (!try_module_get(THIS_MODULE))
J. Bruce Fields6a192752005-06-22 17:16:23 +0000890 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
892 goto out_dec;
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400893 gss_auth->target_name = NULL;
Trond Myklebustc2190662013-08-26 19:23:04 -0400894 if (args->target_name) {
895 gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400896 if (gss_auth->target_name == NULL)
897 goto err_free;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 gss_auth->client = clnt;
Trond Myklebuste726340a2013-08-23 14:42:29 -0400900 gss_auth->net = get_net(rpc_net_ns(clnt));
J. Bruce Fields6a192752005-06-22 17:16:23 +0000901 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
903 if (!gss_auth->mech) {
Trond Myklebust9b1d75b2013-05-01 09:17:50 -0400904 dprintk("RPC: Pseudoflavor %d not found!\n", flavor);
Trond Myklebuste726340a2013-08-23 14:42:29 -0400905 goto err_put_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
J. Bruce Fields438b6fd2005-06-22 17:16:23 +0000908 if (gss_auth->service == 0)
909 goto err_put_mech;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 auth = &gss_auth->rpc_auth;
911 auth->au_cslack = GSS_CRED_SLACK >> 2;
912 auth->au_rslack = GSS_VERF_SLACK >> 2;
913 auth->au_ops = &authgss_ops;
914 auth->au_flavor = flavor;
915 atomic_set(&auth->au_count, 1);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400916 kref_init(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -0500918 /*
919 * Note: if we created the old pipe first, then someone who
920 * examined the directory at the right moment might conclude
921 * that we supported only the old pipe. So we instead create
922 * the new pipe first.
923 */
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300924 gss_auth->pipe[1] = rpc_mkpipe_data(&gss_upcall_ops_v1,
925 RPC_PIPE_WAIT_FOR_OPEN);
926 if (IS_ERR(gss_auth->pipe[1])) {
927 err = PTR_ERR(gss_auth->pipe[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto err_put_mech;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300931 gss_auth->pipe[0] = rpc_mkpipe_data(&gss_upcall_ops_v0,
932 RPC_PIPE_WAIT_FOR_OPEN);
933 if (IS_ERR(gss_auth->pipe[0])) {
934 err = PTR_ERR(gss_auth->pipe[0]);
935 goto err_destroy_pipe_1;
936 }
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400937 err = gss_pipes_dentries_create_net(clnt, auth);
938 if (err)
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300939 goto err_destroy_pipe_0;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400940 err = rpcauth_init_credcache(auth);
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400941 if (err)
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400942 goto err_unlink_pipes;
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return auth;
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400945err_unlink_pipes:
946 gss_pipes_dentries_destroy_net(clnt, auth);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300947err_destroy_pipe_0:
948 rpc_destroy_pipe_data(gss_auth->pipe[0]);
949err_destroy_pipe_1:
950 rpc_destroy_pipe_data(gss_auth->pipe[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951err_put_mech:
952 gss_mech_put(gss_auth->mech);
Trond Myklebuste726340a2013-08-23 14:42:29 -0400953err_put_net:
954 put_net(gss_auth->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955err_free:
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400956 kfree(gss_auth->target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(gss_auth);
958out_dec:
959 module_put(THIS_MODULE);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000960 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static void
Trond Myklebust0285ed12007-06-27 14:29:12 -0400964gss_free(struct gss_auth *gss_auth)
965{
Stanislav Kinsburskyccdc28f2012-01-11 19:18:09 +0400966 gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300967 rpc_destroy_pipe_data(gss_auth->pipe[0]);
968 rpc_destroy_pipe_data(gss_auth->pipe[1]);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400969 gss_mech_put(gss_auth->mech);
Trond Myklebuste726340a2013-08-23 14:42:29 -0400970 put_net(gss_auth->net);
Trond Myklebustbd4a3eb2013-08-23 14:02:24 -0400971 kfree(gss_auth->target_name);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400972
973 kfree(gss_auth);
974 module_put(THIS_MODULE);
975}
976
977static void
978gss_free_callback(struct kref *kref)
979{
980 struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
981
982 gss_free(gss_auth);
983}
984
985static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986gss_destroy(struct rpc_auth *auth)
987{
988 struct gss_auth *gss_auth;
989
Chuck Lever8885cb32007-01-31 12:14:05 -0500990 dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
991 auth, auth->au_flavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400993 rpcauth_destroy_credcache(auth);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400996 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400999/*
1000 * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
1001 * to the server with the GSS control procedure field set to
1002 * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
1003 * all RPCSEC_GSS state associated with that context.
1004 */
1005static int
1006gss_destroying_context(struct rpc_cred *cred)
1007{
1008 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1009 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1010 struct rpc_task *task;
1011
1012 if (gss_cred->gc_ctx == NULL ||
Jeff Layton6dcd3922008-12-23 15:21:57 -05001013 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001014 return 0;
1015
1016 gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
1017 cred->cr_ops = &gss_nullops;
1018
1019 /* Take a reference to ensure the cred will be destroyed either
1020 * by the RPC call or by the put_rpccred() below */
1021 get_rpccred(cred);
1022
Trond Myklebust080a1f12008-04-19 14:22:31 -04001023 task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001024 if (!IS_ERR(task))
1025 rpc_put_task(task);
1026
1027 put_rpccred(cred);
1028 return 1;
1029}
1030
1031/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * to create a new cred or context, so they check that things have been
1033 * allocated before freeing them. */
1034static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -04001035gss_do_free_ctx(struct gss_cl_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Chuck Lever632f0d02012-09-14 17:23:43 -04001037 dprintk("RPC: %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Trond Myklebust0d8a3742010-08-01 20:14:55 -04001039 gss_delete_sec_context(&ctx->gc_gss_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 kfree(ctx->gc_wire_ctx.data);
1041 kfree(ctx);
1042}
1043
1044static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -04001045gss_free_ctx_callback(struct rcu_head *head)
1046{
1047 struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
1048 gss_do_free_ctx(ctx);
1049}
1050
1051static void
1052gss_free_ctx(struct gss_cl_ctx *ctx)
1053{
1054 call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
1055}
1056
1057static void
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001058gss_free_cred(struct gss_cred *gss_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Chuck Lever632f0d02012-09-14 17:23:43 -04001060 dprintk("RPC: %s cred=%p\n", __func__, gss_cred);
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001061 kfree(gss_cred);
1062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001064static void
1065gss_free_cred_callback(struct rcu_head *head)
1066{
1067 struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
1068 gss_free_cred(gss_cred);
1069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001071static void
Jeff Layton6dcd3922008-12-23 15:21:57 -05001072gss_destroy_nullcred(struct rpc_cred *cred)
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001073{
Trond Myklebust5d28dc82007-06-26 19:18:38 -04001074 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
Trond Myklebust0285ed12007-06-27 14:29:12 -04001075 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
Trond Myklebust5d28dc82007-06-26 19:18:38 -04001076 struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
1077
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001078 RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
Trond Myklebust31be5bf2007-06-24 15:55:26 -04001079 call_rcu(&cred->cr_rcu, gss_free_cred_callback);
Trond Myklebust5d28dc82007-06-26 19:18:38 -04001080 if (ctx)
1081 gss_put_ctx(ctx);
Trond Myklebust0285ed12007-06-27 14:29:12 -04001082 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Jeff Layton6dcd3922008-12-23 15:21:57 -05001085static void
1086gss_destroy_cred(struct rpc_cred *cred)
1087{
1088
1089 if (gss_destroying_context(cred))
1090 return;
1091 gss_destroy_nullcred(cred);
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/*
1095 * Lookup RPCSEC_GSS cred for the current process
1096 */
1097static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -05001098gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Trond Myklebust8a317762006-02-01 12:18:36 -05001100 return rpcauth_lookup_credcache(auth, acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -05001104gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1107 struct gss_cred *cred = NULL;
1108 int err = -ENOMEM;
1109
Chuck Lever632f0d02012-09-14 17:23:43 -04001110 dprintk("RPC: %s for uid %d, flavor %d\n",
Eric W. Biedermancdba3212013-02-01 17:10:52 -08001111 __func__, from_kuid(&init_user_ns, acred->uid),
1112 auth->au_flavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Trond Myklebust0f38b872008-06-10 18:31:01 -04001114 if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 goto out_err;
1116
Trond Myklebust5fe47552007-06-23 19:55:31 -04001117 rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * Note: in order to force a call to call_refresh(), we deliberately
1120 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
1121 */
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001122 cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 cred->gc_service = gss_auth->service;
Trond Myklebust68c97152012-01-03 13:22:46 -05001124 cred->gc_principal = NULL;
1125 if (acred->machine_cred)
1126 cred->gc_principal = acred->principal;
Trond Myklebust0285ed12007-06-27 14:29:12 -04001127 kref_get(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return &cred->gc_base;
1129
1130out_err:
Chuck Lever632f0d02012-09-14 17:23:43 -04001131 dprintk("RPC: %s failed with error %d\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return ERR_PTR(err);
1133}
1134
1135static int
Trond Myklebustfba3bad2006-02-01 12:19:27 -05001136gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1137{
1138 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1139 struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1140 int err;
1141
1142 do {
1143 err = gss_create_upcall(gss_auth, gss_cred);
1144 } while (err == -EAGAIN);
1145 return err;
1146}
1147
1148static int
Trond Myklebust8a317762006-02-01 12:18:36 -05001149gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1152
Trond Myklebustcd019f72008-04-17 17:03:58 -04001153 if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
Trond Myklebust8a317762006-02-01 12:18:36 -05001154 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* Don't match with creds that have expired. */
Trond Myklebustcd019f72008-04-17 17:03:58 -04001156 if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1157 return 0;
1158 if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
Trond Myklebust8a317762006-02-01 12:18:36 -05001160out:
Trond Myklebust68c97152012-01-03 13:22:46 -05001161 if (acred->principal != NULL) {
1162 if (gss_cred->gc_principal == NULL)
1163 return 0;
1164 return strcmp(acred->principal, gss_cred->gc_principal) == 0;
1165 }
1166 if (gss_cred->gc_principal != NULL)
Trond Myklebust7c67db32008-04-07 20:50:11 -04001167 return 0;
Eric W. Biederman0b4d51b2013-02-01 16:39:32 -08001168 return uid_eq(rc->cr_uid, acred->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/*
1172* Marshal credentials.
1173* Maybe we should keep a cached credential for performance reasons.
1174*/
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001175static __be32 *
1176gss_marshal(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001178 struct rpc_rqst *req = task->tk_rqstp;
1179 struct rpc_cred *cred = req->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1181 gc_base);
1182 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001183 __be32 *cred_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 u32 maj_stat = 0;
1185 struct xdr_netobj mic;
1186 struct kvec iov;
1187 struct xdr_buf verf_buf;
1188
Chuck Lever632f0d02012-09-14 17:23:43 -04001189 dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 *p++ = htonl(RPC_AUTH_GSS);
1192 cred_len = p++;
1193
1194 spin_lock(&ctx->gc_seq_lock);
1195 req->rq_seqno = ctx->gc_seq++;
1196 spin_unlock(&ctx->gc_seq_lock);
1197
1198 *p++ = htonl((u32) RPC_GSS_VERSION);
1199 *p++ = htonl((u32) ctx->gc_proc);
1200 *p++ = htonl((u32) req->rq_seqno);
1201 *p++ = htonl((u32) gss_cred->gc_service);
1202 p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
1203 *cred_len = htonl((p - (cred_len + 1)) << 2);
1204
1205 /* We compute the checksum for the verifier over the xdr-encoded bytes
1206 * starting with the xid and ending at the end of the credential: */
Trond Myklebusta4f08352013-01-08 09:10:21 -05001207 iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
Chuck Lever808012f2005-08-25 16:25:49 -07001208 req->rq_snd_buf.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
1210 xdr_buf_from_iov(&iov, &verf_buf);
1211
1212 /* set verifier flavor*/
1213 *p++ = htonl(RPC_AUTH_GSS);
1214
1215 mic.data = (u8 *)(p + 1);
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001216 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001218 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 } else if (maj_stat != 0) {
1220 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
1221 goto out_put_ctx;
1222 }
1223 p = xdr_encode_opaque(p, NULL, mic.len);
1224 gss_put_ctx(ctx);
1225 return p;
1226out_put_ctx:
1227 gss_put_ctx(ctx);
1228 return NULL;
1229}
1230
Trond Myklebustcd019f72008-04-17 17:03:58 -04001231static int gss_renew_cred(struct rpc_task *task)
1232{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001233 struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
Trond Myklebustcd019f72008-04-17 17:03:58 -04001234 struct gss_cred *gss_cred = container_of(oldcred,
1235 struct gss_cred,
1236 gc_base);
1237 struct rpc_auth *auth = oldcred->cr_auth;
1238 struct auth_cred acred = {
1239 .uid = oldcred->cr_uid,
Trond Myklebust68c97152012-01-03 13:22:46 -05001240 .principal = gss_cred->gc_principal,
1241 .machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
Trond Myklebustcd019f72008-04-17 17:03:58 -04001242 };
1243 struct rpc_cred *new;
1244
1245 new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1246 if (IS_ERR(new))
1247 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -04001248 task->tk_rqstp->rq_cred = new;
Trond Myklebustcd019f72008-04-17 17:03:58 -04001249 put_rpccred(oldcred);
1250 return 0;
1251}
1252
Trond Myklebust126e2162010-05-13 12:55:38 -04001253static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1254{
1255 if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1256 unsigned long now = jiffies;
1257 unsigned long begin, expire;
1258 struct gss_cred *gss_cred;
1259
1260 gss_cred = container_of(cred, struct gss_cred, gc_base);
1261 begin = gss_cred->gc_upcall_timestamp;
1262 expire = begin + gss_expired_cred_retry_delay * HZ;
1263
1264 if (time_in_range_open(now, begin, expire))
1265 return 1;
1266 }
1267 return 0;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271* Refresh credentials. XXX - finish
1272*/
1273static int
1274gss_refresh(struct rpc_task *task)
1275{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001276 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustcd019f72008-04-17 17:03:58 -04001277 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Trond Myklebust126e2162010-05-13 12:55:38 -04001279 if (gss_cred_is_negative_entry(cred))
1280 return -EKEYEXPIRED;
1281
Trond Myklebustcd019f72008-04-17 17:03:58 -04001282 if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1283 !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1284 ret = gss_renew_cred(task);
1285 if (ret < 0)
1286 goto out;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001287 cred = task->tk_rqstp->rq_cred;
Trond Myklebustcd019f72008-04-17 17:03:58 -04001288 }
1289
1290 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1291 ret = gss_refresh_upcall(task);
1292out:
1293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001296/* Dummy refresh routine: used only when destroying the context */
1297static int
1298gss_refresh_null(struct rpc_task *task)
1299{
1300 return -EACCES;
1301}
1302
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001303static __be32 *
1304gss_validate(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001306 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001308 __be32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct kvec iov;
1310 struct xdr_buf verf_buf;
1311 struct xdr_netobj mic;
1312 u32 flav,len;
1313 u32 maj_stat;
1314
Chuck Lever632f0d02012-09-14 17:23:43 -04001315 dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 flav = ntohl(*p++);
1318 if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001319 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (flav != RPC_AUTH_GSS)
1321 goto out_bad;
1322 seq = htonl(task->tk_rqstp->rq_seqno);
1323 iov.iov_base = &seq;
1324 iov.iov_len = sizeof(seq);
1325 xdr_buf_from_iov(&iov, &verf_buf);
1326 mic.data = (u8 *)p;
1327 mic.len = len;
1328
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001329 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001331 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001332 if (maj_stat) {
Chuck Lever632f0d02012-09-14 17:23:43 -04001333 dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1334 task->tk_pid, __func__, maj_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out_bad;
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001336 }
J. Bruce Fields24b26052005-10-13 16:54:53 -04001337 /* We leave it to unwrap to calculate au_rslack. For now we just
1338 * calculate the length of the verifier: */
Trond Myklebust1be27f32007-06-27 14:29:04 -04001339 cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 gss_put_ctx(ctx);
Chuck Lever632f0d02012-09-14 17:23:43 -04001341 dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1342 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return p + XDR_QUADLEN(len);
1344out_bad:
1345 gss_put_ctx(ctx);
Chuck Lever632f0d02012-09-14 17:23:43 -04001346 dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return NULL;
1348}
1349
Chuck Lever9f06c712010-12-14 14:59:18 +00001350static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
1351 __be32 *p, void *obj)
1352{
1353 struct xdr_stream xdr;
1354
1355 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
1356 encode(rqstp, &xdr, obj);
1357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359static inline int
1360gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Chuck Lever9f06c712010-12-14 14:59:18 +00001361 kxdreproc_t encode, struct rpc_rqst *rqstp,
1362 __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1365 struct xdr_buf integ_buf;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001366 __be32 *integ_len = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct xdr_netobj mic;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001368 u32 offset;
1369 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct kvec *iov;
1371 u32 maj_stat = 0;
1372 int status = -EIO;
1373
1374 integ_len = p++;
1375 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1376 *p++ = htonl(rqstp->rq_seqno);
1377
Chuck Lever9f06c712010-12-14 14:59:18 +00001378 gss_wrap_req_encode(encode, rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (xdr_buf_subsegment(snd_buf, &integ_buf,
1381 offset, snd_buf->len - offset))
1382 return status;
1383 *integ_len = htonl(integ_buf.len);
1384
1385 /* guess whether we're in the head or the tail: */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001386 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 iov = snd_buf->tail;
1388 else
1389 iov = snd_buf->head;
1390 p = iov->iov_base + iov->iov_len;
1391 mic.data = (u8 *)(p + 1);
1392
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001393 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 status = -EIO; /* XXX? */
1395 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001396 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 else if (maj_stat)
1398 return status;
1399 q = xdr_encode_opaque(p, NULL, mic.len);
1400
1401 offset = (u8 *)q - (u8 *)p;
1402 iov->iov_len += offset;
1403 snd_buf->len += offset;
1404 return 0;
1405}
1406
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001407static void
1408priv_release_snd_buf(struct rpc_rqst *rqstp)
1409{
1410 int i;
1411
1412 for (i=0; i < rqstp->rq_enc_pages_num; i++)
1413 __free_page(rqstp->rq_enc_pages[i]);
1414 kfree(rqstp->rq_enc_pages);
1415}
1416
1417static int
1418alloc_enc_pages(struct rpc_rqst *rqstp)
1419{
1420 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1421 int first, last, i;
1422
1423 if (snd_buf->page_len == 0) {
1424 rqstp->rq_enc_pages_num = 0;
1425 return 0;
1426 }
1427
1428 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1429 last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
1430 rqstp->rq_enc_pages_num = last - first + 1 + 1;
1431 rqstp->rq_enc_pages
1432 = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
1433 GFP_NOFS);
1434 if (!rqstp->rq_enc_pages)
1435 goto out;
1436 for (i=0; i < rqstp->rq_enc_pages_num; i++) {
1437 rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
1438 if (rqstp->rq_enc_pages[i] == NULL)
1439 goto out_free;
1440 }
1441 rqstp->rq_release_snd_buf = priv_release_snd_buf;
1442 return 0;
1443out_free:
Trond Myklebustcdead7c2010-03-19 15:36:22 -04001444 rqstp->rq_enc_pages_num = i;
1445 priv_release_snd_buf(rqstp);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001446out:
1447 return -EAGAIN;
1448}
1449
1450static inline int
1451gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Chuck Lever9f06c712010-12-14 14:59:18 +00001452 kxdreproc_t encode, struct rpc_rqst *rqstp,
1453 __be32 *p, void *obj)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001454{
1455 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1456 u32 offset;
1457 u32 maj_stat;
1458 int status;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001459 __be32 *opaque_len;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001460 struct page **inpages;
1461 int first;
1462 int pad;
1463 struct kvec *iov;
1464 char *tmp;
1465
1466 opaque_len = p++;
1467 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1468 *p++ = htonl(rqstp->rq_seqno);
1469
Chuck Lever9f06c712010-12-14 14:59:18 +00001470 gss_wrap_req_encode(encode, rqstp, p, obj);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001471
1472 status = alloc_enc_pages(rqstp);
1473 if (status)
1474 return status;
1475 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1476 inpages = snd_buf->pages + first;
1477 snd_buf->pages = rqstp->rq_enc_pages;
1478 snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
Kevin Coffman75610422010-03-17 13:02:47 -04001479 /*
1480 * Give the tail its own page, in case we need extra space in the
1481 * head when wrapping:
1482 *
1483 * call_allocate() allocates twice the slack space required
1484 * by the authentication flavor to rq_callsize.
1485 * For GSS, slack is GSS_CRED_SLACK.
1486 */
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001487 if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1488 tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
1489 memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
1490 snd_buf->tail[0].iov_base = tmp;
1491 }
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001492 maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
Kevin Coffman75610422010-03-17 13:02:47 -04001493 /* slack space should prevent this ever happening: */
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001494 BUG_ON(snd_buf->len > snd_buf->buflen);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001495 status = -EIO;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001496 /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
1497 * done anyway, so it's safe to put the request on the wire: */
1498 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001499 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001500 else if (maj_stat)
1501 return status;
1502
1503 *opaque_len = htonl(snd_buf->len - offset);
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 pad = 3 - ((snd_buf->len - offset - 1) & 3);
1511 memset(p, 0, pad);
1512 iov->iov_len += pad;
1513 snd_buf->len += pad;
1514
1515 return 0;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static int
1519gss_wrap_req(struct rpc_task *task,
Chuck Lever9f06c712010-12-14 14:59:18 +00001520 kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001522 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1524 gc_base);
1525 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1526 int status = -EIO;
1527
Chuck Lever632f0d02012-09-14 17:23:43 -04001528 dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
1530 /* The spec seems a little ambiguous here, but I think that not
1531 * wrapping context destruction requests makes the most sense.
1532 */
Chuck Lever9f06c712010-12-14 14:59:18 +00001533 gss_wrap_req_encode(encode, rqstp, p, obj);
1534 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto out;
1536 }
1537 switch (gss_cred->gc_service) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00001538 case RPC_GSS_SVC_NONE:
1539 gss_wrap_req_encode(encode, rqstp, p, obj);
1540 status = 0;
1541 break;
1542 case RPC_GSS_SVC_INTEGRITY:
1543 status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
1544 break;
1545 case RPC_GSS_SVC_PRIVACY:
1546 status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
1547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549out:
1550 gss_put_ctx(ctx);
Chuck Lever632f0d02012-09-14 17:23:43 -04001551 dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return status;
1553}
1554
1555static inline int
1556gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001557 struct rpc_rqst *rqstp, __be32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1560 struct xdr_buf integ_buf;
1561 struct xdr_netobj mic;
1562 u32 data_offset, mic_offset;
1563 u32 integ_len;
1564 u32 maj_stat;
1565 int status = -EIO;
1566
1567 integ_len = ntohl(*(*p)++);
1568 if (integ_len & 3)
1569 return status;
1570 data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1571 mic_offset = integ_len + data_offset;
1572 if (mic_offset > rcv_buf->len)
1573 return status;
1574 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1575 return status;
1576
1577 if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
1578 mic_offset - data_offset))
1579 return status;
1580
1581 if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1582 return status;
1583
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001584 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001586 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (maj_stat != GSS_S_COMPLETE)
1588 return status;
1589 return 0;
1590}
1591
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001592static inline int
1593gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001594 struct rpc_rqst *rqstp, __be32 **p)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001595{
1596 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1597 u32 offset;
1598 u32 opaque_len;
1599 u32 maj_stat;
1600 int status = -EIO;
1601
1602 opaque_len = ntohl(*(*p)++);
1603 offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1604 if (offset + opaque_len > rcv_buf->len)
1605 return status;
1606 /* remove padding: */
1607 rcv_buf->len = offset + opaque_len;
1608
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001609 maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001610 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001611 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001612 if (maj_stat != GSS_S_COMPLETE)
1613 return status;
1614 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1615 return status;
1616
1617 return 0;
1618}
1619
Chuck Leverbf269552010-12-14 14:59:29 +00001620static int
1621gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1622 __be32 *p, void *obj)
1623{
1624 struct xdr_stream xdr;
1625
1626 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1627 return decode(rqstp, &xdr, obj);
1628}
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static int
1631gss_unwrap_resp(struct rpc_task *task,
Chuck Leverbf269552010-12-14 14:59:29 +00001632 kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Trond Myklebusta17c2152010-07-31 14:29:08 -04001634 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1636 gc_base);
1637 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001638 __be32 *savedp = p;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001639 struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
1640 int savedlen = head->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int status = -EIO;
1642
1643 if (ctx->gc_proc != RPC_GSS_PROC_DATA)
1644 goto out_decode;
1645 switch (gss_cred->gc_service) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00001646 case RPC_GSS_SVC_NONE:
1647 break;
1648 case RPC_GSS_SVC_INTEGRITY:
1649 status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
1650 if (status)
1651 goto out;
1652 break;
1653 case RPC_GSS_SVC_PRIVACY:
1654 status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
1655 if (status)
1656 goto out;
1657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
J. Bruce Fields24b26052005-10-13 16:54:53 -04001659 /* take into account extra slack for integrity and privacy cases: */
Trond Myklebust1be27f32007-06-27 14:29:04 -04001660 cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001661 + (savedlen - head->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662out_decode:
Chuck Leverbf269552010-12-14 14:59:29 +00001663 status = gss_unwrap_req_decode(decode, rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664out:
1665 gss_put_ctx(ctx);
Chuck Lever632f0d02012-09-14 17:23:43 -04001666 dprintk("RPC: %5u %s returning %d\n",
1667 task->tk_pid, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return status;
1669}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001670
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001671static const struct rpc_authops authgss_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .owner = THIS_MODULE,
1673 .au_flavor = RPC_AUTH_GSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 .au_name = "RPCSEC_GSS",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .create = gss_create,
1676 .destroy = gss_destroy,
1677 .lookup_cred = gss_lookup_cred,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001678 .crcreate = gss_create_cred,
1679 .pipes_create = gss_pipes_dentries_create,
1680 .pipes_destroy = gss_pipes_dentries_destroy,
Chuck Lever6a1a1e32012-07-11 16:31:08 -04001681 .list_pseudoflavors = gss_mech_list_pseudoflavors,
Chuck Lever9568c5e2013-03-16 15:54:43 -04001682 .info2flavor = gss_mech_info2flavor,
Chuck Levera77c8062013-03-16 15:55:10 -04001683 .flavor2info = gss_mech_flavor2info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684};
1685
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001686static const struct rpc_credops gss_credops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 .cr_name = "AUTH_GSS",
1688 .crdestroy = gss_destroy_cred,
Trond Myklebustfba3bad2006-02-01 12:19:27 -05001689 .cr_init = gss_cred_init,
Trond Myklebust5c691042008-03-12 16:21:07 -04001690 .crbind = rpcauth_generic_bind_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 .crmatch = gss_match,
1692 .crmarshal = gss_marshal,
1693 .crrefresh = gss_refresh,
1694 .crvalidate = gss_validate,
1695 .crwrap_req = gss_wrap_req,
1696 .crunwrap_resp = gss_unwrap_resp,
1697};
1698
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001699static const struct rpc_credops gss_nullops = {
1700 .cr_name = "AUTH_GSS",
Jeff Layton6dcd3922008-12-23 15:21:57 -05001701 .crdestroy = gss_destroy_nullcred,
Trond Myklebust5c691042008-03-12 16:21:07 -04001702 .crbind = rpcauth_generic_bind_cred,
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001703 .crmatch = gss_match,
1704 .crmarshal = gss_marshal,
1705 .crrefresh = gss_refresh_null,
1706 .crvalidate = gss_validate,
1707 .crwrap_req = gss_wrap_req,
1708 .crunwrap_resp = gss_unwrap_resp,
1709};
1710
Trond Myklebustb693ba42009-08-09 15:14:15 -04001711static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
Peng Taoc1225152011-09-22 21:50:10 -04001712 .upcall = rpc_pipe_generic_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 .downcall = gss_pipe_downcall,
1714 .destroy_msg = gss_pipe_destroy_msg,
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -05001715 .open_pipe = gss_pipe_open_v0,
1716 .release_pipe = gss_pipe_release,
1717};
1718
Trond Myklebustb693ba42009-08-09 15:14:15 -04001719static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
Peng Taoc1225152011-09-22 21:50:10 -04001720 .upcall = rpc_pipe_generic_upcall,
\"J. Bruce Fields\34769fc2008-12-23 16:16:37 -05001721 .downcall = gss_pipe_downcall,
1722 .destroy_msg = gss_pipe_destroy_msg,
1723 .open_pipe = gss_pipe_open_v1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 .release_pipe = gss_pipe_release,
1725};
1726
Stanislav Kinsburskya1db4102012-01-19 21:42:37 +04001727static __net_init int rpcsec_gss_init_net(struct net *net)
1728{
1729 return gss_svc_init_net(net);
1730}
1731
1732static __net_exit void rpcsec_gss_exit_net(struct net *net)
1733{
1734 gss_svc_shutdown_net(net);
1735}
1736
1737static struct pernet_operations rpcsec_gss_net_ops = {
1738 .init = rpcsec_gss_init_net,
1739 .exit = rpcsec_gss_exit_net,
1740};
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/*
1743 * Initialize RPCSEC_GSS module
1744 */
1745static int __init init_rpcsec_gss(void)
1746{
1747 int err = 0;
1748
1749 err = rpcauth_register(&authgss_ops);
1750 if (err)
1751 goto out;
1752 err = gss_svc_init();
1753 if (err)
1754 goto out_unregister;
Stanislav Kinsburskya1db4102012-01-19 21:42:37 +04001755 err = register_pernet_subsys(&rpcsec_gss_net_ops);
1756 if (err)
1757 goto out_svc_exit;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -05001758 rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return 0;
Stanislav Kinsburskya1db4102012-01-19 21:42:37 +04001760out_svc_exit:
1761 gss_svc_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762out_unregister:
1763 rpcauth_unregister(&authgss_ops);
1764out:
1765 return err;
1766}
1767
1768static void __exit exit_rpcsec_gss(void)
1769{
Stanislav Kinsburskya1db4102012-01-19 21:42:37 +04001770 unregister_pernet_subsys(&rpcsec_gss_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 gss_svc_shutdown();
1772 rpcauth_unregister(&authgss_ops);
Jesper Dangaard Brouerbf126912009-06-08 03:11:48 +00001773 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Chuck Lever71afa852013-03-16 15:54:16 -04001776MODULE_ALIAS("rpc-auth-6");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777MODULE_LICENSE("GPL");
Trond Myklebust126e2162010-05-13 12:55:38 -04001778module_param_named(expired_cred_retry_delay,
1779 gss_expired_cred_retry_delay,
1780 uint, 0644);
1781MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1782 "the RPC engine retries an expired credential");
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784module_init(init_rpcsec_gss)
1785module_exit(exit_rpcsec_gss)