auth_generic.c 7.9 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Generic RPC credential
 *
 * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
 */

#include <linux/err.h>
8
#include <linux/slab.h>
9 10 11 12 13 14 15 16
#include <linux/types.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sunrpc/auth.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/debug.h>
#include <linux/sunrpc/sched.h>

J
Jeff Layton 已提交
17
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
18 19 20
# define RPCDBG_FACILITY	RPCDBG_AUTH
#endif

21 22
#define RPC_MACHINE_CRED_USERID		GLOBAL_ROOT_UID
#define RPC_MACHINE_CRED_GROUPID	GLOBAL_ROOT_GID
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
struct generic_cred {
	struct rpc_cred gc_base;
	struct auth_cred acred;
};

static struct rpc_auth generic_auth;
static const struct rpc_credops generic_credops;

/*
 * Public call interface
 */
struct rpc_cred *rpc_lookup_cred(void)
{
	return rpcauth_lookupcred(&generic_auth, 0);
}
EXPORT_SYMBOL_GPL(rpc_lookup_cred);

41 42 43 44 45 46 47
struct rpc_cred *
rpc_lookup_generic_cred(struct auth_cred *acred, int flags, gfp_t gfp)
{
	return rpcauth_lookup_credcache(&generic_auth, acred, flags, gfp);
}
EXPORT_SYMBOL_GPL(rpc_lookup_generic_cred);

48 49 50 51 52 53
struct rpc_cred *rpc_lookup_cred_nonblock(void)
{
	return rpcauth_lookupcred(&generic_auth, RPCAUTH_LOOKUP_RCU);
}
EXPORT_SYMBOL_GPL(rpc_lookup_cred_nonblock);

54 55 56
/*
 * Public call interface for looking up machine creds.
 */
57
struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
58 59
{
	struct auth_cred acred = {
60 61
		.uid = RPC_MACHINE_CRED_USERID,
		.gid = RPC_MACHINE_CRED_GROUPID,
62
		.principal = service_name,
63 64 65
		.machine_cred = 1,
	};

66 67
	dprintk("RPC:       looking up machine cred for service %s\n",
			service_name);
68 69 70 71
	return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
}
EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);

72 73
static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
		struct rpc_cred *cred, int lookupflags)
74 75 76 77
{
	struct rpc_auth *auth = task->tk_client->cl_auth;
	struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;

78
	return auth->au_ops->lookup_cred(auth, acred, lookupflags);
79 80
}

81 82 83 84 85 86 87 88
static int
generic_hash_cred(struct auth_cred *acred, unsigned int hashbits)
{
	return hash_64(from_kgid(&init_user_ns, acred->gid) |
		((u64)from_kuid(&init_user_ns, acred->uid) <<
			(sizeof(gid_t) * 8)), hashbits);
}

89 90 91 92 93 94
/*
 * Lookup generic creds for current process
 */
static struct rpc_cred *
generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
{
95
	return rpcauth_lookup_credcache(&generic_auth, acred, flags, GFP_KERNEL);
96 97 98
}

static struct rpc_cred *
99
generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
100 101 102
{
	struct generic_cred *gcred;

103
	gcred = kmalloc(sizeof(*gcred), gfp);
104 105 106 107 108 109 110 111 112
	if (gcred == NULL)
		return ERR_PTR(-ENOMEM);

	rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
	gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;

	gcred->acred.uid = acred->uid;
	gcred->acred.gid = acred->gid;
	gcred->acred.group_info = acred->group_info;
113
	gcred->acred.ac_flags = 0;
114 115
	if (gcred->acred.group_info != NULL)
		get_group_info(gcred->acred.group_info);
116
	gcred->acred.machine_cred = acred->machine_cred;
117
	gcred->acred.principal = acred->principal;
118

119 120
	dprintk("RPC:       allocated %s cred %p for uid %d gid %d\n",
			gcred->acred.machine_cred ? "machine" : "generic",
121 122 123
			gcred,
			from_kuid(&init_user_ns, acred->uid),
			from_kgid(&init_user_ns, acred->gid));
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	return &gcred->gc_base;
}

static void
generic_free_cred(struct rpc_cred *cred)
{
	struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);

	dprintk("RPC:       generic_free_cred %p\n", gcred);
	if (gcred->acred.group_info != NULL)
		put_group_info(gcred->acred.group_info);
	kfree(gcred);
}

static void
generic_free_cred_callback(struct rcu_head *head)
{
	struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
	generic_free_cred(cred);
}

static void
generic_destroy_cred(struct rpc_cred *cred)
{
	call_rcu(&cred->cr_rcu, generic_free_cred_callback);
}

