Commit d84f4f99 authored by David Howells's avatar David Howells Committed by James Morris

CRED: Inaugurate COW credentials

Inaugurate copy-on-write credentials management.  This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.

A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().

With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:

	struct cred *new = prepare_creds();
	int ret = blah(new);
	if (ret < 0) {
		abort_creds(new);
		return ret;
	}
	return commit_creds(new);

There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.

To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const.  The purpose of this is compile-time
discouragement of altering credentials through those pointers.  Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:

  (1) Its reference count may incremented and decremented.

  (2) The keyrings to which it points may be modified, but not replaced.

The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).

This patch and the preceding patches have been tested with the LTP SELinux
testsuite.

This patch makes several logical sets of alteration:

 (1) execve().

     This now prepares and commits credentials in various places in the
     security code rather than altering the current creds directly.

 (2) Temporary credential overrides.

     do_coredump() and sys_faccessat() now prepare their own credentials and
     temporarily override the ones currently on the acting thread, whilst
     preventing interference from other threads by holding cred_replace_mutex
     on the thread being dumped.

     This will be replaced in a future patch by something that hands down the
     credentials directly to the functions being called, rather than altering
     the task's objective credentials.

 (3) LSM interface.

     A number of functions have been changed, added or removed:

     (*) security_capset_check(), ->capset_check()
     (*) security_capset_set(), ->capset_set()

     	 Removed in favour of security_capset().

     (*) security_capset(), ->capset()

     	 New.  This is passed a pointer to the new creds, a pointer to the old
     	 creds and the proposed capability sets.  It should fill in the new
     	 creds or return an error.  All pointers, barring the pointer to the
     	 new creds, are now const.

     (*) security_bprm_apply_creds(), ->bprm_apply_creds()

     	 Changed; now returns a value, which will cause the process to be
     	 killed if it's an error.

     (*) security_task_alloc(), ->task_alloc_security()

     	 Removed in favour of security_prepare_creds().

     (*) security_cred_free(), ->cred_free()

     	 New.  Free security data attached to cred->security.

     (*) security_prepare_creds(), ->cred_prepare()

     	 New. Duplicate any security data attached to cred->security.

     (*) security_commit_creds(), ->cred_commit()

     	 New. Apply any security effects for the upcoming installation of new
     	 security by commit_creds().

     (*) security_task_post_setuid(), ->task_post_setuid()

     	 Removed in favour of security_task_fix_setuid().

     (*) security_task_fix_setuid(), ->task_fix_setuid()

     	 Fix up the proposed new credentials for setuid().  This is used by
     	 cap_set_fix_setuid() to implicitly adjust capabilities in line with
     	 setuid() changes.  Changes are made to the new credentials, rather
     	 than the task itself as in security_task_post_setuid().

     (*) security_task_reparent_to_init(), ->task_reparent_to_init()

     	 Removed.  Instead the task being reparented to init is referred
     	 directly to init's credentials.

	 NOTE!  This results in the loss of some state: SELinux's osid no
	 longer records the sid of the thread that forked it.

     (*) security_key_alloc(), ->key_alloc()
     (*) security_key_permission(), ->key_permission()

     	 Changed.  These now take cred pointers rather than task pointers to
     	 refer to the security context.

 (4) sys_capset().

     This has been simplified and uses less locking.  The LSM functions it
     calls have been merged.

 (5) reparent_to_kthreadd().

     This gives the current thread the same credentials as init by simply using
     commit_thread() to point that way.

 (6) __sigqueue_alloc() and switch_uid()

     __sigqueue_alloc() can't stop the target task from changing its creds
     beneath it, so this function gets a reference to the currently applicable
     user_struct which it then passes into the sigqueue struct it returns if
     successful.

     switch_uid() is now called from commit_creds(), and possibly should be
     folded into that.  commit_creds() should take care of protecting
     __sigqueue_alloc().

 (7) [sg]et[ug]id() and co and [sg]et_current_groups.

     The set functions now all use prepare_creds(), commit_creds() and
     abort_creds() to build and check a new set of credentials before applying
     it.

     security_task_set[ug]id() is called inside the prepared section.  This
     guarantees that nothing else will affect the creds until we've finished.

     The calling of set_dumpable() has been moved into commit_creds().

     Much of the functionality of set_user() has been moved into
     commit_creds().

     The get functions all simply access the data directly.

 (8) security_task_prctl() and cap_task_prctl().

     security_task_prctl() has been modified to return -ENOSYS if it doesn't
     want to handle a function, or otherwise return the return value directly
     rather than through an argument.

     Additionally, cap_task_prctl() now prepares a new set of credentials, even
     if it doesn't end up using it.

 (9) Keyrings.

     A number of changes have been made to the keyrings code:

     (a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
     	 all been dropped and built in to the credentials functions directly.
     	 They may want separating out again later.

     (b) key_alloc() and search_process_keyrings() now take a cred pointer
     	 rather than a task pointer to specify the security context.

     (c) copy_creds() gives a new thread within the same thread group a new
     	 thread keyring if its parent had one, otherwise it discards the thread
     	 keyring.

     (d) The authorisation key now points directly to the credentials to extend
     	 the search into rather pointing to the task that carries them.

     (e) Installing thread, process or session keyrings causes a new set of
     	 credentials to be created, even though it's not strictly necessary for
     	 process or session keyrings (they're shared).

(10) Usermode helper.

     The usermode helper code now carries a cred struct pointer in its
     subprocess_info struct instead of a new session keyring pointer.  This set
     of credentials is derived from init_cred and installed on the new process
     after it has been cloned.

     call_usermodehelper_setup() allocates the new credentials and
     call_usermodehelper_freeinfo() discards them if they haven't been used.  A
     special cred function (prepare_usermodeinfo_creds()) is provided
     specifically for call_usermodehelper_setup() to call.

     call_usermodehelper_setkeys() adjusts the credentials to sport the
     supplied keyring as the new session keyring.

(11) SELinux.

     SELinux has a number of changes, in addition to those to support the LSM
     interface changes mentioned above:

     (a) selinux_setprocattr() no longer does its check for whether the
     	 current ptracer can access processes with the new SID inside the lock
     	 that covers getting the ptracer's SID.  Whilst this lock ensures that
     	 the check is done with the ptracer pinned, the result is only valid
     	 until the lock is released, so there's no point doing it inside the
     	 lock.

(12) is_single_threaded().

     This function has been extracted from selinux_setprocattr() and put into
     a file of its own in the lib/ directory as join_session_keyring() now
     wants to use it too.

     The code in SELinux just checked to see whether a task shared mm_structs
     with other tasks (CLONE_VM), but that isn't good enough.  We really want
     to know if they're part of the same thread group (CLONE_THREAD).

(13) nfsd.

     The NFS server daemon now has to use the COW credentials to set the
     credentials it is going to use.  It really needs to pass the credentials
     down to the functions it calls, but it can't do that until other patches
     in this series have been applied.
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Acked-by: default avatarJames Morris <jmorris@namei.org>
Signed-off-by: default avatarJames Morris <jmorris@namei.org>
parent 745ca247
...@@ -1007,13 +1007,12 @@ int flush_old_exec(struct linux_binprm * bprm) ...@@ -1007,13 +1007,12 @@ int flush_old_exec(struct linux_binprm * bprm)
*/ */
current->mm->task_size = TASK_SIZE; current->mm->task_size = TASK_SIZE;
if (bprm->e_uid != current_euid() || bprm->e_gid != current_egid()) { if (bprm->e_uid != current_euid() ||
suid_keys(current); bprm->e_gid != current_egid()) {
set_dumpable(current->mm, suid_dumpable); set_dumpable(current->mm, suid_dumpable);
current->pdeath_signal = 0; current->pdeath_signal = 0;
} else if (file_permission(bprm->file, MAY_READ) || } else if (file_permission(bprm->file, MAY_READ) ||
(bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) { (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
suid_keys(current);
set_dumpable(current->mm, suid_dumpable); set_dumpable(current->mm, suid_dumpable);
} }
...@@ -1096,10 +1095,8 @@ void compute_creds(struct linux_binprm *bprm) ...@@ -1096,10 +1095,8 @@ void compute_creds(struct linux_binprm *bprm)
{ {
int unsafe; int unsafe;
if (bprm->e_uid != current_uid()) { if (bprm->e_uid != current_uid())
suid_keys(current);
current->pdeath_signal = 0; current->pdeath_signal = 0;
}
exec_keys(current); exec_keys(current);
task_lock(current); task_lock(current);
...@@ -1709,8 +1706,9 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) ...@@ -1709,8 +1706,9 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
struct linux_binfmt * binfmt; struct linux_binfmt * binfmt;
struct inode * inode; struct inode * inode;
struct file * file; struct file * file;
const struct cred *old_cred;
struct cred *cred;
int retval = 0; int retval = 0;
int fsuid = current_fsuid();
int flag = 0; int flag = 0;
int ispipe = 0; int ispipe = 0;
unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur; unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
...@@ -1723,12 +1721,20 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) ...@@ -1723,12 +1721,20 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
binfmt = current->binfmt; binfmt = current->binfmt;
if (!binfmt || !binfmt->core_dump) if (!binfmt || !binfmt->core_dump)
goto fail; goto fail;
cred = prepare_creds();
if (!cred) {
retval = -ENOMEM;
goto fail;
}
down_write(&mm->mmap_sem); down_write(&mm->mmap_sem);
/* /*
* If another thread got here first, or we are not dumpable, bail out. * If another thread got here first, or we are not dumpable, bail out.
*/ */
if (mm->core_state || !get_dumpable(mm)) { if (mm->core_state || !get_dumpable(mm)) {
up_write(&mm->mmap_sem); up_write(&mm->mmap_sem);
put_cred(cred);
goto fail; goto fail;
} }
...@@ -1739,12 +1745,16 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) ...@@ -1739,12 +1745,16 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
*/ */
if (get_dumpable(mm) == 2) { /* Setuid core dump mode */ if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
flag = O_EXCL; /* Stop rewrite attacks */ flag = O_EXCL; /* Stop rewrite attacks */
current->cred->fsuid = 0; /* Dump root private */ cred->fsuid = 0; /* Dump root private */
} }
retval = coredump_wait(exit_code, &core_state); retval = coredump_wait(exit_code, &core_state);
if (retval < 0) if (retval < 0) {
put_cred(cred);
goto fail; goto fail;
}
old_cred = override_creds(cred);
/* /*
* Clear any false indication of pending signals that might * Clear any false indication of pending signals that might
...@@ -1835,7 +1845,8 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) ...@@ -1835,7 +1845,8 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs)
if (helper_argv) if (helper_argv)
argv_free(helper_argv); argv_free(helper_argv);
current->cred->fsuid = fsuid; revert_creds(old_cred);
put_cred(cred);
coredump_finish(mm); coredump_finish(mm);
fail: fail:
return retval; return retval;
......
...@@ -27,55 +27,67 @@ int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp) ...@@ -27,55 +27,67 @@ int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
{ {
struct cred *act_as = current->cred ; struct group_info *rqgi;
struct svc_cred cred = rqstp->rq_cred; struct group_info *gi;
struct cred *new;
int i; int i;
int flags = nfsexp_flags(rqstp, exp); int flags = nfsexp_flags(rqstp, exp);
int ret; int ret;
new = prepare_creds();
if (!new)
return -ENOMEM;
new->fsuid = rqstp->rq_cred.cr_uid;
new->fsgid = rqstp->rq_cred.cr_gid;
rqgi = rqstp->rq_cred.cr_group_info;
if (flags & NFSEXP_ALLSQUASH) { if (flags & NFSEXP_ALLSQUASH) {
cred.cr_uid = exp->ex_anon_uid; new->fsuid = exp->ex_anon_uid;
cred.cr_gid = exp->ex_anon_gid; new->fsgid = exp->ex_anon_gid;
cred.cr_group_info = groups_alloc(0); gi = groups_alloc(0);
} else if (flags & NFSEXP_ROOTSQUASH) { } else if (flags & NFSEXP_ROOTSQUASH) {
struct group_info *gi; if (!new->fsuid)
if (!cred.cr_uid) new->fsuid = exp->ex_anon_uid;
cred.cr_uid = exp->ex_anon_uid; if (!new->fsgid)
if (!cred.cr_gid) new->fsgid = exp->ex_anon_gid;
cred.cr_gid = exp->ex_anon_gid;
gi = groups_alloc(cred.cr_group_info->ngroups); gi = groups_alloc(rqgi->ngroups);
if (gi) if (!gi)
for (i = 0; i < cred.cr_group_info->ngroups; i++) { goto oom;
if (!GROUP_AT(cred.cr_group_info, i))
for (i = 0; i < rqgi->ngroups; i++) {
if (!GROUP_AT(rqgi, i))
GROUP_AT(gi, i) = exp->ex_anon_gid; GROUP_AT(gi, i) = exp->ex_anon_gid;
else else
GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i); GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
}
} else {
gi = get_group_info(rqgi);
} }
cred.cr_group_info = gi;
} else
get_group_info(cred.cr_group_info);
if (cred.cr_uid != (uid_t) -1) if (new->fsuid == (uid_t) -1)
act_as->fsuid = cred.cr_uid; new->fsuid = exp->ex_anon_uid;
else if (new->fsgid == (gid_t) -1)
act_as->fsuid = exp->ex_anon_uid; new->fsgid = exp->ex_anon_gid;
if (cred.cr_gid != (gid_t) -1)
act_as->fsgid = cred.cr_gid; ret = set_groups(new, gi);
put_group_info(gi);
if (!ret)
goto error;
if (new->uid)
new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
else else
act_as->fsgid = exp->ex_anon_gid; new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
new->cap_permitted);
return commit_creds(new);
if (!cred.cr_group_info) oom:
return -ENOMEM; ret = -ENOMEM;
ret = set_groups(act_as, cred.cr_group_info); error:
put_group_info(cred.cr_group_info); abort_creds(new);
if ((cred.cr_uid)) {
act_as->cap_effective =
cap_drop_nfsd_set(act_as->cap_effective);
} else {
act_as->cap_effective =
cap_raise_nfsd_set(act_as->cap_effective,
act_as->cap_permitted);
}
return ret; return ret;
} }
...@@ -54,20 +54,26 @@ ...@@ -54,20 +54,26 @@
static struct path rec_dir; static struct path rec_dir;
static int rec_dir_init = 0; static int rec_dir_init = 0;
static void static int
nfs4_save_user(uid_t *saveuid, gid_t *savegid) nfs4_save_creds(const struct cred **original_creds)
{ {
*saveuid = current->cred->fsuid; struct cred *new;
*savegid = current->cred->fsgid;
current->cred->fsuid = 0; new = prepare_creds();
current->cred->fsgid = 0; if (!new)
return -ENOMEM;
new->fsuid = 0;
new->fsgid = 0;
*original_creds = override_creds(new);
put_cred(new);
return 0;
} }
static void static void
nfs4_reset_user(uid_t saveuid, gid_t savegid) nfs4_reset_creds(const struct cred *original)
{ {
current->cred->fsuid = saveuid; revert_creds(original);
current->cred->fsgid = savegid;
} }
static void static void
...@@ -129,10 +135,9 @@ nfsd4_sync_rec_dir(void) ...@@ -129,10 +135,9 @@ nfsd4_sync_rec_dir(void)
int int
nfsd4_create_clid_dir(struct nfs4_client *clp) nfsd4_create_clid_dir(struct nfs4_client *clp)
{ {
const struct cred *original_cred;
char *dname = clp->cl_recdir; char *dname = clp->cl_recdir;
struct dentry *dentry; struct dentry *dentry;
uid_t uid;
gid_t gid;
int status; int status;
dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname); dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
...@@ -140,7 +145,9 @@ nfsd4_create_clid_dir(struct nfs4_client *clp) ...@@ -140,7 +145,9 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
if (!rec_dir_init || clp->cl_firststate) if (!rec_dir_init || clp->cl_firststate)
return 0; return 0;
nfs4_save_user(&uid, &gid); status = nfs4_save_creds(&original_cred);
if (status < 0)
return status;
/* lock the parent */ /* lock the parent */
mutex_lock(&rec_dir.dentry->d_inode->i_mutex); mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
...@@ -168,7 +175,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp) ...@@ -168,7 +175,7 @@ nfsd4_create_clid_dir(struct nfs4_client *clp)
clp->cl_firststate = 1; clp->cl_firststate = 1;
nfsd4_sync_rec_dir(); nfsd4_sync_rec_dir();
} }
nfs4_reset_user(uid, gid); nfs4_reset_creds(original_cred);
dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status); dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
return status; return status;
} }
...@@ -211,20 +218,21 @@ nfsd4_build_dentrylist(void *arg, const char *name, int namlen, ...@@ -211,20 +218,21 @@ nfsd4_build_dentrylist(void *arg, const char *name, int namlen,
static int static int
nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f) nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
{ {
const struct cred *original_cred;
struct file *filp; struct file *filp;
struct dentry_list_arg dla = { struct dentry_list_arg dla = {
.parent = dir, .parent = dir,
}; };
struct list_head *dentries = &dla.dentries; struct list_head *dentries = &dla.dentries;
struct dentry_list *child; struct dentry_list *child;
uid_t uid;
gid_t gid;
int status; int status;
if (!rec_dir_init) if (!rec_dir_init)
return 0; return 0;
nfs4_save_user(&uid, &gid); status = nfs4_save_creds(&original_cred);
if (status < 0)
return status;
filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY, filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
current_cred()); current_cred());
...@@ -250,7 +258,7 @@ nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f) ...@@ -250,7 +258,7 @@ nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
dput(child->dentry); dput(child->dentry);
kfree(child); kfree(child);
} }
nfs4_reset_user(uid, gid); nfs4_reset_creds(original_cred);
return status; return status;
} }
...@@ -312,8 +320,7 @@ nfsd4_unlink_clid_dir(char *name, int namlen) ...@@ -312,8 +320,7 @@ nfsd4_unlink_clid_dir(char *name, int namlen)
void void
nfsd4_remove_clid_dir(struct nfs4_client *clp) nfsd4_remove_clid_dir(struct nfs4_client *clp)
{ {
uid_t uid; const struct cred *original_cred;
gid_t gid;
int status; int status;
if (!rec_dir_init || !clp->cl_firststate) if (!rec_dir_init || !clp->cl_firststate)
...@@ -323,9 +330,13 @@ nfsd4_remove_clid_dir(struct nfs4_client *clp) ...@@ -323,9 +330,13 @@ nfsd4_remove_clid_dir(struct nfs4_client *clp)
if (status) if (status)
goto out; goto out;
clp->cl_firststate = 0; clp->cl_firststate = 0;
nfs4_save_user(&uid, &gid);
status = nfs4_save_creds(&original_cred);
if (status < 0)
goto out;
status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1); status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
nfs4_reset_user(uid, gid); nfs4_reset_creds(original_cred);
if (status == 0) if (status == 0)
nfsd4_sync_rec_dir(); nfsd4_sync_rec_dir();
mnt_drop_write(rec_dir.mnt); mnt_drop_write(rec_dir.mnt);
...@@ -402,8 +413,7 @@ nfsd4_recdir_load(void) { ...@@ -402,8 +413,7 @@ nfsd4_recdir_load(void) {
void void
nfsd4_init_recdir(char *rec_dirname) nfsd4_init_recdir(char *rec_dirname)
{ {
uid_t uid = 0; const struct cred *original_cred;
gid_t gid = 0;
int status; int status;
printk("NFSD: Using %s as the NFSv4 state recovery directory\n", printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
...@@ -411,7 +421,13 @@ nfsd4_init_recdir(char *rec_dirname) ...@@ -411,7 +421,13 @@ nfsd4_init_recdir(char *rec_dirname)
BUG_ON(rec_dir_init); BUG_ON(rec_dir_init);
nfs4_save_user(&uid, &gid); status = nfs4_save_creds(&original_cred);
if (status < 0) {
printk("NFSD: Unable to change credentials to find recovery"
" directory: error %d\n",
status);
return;
}
status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
&rec_dir); &rec_dir);
...@@ -421,7 +437,7 @@ nfsd4_init_recdir(char *rec_dirname) ...@@ -421,7 +437,7 @@ nfsd4_init_recdir(char *rec_dirname)
if (!status) if (!status)
rec_dir_init = 1; rec_dir_init = 1;
nfs4_reset_user(uid, gid); nfs4_reset_creds(original_cred);
} }
void void
......
...@@ -186,9 +186,14 @@ static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp) ...@@ -186,9 +186,14 @@ static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
* access control settings being in effect, we cannot * access control settings being in effect, we cannot
* fix that case easily. * fix that case easily.
*/ */
current->cred->cap_effective = struct cred *new = prepare_creds();
cap_raise_nfsd_set(current->cred->cap_effective, if (!new)
current->cred->cap_permitted); return nfserrno(-ENOMEM);
new->cap_effective =
cap_raise_nfsd_set(new->cap_effective,
new->cap_permitted);
put_cred(override_creds(new));
put_cred(new);
} else { } else {
error = nfsd_setuser_and_check_port(rqstp, exp); error = nfsd_setuser_and_check_port(rqstp, exp);
if (error) if (error)
......
...@@ -425,30 +425,33 @@ asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len) ...@@ -425,30 +425,33 @@ asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
*/ */
asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode) asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
{ {
struct cred *cred = current->cred; const struct cred *old_cred;
struct cred *override_cred;
struct path path; struct path path;
struct inode *inode; struct inode *inode;
int old_fsuid, old_fsgid;
kernel_cap_t uninitialized_var(old_cap); /* !SECURE_NO_SETUID_FIXUP */
int res; int res;
if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
return -EINVAL; return -EINVAL;
old_fsuid = cred->fsuid; override_cred = prepare_creds();
old_fsgid = cred->fsgid; if (!override_cred)
return -ENOMEM;
cred->fsuid = cred->uid; override_cred->fsuid = override_cred->uid;
cred->fsgid = cred->gid; override_cred->fsgid = override_cred->gid;
if (!issecure(SECURE_NO_SETUID_FIXUP)) { if (!issecure(SECURE_NO_SETUID_FIXUP)) {
/* Clear the capabilities if we switch to a non-root user */ /* Clear the capabilities if we switch to a non-root user */
if (current->cred->uid) if (override_cred->uid)
old_cap = cap_set_effective(__cap_empty_set); cap_clear(override_cred->cap_effective);
else else
old_cap = cap_set_effective(cred->cap_permitted); override_cred->cap_effective =
override_cred->cap_permitted;
} }
old_cred = override_creds(override_cred);
res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path); res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
if (res) if (res)
goto out; goto out;
...@@ -485,12 +488,8 @@ asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode) ...@@ -485,12 +488,8 @@ asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
out_path_release: out_path_release:
path_put(&path); path_put(&path);
out: out:
cred->fsuid = old_fsuid; revert_creds(old_cred);
cred->fsgid = old_fsgid; put_cred(override_cred);
if (!issecure(SECURE_NO_SETUID_FIXUP))
cap_set_effective(old_cap);
return res; return res;
} }
......
...@@ -454,8 +454,10 @@ extern int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_pr ...@@ -454,8 +454,10 @@ extern int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_pr
extern int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len, unsigned int __user *u_msg_prio, const struct timespec __user *u_abs_timeout); extern int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len, unsigned int __user *u_msg_prio, const struct timespec __user *u_abs_timeout);
extern int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification); extern int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification);
extern int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat); extern int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
extern void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE); extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
extern int __audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_cap_t *perm); const struct cred *new,
const struct cred *old);
extern int __audit_log_capset(pid_t pid, const struct cred *new, const struct cred *old);
static inline int audit_ipc_obj(struct kern_ipc_perm *ipcp) static inline int audit_ipc_obj(struct kern_ipc_perm *ipcp)
{ {
...@@ -522,16 +524,20 @@ static inline int audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) ...@@ -522,16 +524,20 @@ static inline int audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
* *
* -Eric * -Eric
*/ */
static inline void audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE) static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
const struct cred *new,
const struct cred *old)
{ {
if (unlikely(!audit_dummy_context())) if (unlikely(!audit_dummy_context()))
__audit_log_bprm_fcaps(bprm, pP, pE); return __audit_log_bprm_fcaps(bprm, new, old);
return 0;
} }
static inline int audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_cap_t *perm) static inline int audit_log_capset(pid_t pid, const struct cred *new,
const struct cred *old)
{ {
if (unlikely(!audit_dummy_context())) if (unlikely(!audit_dummy_context()))
return __audit_log_capset(pid, eff, inh, perm); return __audit_log_capset(pid, new, old);
return 0; return 0;
} }
...@@ -566,8 +572,8 @@ extern int audit_signals; ...@@ -566,8 +572,8 @@ extern int audit_signals;
#define audit_mq_timedreceive(d,l,p,t) ({ 0; }) #define audit_mq_timedreceive(d,l,p,t) ({ 0; })
#define audit_mq_notify(d,n) ({ 0; }) #define audit_mq_notify(d,n) ({ 0; })
#define audit_mq_getsetattr(d,s) ({ 0; }) #define audit_mq_getsetattr(d,s) ({ 0; })
#define audit_log_bprm_fcaps(b, p, e) do { ; } while (0) #define audit_log_bprm_fcaps(b, ncr, ocr) ({ 0; })
#define audit_log_capset(pid, e, i, p) ({ 0; }) #define audit_log_capset(pid, ncr, ocr) ({ 0; })
#define audit_ptrace(t) ((void)0) #define audit_ptrace(t) ((void)0)
#define audit_n_rules 0 #define audit_n_rules 0
#define audit_signals 0 #define audit_signals 0
......
...@@ -519,8 +519,6 @@ extern const kernel_cap_t __cap_empty_set; ...@@ -519,8 +519,6 @@ extern const kernel_cap_t __cap_empty_set;
extern const kernel_cap_t __cap_full_set; extern const kernel_cap_t __cap_full_set;
extern const kernel_cap_t __cap_init_eff_set; extern const kernel_cap_t __cap_init_eff_set;
kernel_cap_t cap_set_effective(const kernel_cap_t pE_new);
/** /**
* has_capability - Determine if a task has a superior capability available * has_capability - Determine if a task has a superior capability available
* @t: The task in question * @t: The task in question
......
...@@ -84,6 +84,8 @@ struct thread_group_cred { ...@@ -84,6 +84,8 @@ struct thread_group_cred {
struct key *process_keyring; /* keyring private to this process */ struct key *process_keyring; /* keyring private to this process */
struct rcu_head rcu; /* RCU deletion hook */ struct rcu_head rcu; /* RCU deletion hook */
}; };
extern void release_tgcred(struct cred *cred);
#endif #endif
/* /*
...@@ -137,11 +139,30 @@ struct cred { ...@@ -137,11 +139,30 @@ struct cred {
struct user_struct *user; /* real user ID subscription */ struct user_struct *user; /* real user ID subscription */
struct group_info *group_info; /* supplementary groups for euid/fsgid */ struct group_info *group_info; /* supplementary groups for euid/fsgid */
struct rcu_head rcu; /* RCU deletion hook */ struct rcu_head rcu; /* RCU deletion hook */
spinlock_t lock; /* lock for pointer changes */
}; };
extern void __put_cred(struct cred *); extern void __put_cred(struct cred *);
extern int copy_creds(struct task_struct *, unsigned long); extern int copy_creds(struct task_struct *, unsigned long);
extern struct cred *prepare_creds(void);
extern struct cred *prepare_usermodehelper_creds(void);
extern int commit_creds(struct cred *);
extern void abort_creds(struct cred *);
extern const struct cred *override_creds(const struct cred *) __deprecated;
extern void revert_creds(const struct cred *) __deprecated;
extern void __init cred_init(void);
/**
* get_new_cred - Get a reference on a new set of credentials
* @cred: The new credentials to reference
*
* Get a reference on the specified set of new credentials. The caller must
* release the reference.
*/
static inline struct cred *get_new_cred(struct cred *cred)
{
atomic_inc(&cred->usage);
return cred;
}
/** /**
* get_cred - Get a reference on a set of credentials * get_cred - Get a reference on a set of credentials
...@@ -150,10 +171,9 @@ extern int copy_creds(struct task_struct *, unsigned long); ...@@ -150,10 +171,9 @@ extern int copy_creds(struct task_struct *, unsigned long);
* Get a reference on the specified set of credentials. The caller must * Get a reference on the specified set of credentials. The caller must
* release the reference. * release the reference.
*/ */
static inline struct cred *get_cred(struct cred *cred) static inline const struct cred *get_cred(const struct cred *cred)
{ {
atomic_inc(&cred->usage); return get_new_cred((struct cred *) cred);
return cred;
} }
/** /**
...@@ -166,6 +186,8 @@ static inline struct cred *get_cred(struct cred *cred) ...@@ -166,6 +186,8 @@ static inline struct cred *get_cred(struct cred *cred)
static inline void put_cred(const struct cred *_cred) static inline void put_cred(const struct cred *_cred)
{ {
struct cred *cred = (struct cred *) _cred; struct cred *cred = (struct cred *) _cred;
BUG_ON(atomic_read(&(cred)->usage) <= 0);
if (atomic_dec_and_test(&(cred)->usage)) if (atomic_dec_and_test(&(cred)->usage))
__put_cred(cred); __put_cred(cred);
} }
...@@ -252,7 +274,7 @@ static inline void put_cred(const struct cred *_cred) ...@@ -252,7 +274,7 @@ static inline void put_cred(const struct cred *_cred)
#define task_cred_xxx(task, xxx) \ #define task_cred_xxx(task, xxx) \
({ \ ({ \
__typeof__(task->cred->xxx) ___val; \ __typeof__(((struct cred *)NULL)->xxx) ___val; \
rcu_read_lock(); \ rcu_read_lock(); \
___val = __task_cred((task))->xxx; \ ___val = __task_cred((task))->xxx; \
rcu_read_unlock(); \ rcu_read_unlock(); \
......
...@@ -150,6 +150,8 @@ extern struct cred init_cred; ...@@ -150,6 +150,8 @@ extern struct cred init_cred;
.sibling = LIST_HEAD_INIT(tsk.sibling), \ .sibling = LIST_HEAD_INIT(tsk.sibling), \
.group_leader = &tsk, \ .group_leader = &tsk, \
.cred = &init_cred, \ .cred = &init_cred, \
.cred_exec_mutex = \
__MUTEX_INITIALIZER(tsk.cred_exec_mutex), \
.comm = "swapper", \ .comm = "swapper", \
.thread = INIT_THREAD, \ .thread = INIT_THREAD, \
.fs = &init_fs, \ .fs = &init_fs, \
......
...@@ -73,6 +73,7 @@ struct key; ...@@ -73,6 +73,7 @@ struct key;
struct seq_file; struct seq_file;
struct user_struct; struct user_struct;
struct signal_struct; struct signal_struct;
struct cred;
struct key_type; struct key_type;
struct key_owner; struct key_owner;
...@@ -181,7 +182,7 @@ struct key { ...@@ -181,7 +182,7 @@ struct key {
extern struct key *key_alloc(struct key_type *type, extern struct key *key_alloc(struct key_type *type,
const char *desc, const char *desc,
uid_t uid, gid_t gid, uid_t uid, gid_t gid,
struct task_struct *ctx, const struct cred *cred,
key_perm_t perm, key_perm_t perm,
unsigned long flags); unsigned long flags);
...@@ -249,7 +250,7 @@ extern int key_unlink(struct key *keyring, ...@@ -249,7 +250,7 @@ extern int key_unlink(struct key *keyring,
struct key *key); struct key *key);
extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
struct task_struct *ctx, const struct cred *cred,
unsigned long flags, unsigned long flags,
struct key *dest); struct key *dest);
...@@ -276,22 +277,12 @@ extern ctl_table key_sysctls[]; ...@@ -276,22 +277,12 @@ extern ctl_table key_sysctls[];
/* /*
* the userspace interface * the userspace interface
*/ */
extern void switch_uid_keyring(struct user_struct *new_user); extern int install_thread_keyring_to_cred(struct cred *cred);
extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
extern void exit_keys(struct task_struct *tsk);
extern int suid_keys(struct task_struct *tsk);
extern int exec_keys(struct task_struct *tsk); extern int exec_keys(struct task_struct *tsk);
extern void key_fsuid_changed(struct task_struct *tsk); extern void key_fsuid_changed(struct task_struct *tsk);
extern void key_fsgid_changed(struct task_struct *tsk); extern void key_fsgid_changed(struct task_struct *tsk);
extern void key_init(void); extern void key_init(void);
#define __install_session_keyring(keyring) \
({ \
struct key *old_session = current->cred->tgcred->session_keyring; \
current->cred->tgcred->session_keyring = keyring; \
old_session; \
})
#else /* CONFIG_KEYS */ #else /* CONFIG_KEYS */
#define key_validate(k) 0 #define key_validate(k) 0
...@@ -303,11 +294,6 @@ extern void key_init(void); ...@@ -303,11 +294,6 @@ extern void key_init(void);
#define make_key_ref(k, p) NULL #define make_key_ref(k, p) NULL
#define key_ref_to_ptr(k) NULL #define key_ref_to_ptr(k) NULL
#define is_key_possessed(k) 0 #define is_key_possessed(k) 0
#define switch_uid_keyring(u) do { } while(0)
#define __install_session_keyring(k) ({ NULL; })
#define copy_keys(f,t) 0
#define exit_keys(t) do { } while(0)
#define suid_keys(t) do { } while(0)
#define exec_keys(t) do { } while(0) #define exec_keys(t) do { } while(0)
#define key_fsuid_changed(t) do { } while(0) #define key_fsuid_changed(t) do { } while(0)
#define key_fsgid_changed(t) do { } while(0) #define key_fsgid_changed(t) do { } while(0)
......
...@@ -1145,7 +1145,8 @@ struct task_struct { ...@@ -1145,7 +1145,8 @@ struct task_struct {
struct list_head cpu_timers[3]; struct list_head cpu_timers[3];
/* process credentials */ /* process credentials */
struct cred *cred; /* actual/objective task credentials */ const struct cred *cred; /* actual/objective task credentials (COW) */
struct mutex cred_exec_mutex; /* execve vs ptrace cred calculation mutex */
char comm[TASK_COMM_LEN]; /* executable name excluding path char comm[TASK_COMM_LEN]; /* executable name excluding path
- access with [gs]et_task_comm (which lock - access with [gs]et_task_comm (which lock
...@@ -1720,7 +1721,6 @@ static inline struct user_struct *get_uid(struct user_struct *u) ...@@ -1720,7 +1721,6 @@ static inline struct user_struct *get_uid(struct user_struct *u)
return u; return u;
} }
extern void free_uid(struct user_struct *); extern void free_uid(struct user_struct *);
extern void switch_uid(struct user_struct *);
extern void release_uids(struct user_namespace *ns); extern void release_uids(struct user_namespace *ns);
#include <asm/current.h> #include <asm/current.h>
...@@ -1870,6 +1870,8 @@ static inline unsigned long wait_task_inactive(struct task_struct *p, ...@@ -1870,6 +1870,8 @@ static inline unsigned long wait_task_inactive(struct task_struct *p,
#define for_each_process(p) \ #define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; ) for (p = &init_task ; (p = next_task(p)) != &init_task ; )
extern bool is_single_threaded(struct task_struct *);
/* /*
* Careful: do_each_thread/while_each_thread is a double loop so * Careful: do_each_thread/while_each_thread is a double loop so
* 'break' will not work as expected - use goto instead. * 'break' will not work as expected - use goto instead.
......
This diff is collapsed.
...@@ -669,6 +669,7 @@ asmlinkage void __init start_kernel(void) ...@@ -669,6 +669,7 @@ asmlinkage void __init start_kernel(void)
efi_enter_virtual_mode(); efi_enter_virtual_mode();
#endif #endif
thread_info_cache_init(); thread_info_cache_init();
cred_init();
fork_init(num_physpages); fork_init(num_physpages);
proc_caches_init(); proc_caches_init();
buffer_init(); buffer_init();
......
...@@ -2546,18 +2546,17 @@ int __audit_signal_info(int sig, struct task_struct *t) ...@@ -2546,18 +2546,17 @@ int __audit_signal_info(int sig, struct task_struct *t)
/** /**
* __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
* @bprm pointer to the bprm being processed * @bprm: pointer to the bprm being processed
* @caps the caps read from the disk * @new: the proposed new credentials
* @old: the old credentials
* *
* Simply check if the proc already has the caps given by the file and if not * Simply check if the proc already has the caps given by the file and if not
* store the priv escalation info for later auditing at the end of the syscall * store the priv escalation info for later auditing at the end of the syscall
* *
* this can fail and we don't care. See the note in audit.h for
* audit_log_bprm_fcaps() for my explaination....
*
* -Eric * -Eric
*/ */
void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE) int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
const struct cred *new, const struct cred *old)
{ {
struct audit_aux_data_bprm_fcaps *ax; struct audit_aux_data_bprm_fcaps *ax;
struct audit_context *context = current->audit_context; struct audit_context *context = current->audit_context;
...@@ -2566,7 +2565,7 @@ void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_ ...@@ -2566,7 +2565,7 @@ void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_
ax = kmalloc(sizeof(*ax), GFP_KERNEL); ax = kmalloc(sizeof(*ax), GFP_KERNEL);
if (!ax) if (!ax)
return; return -ENOMEM;
ax->d.type = AUDIT_BPRM_FCAPS; ax->d.type = AUDIT_BPRM_FCAPS;
ax->d.next = context->aux; ax->d.next = context->aux;
...@@ -2581,26 +2580,27 @@ void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_ ...@@ -2581,26 +2580,27 @@ void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_
ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT; ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
ax->old_pcap.permitted = *pP; ax->old_pcap.permitted = old->cap_permitted;
ax->old_pcap.inheritable = current->cred->cap_inheritable; ax->old_pcap.inheritable = old->cap_inheritable;
ax->old_pcap.effective = *pE; ax->old_pcap.effective = old->cap_effective;
ax->new_pcap.permitted = current->cred->cap_permitted; ax->new_pcap.permitted = new->cap_permitted;
ax->new_pcap.inheritable = current->cred->cap_inheritable; ax->new_pcap.inheritable = new->cap_inheritable;
ax->new_pcap.effective = current->cred->cap_effective; ax->new_pcap.effective = new->cap_effective;
return 0;
} }
/** /**
* __audit_log_capset - store information about the arguments to the capset syscall * __audit_log_capset - store information about the arguments to the capset syscall
* @pid target pid of the capset call * @pid: target pid of the capset call
* @eff effective cap set * @new: the new credentials
* @inh inheritible cap set * @old: the old (current) credentials
* @perm permited cap set
* *
* Record the aguments userspace sent to sys_capset for later printing by the * Record the aguments userspace sent to sys_capset for later printing by the
* audit system if applicable * audit system if applicable
*/ */
int __audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_cap_t *perm) int __audit_log_capset(pid_t pid,
const struct cred *new, const struct cred *old)
{ {
struct audit_aux_data_capset *ax; struct audit_aux_data_capset *ax;
struct audit_context *context = current->audit_context; struct audit_context *context = current->audit_context;
...@@ -2617,9 +2617,9 @@ int __audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_c ...@@ -2617,9 +2617,9 @@ int __audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_c
context->aux = (void *)ax; context->aux = (void *)ax;
ax->pid = pid; ax->pid = pid;
ax->cap.effective = *eff; ax->cap.effective = new->cap_effective;
ax->cap.inheritable = *eff; ax->cap.inheritable = new->cap_effective;
ax->cap.permitted = *perm; ax->cap.permitted = new->cap_permitted;
return 0; return 0;
} }
......
...@@ -15,12 +15,7 @@ ...@@ -15,12 +15,7 @@
#include <linux/syscalls.h> #include <linux/syscalls.h>
#include <linux/pid_namespace.h> #include <linux/pid_namespace.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include "cred-internals.h"
/*
* This lock protects task->cap_* for all tasks including current.
* Locking rule: acquire this prior to tasklist_lock.
*/
static DEFINE_SPINLOCK(task_capability_lock);
/* /*
* Leveraged for setting/resetting capabilities * Leveraged for setting/resetting capabilities
...@@ -128,12 +123,11 @@ static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy) ...@@ -128,12 +123,11 @@ static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
} }
/* /*
* If we have configured with filesystem capability support, then the * The only thing that can change the capabilities of the current
* only thing that can change the capabilities of the current process * process is the current process. As such, we can't be in this code
* is the current process. As such, we can't be in this code at the * at the same time as we are in the process of setting capabilities
* same time as we are in the process of setting capabilities in this * in this process. The net result is that we can limit our use of
* process. The net result is that we can limit our use of locks to * locks to when we are reading the caps of another process.
* when we are reading the caps of another process.
*/ */
static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
kernel_cap_t *pIp, kernel_cap_t *pPp) kernel_cap_t *pIp, kernel_cap_t *pPp)
...@@ -143,7 +137,6 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, ...@@ -143,7 +137,6 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
if (pid && (pid != task_pid_vnr(current))) { if (pid && (pid != task_pid_vnr(current))) {
struct task_struct *target; struct task_struct *target;
spin_lock(&task_capability_lock);
read_lock(&tasklist_lock); read_lock(&tasklist_lock);
target = find_task_by_vpid(pid); target = find_task_by_vpid(pid);
...@@ -153,34 +146,12 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, ...@@ -153,34 +146,12 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
ret = security_capget(target, pEp, pIp, pPp); ret = security_capget(target, pEp, pIp, pPp);
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
spin_unlock(&task_capability_lock);
} else } else
ret = security_capget(current, pEp, pIp, pPp); ret = security_capget(current, pEp, pIp, pPp);
return ret; return ret;
} }
/*
* Atomically modify the effective capabilities returning the original
* value. No permission check is performed here - it is assumed that the
* caller is permitted to set the desired effective capabilities.
*/
kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
{
kernel_cap_t pE_old;
spin_lock(&task_capability_lock);
pE_old = current->cred->cap_effective;
current->cred->cap_effective = pE_new;
spin_unlock(&task_capability_lock);
return pE_old;
}
EXPORT_SYMBOL(cap_set_effective);
/** /**
* sys_capget - get the capabilities of a given process. * sys_capget - get the capabilities of a given process.
* @header: pointer to struct that contains capability version and * @header: pointer to struct that contains capability version and
...@@ -208,7 +179,6 @@ asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) ...@@ -208,7 +179,6 @@ asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
return -EINVAL; return -EINVAL;
ret = cap_get_target_pid(pid, &pE, &pI, &pP); ret = cap_get_target_pid(pid, &pE, &pI, &pP);
if (!ret) { if (!ret) {
struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
unsigned i; unsigned i;
...@@ -270,6 +240,7 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) ...@@ -270,6 +240,7 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
unsigned i, tocopy; unsigned i, tocopy;
kernel_cap_t inheritable, permitted, effective; kernel_cap_t inheritable, permitted, effective;
struct cred *new;
int ret; int ret;
pid_t pid; pid_t pid;
...@@ -284,8 +255,8 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) ...@@ -284,8 +255,8 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
if (pid != 0 && pid != task_pid_vnr(current)) if (pid != 0 && pid != task_pid_vnr(current))
return -EPERM; return -EPERM;
if (copy_from_user(&kdata, data, tocopy if (copy_from_user(&kdata, data,
* sizeof(struct __user_cap_data_struct))) tocopy * sizeof(struct __user_cap_data_struct)))
return -EFAULT; return -EFAULT;
for (i = 0; i < tocopy; i++) { for (i = 0; i < tocopy; i++) {
...@@ -300,24 +271,23 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) ...@@ -300,24 +271,23 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
i++; i++;
} }
ret = audit_log_capset(pid, &effective, &inheritable, &permitted); new = prepare_creds();
if (ret) if (!new)
return -ENOMEM;
ret = security_capset(new, current_cred(),
&effective, &inheritable, &permitted);
if (ret < 0)
goto error;
ret = audit_log_capset(pid, new, current_cred());
if (ret < 0)
return ret; return ret;
/* This lock is required even when filesystem capability support is return commit_creds(new);
* configured - it protects the sys_capget() call from returning
* incorrect data in the case that the targeted process is not the
* current one.
*/
spin_lock(&task_capability_lock);
ret = security_capset_check(&effective, &inheritable, &permitted); error:
/* Having verified that the proposed changes are legal, we now put them abort_creds(new);
* into effect.
*/
if (!ret)
security_capset_set(&effective, &inheritable, &permitted);
spin_unlock(&task_capability_lock);
return ret; return ret;
} }
......
/* Internal credentials stuff
*
* Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public Licence
* as published by the Free Software Foundation; either version
* 2 of the Licence, or (at your option) any later version.
*/
/*
* user.c
*/
static inline void sched_switch_user(struct task_struct *p)
{
#ifdef CONFIG_USER_SCHED
sched_move_task(p);
#endif /* CONFIG_USER_SCHED */
}
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
#include <linux/keyctl.h> #include <linux/keyctl.h>
#include <linux/init_task.h> #include <linux/init_task.h>
#include <linux/security.h> #include <linux/security.h>
#include <linux/cn_proc.h>
#include "cred-internals.h"
static struct kmem_cache *cred_jar;
/* /*
* The common credentials for the initial task's thread group * The common credentials for the initial task's thread group
...@@ -64,7 +68,7 @@ static void release_tgcred_rcu(struct rcu_head *rcu) ...@@ -64,7 +68,7 @@ static void release_tgcred_rcu(struct rcu_head *rcu)
/* /*
* Release a set of thread group credentials. * Release a set of thread group credentials.
*/ */
static void release_tgcred(struct cred *cred) void release_tgcred(struct cred *cred)
{ {
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
struct thread_group_cred *tgcred = cred->tgcred; struct thread_group_cred *tgcred = cred->tgcred;
...@@ -81,79 +85,322 @@ static void put_cred_rcu(struct rcu_head *rcu) ...@@ -81,79 +85,322 @@ static void put_cred_rcu(struct rcu_head *rcu)
{ {
struct cred *cred = container_of(rcu, struct cred, rcu); struct cred *cred = container_of(rcu, struct cred, rcu);
BUG_ON(atomic_read(&cred->usage) != 0); if (atomic_read(&cred->usage) != 0)
panic("CRED: put_cred_rcu() sees %p with usage %d\n",
cred, atomic_read(&cred->usage));
security_cred_free(cred);
key_put(cred->thread_keyring); key_put(cred->thread_keyring);
key_put(cred->request_key_auth); key_put(cred->request_key_auth);
release_tgcred(cred); release_tgcred(cred);
put_group_info(cred->group_info); put_group_info(cred->group_info);
free_uid(cred->user); free_uid(cred->user);
security_cred_free(cred); kmem_cache_free(cred_jar, cred);
kfree(cred);
} }
/** /**
* __put_cred - Destroy a set of credentials * __put_cred - Destroy a set of credentials
* @sec: The record to release * @cred: The record to release
* *
* Destroy a set of credentials on which no references remain. * Destroy a set of credentials on which no references remain.
*/ */
void __put_cred(struct cred *cred) void __put_cred(struct cred *cred)
{ {
BUG_ON(atomic_read(&cred->usage) != 0);
call_rcu(&cred->rcu, put_cred_rcu); call_rcu(&cred->rcu, put_cred_rcu);
} }
EXPORT_SYMBOL(__put_cred); EXPORT_SYMBOL(__put_cred);
/**
* prepare_creds - Prepare a new set of credentials for modification
*
* Prepare a new set of task credentials for modification. A task's creds
* shouldn't generally be modified directly, therefore this function is used to
* prepare a new copy, which the caller then modifies and then commits by
* calling commit_creds().
*
* Returns a pointer to the new creds-to-be if successful, NULL otherwise.
*
* Call commit_creds() or abort_creds() to clean up.
*/
struct cred *prepare_creds(void)
{
struct task_struct *task = current;
const struct cred *old;
struct cred *new;
BUG_ON(atomic_read(&task->cred->usage) < 1);
new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
if (!new)
return NULL;
old = task->cred;
memcpy(new, old, sizeof(struct cred));
atomic_set(&new->usage, 1);
get_group_info(new->group_info);
get_uid(new->user);
#ifdef CONFIG_KEYS
key_get(new->thread_keyring);
key_get(new->request_key_auth);
atomic_inc(&new->tgcred->usage);
#endif
#ifdef CONFIG_SECURITY
new->security = NULL;
#endif
if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
goto error;
return new;
error:
abort_creds(new);
return NULL;
}
EXPORT_SYMBOL(prepare_creds);
/*
* prepare new credentials for the usermode helper dispatcher
*/
struct cred *prepare_usermodehelper_creds(void)
{
#ifdef CONFIG_KEYS
struct thread_group_cred *tgcred = NULL;
#endif
struct cred *new;
#ifdef CONFIG_KEYS
tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
if (!tgcred)
return NULL;
#endif
new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
if (!new)
return NULL;
memcpy(new, &init_cred, sizeof(struct cred));
atomic_set(&new->usage, 1);
get_group_info(new->group_info);
get_uid(new->user);
#ifdef CONFIG_KEYS
new->thread_keyring = NULL;
new->request_key_auth = NULL;
new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
atomic_set(&tgcred->usage, 1);
spin_lock_init(&tgcred->lock);
new->tgcred = tgcred;
#endif
#ifdef CONFIG_SECURITY
new->security = NULL;
#endif
if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
goto error;
BUG_ON(atomic_read(&new->usage) != 1);
return new;
error:
put_cred(new);
return NULL;
}
/* /*
* Copy credentials for the new process created by fork() * Copy credentials for the new process created by fork()
*
* We share if we can, but under some circumstances we have to generate a new
* set.
*/ */
int copy_creds(struct task_struct *p, unsigned long clone_flags) int copy_creds(struct task_struct *p, unsigned long clone_flags)
{ {
struct cred *pcred; #ifdef CONFIG_KEYS
int ret; struct thread_group_cred *tgcred;
#endif
struct cred *new;
pcred = kmemdup(p->cred, sizeof(*p->cred), GFP_KERNEL); mutex_init(&p->cred_exec_mutex);
if (!pcred)
if (
#ifdef CONFIG_KEYS
!p->cred->thread_keyring &&
#endif
clone_flags & CLONE_THREAD
) {
get_cred(p->cred);
atomic_inc(&p->cred->user->processes);
return 0;
}
new = prepare_creds();
if (!new)
return -ENOMEM; return -ENOMEM;
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
if (clone_flags & CLONE_THREAD) { /* new threads get their own thread keyrings if their parent already
atomic_inc(&pcred->tgcred->usage); * had one */
} else { if (new->thread_keyring) {
pcred->tgcred = kmalloc(sizeof(struct cred), GFP_KERNEL); key_put(new->thread_keyring);
if (!pcred->tgcred) { new->thread_keyring = NULL;
kfree(pcred); if (clone_flags & CLONE_THREAD)
install_thread_keyring_to_cred(new);
}
/* we share the process and session keyrings between all the threads in
* a process - this is slightly icky as we violate COW credentials a
* bit */
if (!(clone_flags & CLONE_THREAD)) {
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
if (!tgcred) {
put_cred(new);
return -ENOMEM; return -ENOMEM;
} }
atomic_set(&pcred->tgcred->usage, 1); atomic_set(&tgcred->usage, 1);
spin_lock_init(&pcred->tgcred->lock); spin_lock_init(&tgcred->lock);
pcred->tgcred->process_keyring = NULL; tgcred->process_keyring = NULL;
pcred->tgcred->session_keyring = tgcred->session_keyring = key_get(new->tgcred->session_keyring);
key_get(p->cred->tgcred->session_keyring);
release_tgcred(new);
new->tgcred = tgcred;
} }
#endif #endif
#ifdef CONFIG_SECURITY atomic_inc(&new->user->processes);
pcred->security = NULL; p->cred = new;
#endif return 0;
}
ret = security_cred_alloc(pcred); /**
if (ret < 0) { * commit_creds - Install new credentials upon the current task
release_tgcred(pcred); * @new: The credentials to be assigned
kfree(pcred); *
return ret; * Install a new set of credentials to the current task, using RCU to replace
* the old set.
*
* This function eats the caller's reference to the new credentials.
*
* Always returns 0 thus allowing this function to be tail-called at the end
* of, say, sys_setgid().
*/
int commit_creds(struct cred *new)
{
struct task_struct *task = current;
const struct cred *old;
BUG_ON(atomic_read(&new->usage) < 1);
BUG_ON(atomic_read(&task->cred->usage) < 1);
old = task->cred;
security_commit_creds(new, old);
/* dumpability changes */
if (old->euid != new->euid ||
old->egid != new->egid ||
old->fsuid != new->fsuid ||
old->fsgid != new->fsgid ||
!cap_issubset(new->cap_permitted, old->cap_permitted)) {
set_dumpable(task->mm, suid_dumpable);
task->pdeath_signal = 0;
smp_wmb();
} }
atomic_set(&pcred->usage, 1); /* alter the thread keyring */
get_group_info(pcred->group_info); if (new->fsuid != old->fsuid)
get_uid(pcred->user); key_fsuid_changed(task);
key_get(pcred->thread_keyring); if (new->fsgid != old->fsgid)
key_get(pcred->request_key_auth); key_fsgid_changed(task);
/* do it
* - What if a process setreuid()'s and this brings the
* new uid over his NPROC rlimit? We can check this now
* cheaply with the new uid cache, so if it matters
* we should be checking for it. -DaveM
*/
if (new->user != old->user)
atomic_inc(&new->user->processes);
rcu_assign_pointer(task->cred, new);
if (new->user != old->user)
atomic_dec(&old->user->processes);
sched_switch_user(task);
/* send notifications */
if (new->uid != old->uid ||
new->euid != old->euid ||
new->suid != old->suid ||
new->fsuid != old->fsuid)
proc_id_connector(task, PROC_EVENT_UID);
atomic_inc(&pcred->user->processes); if (new->gid != old->gid ||
new->egid != old->egid ||
new->sgid != old->sgid ||
new->fsgid != old->fsgid)
proc_id_connector(task, PROC_EVENT_GID);
/* RCU assignment is unneeded here as no-one can have accessed this put_cred(old);
* pointer yet, barring us */
p->cred = pcred;
return 0; return 0;
} }
EXPORT_SYMBOL(commit_creds);
/**
* abort_creds - Discard a set of credentials and unlock the current task
* @new: The credentials that were going to be applied
*
* Discard a set of credentials that were under construction and unlock the
* current task.
*/
void abort_creds(struct cred *new)
{
BUG_ON(atomic_read(&new->usage) < 1);
put_cred(new);
}
EXPORT_SYMBOL(abort_creds);
/**
* override_creds - Temporarily override the current process's credentials
* @new: The credentials to be assigned
*
* Install a set of temporary override credentials on the current process,
* returning the old set for later reversion.
*/
const struct cred *override_creds(const struct cred *new)
{
const struct cred *old = current->cred;
rcu_assign_pointer(current->cred, get_cred(new));
return old;
}
EXPORT_SYMBOL(override_creds);
/**
* revert_creds - Revert a temporary credentials override
* @old: The credentials to be restored
*
* Revert a temporary set of override credentials to an old set, discarding the
* override set.
*/
void revert_creds(const struct cred *old)
{
const struct cred *override = current->cred;
rcu_assign_pointer(current->cred, old);
put_cred(override);
}
EXPORT_SYMBOL(revert_creds);
/*
* initialise the credentials stuff
*/
void __init cred_init(void)
{
/* allocate a slab in which we can store credentials */
cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
}
...@@ -47,12 +47,14 @@ ...@@ -47,12 +47,14 @@
#include <linux/blkdev.h> #include <linux/blkdev.h>
#include <linux/task_io_accounting_ops.h> #include <linux/task_io_accounting_ops.h>
#include <linux/tracehook.h> #include <linux/tracehook.h>
#include <linux/init_task.h>
#include <trace/sched.h> #include <trace/sched.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/unistd.h> #include <asm/unistd.h>
#include <asm/pgtable.h> #include <asm/pgtable.h>
#include <asm/mmu_context.h> #include <asm/mmu_context.h>
#include "cred-internals.h"
static void exit_mm(struct task_struct * tsk); static void exit_mm(struct task_struct * tsk);
...@@ -338,12 +340,12 @@ static void reparent_to_kthreadd(void) ...@@ -338,12 +340,12 @@ static void reparent_to_kthreadd(void)
/* cpus_allowed? */ /* cpus_allowed? */
/* rt_priority? */ /* rt_priority? */
/* signals? */ /* signals? */
security_task_reparent_to_init(current);
memcpy(current->signal->rlim, init_task.signal->rlim, memcpy(current->signal->rlim, init_task.signal->rlim,
sizeof(current->signal->rlim)); sizeof(current->signal->rlim));
atomic_inc(&(INIT_USER->__count));
atomic_inc(&init_cred.usage);
commit_creds(&init_cred);
write_unlock_irq(&tasklist_lock); write_unlock_irq(&tasklist_lock);
switch_uid(INIT_USER);
} }
void __set_special_pids(struct pid *pid) void __set_special_pids(struct pid *pid)
...@@ -1085,7 +1087,6 @@ NORET_TYPE void do_exit(long code) ...@@ -1085,7 +1087,6 @@ NORET_TYPE void do_exit(long code)
check_stack_usage(); check_stack_usage();
exit_thread(); exit_thread();
cgroup_exit(tsk, 1); cgroup_exit(tsk, 1);
exit_keys(tsk);
if (group_dead && tsk->signal->leader) if (group_dead && tsk->signal->leader)
disassociate_ctty(1); disassociate_ctty(1);
......
...@@ -1084,10 +1084,8 @@ static struct task_struct *copy_process(unsigned long clone_flags, ...@@ -1084,10 +1084,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
goto bad_fork_cleanup_sighand; goto bad_fork_cleanup_sighand;
if ((retval = copy_mm(clone_flags, p))) if ((retval = copy_mm(clone_flags, p)))
goto bad_fork_cleanup_signal; goto bad_fork_cleanup_signal;
if ((retval = copy_keys(clone_flags, p)))
goto bad_fork_cleanup_mm;
if ((retval = copy_namespaces(clone_flags, p))) if ((retval = copy_namespaces(clone_flags, p)))
goto bad_fork_cleanup_keys; goto bad_fork_cleanup_mm;
if ((retval = copy_io(clone_flags, p))) if ((retval = copy_io(clone_flags, p)))
goto bad_fork_cleanup_namespaces; goto bad_fork_cleanup_namespaces;
retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs); retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
...@@ -1252,8 +1250,6 @@ static struct task_struct *copy_process(unsigned long clone_flags, ...@@ -1252,8 +1250,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
put_io_context(p->io_context); put_io_context(p->io_context);
bad_fork_cleanup_namespaces: bad_fork_cleanup_namespaces:
exit_task_namespaces(p); exit_task_namespaces(p);
bad_fork_cleanup_keys:
exit_keys(p);
bad_fork_cleanup_mm: bad_fork_cleanup_mm:
if (p->mm) if (p->mm)
mmput(p->mm); mmput(p->mm);
...@@ -1281,6 +1277,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, ...@@ -1281,6 +1277,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
bad_fork_cleanup_put_domain: bad_fork_cleanup_put_domain:
module_put(task_thread_info(p)->exec_domain->module); module_put(task_thread_info(p)->exec_domain->module);
bad_fork_cleanup_count: bad_fork_cleanup_count:
atomic_dec(&p->cred->user->processes);
put_cred(p->cred); put_cred(p->cred);
bad_fork_free: bad_fork_free:
free_task(p); free_task(p);
......
...@@ -118,10 +118,10 @@ EXPORT_SYMBOL(request_module); ...@@ -118,10 +118,10 @@ EXPORT_SYMBOL(request_module);
struct subprocess_info { struct subprocess_info {
struct work_struct work; struct work_struct work;
struct completion *complete; struct completion *complete;
struct cred *cred;
char *path; char *path;
char **argv; char **argv;
char **envp; char **envp;
struct key *ring;
enum umh_wait wait; enum umh_wait wait;
int retval; int retval;
struct file *stdin; struct file *stdin;
...@@ -134,19 +134,20 @@ struct subprocess_info { ...@@ -134,19 +134,20 @@ struct subprocess_info {
static int ____call_usermodehelper(void *data) static int ____call_usermodehelper(void *data)
{ {
struct subprocess_info *sub_info = data; struct subprocess_info *sub_info = data;
struct key *new_session, *old_session;
int retval; int retval;
/* Unblock all signals and set the session keyring. */ BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
new_session = key_get(sub_info->ring);
/* Unblock all signals */
spin_lock_irq(&current->sighand->siglock); spin_lock_irq(&current->sighand->siglock);
old_session = __install_session_keyring(new_session);
flush_signal_handlers(current, 1); flush_signal_handlers(current, 1);
sigemptyset(&current->blocked); sigemptyset(&current->blocked);
recalc_sigpending(); recalc_sigpending();
spin_unlock_irq(&current->sighand->siglock); spin_unlock_irq(&current->sighand->siglock);
key_put(old_session); /* Install the credentials */
commit_creds(sub_info->cred);
sub_info->cred = NULL;
/* Install input pipe when needed */ /* Install input pipe when needed */
if (sub_info->stdin) { if (sub_info->stdin) {
...@@ -185,6 +186,8 @@ void call_usermodehelper_freeinfo(struct subprocess_info *info) ...@@ -185,6 +186,8 @@ void call_usermodehelper_freeinfo(struct subprocess_info *info)
{ {
if (info->cleanup) if (info->cleanup)
(*info->cleanup)(info->argv, info->envp); (*info->cleanup)(info->argv, info->envp);
if (info->cred)
put_cred(info->cred);
kfree(info); kfree(info);
} }
EXPORT_SYMBOL(call_usermodehelper_freeinfo); EXPORT_SYMBOL(call_usermodehelper_freeinfo);
...@@ -240,6 +243,8 @@ static void __call_usermodehelper(struct work_struct *work) ...@@ -240,6 +243,8 @@ static void __call_usermodehelper(struct work_struct *work)
pid_t pid; pid_t pid;
enum umh_wait wait = sub_info->wait; enum umh_wait wait = sub_info->wait;
BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
/* CLONE_VFORK: wait until the usermode helper has execve'd /* CLONE_VFORK: wait until the usermode helper has execve'd
* successfully We need the data structures to stay around * successfully We need the data structures to stay around
* until that is done. */ * until that is done. */
...@@ -362,6 +367,9 @@ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv, ...@@ -362,6 +367,9 @@ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
sub_info->path = path; sub_info->path = path;
sub_info->argv = argv; sub_info->argv = argv;
sub_info->envp = envp; sub_info->envp = envp;
sub_info->cred = prepare_usermodehelper_creds();
if (!sub_info->cred)
return NULL;
out: out:
return sub_info; return sub_info;
...@@ -376,7 +384,13 @@ EXPORT_SYMBOL(call_usermodehelper_setup); ...@@ -376,7 +384,13 @@ EXPORT_SYMBOL(call_usermodehelper_setup);
void call_usermodehelper_setkeys(struct subprocess_info *info, void call_usermodehelper_setkeys(struct subprocess_info *info,
struct key *session_keyring) struct key *session_keyring)
{ {
info->ring = session_keyring; #ifdef CONFIG_KEYS
struct thread_group_cred *tgcred = info->cred->tgcred;
key_put(tgcred->session_keyring);
tgcred->session_keyring = key_get(session_keyring);
#else
BUG();
#endif
} }
EXPORT_SYMBOL(call_usermodehelper_setkeys); EXPORT_SYMBOL(call_usermodehelper_setkeys);
...@@ -444,6 +458,8 @@ int call_usermodehelper_exec(struct subprocess_info *sub_info, ...@@ -444,6 +458,8 @@ int call_usermodehelper_exec(struct subprocess_info *sub_info,
DECLARE_COMPLETION_ONSTACK(done); DECLARE_COMPLETION_ONSTACK(done);
int retval = 0; int retval = 0;
BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
helper_lock(); helper_lock();
if (sub_info->path[0] == '\0') if (sub_info->path[0] == '\0')
goto out; goto out;
......
...@@ -171,6 +171,14 @@ int ptrace_attach(struct task_struct *task) ...@@ -171,6 +171,14 @@ int ptrace_attach(struct task_struct *task)
if (same_thread_group(task, current)) if (same_thread_group(task, current))
goto out; goto out;
/* Protect exec's credential calculations against our interference;
* SUID, SGID and LSM creds get determined differently under ptrace.
*/
retval = mutex_lock_interruptible(&current->cred_exec_mutex);
if (retval < 0)
goto out;
retval = -EPERM;
repeat: repeat:
/* /*
* Nasty, nasty. * Nasty, nasty.
...@@ -210,6 +218,7 @@ int ptrace_attach(struct task_struct *task) ...@@ -210,6 +218,7 @@ int ptrace_attach(struct task_struct *task)
bad: bad:
write_unlock_irqrestore(&tasklist_lock, flags); write_unlock_irqrestore(&tasklist_lock, flags);
task_unlock(task); task_unlock(task);
mutex_unlock(&current->cred_exec_mutex);
out: out:
return retval; return retval;
} }
......
...@@ -180,7 +180,7 @@ int next_signal(struct sigpending *pending, sigset_t *mask) ...@@ -180,7 +180,7 @@ int next_signal(struct sigpending *pending, sigset_t *mask)
/* /*
* allocate a new signal queue record * allocate a new signal queue record
* - this may be called without locks if and only if t == current, otherwise an * - this may be called without locks if and only if t == current, otherwise an
* appopriate lock must be held to protect t's user_struct * appopriate lock must be held to stop the target task from exiting
*/ */
static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags, static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
int override_rlimit) int override_rlimit)
...@@ -194,7 +194,7 @@ static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags, ...@@ -194,7 +194,7 @@ static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
* caller must be holding the RCU readlock (by way of a spinlock) and * caller must be holding the RCU readlock (by way of a spinlock) and
* we use RCU protection here * we use RCU protection here
*/ */
user = __task_cred(t)->user; user = get_uid(__task_cred(t)->user);
atomic_inc(&user->sigpending); atomic_inc(&user->sigpending);
if (override_rlimit || if (override_rlimit ||
atomic_read(&user->sigpending) <= atomic_read(&user->sigpending) <=
...@@ -202,12 +202,14 @@ static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags, ...@@ -202,12 +202,14 @@ static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
q = kmem_cache_alloc(sigqueue_cachep, flags); q = kmem_cache_alloc(sigqueue_cachep, flags);
if (unlikely(q == NULL)) { if (unlikely(q == NULL)) {
atomic_dec(&user->sigpending); atomic_dec(&user->sigpending);
free_uid(user);
} else { } else {
INIT_LIST_HEAD(&q->list); INIT_LIST_HEAD(&q->list);
q->flags = 0; q->flags = 0;
q->user = get_uid(user); q->user = user;
} }
return(q);
return q;
} }
static void __sigqueue_free(struct sigqueue *q) static void __sigqueue_free(struct sigqueue *q)
......
This diff is collapsed.
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/user_namespace.h> #include <linux/user_namespace.h>
#include "cred-internals.h"
struct user_namespace init_user_ns = { struct user_namespace init_user_ns = {
.kref = { .kref = {
...@@ -104,16 +105,10 @@ static int sched_create_user(struct user_struct *up) ...@@ -104,16 +105,10 @@ static int sched_create_user(struct user_struct *up)
return rc; return rc;
} }
static void sched_switch_user(struct task_struct *p)
{
sched_move_task(p);
}
#else /* CONFIG_USER_SCHED */ #else /* CONFIG_USER_SCHED */
static void sched_destroy_user(struct user_struct *up) { } static void sched_destroy_user(struct user_struct *up) { }
static int sched_create_user(struct user_struct *up) { return 0; } static int sched_create_user(struct user_struct *up) { return 0; }
static void sched_switch_user(struct task_struct *p) { }
#endif /* CONFIG_USER_SCHED */ #endif /* CONFIG_USER_SCHED */
...@@ -448,36 +443,6 @@ struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid) ...@@ -448,36 +443,6 @@ struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
return NULL; return NULL;
} }
void switch_uid(struct user_struct *new_user)
{
struct user_struct *old_user;
/* What if a process setreuid()'s and this brings the
* new uid over his NPROC rlimit? We can check this now
* cheaply with the new uid cache, so if it matters
* we should be checking for it. -DaveM
*/
old_user = current->cred->user;
atomic_inc(&new_user->processes);
atomic_dec(&old_user->processes);
switch_uid_keyring(new_user);
current->cred->user = new_user;
sched_switch_user(current);
/*
* We need to synchronize with __sigqueue_alloc()
* doing a get_uid(p->user).. If that saw the old
* user value, we need to wait until it has exited
* its critical region before we can free the old
* structure.
*/
smp_mb();
spin_unlock_wait(&current->sighand->siglock);
free_uid(old_user);
suid_keys(current);
}
#ifdef CONFIG_USER_NS #ifdef CONFIG_USER_NS
void release_uids(struct user_namespace *ns) void release_uids(struct user_namespace *ns)
{ {
......
...@@ -19,6 +19,7 @@ static struct user_namespace *clone_user_ns(struct user_namespace *old_ns) ...@@ -19,6 +19,7 @@ static struct user_namespace *clone_user_ns(struct user_namespace *old_ns)
{ {
struct user_namespace *ns; struct user_namespace *ns;
struct user_struct *new_user; struct user_struct *new_user;
struct cred *new;
int n; int n;
ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL); ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL);
...@@ -45,7 +46,16 @@ static struct user_namespace *clone_user_ns(struct user_namespace *old_ns) ...@@ -45,7 +46,16 @@ static struct user_namespace *clone_user_ns(struct user_namespace *old_ns)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
switch_uid(new_user); /* Install the new user */
new = prepare_creds();
if (!new) {
free_uid(new_user);
free_uid(ns->root_user);
kfree(ns);
}
free_uid(new->user);
new->user = new_user;
commit_creds(new);
return ns; return ns;
} }
......
...@@ -11,7 +11,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \ ...@@ -11,7 +11,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
rbtree.o radix-tree.o dump_stack.o \ rbtree.o radix-tree.o dump_stack.o \
idr.o int_sqrt.o extable.o prio_tree.o \ idr.o int_sqrt.o extable.o prio_tree.o \
sha1.o irq_regs.o reciprocal_div.o argv_split.o \ sha1.o irq_regs.o reciprocal_div.o argv_split.o \
proportions.o prio_heap.o ratelimit.o show_mem.o proportions.o prio_heap.o ratelimit.o show_mem.o is_single_threaded.o
lib-$(CONFIG_MMU) += ioremap.o lib-$(CONFIG_MMU) += ioremap.o
lib-$(CONFIG_SMP) += cpumask.o lib-$(CONFIG_SMP) += cpumask.o
......
...@@ -287,6 +287,7 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *conn, ...@@ -287,6 +287,7 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
time_t expiry, time_t expiry,
u32 kvno) u32 kvno)
{ {
const struct cred *cred = current_cred();
struct key *key; struct key *key;
int ret; int ret;
...@@ -297,7 +298,7 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *conn, ...@@ -297,7 +298,7 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
_enter(""); _enter("");
key = key_alloc(&key_type_rxrpc, "x", 0, 0, current, 0, key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
KEY_ALLOC_NOT_IN_QUOTA); KEY_ALLOC_NOT_IN_QUOTA);
if (IS_ERR(key)) { if (IS_ERR(key)) {
_leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key)); _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
...@@ -340,10 +341,11 @@ EXPORT_SYMBOL(rxrpc_get_server_data_key); ...@@ -340,10 +341,11 @@ EXPORT_SYMBOL(rxrpc_get_server_data_key);
*/ */
struct key *rxrpc_get_null_key(const char *keyname) struct key *rxrpc_get_null_key(const char *keyname)
{ {
const struct cred *cred = current_cred();
struct key *key; struct key *key;
int ret; int ret;
key = key_alloc(&key_type_rxrpc, keyname, 0, 0, current, key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA); KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
if (IS_ERR(key)) if (IS_ERR(key))
return key; return key;
......
...@@ -340,12 +340,16 @@ static int cap_task_create(unsigned long clone_flags) ...@@ -340,12 +340,16 @@ static int cap_task_create(unsigned long clone_flags)
return 0; return 0;
} }
static int cap_cred_alloc_security(struct cred *cred) static void cap_cred_free(struct cred *cred)
{
}
static int cap_cred_prepare(struct cred *new, const struct cred *old, gfp_t gfp)
{ {
return 0; return 0;
} }
static void cap_cred_free(struct cred *cred) static void cap_cred_commit(struct cred *new, const struct cred *old)
{ {
} }
...@@ -750,7 +754,7 @@ static void cap_release_secctx(char *secdata, u32 seclen) ...@@ -750,7 +754,7 @@ static void cap_release_secctx(char *secdata, u32 seclen)
} }
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
static int cap_key_alloc(struct key *key, struct task_struct *ctx, static int cap_key_alloc(struct key *key, const struct cred *cred,
unsigned long flags) unsigned long flags)
{ {
return 0; return 0;
...@@ -760,7 +764,7 @@ static void cap_key_free(struct key *key) ...@@ -760,7 +764,7 @@ static void cap_key_free(struct key *key)
{ {
} }
static int cap_key_permission(key_ref_t key_ref, struct task_struct *context, static int cap_key_permission(key_ref_t key_ref, const struct cred *cred,
key_perm_t perm) key_perm_t perm)
{ {
return 0; return 0;
...@@ -814,8 +818,7 @@ void security_fixup_ops(struct security_operations *ops) ...@@ -814,8 +818,7 @@ void security_fixup_ops(struct security_operations *ops)
set_to_cap_if_null(ops, ptrace_may_access); set_to_cap_if_null(ops, ptrace_may_access);
set_to_cap_if_null(ops, ptrace_traceme); set_to_cap_if_null(ops, ptrace_traceme);
set_to_cap_if_null(ops, capget); set_to_cap_if_null(ops, capget);
set_to_cap_if_null(ops, capset_check); set_to_cap_if_null(ops, capset);
set_to_cap_if_null(ops, capset_set);
set_to_cap_if_null(ops, acct); set_to_cap_if_null(ops, acct);
set_to_cap_if_null(ops, capable); set_to_cap_if_null(ops, capable);
set_to_cap_if_null(ops, quotactl); set_to_cap_if_null(ops, quotactl);
...@@ -890,10 +893,11 @@ void security_fixup_ops(struct security_operations *ops) ...@@ -890,10 +893,11 @@ void security_fixup_ops(struct security_operations *ops)
set_to_cap_if_null(ops, file_receive); set_to_cap_if_null(ops, file_receive);
set_to_cap_if_null(ops, dentry_open); set_to_cap_if_null(ops, dentry_open);
set_to_cap_if_null(ops, task_create); set_to_cap_if_null(ops, task_create);
set_to_cap_if_null(ops, cred_alloc_security);
set_to_cap_if_null(ops, cred_free); set_to_cap_if_null(ops, cred_free);
set_to_cap_if_null(ops, cred_prepare);
set_to_cap_if_null(ops, cred_commit);
set_to_cap_if_null(ops, task_setuid); set_to_cap_if_null(ops, task_setuid);
set_to_cap_if_null(ops, task_post_setuid); set_to_cap_if_null(ops, task_fix_setuid);
set_to_cap_if_null(ops, task_setgid); set_to_cap_if_null(ops, task_setgid);
set_to_cap_if_null(ops, task_setpgid); set_to_cap_if_null(ops, task_setpgid);
set_to_cap_if_null(ops, task_getpgid); set_to_cap_if_null(ops, task_getpgid);
...@@ -910,7 +914,6 @@ void security_fixup_ops(struct security_operations *ops) ...@@ -910,7 +914,6 @@ void security_fixup_ops(struct security_operations *ops)
set_to_cap_if_null(ops, task_wait); set_to_cap_if_null(ops, task_wait);
set_to_cap_if_null(ops, task_kill); set_to_cap_if_null(ops, task_kill);
set_to_cap_if_null(ops, task_prctl); set_to_cap_if_null(ops, task_prctl);
set_to_cap_if_null(ops, task_reparent_to_init);
set_to_cap_if_null(ops, task_to_inode); set_to_cap_if_null(ops, task_to_inode);
set_to_cap_if_null(ops, ipc_permission); set_to_cap_if_null(ops, ipc_permission);
set_to_cap_if_null(ops, ipc_getsecid); set_to_cap_if_null(ops, ipc_getsecid);
......
This diff is collapsed.
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#ifndef _INTERNAL_H #ifndef _INTERNAL_H
#define _INTERNAL_H #define _INTERNAL_H
#include <linux/sched.h>
#include <linux/key-type.h> #include <linux/key-type.h>
static inline __attribute__((format(printf, 1, 2))) static inline __attribute__((format(printf, 1, 2)))
...@@ -25,7 +26,7 @@ void no_printk(const char *fmt, ...) ...@@ -25,7 +26,7 @@ void no_printk(const char *fmt, ...)
#define kleave(FMT, ...) \ #define kleave(FMT, ...) \
printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
#define kdebug(FMT, ...) \ #define kdebug(FMT, ...) \
printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__) printk(KERN_DEBUG " "FMT"\n", ##__VA_ARGS__)
#else #else
#define kenter(FMT, ...) \ #define kenter(FMT, ...) \
no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
...@@ -97,7 +98,7 @@ extern struct key *keyring_search_instkey(struct key *keyring, ...@@ -97,7 +98,7 @@ extern struct key *keyring_search_instkey(struct key *keyring,
typedef int (*key_match_func_t)(const struct key *, const void *); typedef int (*key_match_func_t)(const struct key *, const void *);
extern key_ref_t keyring_search_aux(key_ref_t keyring_ref, extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
struct task_struct *tsk, const struct cred *cred,
struct key_type *type, struct key_type *type,
const void *description, const void *description,
key_match_func_t match); key_match_func_t match);
...@@ -105,13 +106,13 @@ extern key_ref_t keyring_search_aux(key_ref_t keyring_ref, ...@@ -105,13 +106,13 @@ extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
extern key_ref_t search_process_keyrings(struct key_type *type, extern key_ref_t search_process_keyrings(struct key_type *type,
const void *description, const void *description,
key_match_func_t match, key_match_func_t match,
struct task_struct *tsk); const struct cred *cred);
extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check); extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
extern int install_user_keyrings(void); extern int install_user_keyrings(void);
extern int install_thread_keyring(void); extern int install_thread_keyring_to_cred(struct cred *);
extern int install_process_keyring(void); extern int install_process_keyring_to_cred(struct cred *);
extern struct key *request_key_and_link(struct key_type *type, extern struct key *request_key_and_link(struct key_type *type,
const char *description, const char *description,
...@@ -130,12 +131,12 @@ extern long join_session_keyring(const char *name); ...@@ -130,12 +131,12 @@ extern long join_session_keyring(const char *name);
* check to see whether permission is granted to use a key in the desired way * check to see whether permission is granted to use a key in the desired way
*/ */
extern int key_task_permission(const key_ref_t key_ref, extern int key_task_permission(const key_ref_t key_ref,
struct task_struct *context, const struct cred *cred,
key_perm_t perm); key_perm_t perm);
static inline int key_permission(const key_ref_t key_ref, key_perm_t perm) static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
{ {
return key_task_permission(key_ref, current, perm); return key_task_permission(key_ref, current_cred(), perm);
} }
/* required permissions */ /* required permissions */
...@@ -153,7 +154,7 @@ static inline int key_permission(const key_ref_t key_ref, key_perm_t perm) ...@@ -153,7 +154,7 @@ static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
struct request_key_auth { struct request_key_auth {
struct key *target_key; struct key *target_key;
struct key *dest_keyring; struct key *dest_keyring;
struct task_struct *context; const struct cred *cred;
void *callout_info; void *callout_info;
size_t callout_len; size_t callout_len;
pid_t pid; pid_t pid;
......
...@@ -218,7 +218,7 @@ static inline void key_alloc_serial(struct key *key) ...@@ -218,7 +218,7 @@ static inline void key_alloc_serial(struct key *key)
* instantiate the key or discard it before returning * instantiate the key or discard it before returning
*/ */
struct key *key_alloc(struct key_type *type, const char *desc, struct key *key_alloc(struct key_type *type, const char *desc,
uid_t uid, gid_t gid, struct task_struct *ctx, uid_t uid, gid_t gid, const struct cred *cred,
key_perm_t perm, unsigned long flags) key_perm_t perm, unsigned long flags)
{ {
struct key_user *user = NULL; struct key_user *user = NULL;
...@@ -294,7 +294,7 @@ struct key *key_alloc(struct key_type *type, const char *desc, ...@@ -294,7 +294,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
#endif #endif
/* let the security module know about the key */ /* let the security module know about the key */
ret = security_key_alloc(key, ctx, flags); ret = security_key_alloc(key, cred, flags);
if (ret < 0) if (ret < 0)
goto security_error; goto security_error;
...@@ -391,7 +391,7 @@ static int __key_instantiate_and_link(struct key *key, ...@@ -391,7 +391,7 @@ static int __key_instantiate_and_link(struct key *key,
const void *data, const void *data,
size_t datalen, size_t datalen,
struct key *keyring, struct key *keyring,
struct key *instkey) struct key *authkey)
{ {
int ret, awaken; int ret, awaken;
...@@ -421,8 +421,8 @@ static int __key_instantiate_and_link(struct key *key, ...@@ -421,8 +421,8 @@ static int __key_instantiate_and_link(struct key *key,
ret = __key_link(keyring, key); ret = __key_link(keyring, key);
/* disable the authorisation key */ /* disable the authorisation key */
if (instkey) if (authkey)
key_revoke(instkey); key_revoke(authkey);
} }
} }
...@@ -444,14 +444,14 @@ int key_instantiate_and_link(struct key *key, ...@@ -444,14 +444,14 @@ int key_instantiate_and_link(struct key *key,
const void *data, const void *data,
size_t datalen, size_t datalen,
struct key *keyring, struct key *keyring,
struct key *instkey) struct key *authkey)
{ {
int ret; int ret;
if (keyring) if (keyring)
down_write(&keyring->sem); down_write(&keyring->sem);
ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey); ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey);
if (keyring) if (keyring)
up_write(&keyring->sem); up_write(&keyring->sem);
...@@ -469,7 +469,7 @@ EXPORT_SYMBOL(key_instantiate_and_link); ...@@ -469,7 +469,7 @@ EXPORT_SYMBOL(key_instantiate_and_link);
int key_negate_and_link(struct key *key, int key_negate_and_link(struct key *key,
unsigned timeout, unsigned timeout,
struct key *keyring, struct key *keyring,
struct key *instkey) struct key *authkey)
{ {
struct timespec now; struct timespec now;
int ret, awaken; int ret, awaken;
...@@ -504,8 +504,8 @@ int key_negate_and_link(struct key *key, ...@@ -504,8 +504,8 @@ int key_negate_and_link(struct key *key,
ret = __key_link(keyring, key); ret = __key_link(keyring, key);
/* disable the authorisation key */ /* disable the authorisation key */
if (instkey) if (authkey)
key_revoke(instkey); key_revoke(authkey);
} }
mutex_unlock(&key_construction_mutex); mutex_unlock(&key_construction_mutex);
...@@ -743,6 +743,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, ...@@ -743,6 +743,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
key_perm_t perm, key_perm_t perm,
unsigned long flags) unsigned long flags)
{ {
const struct cred *cred = current_cred();
struct key_type *ktype; struct key_type *ktype;
struct key *keyring, *key = NULL; struct key *keyring, *key = NULL;
key_ref_t key_ref; key_ref_t key_ref;
...@@ -802,8 +803,8 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, ...@@ -802,8 +803,8 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
} }
/* allocate a new key */ /* allocate a new key */
key = key_alloc(ktype, description, current_fsuid(), current_fsgid(), key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
current, perm, flags); perm, flags);
if (IS_ERR(key)) { if (IS_ERR(key)) {
key_ref = ERR_CAST(key); key_ref = ERR_CAST(key);
goto error_3; goto error_3;
......
...@@ -866,6 +866,23 @@ static long get_instantiation_keyring(key_serial_t ringid, ...@@ -866,6 +866,23 @@ static long get_instantiation_keyring(key_serial_t ringid,
return -ENOKEY; return -ENOKEY;
} }
/*
* change the request_key authorisation key on the current process
*/
static int keyctl_change_reqkey_auth(struct key *key)
{
struct cred *new;
new = prepare_creds();
if (!new)
return -ENOMEM;
key_put(new->request_key_auth);
new->request_key_auth = key_get(key);
return commit_creds(new);
}
/*****************************************************************************/ /*****************************************************************************/
/* /*
* instantiate the key with the specified payload, and, if one is given, link * instantiate the key with the specified payload, and, if one is given, link
...@@ -876,12 +893,15 @@ long keyctl_instantiate_key(key_serial_t id, ...@@ -876,12 +893,15 @@ long keyctl_instantiate_key(key_serial_t id,
size_t plen, size_t plen,
key_serial_t ringid) key_serial_t ringid)
{ {
const struct cred *cred = current_cred();
struct request_key_auth *rka; struct request_key_auth *rka;
struct key *instkey, *dest_keyring; struct key *instkey, *dest_keyring;
void *payload; void *payload;
long ret; long ret;
bool vm = false; bool vm = false;
kenter("%d,,%zu,%d", id, plen, ringid);
ret = -EINVAL; ret = -EINVAL;
if (plen > 1024 * 1024 - 1) if (plen > 1024 * 1024 - 1)
goto error; goto error;
...@@ -889,7 +909,7 @@ long keyctl_instantiate_key(key_serial_t id, ...@@ -889,7 +909,7 @@ long keyctl_instantiate_key(key_serial_t id,
/* the appropriate instantiation authorisation key must have been /* the appropriate instantiation authorisation key must have been
* assumed before calling this */ * assumed before calling this */
ret = -EPERM; ret = -EPERM;
instkey = current->cred->request_key_auth; instkey = cred->request_key_auth;
if (!instkey) if (!instkey)
goto error; goto error;
...@@ -931,10 +951,8 @@ long keyctl_instantiate_key(key_serial_t id, ...@@ -931,10 +951,8 @@ long keyctl_instantiate_key(key_serial_t id,
/* discard the assumed authority if it's just been disabled by /* discard the assumed authority if it's just been disabled by
* instantiation of the key */ * instantiation of the key */
if (ret == 0) { if (ret == 0)
key_put(current->cred->request_key_auth); keyctl_change_reqkey_auth(NULL);
current->cred->request_key_auth = NULL;
}
error2: error2:
if (!vm) if (!vm)
...@@ -953,14 +971,17 @@ long keyctl_instantiate_key(key_serial_t id, ...@@ -953,14 +971,17 @@ long keyctl_instantiate_key(key_serial_t id,
*/ */
long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
{ {
const struct cred *cred = current_cred();
struct request_key_auth *rka; struct request_key_auth *rka;
struct key *instkey, *dest_keyring; struct key *instkey, *dest_keyring;
long ret; long ret;
kenter("%d,%u,%d", id, timeout, ringid);
/* the appropriate instantiation authorisation key must have been /* the appropriate instantiation authorisation key must have been
* assumed before calling this */ * assumed before calling this */
ret = -EPERM; ret = -EPERM;
instkey = current->cred->request_key_auth; instkey = cred->request_key_auth;
if (!instkey) if (!instkey)
goto error; goto error;
...@@ -982,10 +1003,8 @@ long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) ...@@ -982,10 +1003,8 @@ long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
/* discard the assumed authority if it's just been disabled by /* discard the assumed authority if it's just been disabled by
* instantiation of the key */ * instantiation of the key */
if (ret == 0) { if (ret == 0)
key_put(current->cred->request_key_auth); keyctl_change_reqkey_auth(NULL);
current->cred->request_key_auth = NULL;
}
error: error:
return ret; return ret;
...@@ -999,36 +1018,56 @@ long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) ...@@ -999,36 +1018,56 @@ long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
*/ */
long keyctl_set_reqkey_keyring(int reqkey_defl) long keyctl_set_reqkey_keyring(int reqkey_defl)
{ {
struct cred *cred = current->cred; struct cred *new;
int ret; int ret, old_setting;
old_setting = current_cred_xxx(jit_keyring);
if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
return old_setting;
new = prepare_creds();
if (!new)
return -ENOMEM;
switch (reqkey_defl) { switch (reqkey_defl) {
case KEY_REQKEY_DEFL_THREAD_KEYRING: case KEY_REQKEY_DEFL_THREAD_KEYRING:
ret = install_thread_keyring(); ret = install_thread_keyring_to_cred(new);
if (ret < 0) if (ret < 0)
return ret; goto error;
goto set; goto set;
case KEY_REQKEY_DEFL_PROCESS_KEYRING: case KEY_REQKEY_DEFL_PROCESS_KEYRING:
ret = install_process_keyring(); ret = install_process_keyring_to_cred(new);
if (ret < 0) if (ret < 0) {
return ret; if (ret != -EEXIST)
goto error;
ret = 0;
}
goto set;
case KEY_REQKEY_DEFL_DEFAULT: case KEY_REQKEY_DEFL_DEFAULT:
case KEY_REQKEY_DEFL_SESSION_KEYRING: case KEY_REQKEY_DEFL_SESSION_KEYRING:
case KEY_REQKEY_DEFL_USER_KEYRING: case KEY_REQKEY_DEFL_USER_KEYRING:
case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
set: case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
cred->jit_keyring = reqkey_defl; goto set;
case KEY_REQKEY_DEFL_NO_CHANGE: case KEY_REQKEY_DEFL_NO_CHANGE:
return cred->jit_keyring;
case KEY_REQKEY_DEFL_GROUP_KEYRING: case KEY_REQKEY_DEFL_GROUP_KEYRING:
default: default:
return -EINVAL; ret = -EINVAL;
goto error;
} }
set:
new->jit_keyring = reqkey_defl;
commit_creds(new);
return old_setting;
error:
abort_creds(new);
return -EINVAL;
} /* end keyctl_set_reqkey_keyring() */ } /* end keyctl_set_reqkey_keyring() */
/*****************************************************************************/ /*****************************************************************************/
...@@ -1087,9 +1126,7 @@ long keyctl_assume_authority(key_serial_t id) ...@@ -1087,9 +1126,7 @@ long keyctl_assume_authority(key_serial_t id)
/* we divest ourselves of authority if given an ID of 0 */ /* we divest ourselves of authority if given an ID of 0 */
if (id == 0) { if (id == 0) {
key_put(current->cred->request_key_auth); ret = keyctl_change_reqkey_auth(NULL);
current->cred->request_key_auth = NULL;
ret = 0;
goto error; goto error;
} }
...@@ -1104,10 +1141,12 @@ long keyctl_assume_authority(key_serial_t id) ...@@ -1104,10 +1141,12 @@ long keyctl_assume_authority(key_serial_t id)
goto error; goto error;
} }
key_put(current->cred->request_key_auth); ret = keyctl_change_reqkey_auth(authkey);
current->cred->request_key_auth = authkey; if (ret < 0)
ret = authkey->serial; goto error;
key_put(authkey);
ret = authkey->serial;
error: error:
return ret; return ret;
......
...@@ -245,14 +245,14 @@ static long keyring_read(const struct key *keyring, ...@@ -245,14 +245,14 @@ static long keyring_read(const struct key *keyring,
* allocate a keyring and link into the destination keyring * allocate a keyring and link into the destination keyring
*/ */
struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
struct task_struct *ctx, unsigned long flags, const struct cred *cred, unsigned long flags,
struct key *dest) struct key *dest)
{ {
struct key *keyring; struct key *keyring;
int ret; int ret;
keyring = key_alloc(&key_type_keyring, description, keyring = key_alloc(&key_type_keyring, description,
uid, gid, ctx, uid, gid, cred,
(KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL, (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
flags); flags);
...@@ -281,7 +281,7 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, ...@@ -281,7 +281,7 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
* - we propagate the possession attribute from the keyring ref to the key ref * - we propagate the possession attribute from the keyring ref to the key ref
*/ */
key_ref_t keyring_search_aux(key_ref_t keyring_ref, key_ref_t keyring_search_aux(key_ref_t keyring_ref,
struct task_struct *context, const struct cred *cred,
struct key_type *type, struct key_type *type,
const void *description, const void *description,
key_match_func_t match) key_match_func_t match)
...@@ -304,7 +304,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref, ...@@ -304,7 +304,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
key_check(keyring); key_check(keyring);
/* top keyring must have search permission to begin the search */ /* top keyring must have search permission to begin the search */
err = key_task_permission(keyring_ref, context, KEY_SEARCH); err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
if (err < 0) { if (err < 0) {
key_ref = ERR_PTR(err); key_ref = ERR_PTR(err);
goto error; goto error;
...@@ -377,7 +377,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref, ...@@ -377,7 +377,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
/* key must have search permissions */ /* key must have search permissions */
if (key_task_permission(make_key_ref(key, possessed), if (key_task_permission(make_key_ref(key, possessed),
context, KEY_SEARCH) < 0) cred, KEY_SEARCH) < 0)
continue; continue;
/* we set a different error code if we pass a negative key */ /* we set a different error code if we pass a negative key */
...@@ -404,7 +404,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref, ...@@ -404,7 +404,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
continue; continue;
if (key_task_permission(make_key_ref(key, possessed), if (key_task_permission(make_key_ref(key, possessed),
context, KEY_SEARCH) < 0) cred, KEY_SEARCH) < 0)
continue; continue;
/* stack the current position */ /* stack the current position */
...@@ -459,7 +459,7 @@ key_ref_t keyring_search(key_ref_t keyring, ...@@ -459,7 +459,7 @@ key_ref_t keyring_search(key_ref_t keyring,
if (!type->match) if (!type->match)
return ERR_PTR(-ENOKEY); return ERR_PTR(-ENOKEY);
return keyring_search_aux(keyring, current, return keyring_search_aux(keyring, current->cred,
type, description, type->match); type, description, type->match);
} /* end keyring_search() */ } /* end keyring_search() */
......
...@@ -14,24 +14,27 @@ ...@@ -14,24 +14,27 @@
#include "internal.h" #include "internal.h"
/*****************************************************************************/ /*****************************************************************************/
/* /**
* check to see whether permission is granted to use a key in the desired way, * key_task_permission - Check a key can be used
* but permit the security modules to override * @key_ref: The key to check
* @cred: The credentials to use
* @perm: The permissions to check for
*
* Check to see whether permission is granted to use a key in the desired way,
* but permit the security modules to override.
*
* The caller must hold either a ref on cred or must hold the RCU readlock or a
* spinlock.
*/ */
int key_task_permission(const key_ref_t key_ref, int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
struct task_struct *context,
key_perm_t perm) key_perm_t perm)
{ {
const struct cred *cred;
struct key *key; struct key *key;
key_perm_t kperm; key_perm_t kperm;
int ret; int ret;
key = key_ref_to_ptr(key_ref); key = key_ref_to_ptr(key_ref);
rcu_read_lock();
cred = __task_cred(context);
/* use the second 8-bits of permissions for keys the caller owns */ /* use the second 8-bits of permissions for keys the caller owns */
if (key->uid == cred->fsuid) { if (key->uid == cred->fsuid) {
kperm = key->perm >> 16; kperm = key->perm >> 16;
...@@ -57,7 +60,6 @@ int key_task_permission(const key_ref_t key_ref, ...@@ -57,7 +60,6 @@ int key_task_permission(const key_ref_t key_ref,
kperm = key->perm; kperm = key->perm;
use_these_perms: use_these_perms:
rcu_read_lock();
/* use the top 8-bits of permissions for keys the caller possesses /* use the top 8-bits of permissions for keys the caller possesses
* - possessor permissions are additive with other permissions * - possessor permissions are additive with other permissions
...@@ -71,7 +73,7 @@ int key_task_permission(const key_ref_t key_ref, ...@@ -71,7 +73,7 @@ int key_task_permission(const key_ref_t key_ref,
return -EACCES; return -EACCES;
/* let LSM be the final arbiter */ /* let LSM be the final arbiter */
return security_key_permission(key_ref, context, perm); return security_key_permission(key_ref, cred, perm);
} /* end key_task_permission() */ } /* end key_task_permission() */
......
...@@ -136,8 +136,12 @@ static int proc_keys_show(struct seq_file *m, void *v) ...@@ -136,8 +136,12 @@ static int proc_keys_show(struct seq_file *m, void *v)
int rc; int rc;
/* check whether the current task is allowed to view the key (assuming /* check whether the current task is allowed to view the key (assuming
* non-possession) */ * non-possession)
rc = key_task_permission(make_key_ref(key, 0), current, KEY_VIEW); * - the caller holds a spinlock, and thus the RCU read lock, making our
* access to __current_cred() safe
*/
rc = key_task_permission(make_key_ref(key, 0), current_cred(),
KEY_VIEW);
if (rc < 0) if (rc < 0)
return 0; return 0;
......
This diff is collapsed.
...@@ -83,8 +83,10 @@ static int call_sbin_request_key(struct key_construction *cons, ...@@ -83,8 +83,10 @@ static int call_sbin_request_key(struct key_construction *cons,
/* allocate a new session keyring */ /* allocate a new session keyring */
sprintf(desc, "_req.%u", key->serial); sprintf(desc, "_req.%u", key->serial);
keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current, cred = get_current_cred();
keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
KEY_ALLOC_QUOTA_OVERRUN, NULL); KEY_ALLOC_QUOTA_OVERRUN, NULL);
put_cred(cred);
if (IS_ERR(keyring)) { if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring); ret = PTR_ERR(keyring);
goto error_alloc; goto error_alloc;
...@@ -104,8 +106,7 @@ static int call_sbin_request_key(struct key_construction *cons, ...@@ -104,8 +106,7 @@ static int call_sbin_request_key(struct key_construction *cons,
/* we specify the process's default keyrings */ /* we specify the process's default keyrings */
sprintf(keyring_str[0], "%d", sprintf(keyring_str[0], "%d",
cred->thread_keyring ? cred->thread_keyring ? cred->thread_keyring->serial : 0);
cred->thread_keyring->serial : 0);
prkey = 0; prkey = 0;
if (cred->tgcred->process_keyring) if (cred->tgcred->process_keyring)
...@@ -155,8 +156,8 @@ static int call_sbin_request_key(struct key_construction *cons, ...@@ -155,8 +156,8 @@ static int call_sbin_request_key(struct key_construction *cons,
key_put(keyring); key_put(keyring);
error_alloc: error_alloc:
kleave(" = %d", ret);
complete_request_key(cons, ret); complete_request_key(cons, ret);
kleave(" = %d", ret);
return ret; return ret;
} }
...@@ -295,6 +296,7 @@ static int construct_alloc_key(struct key_type *type, ...@@ -295,6 +296,7 @@ static int construct_alloc_key(struct key_type *type,
struct key_user *user, struct key_user *user,
struct key **_key) struct key **_key)
{ {
const struct cred *cred = current_cred();
struct key *key; struct key *key;
key_ref_t key_ref; key_ref_t key_ref;
...@@ -302,9 +304,8 @@ static int construct_alloc_key(struct key_type *type, ...@@ -302,9 +304,8 @@ static int construct_alloc_key(struct key_type *type,
mutex_lock(&user->cons_lock); mutex_lock(&user->cons_lock);
key = key_alloc(type, description, key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
current_fsuid(), current_fsgid(), current, KEY_POS_ALL, KEY_POS_ALL, flags);
flags);
if (IS_ERR(key)) if (IS_ERR(key))
goto alloc_failed; goto alloc_failed;
...@@ -317,8 +318,7 @@ static int construct_alloc_key(struct key_type *type, ...@@ -317,8 +318,7 @@ static int construct_alloc_key(struct key_type *type,
* waited for locks */ * waited for locks */
mutex_lock(&key_construction_mutex); mutex_lock(&key_construction_mutex);
key_ref = search_process_keyrings(type, description, type->match, key_ref = search_process_keyrings(type, description, type->match, cred);
current);
if (!IS_ERR(key_ref)) if (!IS_ERR(key_ref))
goto key_already_present; goto key_already_present;
...@@ -363,6 +363,8 @@ static struct key *construct_key_and_link(struct key_type *type, ...@@ -363,6 +363,8 @@ static struct key *construct_key_and_link(struct key_type *type,
struct key *key; struct key *key;
int ret; int ret;
kenter("");
user = key_user_lookup(current_fsuid()); user = key_user_lookup(current_fsuid());
if (!user) if (!user)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -376,17 +378,21 @@ static struct key *construct_key_and_link(struct key_type *type, ...@@ -376,17 +378,21 @@ static struct key *construct_key_and_link(struct key_type *type,
if (ret == 0) { if (ret == 0) {
ret = construct_key(key, callout_info, callout_len, aux, ret = construct_key(key, callout_info, callout_len, aux,
dest_keyring); dest_keyring);
if (ret < 0) if (ret < 0) {
kdebug("cons failed");
goto construction_failed; goto construction_failed;
} }
}
key_put(dest_keyring); key_put(dest_keyring);
kleave(" = key %d", key_serial(key));
return key; return key;
construction_failed: construction_failed:
key_negate_and_link(key, key_negative_timeout, NULL, NULL); key_negate_and_link(key, key_negative_timeout, NULL, NULL);
key_put(key); key_put(key);
key_put(dest_keyring); key_put(dest_keyring);
kleave(" = %d", ret);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
...@@ -405,6 +411,7 @@ struct key *request_key_and_link(struct key_type *type, ...@@ -405,6 +411,7 @@ struct key *request_key_and_link(struct key_type *type,
struct key *dest_keyring, struct key *dest_keyring,
unsigned long flags) unsigned long flags)
{ {
const struct cred *cred = current_cred();
struct key *key; struct key *key;
key_ref_t key_ref; key_ref_t key_ref;
...@@ -414,7 +421,7 @@ struct key *request_key_and_link(struct key_type *type, ...@@ -414,7 +421,7 @@ struct key *request_key_and_link(struct key_type *type,
/* search all the process keyrings for a key */ /* search all the process keyrings for a key */
key_ref = search_process_keyrings(type, description, type->match, key_ref = search_process_keyrings(type, description, type->match,
current); cred);
if (!IS_ERR(key_ref)) { if (!IS_ERR(key_ref)) {
key = key_ref_to_ptr(key_ref); key = key_ref_to_ptr(key_ref);
......
...@@ -105,9 +105,9 @@ static void request_key_auth_revoke(struct key *key) ...@@ -105,9 +105,9 @@ static void request_key_auth_revoke(struct key *key)
kenter("{%d}", key->serial); kenter("{%d}", key->serial);
if (rka->context) { if (rka->cred) {
put_task_struct(rka->context); put_cred(rka->cred);
rka->context = NULL; rka->cred = NULL;
} }
} /* end request_key_auth_revoke() */ } /* end request_key_auth_revoke() */
...@@ -122,9 +122,9 @@ static void request_key_auth_destroy(struct key *key) ...@@ -122,9 +122,9 @@ static void request_key_auth_destroy(struct key *key)
kenter("{%d}", key->serial); kenter("{%d}", key->serial);
if (rka->context) { if (rka->cred) {
put_task_struct(rka->context); put_cred(rka->cred);
rka->context = NULL; rka->cred = NULL;
} }
key_put(rka->target_key); key_put(rka->target_key);
...@@ -143,6 +143,7 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info, ...@@ -143,6 +143,7 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info,
size_t callout_len, struct key *dest_keyring) size_t callout_len, struct key *dest_keyring)
{ {
struct request_key_auth *rka, *irka; struct request_key_auth *rka, *irka;
const struct cred *cred = current->cred;
struct key *authkey = NULL; struct key *authkey = NULL;
char desc[20]; char desc[20];
int ret; int ret;
...@@ -164,28 +165,25 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info, ...@@ -164,28 +165,25 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info,
/* see if the calling process is already servicing the key request of /* see if the calling process is already servicing the key request of
* another process */ * another process */
if (current->cred->request_key_auth) { if (cred->request_key_auth) {
/* it is - use that instantiation context here too */ /* it is - use that instantiation context here too */
down_read(&current->cred->request_key_auth->sem); down_read(&cred->request_key_auth->sem);
/* if the auth key has been revoked, then the key we're /* if the auth key has been revoked, then the key we're
* servicing is already instantiated */ * servicing is already instantiated */
if (test_bit(KEY_FLAG_REVOKED, if (test_bit(KEY_FLAG_REVOKED, &cred->request_key_auth->flags))
&current->cred->request_key_auth->flags))
goto auth_key_revoked; goto auth_key_revoked;
irka = current->cred->request_key_auth->payload.data; irka = cred->request_key_auth->payload.data;
rka->context = irka->context; rka->cred = get_cred(irka->cred);
rka->pid = irka->pid; rka->pid = irka->pid;
get_task_struct(rka->context);
up_read(&current->cred->request_key_auth->sem); up_read(&cred->request_key_auth->sem);
} }
else { else {
/* it isn't - use this process as the context */ /* it isn't - use this process as the context */
rka->context = current; rka->cred = get_cred(cred);
rka->pid = current->pid; rka->pid = current->pid;
get_task_struct(rka->context);
} }
rka->target_key = key_get(target); rka->target_key = key_get(target);
...@@ -197,7 +195,7 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info, ...@@ -197,7 +195,7 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info,
sprintf(desc, "%x", target->serial); sprintf(desc, "%x", target->serial);
authkey = key_alloc(&key_type_request_key_auth, desc, authkey = key_alloc(&key_type_request_key_auth, desc,
current_fsuid(), current_fsgid(), current, cred->fsuid, cred->fsgid, cred,
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA); KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA);
if (IS_ERR(authkey)) { if (IS_ERR(authkey)) {
...@@ -205,16 +203,16 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info, ...@@ -205,16 +203,16 @@ struct key *request_key_auth_new(struct key *target, const void *callout_info,
goto error_alloc; goto error_alloc;
} }
/* construct and attach to the keyring */ /* construct the auth key */
ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL); ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
if (ret < 0) if (ret < 0)
goto error_inst; goto error_inst;
kleave(" = {%d}", authkey->serial); kleave(" = {%d,%d}", authkey->serial, atomic_read(&authkey->usage));
return authkey; return authkey;
auth_key_revoked: auth_key_revoked:
up_read(&current->cred->request_key_auth->sem); up_read(&cred->request_key_auth->sem);
kfree(rka->callout_info); kfree(rka->callout_info);
kfree(rka); kfree(rka);
kleave("= -EKEYREVOKED"); kleave("= -EKEYREVOKED");
...@@ -257,6 +255,7 @@ static int key_get_instantiation_authkey_match(const struct key *key, ...@@ -257,6 +255,7 @@ static int key_get_instantiation_authkey_match(const struct key *key,
*/ */
struct key *key_get_instantiation_authkey(key_serial_t target_id) struct key *key_get_instantiation_authkey(key_serial_t target_id)
{ {
const struct cred *cred = current_cred();
struct key *authkey; struct key *authkey;
key_ref_t authkey_ref; key_ref_t authkey_ref;
...@@ -264,7 +263,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id) ...@@ -264,7 +263,7 @@ struct key *key_get_instantiation_authkey(key_serial_t target_id)
&key_type_request_key_auth, &key_type_request_key_auth,
(void *) (unsigned long) target_id, (void *) (unsigned long) target_id,
key_get_instantiation_authkey_match, key_get_instantiation_authkey_match,
current); cred);
if (IS_ERR(authkey_ref)) { if (IS_ERR(authkey_ref)) {
authkey = ERR_CAST(authkey_ref); authkey = ERR_CAST(authkey_ref);
......
...@@ -145,18 +145,13 @@ int security_capget(struct task_struct *target, ...@@ -145,18 +145,13 @@ int security_capget(struct task_struct *target,
return security_ops->capget(target, effective, inheritable, permitted); return security_ops->capget(target, effective, inheritable, permitted);
} }
int security_capset_check(const kernel_cap_t *effective, int security_capset(struct cred *new, const struct cred *old,
const kernel_cap_t *effective,
const kernel_cap_t *inheritable, const kernel_cap_t *inheritable,
const kernel_cap_t *permitted) const kernel_cap_t *permitted)
{ {
return security_ops->capset_check(effective, inheritable, permitted); return security_ops->capset(new, old,
} effective, inheritable, permitted);
void security_capset_set(const kernel_cap_t *effective,
const kernel_cap_t *inheritable,
const kernel_cap_t *permitted)
{
security_ops->capset_set(effective, inheritable, permitted);
} }
int security_capable(struct task_struct *tsk, int cap) int security_capable(struct task_struct *tsk, int cap)
...@@ -228,9 +223,9 @@ void security_bprm_free(struct linux_binprm *bprm) ...@@ -228,9 +223,9 @@ void security_bprm_free(struct linux_binprm *bprm)
security_ops->bprm_free_security(bprm); security_ops->bprm_free_security(bprm);
} }
void security_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) int security_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
{ {
security_ops->bprm_apply_creds(bprm, unsafe); return security_ops->bprm_apply_creds(bprm, unsafe);
} }
void security_bprm_post_apply_creds(struct linux_binprm *bprm) void security_bprm_post_apply_creds(struct linux_binprm *bprm)
...@@ -616,14 +611,19 @@ int security_task_create(unsigned long clone_flags) ...@@ -616,14 +611,19 @@ int security_task_create(unsigned long clone_flags)
return security_ops->task_create(clone_flags); return security_ops->task_create(clone_flags);
} }
int security_cred_alloc(struct cred *cred) void security_cred_free(struct cred *cred)
{ {
return security_ops->cred_alloc_security(cred); security_ops->cred_free(cred);
} }
void security_cred_free(struct cred *cred) int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
{ {
security_ops->cred_free(cred); return security_ops->cred_prepare(new, old, gfp);
}
void security_commit_creds(struct cred *new, const struct cred *old)
{
return security_ops->cred_commit(new, old);
} }
int security_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) int security_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
...@@ -631,10 +631,10 @@ int security_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) ...@@ -631,10 +631,10 @@ int security_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
return security_ops->task_setuid(id0, id1, id2, flags); return security_ops->task_setuid(id0, id1, id2, flags);
} }
int security_task_post_setuid(uid_t old_ruid, uid_t old_euid, int security_task_fix_setuid(struct cred *new, const struct cred *old,
uid_t old_suid, int flags) int flags)
{ {
return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, flags); return security_ops->task_fix_setuid(new, old, flags);
} }
int security_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) int security_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
...@@ -716,14 +716,9 @@ int security_task_wait(struct task_struct *p) ...@@ -716,14 +716,9 @@ int security_task_wait(struct task_struct *p)
} }
int security_task_prctl(int option, unsigned long arg2, unsigned long arg3, int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
unsigned long arg4, unsigned long arg5, long *rc_p) unsigned long arg4, unsigned long arg5)
{
return security_ops->task_prctl(option, arg2, arg3, arg4, arg5, rc_p);
}
void security_task_reparent_to_init(struct task_struct *p)
{ {
security_ops->task_reparent_to_init(p); return security_ops->task_prctl(option, arg2, arg3, arg4, arg5);
} }
void security_task_to_inode(struct task_struct *p, struct inode *inode) void security_task_to_inode(struct task_struct *p, struct inode *inode)
...@@ -1123,9 +1118,10 @@ EXPORT_SYMBOL(security_skb_classify_flow); ...@@ -1123,9 +1118,10 @@ EXPORT_SYMBOL(security_skb_classify_flow);
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
int security_key_alloc(struct key *key, struct task_struct *tsk, unsigned long flags) int security_key_alloc(struct key *key, const struct cred *cred,
unsigned long flags)
{ {
return security_ops->key_alloc(key, tsk, flags); return security_ops->key_alloc(key, cred, flags);
} }
void security_key_free(struct key *key) void security_key_free(struct key *key)
...@@ -1134,9 +1130,9 @@ void security_key_free(struct key *key) ...@@ -1134,9 +1130,9 @@ void security_key_free(struct key *key)
} }
int security_key_permission(key_ref_t key_ref, int security_key_permission(key_ref_t key_ref,
struct task_struct *context, key_perm_t perm) const struct cred *cred, key_perm_t perm)
{ {
return security_ops->key_permission(key_ref, context, perm); return security_ops->key_permission(key_ref, cred, perm);
} }
int security_key_getsecurity(struct key *key, char **_buffer) int security_key_getsecurity(struct key *key, char **_buffer)
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment