2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* linux/net/sunrpc/svcauth.c
|
|
|
|
*
|
|
|
|
* The generic interface for RPC authentication on the server side.
|
2007-02-10 07:38:13 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
|
|
|
|
*
|
|
|
|
* CHANGES
|
|
|
|
* 19-Apr-2000 Chris Evans - Security fix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sunrpc/types.h>
|
|
|
|
#include <linux/sunrpc/xdr.h>
|
|
|
|
#include <linux/sunrpc/svcsock.h>
|
|
|
|
#include <linux/sunrpc/svcauth.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/hash.h>
|
|
|
|
|
2019-10-24 21:34:10 +08:00
|
|
|
#include <trace/events/sunrpc.h>
|
|
|
|
|
2020-05-22 10:01:32 +08:00
|
|
|
#include "sunrpc.h"
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#define RPCDBG_FACILITY RPCDBG_AUTH
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table of authenticators
|
|
|
|
*/
|
|
|
|
extern struct auth_ops svcauth_null;
|
|
|
|
extern struct auth_ops svcauth_unix;
|
|
|
|
|
2018-10-01 22:41:43 +08:00
|
|
|
static struct auth_ops __rcu *authtab[RPC_AUTH_MAXFLAVOR] = {
|
|
|
|
[RPC_AUTH_NULL] = (struct auth_ops __force __rcu *)&svcauth_null,
|
|
|
|
[RPC_AUTH_UNIX] = (struct auth_ops __force __rcu *)&svcauth_unix,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2018-10-01 22:41:43 +08:00
|
|
|
static struct auth_ops *
|
|
|
|
svc_get_auth_ops(rpc_authflavor_t flavor)
|
|
|
|
{
|
|
|
|
struct auth_ops *aops;
|
|
|
|
|
|
|
|
if (flavor >= RPC_AUTH_MAXFLAVOR)
|
|
|
|
return NULL;
|
|
|
|
rcu_read_lock();
|
|
|
|
aops = rcu_dereference(authtab[flavor]);
|
|
|
|
if (aops != NULL && !try_module_get(aops->owner))
|
|
|
|
aops = NULL;
|
|
|
|
rcu_read_unlock();
|
|
|
|
return aops;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
svc_put_auth_ops(struct auth_ops *aops)
|
|
|
|
{
|
|
|
|
module_put(aops->owner);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int
|
2006-09-27 13:29:38 +08:00
|
|
|
svc_authenticate(struct svc_rqst *rqstp, __be32 *authp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
rpc_authflavor_t flavor;
|
|
|
|
struct auth_ops *aops;
|
|
|
|
|
|
|
|
*authp = rpc_auth_ok;
|
|
|
|
|
2006-09-27 13:28:46 +08:00
|
|
|
flavor = svc_getnl(&rqstp->rq_arg.head[0]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
dprintk("svc: svc_authenticate (%d)\n", flavor);
|
|
|
|
|
2018-10-01 22:41:43 +08:00
|
|
|
aops = svc_get_auth_ops(flavor);
|
|
|
|
if (aops == NULL) {
|
2005-04-17 06:20:36 +08:00
|
|
|
*authp = rpc_autherr_badcred;
|
|
|
|
return SVC_DENIED;
|
|
|
|
}
|
|
|
|
|
2014-05-13 06:10:58 +08:00
|
|
|
rqstp->rq_auth_slack = 0;
|
2015-11-21 04:45:35 +08:00
|
|
|
init_svc_cred(&rqstp->rq_cred);
|
2014-05-13 06:10:58 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rqstp->rq_authop = aops;
|
|
|
|
return aops->accept(rqstp, authp);
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(svc_authenticate);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
int svc_set_client(struct svc_rqst *rqstp)
|
|
|
|
{
|
2015-11-21 04:45:35 +08:00
|
|
|
rqstp->rq_client = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
return rqstp->rq_authop->set_client(rqstp);
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(svc_set_client);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* A request, which was authenticated, has now executed.
|
2007-05-09 14:57:56 +08:00
|
|
|
* Time to finalise the credentials and verifier
|
2005-04-17 06:20:36 +08:00
|
|
|
* and release and resources
|
|
|
|
*/
|
|
|
|
int svc_authorise(struct svc_rqst *rqstp)
|
|
|
|
{
|
|
|
|
struct auth_ops *aops = rqstp->rq_authop;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
rqstp->rq_authop = NULL;
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (aops) {
|
|
|
|
rv = aops->release(rqstp);
|
2018-10-01 22:41:43 +08:00
|
|
|
svc_put_auth_ops(aops);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
|
|
|
|
{
|
2018-10-01 22:41:43 +08:00
|
|
|
struct auth_ops *old;
|
2005-04-17 06:20:36 +08:00
|
|
|
int rv = -EINVAL;
|
2018-10-01 22:41:43 +08:00
|
|
|
|
|
|
|
if (flavor < RPC_AUTH_MAXFLAVOR) {
|
|
|
|
old = cmpxchg((struct auth_ops ** __force)&authtab[flavor], NULL, aops);
|
|
|
|
if (old == NULL || old == aops)
|
|
|
|
rv = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(svc_auth_register);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
svc_auth_unregister(rpc_authflavor_t flavor)
|
|
|
|
{
|
|
|
|
if (flavor < RPC_AUTH_MAXFLAVOR)
|
2018-10-01 22:41:43 +08:00
|
|
|
rcu_assign_pointer(authtab[flavor], NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(svc_auth_unregister);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/**************************************************
|
2006-03-27 17:14:59 +08:00
|
|
|
* 'auth_domains' are stored in a hash table indexed by name.
|
|
|
|
* When the last reference to an 'auth_domain' is dropped,
|
|
|
|
* the object is unhashed and freed.
|
|
|
|
* If auth_domain_lookup fails to find an entry, it will return
|
|
|
|
* it's second argument 'new'. If this is non-null, it will
|
|
|
|
* have been atomically linked into the table.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define DN_HASHBITS 6
|
|
|
|
#define DN_HASHMAX (1<<DN_HASHBITS)
|
|
|
|
|
2006-03-27 17:14:59 +08:00
|
|
|
static struct hlist_head auth_domain_table[DN_HASHMAX];
|
2016-12-04 20:45:28 +08:00
|
|
|
static DEFINE_SPINLOCK(auth_domain_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-11-15 01:03:11 +08:00
|
|
|
static void auth_domain_release(struct kref *kref)
|
2018-10-01 22:41:44 +08:00
|
|
|
__releases(&auth_domain_lock)
|
2016-11-15 01:03:11 +08:00
|
|
|
{
|
|
|
|
struct auth_domain *dom = container_of(kref, struct auth_domain, ref);
|
|
|
|
|
2018-10-01 22:41:44 +08:00
|
|
|
hlist_del_rcu(&dom->hash);
|
2016-11-15 01:03:11 +08:00
|
|
|
dom->flavour->domain_release(dom);
|
|
|
|
spin_unlock(&auth_domain_lock);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
void auth_domain_put(struct auth_domain *dom)
|
|
|
|
{
|
2016-11-15 01:03:11 +08:00
|
|
|
kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(auth_domain_put);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct auth_domain *
|
2006-03-27 17:14:59 +08:00
|
|
|
auth_domain_lookup(char *name, struct auth_domain *new)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-03-27 17:14:59 +08:00
|
|
|
struct auth_domain *hp;
|
|
|
|
struct hlist_head *head;
|
|
|
|
|
|
|
|
head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
|
|
|
|
|
|
|
|
spin_lock(&auth_domain_lock);
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 09:06:00 +08:00
|
|
|
hlist_for_each_entry(hp, head, hash) {
|
2006-03-27 17:14:59 +08:00
|
|
|
if (strcmp(hp->name, name)==0) {
|
|
|
|
kref_get(&hp->ref);
|
|
|
|
spin_unlock(&auth_domain_lock);
|
|
|
|
return hp;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2006-10-30 14:46:45 +08:00
|
|
|
if (new)
|
2018-10-01 22:41:44 +08:00
|
|
|
hlist_add_head_rcu(&new->hash, head);
|
2006-03-27 17:14:59 +08:00
|
|
|
spin_unlock(&auth_domain_lock);
|
|
|
|
return new;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(auth_domain_lookup);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct auth_domain *auth_domain_find(char *name)
|
|
|
|
{
|
2018-10-01 22:41:44 +08:00
|
|
|
struct auth_domain *hp;
|
|
|
|
struct hlist_head *head;
|
|
|
|
|
|
|
|
head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
hlist_for_each_entry_rcu(hp, head, hash) {
|
|
|
|
if (strcmp(hp->name, name)==0) {
|
|
|
|
if (!kref_get_unless_zero(&hp->ref))
|
|
|
|
hp = NULL;
|
|
|
|
rcu_read_unlock();
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(auth_domain_find);
|
2020-05-22 10:01:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* auth_domain_cleanup - check that the auth_domain table is empty
|
|
|
|
*
|
|
|
|
* On module unload the auth_domain_table must be empty. To make it
|
|
|
|
* easier to catch bugs which don't clean up domains properly, we
|
|
|
|
* warn if anything remains in the table at cleanup time.
|
|
|
|
*
|
|
|
|
* Note that we cannot proactively remove the domains at this stage.
|
|
|
|
* The ->release() function might be in a module that has already been
|
|
|
|
* unloaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void auth_domain_cleanup(void)
|
|
|
|
{
|
|
|
|
int h;
|
|
|
|
struct auth_domain *hp;
|
|
|
|
|
|
|
|
for (h = 0; h < DN_HASHMAX; h++)
|
|
|
|
hlist_for_each_entry(hp, &auth_domain_table[h], hash)
|
|
|
|
pr_warn("svc: domain %s still present at module unload.\n",
|
|
|
|
hp->name);
|
|
|
|
}
|