151 152 153 154 155
static int
machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
{
	if (!gcred->acred.machine_cred ||
	    gcred->acred.principal != acred->principal ||
156 157
	    !uid_eq(gcred->acred.uid, acred->uid) ||
	    !gid_eq(gcred->acred.gid, acred->gid))
158 159 160 161
		return 0;
	return 1;
}

162 163 164 165 166 167 168
/*
 * Match credentials against current process creds.
 */
static int
generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
{
	struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
169
	int i;
170

171 172 173
	if (acred->machine_cred)
		return machine_cred_match(acred, gcred, flags);

174 175
	if (!uid_eq(gcred->acred.uid, acred->uid) ||
	    !gid_eq(gcred->acred.gid, acred->gid) ||
176
	    gcred->acred.machine_cred != 0)
177 178 179 180 181 182 183 184 185 186
		goto out_nomatch;

	/* Optimisation in the case where pointers are identical... */
	if (gcred->acred.group_info == acred->group_info)
		goto out_match;

	/* Slow path... */
	if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
		goto out_nomatch;
	for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
187 188
		if (!gid_eq(gcred->acred.group_info->gid[i],
				acred->group_info->gid[i]))
189 190 191
			goto out_nomatch;
	}
out_match:
192
	return 1;
193 194
out_nomatch:
	return 0;
195 196
}

197
int __init rpc_init_generic_auth(void)
198
{
199
	return rpcauth_init_credcache(&generic_auth);
200 201
}

202
void rpc_destroy_generic_auth(void)
203
{
204
	rpcauth_destroy_credcache(&generic_auth);
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/*
 * Test the the current time (now) against the underlying credential key expiry
 * minus a timeout and setup notification.
 *
 * The normal case:
 * If 'now' is before the key expiry minus RPC_KEY_EXPIRE_TIMEO, set
 * the RPC_CRED_NOTIFY_TIMEOUT flag to setup the underlying credential
 * rpc_credops crmatch routine to notify this generic cred when it's key
 * expiration is within RPC_KEY_EXPIRE_TIMEO, and return 0.
 *
 * The error case:
 * If the underlying cred lookup fails, return -EACCES.
 *
 * The 'almost' error case:
 * If 'now' is within key expiry minus RPC_KEY_EXPIRE_TIMEO, but not within
 * key expiry minus RPC_KEY_EXPIRE_FAIL, set the RPC_CRED_EXPIRE_SOON bit
 * on the acred ac_flags and return 0.
 */
static int
generic_key_timeout(struct rpc_auth *auth, struct rpc_cred *cred)
{
	struct auth_cred *acred = &container_of(cred, struct generic_cred,
						gc_base)->acred;
	struct rpc_cred *tcred;
	int ret = 0;


	/* Fast track for non crkey_timeout (no key) underlying credentials */
235
	if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		return 0;

	/* Fast track for the normal case */
	if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags))
		return 0;

	/* lookup_cred either returns a valid referenced rpc_cred, or PTR_ERR */
	tcred = auth->au_ops->lookup_cred(auth, acred, 0);
	if (IS_ERR(tcred))
		return -EACCES;

	/* Test for the almost error case */
	ret = tcred->cr_ops->crkey_timeout(tcred);
	if (ret != 0) {
		set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
		ret = 0;
	} else {
		/* In case underlying cred key has been reset */
		if (test_and_clear_bit(RPC_CRED_KEY_EXPIRE_SOON,
					&acred->ac_flags))
			dprintk("RPC:        UID %d Credential key reset\n",
257
				from_kuid(&init_user_ns, tcred->cr_uid));
258 259 260 261 262 263 264 265
		/* set up fasttrack for the normal case */
		set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
	}

	put_rpccred(tcred);
	return ret;
}

266 267 268
static const struct rpc_authops generic_auth_ops = {
	.owner = THIS_MODULE,
	.au_name = "Generic",
269
	.hash_cred = generic_hash_cred,
270 271
	.lookup_cred = generic_lookup_cred,
	.crcreate = generic_create_cred,
272
	.key_timeout = generic_key_timeout,
273 274 275 276
};

static struct rpc_auth generic_auth = {
	.au_ops = &generic_auth_ops,
277
	.au_count = REFCOUNT_INIT(1),
278 279
};

280 281 282 283 284 285 286 287 288 289 290 291 292
static bool generic_key_to_expire(struct rpc_cred *cred)
{
	struct auth_cred *acred = &container_of(cred, struct generic_cred,
						gc_base)->acred;
	bool ret;

	get_rpccred(cred);
	ret = test_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
	put_rpccred(cred);

	return ret;
}

293 294 295
static const struct rpc_credops generic_credops = {
	.cr_name = "Generic cred",
	.crdestroy = generic_destroy_cred,
296
	.crbind = generic_bind_cred,
297
	.crmatch = generic_match,
298
	.crkey_to_expire = generic_key_to_expire,
299
};