Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/net/sunrpc/auth_gss/auth_gss.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * RPCSEC_GSS client authentication. |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/sched.h> |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 44 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Trond Myklebust | abfdbd5 | 2013-05-15 11:28:54 -0700 | [diff] [blame] | 55 | #include "../netns.h" |
| 56 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 57 | static const struct rpc_authops authgss_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 59 | static const struct rpc_credops gss_credops; |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 60 | static const struct rpc_credops gss_nullops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 62 | #define GSS_RETRY_EXPIRED 5 |
| 63 | static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #ifdef RPC_DEBUG |
| 66 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 67 | #endif |
| 68 | |
Kevin Coffman | 725f286 | 2010-03-17 13:02:46 -0400 | [diff] [blame] | 69 | #define GSS_CRED_SLACK (RPC_MAX_AUTH_SIZE * 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* length of a krb5 verifier (48), plus data added before arguments when |
| 71 | * using integrity (two 4-byte integers): */ |
Olga Kornievskaia | adeb813 | 2006-12-04 20:22:34 -0500 | [diff] [blame] | 72 | #define GSS_VERF_SLACK 100 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | struct gss_auth { |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 75 | struct kref kref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | struct rpc_auth rpc_auth; |
| 77 | struct gss_api_mech *mech; |
| 78 | enum rpc_gss_svc service; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct rpc_clnt *client; |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 80 | struct net *net; |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 81 | /* |
| 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 Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 87 | struct rpc_pipe *pipe[2]; |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 88 | const char *target_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 91 | /* pipe_version >= 0 if and only if someone has a pipe open. */ |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 92 | static DEFINE_SPINLOCK(pipe_version_lock); |
| 93 | static struct rpc_wait_queue pipe_version_rpc_waitqueue; |
| 94 | static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue); |
\"J. Bruce Fields\ | cf81939 | 2008-12-23 16:10:19 -0500 | [diff] [blame] | 95 | |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 96 | static void gss_free_ctx(struct gss_cl_ctx *); |
Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 97 | static const struct rpc_pipe_ops gss_upcall_ops_v0; |
| 98 | static const struct rpc_pipe_ops gss_upcall_ops_v1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static inline struct gss_cl_ctx * |
| 101 | gss_get_ctx(struct gss_cl_ctx *ctx) |
| 102 | { |
| 103 | atomic_inc(&ctx->count); |
| 104 | return ctx; |
| 105 | } |
| 106 | |
| 107 | static inline void |
| 108 | gss_put_ctx(struct gss_cl_ctx *ctx) |
| 109 | { |
| 110 | if (atomic_dec_and_test(&ctx->count)) |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 111 | gss_free_ctx(ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 114 | /* gss_cred_set_ctx: |
| 115 | * called by gss_upcall_callback and gss_create_upcall in order |
| 116 | * to set the gss context. The actual exchange of an old context |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 117 | * and a new one is protected by the pipe->lock. |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 118 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | static void |
| 120 | gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx) |
| 121 | { |
| 122 | struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base); |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 123 | |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 124 | if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)) |
| 125 | return; |
Trond Myklebust | 7b6962b | 2008-04-17 16:53:01 -0400 | [diff] [blame] | 126 | gss_get_ctx(ctx); |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 127 | rcu_assign_pointer(gss_cred->gc_ctx, ctx); |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 128 | set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 129 | smp_mb__before_clear_bit(); |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 130 | clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static const void * |
| 134 | simple_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 | |
| 143 | static inline const void * |
| 144 | simple_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 Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 155 | dest->data = kmemdup(p, len, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (unlikely(dest->data == NULL)) |
| 157 | return ERR_PTR(-ENOMEM); |
| 158 | dest->len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return q; |
| 160 | } |
| 161 | |
| 162 | static struct gss_cl_ctx * |
| 163 | gss_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 Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 168 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (gss_cred->gc_ctx) |
| 170 | ctx = gss_get_ctx(gss_cred->gc_ctx); |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 171 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return ctx; |
| 173 | } |
| 174 | |
| 175 | static struct gss_cl_ctx * |
| 176 | gss_alloc_context(void) |
| 177 | { |
| 178 | struct gss_cl_ctx *ctx; |
| 179 | |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 180 | ctx = kzalloc(sizeof(*ctx), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (ctx != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | 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) |
| 191 | static const void * |
| 192 | gss_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 Adamson | 620038f | 2012-11-27 10:34:20 -0500 | [diff] [blame] | 197 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | u32 window_size; |
| 199 | int ret; |
| 200 | |
Andy Adamson | 620038f | 2012-11-27 10:34:20 -0500 | [diff] [blame] | 201 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | 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 Adamson | 620038f | 2012-11-27 10:34:20 -0500 | [diff] [blame] | 210 | ctx->gc_expiry = now + ((unsigned long)timeout * HZ); |
| 211 | /* Sequence number window. Determines the maximum number of |
| 212 | * simultaneous requests |
| 213 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | 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 Layton | dc5ddce | 2010-01-07 09:42:02 -0500 | [diff] [blame] | 220 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | 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 Sorce | 400f26b | 2012-05-25 18:09:53 -0400 | [diff] [blame] | 243 | ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (ret < 0) { |
| 245 | p = ERR_PTR(ret); |
| 246 | goto err; |
| 247 | } |
Andy Adamson | 620038f | 2012-11-27 10:34:20 -0500 | [diff] [blame] | 248 | dprintk("RPC: %s Success. gc_expiry %lu now %lu timeout %u\n", |
| 249 | __func__, ctx->gc_expiry, now, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return q; |
| 251 | err: |
Jeff Layton | 173db30 | 2013-02-07 10:29:06 -0500 | [diff] [blame] | 252 | dprintk("RPC: %s returns error %ld\n", __func__, -PTR_ERR(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return p; |
| 254 | } |
| 255 | |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 256 | #define UPCALL_BUF_LEN 128 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | struct gss_upcall_msg { |
| 259 | atomic_t count; |
Eric W. Biederman | 7eaf040 | 2013-02-01 16:31:17 -0800 | [diff] [blame] | 260 | kuid_t uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | struct rpc_pipe_msg msg; |
| 262 | struct list_head list; |
| 263 | struct gss_auth *auth; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 264 | struct rpc_pipe *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct rpc_wait_queue rpc_waitqueue; |
| 266 | wait_queue_head_t waitqueue; |
| 267 | struct gss_cl_ctx *ctx; |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 268 | char databuf[UPCALL_BUF_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 271 | static int get_pipe_version(struct net *net) |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 272 | { |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 273 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 274 | int ret; |
| 275 | |
| 276 | spin_lock(&pipe_version_lock); |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 277 | if (sn->pipe_version >= 0) { |
| 278 | atomic_inc(&sn->pipe_users); |
| 279 | ret = sn->pipe_version; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 280 | } else |
| 281 | ret = -EAGAIN; |
| 282 | spin_unlock(&pipe_version_lock); |
| 283 | return ret; |
| 284 | } |
| 285 | |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 286 | static void put_pipe_version(struct net *net) |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 287 | { |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 288 | 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\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 292 | spin_unlock(&pipe_version_lock); |
| 293 | } |
| 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | static void |
| 297 | gss_release_msg(struct gss_upcall_msg *gss_msg) |
| 298 | { |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 299 | struct net *net = gss_msg->auth->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (!atomic_dec_and_test(&gss_msg->count)) |
| 301 | return; |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 302 | put_pipe_version(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | BUG_ON(!list_empty(&gss_msg->list)); |
| 304 | if (gss_msg->ctx != NULL) |
| 305 | gss_put_ctx(gss_msg->ctx); |
Trond Myklebust | f6a1cc8 | 2008-02-22 17:06:55 -0500 | [diff] [blame] | 306 | rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | kfree(gss_msg); |
| 308 | } |
| 309 | |
| 310 | static struct gss_upcall_msg * |
Eric W. Biederman | 7eaf040 | 2013-02-01 16:31:17 -0800 | [diff] [blame] | 311 | __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | struct gss_upcall_msg *pos; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 314 | list_for_each_entry(pos, &pipe->in_downcall, list) { |
Eric W. Biederman | 0b4d51b | 2013-02-01 16:39:32 -0800 | [diff] [blame] | 315 | if (!uid_eq(pos->uid, uid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | continue; |
| 317 | atomic_inc(&pos->count); |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 318 | dprintk("RPC: %s found msg %p\n", __func__, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return pos; |
| 320 | } |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 321 | dprintk("RPC: %s found nothing\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return NULL; |
| 323 | } |
| 324 | |
\\\"J. Bruce Fields\\\ | 720b8f2 | 2008-06-09 16:51:33 -0400 | [diff] [blame] | 325 | /* Try to add an upcall to the pipefs queue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | * 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 | */ |
| 329 | static inline struct gss_upcall_msg * |
Suresh Jayaraman | 053e324 | 2009-12-09 23:15:22 +0530 | [diff] [blame] | 330 | gss_add_msg(struct gss_upcall_msg *gss_msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 332 | struct rpc_pipe *pipe = gss_msg->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | struct gss_upcall_msg *old; |
| 334 | |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 335 | spin_lock(&pipe->lock); |
| 336 | old = __gss_find_upcall(pipe, gss_msg->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | if (old == NULL) { |
| 338 | atomic_inc(&gss_msg->count); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 339 | list_add(&gss_msg->list, &pipe->in_downcall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } else |
| 341 | gss_msg = old; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 342 | spin_unlock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return gss_msg; |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | __gss_unhash_msg(struct gss_upcall_msg *gss_msg) |
| 348 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 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 | |
| 355 | static void |
| 356 | gss_unhash_msg(struct gss_upcall_msg *gss_msg) |
| 357 | { |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 358 | struct rpc_pipe *pipe = gss_msg->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 360 | if (list_empty(&gss_msg->list)) |
| 361 | return; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 362 | spin_lock(&pipe->lock); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 363 | if (!list_empty(&gss_msg->list)) |
| 364 | __gss_unhash_msg(gss_msg); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 365 | spin_unlock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static void |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 369 | gss_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 | |
| 386 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | gss_upcall_callback(struct rpc_task *task) |
| 388 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 389 | struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | struct gss_cred, gc_base); |
| 391 | struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 392 | struct rpc_pipe *pipe = gss_msg->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 394 | spin_lock(&pipe->lock); |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 395 | gss_handle_downcall_result(gss_cred, gss_msg); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 396 | spin_unlock(&pipe->lock); |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 397 | task->tk_status = gss_msg->msg.errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | gss_release_msg(gss_msg); |
| 399 | } |
| 400 | |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 401 | static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg) |
| 402 | { |
Eric W. Biederman | 90602c7 | 2013-02-02 00:25:43 -0800 | [diff] [blame] | 403 | 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\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Olga Kornievskaia | 8b1c7bf | 2008-12-23 16:19:26 -0500 | [diff] [blame] | 410 | static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg, |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 411 | const char *service_name, |
| 412 | const char *target_name) |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 413 | { |
Trond Myklebust | 683ac66 | 2010-04-08 14:09:58 -0400 | [diff] [blame] | 414 | struct gss_api_mech *mech = gss_msg->auth->mech; |
Olga Kornievskaia | 8b1c7bf | 2008-12-23 16:19:26 -0500 | [diff] [blame] | 415 | char *p = gss_msg->databuf; |
| 416 | int len = 0; |
| 417 | |
| 418 | gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ", |
Trond Myklebust | 683ac66 | 2010-04-08 14:09:58 -0400 | [diff] [blame] | 419 | mech->gm_name, |
Eric W. Biederman | 90602c7 | 2013-02-02 00:25:43 -0800 | [diff] [blame] | 420 | from_kuid(&init_user_ns, gss_msg->uid)); |
Olga Kornievskaia | 8b1c7bf | 2008-12-23 16:19:26 -0500 | [diff] [blame] | 421 | p += gss_msg->msg.len; |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 422 | if (target_name) { |
| 423 | len = sprintf(p, "target=%s ", target_name); |
Olga Kornievskaia | 8b1c7bf | 2008-12-23 16:19:26 -0500 | [diff] [blame] | 424 | p += len; |
| 425 | gss_msg->msg.len += len; |
| 426 | } |
Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 427 | if (service_name != NULL) { |
| 428 | len = sprintf(p, "service=%s ", service_name); |
Olga Kornievskaia | 2efef70 | 2008-12-23 16:19:56 -0500 | [diff] [blame] | 429 | p += len; |
| 430 | gss_msg->msg.len += len; |
| 431 | } |
Trond Myklebust | 683ac66 | 2010-04-08 14:09:58 -0400 | [diff] [blame] | 432 | if (mech->gm_upcall_enctypes) { |
Kevin Coffman | f862822 | 2011-03-03 00:51:41 +0000 | [diff] [blame] | 433 | len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes); |
Trond Myklebust | 683ac66 | 2010-04-08 14:09:58 -0400 | [diff] [blame] | 434 | p += len; |
| 435 | gss_msg->msg.len += len; |
| 436 | } |
Olga Kornievskaia | 8b1c7bf | 2008-12-23 16:19:26 -0500 | [diff] [blame] | 437 | len = sprintf(p, "\n"); |
| 438 | gss_msg->msg.len += len; |
| 439 | |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 440 | gss_msg->msg.data = gss_msg->databuf; |
| 441 | BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN); |
| 442 | } |
| 443 | |
Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 444 | static struct gss_upcall_msg * |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 445 | gss_alloc_msg(struct gss_auth *gss_auth, |
Eric W. Biederman | 7eaf040 | 2013-02-01 16:31:17 -0800 | [diff] [blame] | 446 | kuid_t uid, const char *service_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | struct gss_upcall_msg *gss_msg; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 449 | int vers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 451 | gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS); |
\"J. Bruce Fields\ | db75b3d | 2008-12-23 16:07:13 -0500 | [diff] [blame] | 452 | if (gss_msg == NULL) |
| 453 | return ERR_PTR(-ENOMEM); |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 454 | vers = get_pipe_version(gss_auth->net); |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 455 | if (vers < 0) { |
| 456 | kfree(gss_msg); |
| 457 | return ERR_PTR(vers); |
| 458 | } |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 459 | gss_msg->pipe = gss_auth->pipe[vers]; |
\"J. Bruce Fields\ | db75b3d | 2008-12-23 16:07:13 -0500 | [diff] [blame] | 460 | 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\ | db75b3d | 2008-12-23 16:07:13 -0500 | [diff] [blame] | 464 | gss_msg->uid = uid; |
| 465 | gss_msg->auth = gss_auth; |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 466 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | return gss_msg; |
| 473 | } |
| 474 | |
| 475 | static struct gss_upcall_msg * |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 476 | gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 478 | struct gss_cred *gss_cred = container_of(cred, |
| 479 | struct gss_cred, gc_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct gss_upcall_msg *gss_new, *gss_msg; |
Eric W. Biederman | 7eaf040 | 2013-02-01 16:31:17 -0800 | [diff] [blame] | 481 | kuid_t uid = cred->cr_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 483 | gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal); |
\"J. Bruce Fields\ | db75b3d | 2008-12-23 16:07:13 -0500 | [diff] [blame] | 484 | if (IS_ERR(gss_new)) |
| 485 | return gss_new; |
Suresh Jayaraman | 053e324 | 2009-12-09 23:15:22 +0530 | [diff] [blame] | 486 | gss_msg = gss_add_msg(gss_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (gss_msg == gss_new) { |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 488 | int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | if (res) { |
| 490 | gss_unhash_msg(gss_new); |
| 491 | gss_msg = ERR_PTR(res); |
| 492 | } |
| 493 | } else |
| 494 | gss_release_msg(gss_new); |
| 495 | return gss_msg; |
| 496 | } |
| 497 | |
\"J. Bruce Fields\ | b03568c | 2008-12-23 16:06:55 -0500 | [diff] [blame] | 498 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | static inline int |
| 511 | gss_refresh_upcall(struct rpc_task *task) |
| 512 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 513 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | 4a8c134 | 2007-06-07 10:14:14 -0400 | [diff] [blame] | 514 | struct gss_auth *gss_auth = container_of(cred->cr_auth, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 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 Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 519 | struct rpc_pipe *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | int err = 0; |
| 521 | |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 522 | dprintk("RPC: %5u %s for uid %u\n", |
Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 523 | task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid)); |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 524 | gss_msg = gss_setup_upcall(gss_auth, cred); |
Roel Kluin | 480e324 | 2009-12-08 13:13:03 -0500 | [diff] [blame] | 525 | if (PTR_ERR(gss_msg) == -EAGAIN) { |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 526 | /* 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 Schumaker | d1a8016 | 2011-04-13 14:31:28 -0400 | [diff] [blame] | 531 | return -EAGAIN; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (IS_ERR(gss_msg)) { |
| 534 | err = PTR_ERR(gss_msg); |
| 535 | goto out; |
| 536 | } |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 537 | pipe = gss_msg->pipe; |
| 538 | spin_lock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (gss_cred->gc_upcall != NULL) |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 540 | rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL); |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 541 | else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | 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 Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 546 | rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback); |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 547 | } else { |
| 548 | gss_handle_downcall_result(gss_cred, gss_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | err = gss_msg->msg.errno; |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 550 | } |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 551 | spin_unlock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | gss_release_msg(gss_msg); |
| 553 | out: |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 554 | dprintk("RPC: %5u %s for uid %u result %d\n", |
Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 555 | task->tk_pid, __func__, |
| 556 | from_kuid(&init_user_ns, cred->cr_uid), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return err; |
| 558 | } |
| 559 | |
| 560 | static inline int |
| 561 | gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred) |
| 562 | { |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 563 | struct net *net = gss_auth->net; |
Trond Myklebust | abfdbd5 | 2013-05-15 11:28:54 -0700 | [diff] [blame] | 564 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 565 | struct rpc_pipe *pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | struct rpc_cred *cred = &gss_cred->gc_base; |
| 567 | struct gss_upcall_msg *gss_msg; |
Trond Myklebust | abfdbd5 | 2013-05-15 11:28:54 -0700 | [diff] [blame] | 568 | unsigned long timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | DEFINE_WAIT(wait); |
Trond Myklebust | d36ccb9 | 2013-05-15 10:46:33 -0700 | [diff] [blame] | 570 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 572 | dprintk("RPC: %s for uid %u\n", |
| 573 | __func__, from_kuid(&init_user_ns, cred->cr_uid)); |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 574 | retry: |
Trond Myklebust | d36ccb9 | 2013-05-15 10:46:33 -0700 | [diff] [blame] | 575 | err = 0; |
Trond Myklebust | abfdbd5 | 2013-05-15 11:28:54 -0700 | [diff] [blame] | 576 | /* 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 Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 580 | gss_msg = gss_setup_upcall(gss_auth, cred); |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 581 | if (PTR_ERR(gss_msg) == -EAGAIN) { |
| 582 | err = wait_event_interruptible_timeout(pipe_version_waitqueue, |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 583 | sn->pipe_version >= 0, timeout); |
| 584 | if (sn->pipe_version < 0) { |
Trond Myklebust | abfdbd5 | 2013-05-15 11:28:54 -0700 | [diff] [blame] | 585 | if (err == 0) |
| 586 | sn->gssd_running = 0; |
Bryan Schumaker | d1a8016 | 2011-04-13 14:31:28 -0400 | [diff] [blame] | 587 | warn_gssd(); |
| 588 | err = -EACCES; |
| 589 | } |
Trond Myklebust | d36ccb9 | 2013-05-15 10:46:33 -0700 | [diff] [blame] | 590 | if (err < 0) |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 591 | goto out; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 592 | goto retry; |
| 593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (IS_ERR(gss_msg)) { |
| 595 | err = PTR_ERR(gss_msg); |
| 596 | goto out; |
| 597 | } |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 598 | pipe = gss_msg->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | for (;;) { |
Trond Myklebust | 5afa9133 | 2011-06-17 10:14:59 -0400 | [diff] [blame] | 600 | prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 601 | spin_lock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | break; |
| 604 | } |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 605 | spin_unlock(&pipe->lock); |
Trond Myklebust | 5afa9133 | 2011-06-17 10:14:59 -0400 | [diff] [blame] | 606 | if (fatal_signal_pending(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | err = -ERESTARTSYS; |
| 608 | goto out_intr; |
| 609 | } |
| 610 | schedule(); |
| 611 | } |
| 612 | if (gss_msg->ctx) |
Trond Myklebust | 7b6962b | 2008-04-17 16:53:01 -0400 | [diff] [blame] | 613 | gss_cred_set_ctx(cred, gss_msg->ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | else |
| 615 | err = gss_msg->msg.errno; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 616 | spin_unlock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | out_intr: |
| 618 | finish_wait(&gss_msg->waitqueue, &wait); |
| 619 | gss_release_msg(gss_msg); |
| 620 | out: |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 621 | dprintk("RPC: %s for uid %u result %d\n", |
Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 622 | __func__, from_kuid(&init_user_ns, cred->cr_uid), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return err; |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | #define MSG_BUF_MAXSIZE 1024 |
| 627 | |
| 628 | static ssize_t |
| 629 | gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) |
| 630 | { |
| 631 | const void *p, *end; |
| 632 | void *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | struct gss_upcall_msg *gss_msg; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 634 | struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | struct gss_cl_ctx *ctx; |
Eric W. Biederman | 90602c7 | 2013-02-02 00:25:43 -0800 | [diff] [blame] | 636 | uid_t id; |
| 637 | kuid_t uid; |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 638 | ssize_t err = -EFBIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | if (mlen > MSG_BUF_MAXSIZE) |
| 641 | goto out; |
| 642 | err = -ENOMEM; |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 643 | buf = kmalloc(mlen, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (!buf) |
| 645 | goto out; |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | err = -EFAULT; |
| 648 | if (copy_from_user(buf, src, mlen)) |
| 649 | goto err; |
| 650 | |
| 651 | end = (const void *)((char *)buf + mlen); |
Eric W. Biederman | 90602c7 | 2013-02-02 00:25:43 -0800 | [diff] [blame] | 652 | p = simple_get_bytes(buf, end, &id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (IS_ERR(p)) { |
| 654 | err = PTR_ERR(p); |
| 655 | goto err; |
| 656 | } |
| 657 | |
Eric W. Biederman | 90602c7 | 2013-02-02 00:25:43 -0800 | [diff] [blame] | 658 | uid = make_kuid(&init_user_ns, id); |
| 659 | if (!uid_valid(uid)) { |
| 660 | err = -EINVAL; |
| 661 | goto err; |
| 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | err = -ENOMEM; |
| 665 | ctx = gss_alloc_context(); |
| 666 | if (ctx == NULL) |
| 667 | goto err; |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 668 | |
| 669 | err = -ENOENT; |
| 670 | /* Find a matching upcall */ |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 671 | spin_lock(&pipe->lock); |
| 672 | gss_msg = __gss_find_upcall(pipe, uid); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 673 | if (gss_msg == NULL) { |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 674 | spin_unlock(&pipe->lock); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 675 | goto err_put_ctx; |
| 676 | } |
| 677 | list_del_init(&gss_msg->list); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 678 | spin_unlock(&pipe->lock); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 679 | |
Trond Myklebust | 6e84c7b | 2007-06-07 15:31:36 -0400 | [diff] [blame] | 680 | p = gss_fill_context(p, end, ctx, gss_msg->auth->mech); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (IS_ERR(p)) { |
| 682 | err = PTR_ERR(p); |
Jeff Layton | 486bad2 | 2009-12-18 16:28:20 -0500 | [diff] [blame] | 683 | switch (err) { |
| 684 | case -EACCES: |
Jeff Layton | dc5ddce | 2010-01-07 09:42:02 -0500 | [diff] [blame] | 685 | case -EKEYEXPIRED: |
Jeff Layton | 486bad2 | 2009-12-18 16:28:20 -0500 | [diff] [blame] | 686 | 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 Dunlap | 6c85309 | 2010-01-06 17:26:27 -0500 | [diff] [blame] | 697 | "gss_fill_context: %zd\n", __func__, err); |
Jeff Layton | 486bad2 | 2009-12-18 16:28:20 -0500 | [diff] [blame] | 698 | BUG(); |
| 699 | } |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 700 | goto err_release_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 702 | gss_msg->ctx = gss_get_ctx(ctx); |
| 703 | err = mlen; |
| 704 | |
| 705 | err_release_msg: |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 706 | spin_lock(&pipe->lock); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 707 | __gss_unhash_msg(gss_msg); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 708 | spin_unlock(&pipe->lock); |
Trond Myklebust | 3b68aae | 2007-06-07 10:14:15 -0400 | [diff] [blame] | 709 | gss_release_msg(gss_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | err_put_ctx: |
| 711 | gss_put_ctx(ctx); |
| 712 | err: |
| 713 | kfree(buf); |
| 714 | out: |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 715 | dprintk("RPC: %s returning %Zd\n", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return err; |
| 717 | } |
| 718 | |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 719 | static int gss_pipe_open(struct inode *inode, int new_version) |
\"J. Bruce Fields\ | cf81939 | 2008-12-23 16:10:19 -0500 | [diff] [blame] | 720 | { |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 721 | struct net *net = inode->i_sb->s_fs_info; |
| 722 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 723 | int ret = 0; |
| 724 | |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 725 | spin_lock(&pipe_version_lock); |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 726 | if (sn->pipe_version < 0) { |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 727 | /* First open of any gss pipe determines the version: */ |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 728 | sn->pipe_version = new_version; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 729 | rpc_wake_up(&pipe_version_rpc_waitqueue); |
| 730 | wake_up(&pipe_version_waitqueue); |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 731 | } else if (sn->pipe_version != new_version) { |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 732 | /* Trying to open a pipe of a different version */ |
| 733 | ret = -EBUSY; |
| 734 | goto out; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 735 | } |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 736 | atomic_inc(&sn->pipe_users); |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 737 | out: |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 738 | spin_unlock(&pipe_version_lock); |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 739 | return ret; |
| 740 | |
| 741 | } |
| 742 | |
| 743 | static int gss_pipe_open_v0(struct inode *inode) |
| 744 | { |
| 745 | return gss_pipe_open(inode, 0); |
| 746 | } |
| 747 | |
| 748 | static int gss_pipe_open_v1(struct inode *inode) |
| 749 | { |
| 750 | return gss_pipe_open(inode, 1); |
\"J. Bruce Fields\ | cf81939 | 2008-12-23 16:10:19 -0500 | [diff] [blame] | 751 | } |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | static void |
| 754 | gss_pipe_release(struct inode *inode) |
| 755 | { |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 756 | struct net *net = inode->i_sb->s_fs_info; |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 757 | struct rpc_pipe *pipe = RPC_I(inode)->pipe; |
Trond Myklebust | 6e84c7b | 2007-06-07 15:31:36 -0400 | [diff] [blame] | 758 | struct gss_upcall_msg *gss_msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 760 | restart: |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 761 | spin_lock(&pipe->lock); |
| 762 | list_for_each_entry(gss_msg, &pipe->in_downcall, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 764 | if (!list_empty(&gss_msg->msg.list)) |
| 765 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | gss_msg->msg.errno = -EPIPE; |
| 767 | atomic_inc(&gss_msg->count); |
| 768 | __gss_unhash_msg(gss_msg); |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 769 | spin_unlock(&pipe->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | gss_release_msg(gss_msg); |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 771 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
Stanislav Kinsbursky | 9beae46 | 2011-12-26 15:43:57 +0300 | [diff] [blame] | 773 | spin_unlock(&pipe->lock); |
\"J. Bruce Fields\ | cf81939 | 2008-12-23 16:10:19 -0500 | [diff] [blame] | 774 | |
Trond Myklebust | 2aed8b4 | 2013-05-15 10:27:32 -0700 | [diff] [blame] | 775 | put_pipe_version(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void |
| 779 | gss_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | if (msg->errno < 0) { |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 784 | dprintk("RPC: %s releasing msg %p\n", |
| 785 | __func__, gss_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | atomic_inc(&gss_msg->count); |
| 787 | gss_unhash_msg(gss_msg); |
\"J. Bruce Fields\ | b03568c | 2008-12-23 16:06:55 -0500 | [diff] [blame] | 788 | if (msg->errno == -ETIMEDOUT) |
| 789 | warn_gssd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | gss_release_msg(gss_msg); |
| 791 | } |
| 792 | } |
| 793 | |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 794 | static 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 Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 799 | if (gss_auth->pipe[0]->dentry) { |
Stanislav Kinsbursky | 80df9d2 | 2012-01-11 19:18:17 +0400 | [diff] [blame] | 800 | rpc_unlink(gss_auth->pipe[0]->dentry); |
Trond Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 801 | gss_auth->pipe[0]->dentry = NULL; |
| 802 | } |
| 803 | if (gss_auth->pipe[1]->dentry) { |
Stanislav Kinsbursky | 80df9d2 | 2012-01-11 19:18:17 +0400 | [diff] [blame] | 804 | rpc_unlink(gss_auth->pipe[1]->dentry); |
Trond Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 805 | gss_auth->pipe[1]->dentry = NULL; |
| 806 | } |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static 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 Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 814 | struct dentry *dentry; |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 815 | |
| 816 | gss_auth = container_of(auth, struct gss_auth, rpc_auth); |
| 817 | clnt = gss_auth->client; |
| 818 | |
Trond Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 819 | 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 Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 830 | goto err_unlink_pipe_1; |
| 831 | } |
| 832 | return 0; |
| 833 | |
| 834 | err_unlink_pipe_1: |
| 835 | rpc_unlink(gss_auth->pipe[1]->dentry); |
Trond Myklebust | 6b2fddd | 2013-08-26 16:05:11 -0400 | [diff] [blame^] | 836 | gss_auth->pipe[1]->dentry = NULL; |
| 837 | err: |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 838 | return err; |
| 839 | } |
| 840 | |
| 841 | static void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt, |
| 842 | struct rpc_auth *auth) |
| 843 | { |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 844 | struct gss_auth *gss_auth = container_of(auth, struct gss_auth, |
| 845 | rpc_auth); |
| 846 | struct net *net = gss_auth->net; |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 847 | struct super_block *sb; |
| 848 | |
| 849 | sb = rpc_get_sb_net(net); |
| 850 | if (sb) { |
Stanislav Kinsbursky | 30507f5 | 2012-01-11 19:18:42 +0400 | [diff] [blame] | 851 | if (clnt->cl_dentry) |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 852 | gss_pipes_dentries_destroy(auth); |
| 853 | rpc_put_sb_net(net); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static int gss_pipes_dentries_create_net(struct rpc_clnt *clnt, |
| 858 | struct rpc_auth *auth) |
| 859 | { |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 860 | struct gss_auth *gss_auth = container_of(auth, struct gss_auth, |
| 861 | rpc_auth); |
| 862 | struct net *net = gss_auth->net; |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 863 | struct super_block *sb; |
| 864 | int err = 0; |
| 865 | |
| 866 | sb = rpc_get_sb_net(net); |
| 867 | if (sb) { |
Stanislav Kinsbursky | 30507f5 | 2012-01-11 19:18:42 +0400 | [diff] [blame] | 868 | if (clnt->cl_dentry) |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 869 | err = gss_pipes_dentries_create(auth); |
| 870 | rpc_put_sb_net(net); |
| 871 | } |
| 872 | return err; |
| 873 | } |
| 874 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 875 | /* |
| 876 | * NOTE: we have the opportunity to use different |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | * parameters based on the input flavor (which must be a pseudoflavor) |
| 878 | */ |
| 879 | static struct rpc_auth * |
Trond Myklebust | c219066 | 2013-08-26 19:23:04 -0400 | [diff] [blame] | 880 | gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Trond Myklebust | c219066 | 2013-08-26 19:23:04 -0400 | [diff] [blame] | 882 | rpc_authflavor_t flavor = args->pseudoflavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | struct gss_auth *gss_auth; |
| 884 | struct rpc_auth * auth; |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 885 | int err = -ENOMEM; /* XXX? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 887 | dprintk("RPC: creating GSS authenticator for client %p\n", clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | if (!try_module_get(THIS_MODULE)) |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 890 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL))) |
| 892 | goto out_dec; |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 893 | gss_auth->target_name = NULL; |
Trond Myklebust | c219066 | 2013-08-26 19:23:04 -0400 | [diff] [blame] | 894 | if (args->target_name) { |
| 895 | gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL); |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 896 | if (gss_auth->target_name == NULL) |
| 897 | goto err_free; |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | gss_auth->client = clnt; |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 900 | gss_auth->net = get_net(rpc_net_ns(clnt)); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 901 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor); |
| 903 | if (!gss_auth->mech) { |
Trond Myklebust | 9b1d75b | 2013-05-01 09:17:50 -0400 | [diff] [blame] | 904 | dprintk("RPC: Pseudoflavor %d not found!\n", flavor); |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 905 | goto err_put_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor); |
J. Bruce Fields | 438b6fd | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 908 | if (gss_auth->service == 0) |
| 909 | goto err_put_mech; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | 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 Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 916 | kref_init(&gss_auth->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 918 | /* |
| 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 Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 924 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | goto err_put_mech; |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 931 | 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 Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 937 | err = gss_pipes_dentries_create_net(clnt, auth); |
| 938 | if (err) |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 939 | goto err_destroy_pipe_0; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 940 | err = rpcauth_init_credcache(auth); |
Trond Myklebust | 07a2bf1 | 2007-06-09 15:42:01 -0400 | [diff] [blame] | 941 | if (err) |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 942 | goto err_unlink_pipes; |
Trond Myklebust | 07a2bf1 | 2007-06-09 15:42:01 -0400 | [diff] [blame] | 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return auth; |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 945 | err_unlink_pipes: |
| 946 | gss_pipes_dentries_destroy_net(clnt, auth); |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 947 | err_destroy_pipe_0: |
| 948 | rpc_destroy_pipe_data(gss_auth->pipe[0]); |
| 949 | err_destroy_pipe_1: |
| 950 | rpc_destroy_pipe_data(gss_auth->pipe[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | err_put_mech: |
| 952 | gss_mech_put(gss_auth->mech); |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 953 | err_put_net: |
| 954 | put_net(gss_auth->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | err_free: |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 956 | kfree(gss_auth->target_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | kfree(gss_auth); |
| 958 | out_dec: |
| 959 | module_put(THIS_MODULE); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 960 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static void |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 964 | gss_free(struct gss_auth *gss_auth) |
| 965 | { |
Stanislav Kinsbursky | ccdc28f | 2012-01-11 19:18:09 +0400 | [diff] [blame] | 966 | gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth); |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 967 | rpc_destroy_pipe_data(gss_auth->pipe[0]); |
| 968 | rpc_destroy_pipe_data(gss_auth->pipe[1]); |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 969 | gss_mech_put(gss_auth->mech); |
Trond Myklebust | e726340a | 2013-08-23 14:42:29 -0400 | [diff] [blame] | 970 | put_net(gss_auth->net); |
Trond Myklebust | bd4a3eb | 2013-08-23 14:02:24 -0400 | [diff] [blame] | 971 | kfree(gss_auth->target_name); |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 972 | |
| 973 | kfree(gss_auth); |
| 974 | module_put(THIS_MODULE); |
| 975 | } |
| 976 | |
| 977 | static void |
| 978 | gss_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 | |
| 985 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | gss_destroy(struct rpc_auth *auth) |
| 987 | { |
| 988 | struct gss_auth *gss_auth; |
| 989 | |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 990 | dprintk("RPC: destroying GSS authenticator %p flavor %d\n", |
| 991 | auth, auth->au_flavor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 993 | rpcauth_destroy_credcache(auth); |
| 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | gss_auth = container_of(auth, struct gss_auth, rpc_auth); |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 996 | kref_put(&gss_auth->kref, gss_free_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 999 | /* |
| 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 | */ |
| 1005 | static int |
| 1006 | gss_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 Layton | 6dcd392 | 2008-12-23 15:21:57 -0500 | [diff] [blame] | 1013 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0) |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1014 | 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 Myklebust | 080a1f1 | 2008-04-19 14:22:31 -0400 | [diff] [blame] | 1023 | task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT); |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1024 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | * to create a new cred or context, so they check that things have been |
| 1033 | * allocated before freeing them. */ |
| 1034 | static void |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 1035 | gss_do_free_ctx(struct gss_cl_ctx *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1037 | dprintk("RPC: %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Trond Myklebust | 0d8a374 | 2010-08-01 20:14:55 -0400 | [diff] [blame] | 1039 | gss_delete_sec_context(&ctx->gc_gss_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | kfree(ctx->gc_wire_ctx.data); |
| 1041 | kfree(ctx); |
| 1042 | } |
| 1043 | |
| 1044 | static void |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 1045 | gss_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 | |
| 1051 | static void |
| 1052 | gss_free_ctx(struct gss_cl_ctx *ctx) |
| 1053 | { |
| 1054 | call_rcu(&ctx->gc_rcu, gss_free_ctx_callback); |
| 1055 | } |
| 1056 | |
| 1057 | static void |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1058 | gss_free_cred(struct gss_cred *gss_cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1060 | dprintk("RPC: %s cred=%p\n", __func__, gss_cred); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1061 | kfree(gss_cred); |
| 1062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1064 | static void |
| 1065 | gss_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1071 | static void |
Jeff Layton | 6dcd392 | 2008-12-23 15:21:57 -0500 | [diff] [blame] | 1072 | gss_destroy_nullcred(struct rpc_cred *cred) |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1073 | { |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 1074 | struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base); |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 1075 | struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth); |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 1076 | struct gss_cl_ctx *ctx = gss_cred->gc_ctx; |
| 1077 | |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1078 | RCU_INIT_POINTER(gss_cred->gc_ctx, NULL); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 1079 | call_rcu(&cred->cr_rcu, gss_free_cred_callback); |
Trond Myklebust | 5d28dc8 | 2007-06-26 19:18:38 -0400 | [diff] [blame] | 1080 | if (ctx) |
| 1081 | gss_put_ctx(ctx); |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 1082 | kref_put(&gss_auth->kref, gss_free_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeff Layton | 6dcd392 | 2008-12-23 15:21:57 -0500 | [diff] [blame] | 1085 | static void |
| 1086 | gss_destroy_cred(struct rpc_cred *cred) |
| 1087 | { |
| 1088 | |
| 1089 | if (gss_destroying_context(cred)) |
| 1090 | return; |
| 1091 | gss_destroy_nullcred(cred); |
| 1092 | } |
| 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * Lookup RPCSEC_GSS cred for the current process |
| 1096 | */ |
| 1097 | static struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1098 | gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | { |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1100 | return rpcauth_lookup_credcache(auth, acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1104 | gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | { |
| 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 Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1110 | dprintk("RPC: %s for uid %d, flavor %d\n", |
Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 1111 | __func__, from_kuid(&init_user_ns, acred->uid), |
| 1112 | auth->au_flavor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
Trond Myklebust | 0f38b87 | 2008-06-10 18:31:01 -0400 | [diff] [blame] | 1114 | if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | goto out_err; |
| 1116 | |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 1117 | rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | /* |
| 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 Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1122 | cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | cred->gc_service = gss_auth->service; |
Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 1124 | cred->gc_principal = NULL; |
| 1125 | if (acred->machine_cred) |
| 1126 | cred->gc_principal = acred->principal; |
Trond Myklebust | 0285ed1 | 2007-06-27 14:29:12 -0400 | [diff] [blame] | 1127 | kref_get(&gss_auth->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | return &cred->gc_base; |
| 1129 | |
| 1130 | out_err: |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1131 | dprintk("RPC: %s failed with error %d\n", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | return ERR_PTR(err); |
| 1133 | } |
| 1134 | |
| 1135 | static int |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 1136 | gss_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 | |
| 1148 | static int |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1149 | gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
| 1151 | struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base); |
| 1152 | |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1153 | if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags)) |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1154 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | /* Don't match with creds that have expired. */ |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1156 | if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry)) |
| 1157 | return 0; |
| 1158 | if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return 0; |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 1160 | out: |
Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 1161 | 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 Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 1167 | return 0; |
Eric W. Biederman | 0b4d51b | 2013-02-01 16:39:32 -0800 | [diff] [blame] | 1168 | return uid_eq(rc->cr_uid, acred->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * Marshal credentials. |
| 1173 | * Maybe we should keep a cached credential for performance reasons. |
| 1174 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1175 | static __be32 * |
| 1176 | gss_marshal(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1178 | struct rpc_rqst *req = task->tk_rqstp; |
| 1179 | struct rpc_cred *cred = req->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | 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 Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1183 | __be32 *cred_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | u32 maj_stat = 0; |
| 1185 | struct xdr_netobj mic; |
| 1186 | struct kvec iov; |
| 1187 | struct xdr_buf verf_buf; |
| 1188 | |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1189 | dprintk("RPC: %5u %s\n", task->tk_pid, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 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 Myklebust | a4f0835 | 2013-01-08 09:10:21 -0500 | [diff] [blame] | 1207 | iov.iov_base = xprt_skip_transport_header(req->rq_xprt, |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1208 | req->rq_snd_buf.head[0].iov_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1216 | maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | if (maj_stat == GSS_S_CONTEXT_EXPIRED) { |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1218 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | } 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; |
| 1226 | out_put_ctx: |
| 1227 | gss_put_ctx(ctx); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1231 | static int gss_renew_cred(struct rpc_task *task) |
| 1232 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1233 | struct rpc_cred *oldcred = task->tk_rqstp->rq_cred; |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1234 | 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 Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 1240 | .principal = gss_cred->gc_principal, |
| 1241 | .machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0), |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1242 | }; |
| 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 Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1248 | task->tk_rqstp->rq_cred = new; |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1249 | put_rpccred(oldcred); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 1253 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | /* |
| 1271 | * Refresh credentials. XXX - finish |
| 1272 | */ |
| 1273 | static int |
| 1274 | gss_refresh(struct rpc_task *task) |
| 1275 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1276 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1277 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 1279 | if (gss_cred_is_negative_entry(cred)) |
| 1280 | return -EKEYEXPIRED; |
| 1281 | |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1282 | 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 Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1287 | cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | cd019f7 | 2008-04-17 17:03:58 -0400 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags)) |
| 1291 | ret = gss_refresh_upcall(task); |
| 1292 | out: |
| 1293 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1296 | /* Dummy refresh routine: used only when destroying the context */ |
| 1297 | static int |
| 1298 | gss_refresh_null(struct rpc_task *task) |
| 1299 | { |
| 1300 | return -EACCES; |
| 1301 | } |
| 1302 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1303 | static __be32 * |
| 1304 | gss_validate(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1306 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred); |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1308 | __be32 seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | struct kvec iov; |
| 1310 | struct xdr_buf verf_buf; |
| 1311 | struct xdr_netobj mic; |
| 1312 | u32 flav,len; |
| 1313 | u32 maj_stat; |
| 1314 | |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1315 | dprintk("RPC: %5u %s\n", task->tk_pid, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | flav = ntohl(*p++); |
| 1318 | if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE) |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1319 | goto out_bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1329 | maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | if (maj_stat == GSS_S_CONTEXT_EXPIRED) |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1331 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1332 | if (maj_stat) { |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1333 | dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n", |
| 1334 | task->tk_pid, __func__, maj_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | goto out_bad; |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1336 | } |
J. Bruce Fields | 24b2605 | 2005-10-13 16:54:53 -0400 | [diff] [blame] | 1337 | /* We leave it to unwrap to calculate au_rslack. For now we just |
| 1338 | * calculate the length of the verifier: */ |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 1339 | cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | gss_put_ctx(ctx); |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1341 | dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n", |
| 1342 | task->tk_pid, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | return p + XDR_QUADLEN(len); |
| 1344 | out_bad: |
| 1345 | gss_put_ctx(ctx); |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1346 | dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return NULL; |
| 1348 | } |
| 1349 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1350 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | static inline int |
| 1360 | gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1361 | kxdreproc_t encode, struct rpc_rqst *rqstp, |
| 1362 | __be32 *p, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
| 1364 | struct xdr_buf *snd_buf = &rqstp->rq_snd_buf; |
| 1365 | struct xdr_buf integ_buf; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1366 | __be32 *integ_len = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | struct xdr_netobj mic; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1368 | u32 offset; |
| 1369 | __be32 *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | 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 Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1378 | gss_wrap_req_encode(encode, rqstp, p, obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 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 Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1386 | if (snd_buf->page_len || snd_buf->tail[0].iov_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1393 | maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | status = -EIO; /* XXX? */ |
| 1395 | if (maj_stat == GSS_S_CONTEXT_EXPIRED) |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1396 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | 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 Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1407 | static void |
| 1408 | priv_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 | |
| 1417 | static int |
| 1418 | alloc_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; |
| 1443 | out_free: |
Trond Myklebust | cdead7c | 2010-03-19 15:36:22 -0400 | [diff] [blame] | 1444 | rqstp->rq_enc_pages_num = i; |
| 1445 | priv_release_snd_buf(rqstp); |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1446 | out: |
| 1447 | return -EAGAIN; |
| 1448 | } |
| 1449 | |
| 1450 | static inline int |
| 1451 | gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1452 | kxdreproc_t encode, struct rpc_rqst *rqstp, |
| 1453 | __be32 *p, void *obj) |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1454 | { |
| 1455 | struct xdr_buf *snd_buf = &rqstp->rq_snd_buf; |
| 1456 | u32 offset; |
| 1457 | u32 maj_stat; |
| 1458 | int status; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1459 | __be32 *opaque_len; |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1460 | 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 Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1470 | gss_wrap_req_encode(encode, rqstp, p, obj); |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1471 | |
| 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 Coffman | 7561042 | 2010-03-17 13:02:47 -0400 | [diff] [blame] | 1479 | /* |
| 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 Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1487 | 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1492 | maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages); |
Kevin Coffman | 7561042 | 2010-03-17 13:02:47 -0400 | [diff] [blame] | 1493 | /* slack space should prevent this ever happening: */ |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1494 | BUG_ON(snd_buf->len > snd_buf->buflen); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1495 | status = -EIO; |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1496 | /* 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 Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1499 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1500 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | static int |
| 1519 | gss_wrap_req(struct rpc_task *task, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1520 | kxdreproc_t encode, void *rqstp, __be32 *p, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1522 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | 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 Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1528 | dprintk("RPC: %5u %s\n", task->tk_pid, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | 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 Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1533 | gss_wrap_req_encode(encode, rqstp, p, obj); |
| 1534 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | goto out; |
| 1536 | } |
| 1537 | switch (gss_cred->gc_service) { |
Joe Perches | 89f0e4f | 2011-07-01 09:43:12 +0000 | [diff] [blame] | 1538 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | out: |
| 1550 | gss_put_ctx(ctx); |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1551 | dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | return status; |
| 1553 | } |
| 1554 | |
| 1555 | static inline int |
| 1556 | gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1557 | struct rpc_rqst *rqstp, __be32 **p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | { |
| 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1584 | maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | if (maj_stat == GSS_S_CONTEXT_EXPIRED) |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1586 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | if (maj_stat != GSS_S_COMPLETE) |
| 1588 | return status; |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1592 | static inline int |
| 1593 | gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1594 | struct rpc_rqst *rqstp, __be32 **p) |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1595 | { |
| 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 Fields | 00fd6e1 | 2005-10-13 16:55:18 -0400 | [diff] [blame] | 1609 | maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf); |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1610 | if (maj_stat == GSS_S_CONTEXT_EXPIRED) |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 1611 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1612 | 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 Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1620 | static int |
| 1621 | gss_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 Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | static int |
| 1631 | gss_unwrap_resp(struct rpc_task *task, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1632 | kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 1634 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | 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 Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1638 | __be32 *savedp = p; |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1639 | struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head; |
| 1640 | int savedlen = head->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | int status = -EIO; |
| 1642 | |
| 1643 | if (ctx->gc_proc != RPC_GSS_PROC_DATA) |
| 1644 | goto out_decode; |
| 1645 | switch (gss_cred->gc_service) { |
Joe Perches | 89f0e4f | 2011-07-01 09:43:12 +0000 | [diff] [blame] | 1646 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
J. Bruce Fields | 24b2605 | 2005-10-13 16:54:53 -0400 | [diff] [blame] | 1659 | /* take into account extra slack for integrity and privacy cases: */ |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 1660 | cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp) |
J. Bruce Fields | 2d2da60c | 2005-10-13 16:54:58 -0400 | [diff] [blame] | 1661 | + (savedlen - head->iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | out_decode: |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1663 | status = gss_unwrap_req_decode(decode, rqstp, p, obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | out: |
| 1665 | gss_put_ctx(ctx); |
Chuck Lever | 632f0d0 | 2012-09-14 17:23:43 -0400 | [diff] [blame] | 1666 | dprintk("RPC: %5u %s returning %d\n", |
| 1667 | task->tk_pid, __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return status; |
| 1669 | } |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1670 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 1671 | static const struct rpc_authops authgss_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | .owner = THIS_MODULE, |
| 1673 | .au_flavor = RPC_AUTH_GSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | .au_name = "RPCSEC_GSS", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | .create = gss_create, |
| 1676 | .destroy = gss_destroy, |
| 1677 | .lookup_cred = gss_lookup_cred, |
Stanislav Kinsbursky | 80df9d2 | 2012-01-11 19:18:17 +0400 | [diff] [blame] | 1678 | .crcreate = gss_create_cred, |
| 1679 | .pipes_create = gss_pipes_dentries_create, |
| 1680 | .pipes_destroy = gss_pipes_dentries_destroy, |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 1681 | .list_pseudoflavors = gss_mech_list_pseudoflavors, |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 1682 | .info2flavor = gss_mech_info2flavor, |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 1683 | .flavor2info = gss_mech_flavor2info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | }; |
| 1685 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 1686 | static const struct rpc_credops gss_credops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | .cr_name = "AUTH_GSS", |
| 1688 | .crdestroy = gss_destroy_cred, |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 1689 | .cr_init = gss_cred_init, |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 1690 | .crbind = rpcauth_generic_bind_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | .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 Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1699 | static const struct rpc_credops gss_nullops = { |
| 1700 | .cr_name = "AUTH_GSS", |
Jeff Layton | 6dcd392 | 2008-12-23 15:21:57 -0500 | [diff] [blame] | 1701 | .crdestroy = gss_destroy_nullcred, |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 1702 | .crbind = rpcauth_generic_bind_cred, |
Trond Myklebust | 0df7fb7 | 2007-06-26 17:04:57 -0400 | [diff] [blame] | 1703 | .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 Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 1711 | static const struct rpc_pipe_ops gss_upcall_ops_v0 = { |
Peng Tao | c122515 | 2011-09-22 21:50:10 -0400 | [diff] [blame] | 1712 | .upcall = rpc_pipe_generic_upcall, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | .downcall = gss_pipe_downcall, |
| 1714 | .destroy_msg = gss_pipe_destroy_msg, |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 1715 | .open_pipe = gss_pipe_open_v0, |
| 1716 | .release_pipe = gss_pipe_release, |
| 1717 | }; |
| 1718 | |
Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 1719 | static const struct rpc_pipe_ops gss_upcall_ops_v1 = { |
Peng Tao | c122515 | 2011-09-22 21:50:10 -0400 | [diff] [blame] | 1720 | .upcall = rpc_pipe_generic_upcall, |
\"J. Bruce Fields\ | 34769fc | 2008-12-23 16:16:37 -0500 | [diff] [blame] | 1721 | .downcall = gss_pipe_downcall, |
| 1722 | .destroy_msg = gss_pipe_destroy_msg, |
| 1723 | .open_pipe = gss_pipe_open_v1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | .release_pipe = gss_pipe_release, |
| 1725 | }; |
| 1726 | |
Stanislav Kinsbursky | a1db410 | 2012-01-19 21:42:37 +0400 | [diff] [blame] | 1727 | static __net_init int rpcsec_gss_init_net(struct net *net) |
| 1728 | { |
| 1729 | return gss_svc_init_net(net); |
| 1730 | } |
| 1731 | |
| 1732 | static __net_exit void rpcsec_gss_exit_net(struct net *net) |
| 1733 | { |
| 1734 | gss_svc_shutdown_net(net); |
| 1735 | } |
| 1736 | |
| 1737 | static struct pernet_operations rpcsec_gss_net_ops = { |
| 1738 | .init = rpcsec_gss_init_net, |
| 1739 | .exit = rpcsec_gss_exit_net, |
| 1740 | }; |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Initialize RPCSEC_GSS module |
| 1744 | */ |
| 1745 | static 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 Kinsbursky | a1db410 | 2012-01-19 21:42:37 +0400 | [diff] [blame] | 1755 | err = register_pernet_subsys(&rpcsec_gss_net_ops); |
| 1756 | if (err) |
| 1757 | goto out_svc_exit; |
\"J. Bruce Fields\ | 79a3f20 | 2008-12-23 16:10:52 -0500 | [diff] [blame] | 1758 | rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | return 0; |
Stanislav Kinsbursky | a1db410 | 2012-01-19 21:42:37 +0400 | [diff] [blame] | 1760 | out_svc_exit: |
| 1761 | gss_svc_shutdown(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | out_unregister: |
| 1763 | rpcauth_unregister(&authgss_ops); |
| 1764 | out: |
| 1765 | return err; |
| 1766 | } |
| 1767 | |
| 1768 | static void __exit exit_rpcsec_gss(void) |
| 1769 | { |
Stanislav Kinsbursky | a1db410 | 2012-01-19 21:42:37 +0400 | [diff] [blame] | 1770 | unregister_pernet_subsys(&rpcsec_gss_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | gss_svc_shutdown(); |
| 1772 | rpcauth_unregister(&authgss_ops); |
Jesper Dangaard Brouer | bf12691 | 2009-06-08 03:11:48 +0000 | [diff] [blame] | 1773 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Chuck Lever | 71afa85 | 2013-03-16 15:54:16 -0400 | [diff] [blame] | 1776 | MODULE_ALIAS("rpc-auth-6"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | MODULE_LICENSE("GPL"); |
Trond Myklebust | 126e216 | 2010-05-13 12:55:38 -0400 | [diff] [blame] | 1778 | module_param_named(expired_cred_retry_delay, |
| 1779 | gss_expired_cred_retry_delay, |
| 1780 | uint, 0644); |
| 1781 | MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until " |
| 1782 | "the RPC engine retries an expired credential"); |
| 1783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | module_init(init_rpcsec_gss) |
| 1785 | module_exit(exit_rpcsec_gss) |