2008-03-12 16:24:49 +00:00
|
|
|
/*
|
|
|
|
* Generic RPC credential
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2008-03-12 16:24:49 +00:00
|
|
|
#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>
|
|
|
|
|
|
|
|
#ifdef RPC_DEBUG
|
|
|
|
# define RPCDBG_FACILITY RPCDBG_AUTH
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 23:55:38 +00:00
|
|
|
#define RPC_MACHINE_CRED_USERID GLOBAL_ROOT_UID
|
|
|
|
#define RPC_MACHINE_CRED_GROUPID GLOBAL_ROOT_GID
|
2008-04-08 00:50:11 +00:00
|
|
|
|
2008-03-12 16:24:49 +00:00
|
|
|
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);
|
|
|
|
|
2008-04-08 00:50:11 +00:00
|
|
|
/*
|
|
|
|
* Public call interface for looking up machine creds.
|
|
|
|
*/
|
2012-01-03 18:22:46 +00:00
|
|
|
struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
|
2008-04-08 00:50:11 +00:00
|
|
|
{
|
|
|
|
struct auth_cred acred = {
|
2008-05-11 19:18:51 +00:00
|
|
|
.uid = RPC_MACHINE_CRED_USERID,
|
|
|
|
.gid = RPC_MACHINE_CRED_GROUPID,
|
2012-01-03 18:22:46 +00:00
|
|
|
.principal = service_name,
|
2008-04-08 00:50:11 +00:00
|
|
|
.machine_cred = 1,
|
|
|
|
};
|
|
|
|
|
2012-01-03 18:22:46 +00:00
|
|
|
dprintk("RPC: looking up machine cred for service %s\n",
|
|
|
|
service_name);
|
2008-04-08 00:50:11 +00:00
|
|
|
return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
|
|
|
|
|
2010-07-31 18:29:08 +00:00
|
|
|
static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
|
|
|
|
struct rpc_cred *cred, int lookupflags)
|
2008-03-12 20:21:07 +00:00
|
|
|
{
|
|
|
|
struct rpc_auth *auth = task->tk_client->cl_auth;
|
|
|
|
struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
|
|
|
|
|
2010-07-31 18:29:08 +00:00
|
|
|
return auth->au_ops->lookup_cred(auth, acred, lookupflags);
|
2008-03-12 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-12 16:24:49 +00:00
|
|
|
/*
|
|
|
|
* Lookup generic creds for current process
|
|
|
|
*/
|
|
|
|
static struct rpc_cred *
|
|
|
|
generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
|
|
|
|
{
|
|
|
|
return rpcauth_lookup_credcache(&generic_auth, acred, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rpc_cred *
|
|
|
|
generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
|
|
|
|
{
|
|
|
|
struct generic_cred *gcred;
|
|
|
|
|
|
|
|
gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
|
|
|
|
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;
|
2013-08-14 15:59:15 +00:00
|
|
|
gcred->acred.ac_flags = 0;
|
2008-03-12 16:24:49 +00:00
|
|
|
if (gcred->acred.group_info != NULL)
|
|
|
|
get_group_info(gcred->acred.group_info);
|
2008-04-08 00:50:11 +00:00
|
|
|
gcred->acred.machine_cred = acred->machine_cred;
|
2012-01-23 17:49:36 +00:00
|
|
|
gcred->acred.principal = acred->principal;
|
2008-03-12 16:24:49 +00:00
|
|
|
|
2008-04-08 00:50:11 +00:00
|
|
|
dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
|
|
|
|
gcred->acred.machine_cred ? "machine" : "generic",
|
2013-02-02 01:10:52 +00:00
|
|
|
gcred,
|
|
|
|
from_kuid(&init_user_ns, acred->uid),
|
|
|
|
from_kgid(&init_user_ns, acred->gid));
|
2008-03-12 16:24:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-01-23 17:49:36 +00:00
|
|
|
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 ||
|
2013-02-02 00:39:32 +00:00
|
|
|
!uid_eq(gcred->acred.uid, acred->uid) ||
|
|
|
|
!gid_eq(gcred->acred.gid, acred->gid))
|
2012-01-23 17:49:36 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-12 16:24:49 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2008-11-20 21:06:21 +00:00
|
|
|
int i;
|
2008-03-12 16:24:49 +00:00
|
|
|
|
2012-01-23 17:49:36 +00:00
|
|
|
if (acred->machine_cred)
|
|
|
|
return machine_cred_match(acred, gcred, flags);
|
|
|
|
|
2013-02-02 00:39:32 +00:00
|
|
|
if (!uid_eq(gcred->acred.uid, acred->uid) ||
|
|
|
|
!gid_eq(gcred->acred.gid, acred->gid) ||
|
2012-01-23 17:49:36 +00:00
|
|
|
gcred->acred.machine_cred != 0)
|
2008-11-20 21:06:21 +00:00
|
|
|
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++) {
|
2011-11-14 23:56:38 +00:00
|
|
|
if (!gid_eq(GROUP_AT(gcred->acred.group_info, i),
|
|
|
|
GROUP_AT(acred->group_info, i)))
|
2008-11-20 21:06:21 +00:00
|
|
|
goto out_nomatch;
|
|
|
|
}
|
|
|
|
out_match:
|
2008-03-12 16:24:49 +00:00
|
|
|
return 1;
|
2008-11-20 21:06:21 +00:00
|
|
|
out_nomatch:
|
|
|
|
return 0;
|
2008-03-12 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
2010-07-31 18:29:07 +00:00
|
|
|
int __init rpc_init_generic_auth(void)
|
2008-03-12 16:24:49 +00:00
|
|
|
{
|
2010-07-31 18:29:07 +00:00
|
|
|
return rpcauth_init_credcache(&generic_auth);
|
2008-03-12 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 04:16:57 +00:00
|
|
|
void rpc_destroy_generic_auth(void)
|
2008-03-12 16:24:49 +00:00
|
|
|
{
|
2010-07-31 18:29:07 +00:00
|
|
|
rpcauth_destroy_credcache(&generic_auth);
|
2008-03-12 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 15:59:15 +00:00
|
|
|
/*
|
|
|
|
* 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 */
|
|
|
|
if (test_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags))
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!tcred->cr_ops->crkey_timeout) {
|
|
|
|
set_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags);
|
|
|
|
ret = 0;
|
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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",
|
2013-09-12 13:09:39 +00:00
|
|
|
from_kuid(&init_user_ns, tcred->cr_uid));
|
2013-08-14 15:59:15 +00:00
|
|
|
/* set up fasttrack for the normal case */
|
|
|
|
set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_put:
|
|
|
|
put_rpccred(tcred);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-12 16:24:49 +00:00
|
|
|
static const struct rpc_authops generic_auth_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.au_name = "Generic",
|
|
|
|
.lookup_cred = generic_lookup_cred,
|
|
|
|
.crcreate = generic_create_cred,
|
2013-08-14 15:59:15 +00:00
|
|
|
.key_timeout = generic_key_timeout,
|
2008-03-12 16:24:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct rpc_auth generic_auth = {
|
|
|
|
.au_ops = &generic_auth_ops,
|
|
|
|
.au_count = ATOMIC_INIT(0),
|
|
|
|
};
|
|
|
|
|
2013-08-14 15:59:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-03-12 16:24:49 +00:00
|
|
|
static const struct rpc_credops generic_credops = {
|
|
|
|
.cr_name = "Generic cred",
|
|
|
|
.crdestroy = generic_destroy_cred,
|
2008-03-12 20:21:07 +00:00
|
|
|
.crbind = generic_bind_cred,
|
2008-03-12 16:24:49 +00:00
|
|
|
.crmatch = generic_match,
|
2013-08-14 15:59:15 +00:00
|
|
|
.crkey_to_expire = generic_key_to_expire,
|
2008-03-12 16:24:49 +00:00
|
|
|
};
|