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

CRED: Make execve() take advantage of copy-on-write credentials

Make execve() take advantage of copy-on-write credentials, allowing it to set
up the credentials in advance, and then commit the whole lot after the point
of no return.

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

This patch makes several logical sets of alteration:

 (1) execve().

     The credential bits from struct linux_binprm are, for the most part,
     replaced with a single credentials pointer (bprm->cred).  This means that
     all the creds can be calculated in advance and then applied at the point
     of no return with no possibility of failure.

     I would like to replace bprm->cap_effective with:

	cap_isclear(bprm->cap_effective)

     but this seems impossible due to special behaviour for processes of pid 1
     (they always retain their parent's capability masks where normally they'd
     be changed - see cap_bprm_set_creds()).

     The following sequence of events now happens:

     (a) At the start of do_execve, the current task's cred_exec_mutex is
     	 locked to prevent PTRACE_ATTACH from obsoleting the calculation of
     	 creds that we make.

     (a) prepare_exec_creds() is then called to make a copy of the current
     	 task's credentials and prepare it.  This copy is then assigned to
     	 bprm->cred.

  	 This renders security_bprm_alloc() and security_bprm_free()
     	 unnecessary, and so they've been removed.

     (b) The determination of unsafe execution is now performed immediately
     	 after (a) rather than later on in the code.  The result is stored in
     	 bprm->unsafe for future reference.

     (c) prepare_binprm() is called, possibly multiple times.

     	 (i) This applies the result of set[ug]id binaries to the new creds
     	     attached to bprm->cred.  Personality bit clearance is recorded,
     	     but now deferred on the basis that the exec procedure may yet
     	     fail.

         (ii) This then calls the new security_bprm_set_creds().  This should
	     calculate the new LSM and capability credentials into *bprm->cred.

	     This folds together security_bprm_set() and parts of
	     security_bprm_apply_creds() (these two have been removed).
	     Anything that might fail must be done at this point.

         (iii) bprm->cred_prepared is set to 1.

	     bprm->cred_prepared is 0 on the first pass of the security
	     calculations, and 1 on all subsequent passes.  This allows SELinux
	     in (ii) to base its calculations only on the initial script and
	     not on the interpreter.

     (d) flush_old_exec() is called to commit the task to execution.  This
     	 performs the following steps with regard to credentials:

	 (i) Clear pdeath_signal and set dumpable on certain circumstances that
	     may not be covered by commit_creds().

         (ii) Clear any bits in current->personality that were deferred from
             (c.i).

     (e) install_exec_creds() [compute_creds() as was] is called to install the
     	 new credentials.  This performs the following steps with regard to
     	 credentials:

         (i) Calls security_bprm_committing_creds() to apply any security
             requirements, such as flushing unauthorised files in SELinux, that
             must be done before the credentials are changed.

	     This is made up of bits of security_bprm_apply_creds() and
	     security_bprm_post_apply_creds(), both of which have been removed.
	     This function is not allowed to fail; anything that might fail
	     must have been done in (c.ii).

         (ii) Calls commit_creds() to apply the new credentials in a single
             assignment (more or less).  Possibly pdeath_signal and dumpable
             should be part of struct creds.

	 (iii) Unlocks the task's cred_replace_mutex, thus allowing
	     PTRACE_ATTACH to take place.

         (iv) Clears The bprm->cred pointer as the credentials it was holding
             are now immutable.

         (v) Calls security_bprm_committed_creds() to apply any security
             alterations that must be done after the creds have been changed.
             SELinux uses this to flush signals and signal handlers.

     (f) If an error occurs before (d.i), bprm_free() will call abort_creds()
     	 to destroy the proposed new credentials and will then unlock
     	 cred_replace_mutex.  No changes to the credentials will have been
     	 made.

 (2) LSM interface.

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

     (*) security_bprm_alloc(), ->bprm_alloc_security()
     (*) security_bprm_free(), ->bprm_free_security()

     	 Removed in favour of preparing new credentials and modifying those.

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

     	 Removed; split between security_bprm_set_creds(),
     	 security_bprm_committing_creds() and security_bprm_committed_creds().

     (*) security_bprm_set(), ->bprm_set_security()

     	 Removed; folded into security_bprm_set_creds().

     (*) security_bprm_set_creds(), ->bprm_set_creds()

     	 New.  The new credentials in bprm->creds should be checked and set up
     	 as appropriate.  bprm->cred_prepared is 0 on the first call, 1 on the
     	 second and subsequent calls.

     (*) security_bprm_committing_creds(), ->bprm_committing_creds()
     (*) security_bprm_committed_creds(), ->bprm_committed_creds()

     	 New.  Apply the security effects of the new credentials.  This
     	 includes closing unauthorised files in SELinux.  This function may not
     	 fail.  When the former is called, the creds haven't yet been applied
     	 to the process; when the latter is called, they have.

 	 The former may access bprm->cred, the latter may not.

 (3) SELinux.

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

     (a) The bprm_security_struct struct has been removed in favour of using
     	 the credentials-under-construction approach.

     (c) flush_unauthorized_files() now takes a cred pointer and passes it on
     	 to inode_has_perm(), file_has_perm() and dentry_open().
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Acked-by: default avatarJames Morris <jmorris@namei.org>
Acked-by: default avatarSerge Hallyn <serue@us.ibm.com>
Signed-off-by: default avatarJames Morris <jmorris@namei.org>
parent d84f4f99
...@@ -327,7 +327,7 @@ static int load_aout_binary(struct linux_binprm *bprm, struct pt_regs *regs) ...@@ -327,7 +327,7 @@ static int load_aout_binary(struct linux_binprm *bprm, struct pt_regs *regs)
current->mm->cached_hole_size = 0; current->mm->cached_hole_size = 0;
current->mm->mmap = NULL; current->mm->mmap = NULL;
compute_creds(bprm); install_exec_creds(bprm);
current->flags &= ~PF_FORKNOEXEC; current->flags &= ~PF_FORKNOEXEC;
if (N_MAGIC(ex) == OMAGIC) { if (N_MAGIC(ex) == OMAGIC) {
......
...@@ -320,7 +320,7 @@ static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs) ...@@ -320,7 +320,7 @@ static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
current->mm->free_area_cache = current->mm->mmap_base; current->mm->free_area_cache = current->mm->mmap_base;
current->mm->cached_hole_size = 0; current->mm->cached_hole_size = 0;
compute_creds(bprm); install_exec_creds(bprm);
current->flags &= ~PF_FORKNOEXEC; current->flags &= ~PF_FORKNOEXEC;
#ifdef __sparc__ #ifdef __sparc__
if (N_MAGIC(ex) == NMAGIC) { if (N_MAGIC(ex) == NMAGIC) {
......
...@@ -956,7 +956,7 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) ...@@ -956,7 +956,7 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
} }
#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */ #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
compute_creds(bprm); install_exec_creds(bprm);
current->flags &= ~PF_FORKNOEXEC; current->flags &= ~PF_FORKNOEXEC;
retval = create_elf_tables(bprm, &loc->elf_ex, retval = create_elf_tables(bprm, &loc->elf_ex,
load_addr, interp_load_addr); load_addr, interp_load_addr);
......
...@@ -404,7 +404,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm, ...@@ -404,7 +404,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,
current->mm->start_stack = current->mm->start_brk + stack_size; current->mm->start_stack = current->mm->start_brk + stack_size;
#endif #endif
compute_creds(bprm); install_exec_creds(bprm);
current->flags &= ~PF_FORKNOEXEC; current->flags &= ~PF_FORKNOEXEC;
if (create_elf_fdpic_tables(bprm, current->mm, if (create_elf_fdpic_tables(bprm, current->mm,
&exec_params, &interp_params) < 0) &exec_params, &interp_params) < 0)
......
...@@ -880,7 +880,7 @@ static int load_flat_binary(struct linux_binprm * bprm, struct pt_regs * regs) ...@@ -880,7 +880,7 @@ static int load_flat_binary(struct linux_binprm * bprm, struct pt_regs * regs)
(libinfo.lib_list[j].loaded)? (libinfo.lib_list[j].loaded)?
libinfo.lib_list[j].start_data:UNLOADED_LIB; libinfo.lib_list[j].start_data:UNLOADED_LIB;
compute_creds(bprm); install_exec_creds(bprm);
current->flags &= ~PF_FORKNOEXEC; current->flags &= ~PF_FORKNOEXEC;
set_binfmt(&flat_format); set_binfmt(&flat_format);
......
...@@ -255,7 +255,7 @@ load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs) ...@@ -255,7 +255,7 @@ load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
kfree(hpuxhdr); kfree(hpuxhdr);
set_binfmt(&som_format); set_binfmt(&som_format);
compute_creds(bprm); install_exec_creds(bprm);
setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT); setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
create_som_tables(bprm); create_som_tables(bprm);
......
...@@ -1393,10 +1393,20 @@ int compat_do_execve(char * filename, ...@@ -1393,10 +1393,20 @@ int compat_do_execve(char * filename,
if (!bprm) if (!bprm)
goto out_ret; goto out_ret;
retval = mutex_lock_interruptible(&current->cred_exec_mutex);
if (retval < 0)
goto out_free;
retval = -ENOMEM;
bprm->cred = prepare_exec_creds();
if (!bprm->cred)
goto out_unlock;
check_unsafe_exec(bprm);
file = open_exec(filename); file = open_exec(filename);
retval = PTR_ERR(file); retval = PTR_ERR(file);
if (IS_ERR(file)) if (IS_ERR(file))
goto out_kfree; goto out_unlock;
sched_exec(); sched_exec();
...@@ -1410,14 +1420,10 @@ int compat_do_execve(char * filename, ...@@ -1410,14 +1420,10 @@ int compat_do_execve(char * filename,
bprm->argc = compat_count(argv, MAX_ARG_STRINGS); bprm->argc = compat_count(argv, MAX_ARG_STRINGS);
if ((retval = bprm->argc) < 0) if ((retval = bprm->argc) < 0)
goto out_mm; goto out;
bprm->envc = compat_count(envp, MAX_ARG_STRINGS); bprm->envc = compat_count(envp, MAX_ARG_STRINGS);
if ((retval = bprm->envc) < 0) if ((retval = bprm->envc) < 0)
goto out_mm;
retval = security_bprm_alloc(bprm);
if (retval)
goto out; goto out;
retval = prepare_binprm(bprm); retval = prepare_binprm(bprm);
...@@ -1438,19 +1444,16 @@ int compat_do_execve(char * filename, ...@@ -1438,19 +1444,16 @@ int compat_do_execve(char * filename,
goto out; goto out;
retval = search_binary_handler(bprm, regs); retval = search_binary_handler(bprm, regs);
if (retval >= 0) { if (retval < 0)
/* execve success */ goto out;
security_bprm_free(bprm);
acct_update_integrals(current);
free_bprm(bprm);
return retval;
}
out: /* execve succeeded */
if (bprm->security) mutex_unlock(&current->cred_exec_mutex);
security_bprm_free(bprm); acct_update_integrals(current);
free_bprm(bprm);
return retval;
out_mm: out:
if (bprm->mm) if (bprm->mm)
mmput(bprm->mm); mmput(bprm->mm);
...@@ -1460,7 +1463,10 @@ out_file: ...@@ -1460,7 +1463,10 @@ out_file:
fput(bprm->file); fput(bprm->file);
} }
out_kfree: out_unlock:
mutex_unlock(&current->cred_exec_mutex);
out_free:
free_bprm(bprm); free_bprm(bprm);
out_ret: out_ret:
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/mmu_context.h> #include <asm/mmu_context.h>
#include <asm/tlb.h> #include <asm/tlb.h>
#include "internal.h"
#ifdef __alpha__ #ifdef __alpha__
/* for /sbin/loader handling in search_binary_handler() */ /* for /sbin/loader handling in search_binary_handler() */
...@@ -1007,15 +1008,17 @@ int flush_old_exec(struct linux_binprm * bprm) ...@@ -1007,15 +1008,17 @@ 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() || /* install the new credentials */
bprm->e_gid != current_egid()) { if (bprm->cred->uid != current_euid() ||
set_dumpable(current->mm, suid_dumpable); bprm->cred->gid != current_egid()) {
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) {
set_dumpable(current->mm, suid_dumpable); set_dumpable(current->mm, suid_dumpable);
} }
current->personality &= ~bprm->per_clear;
/* An exec changes our domain. We are no longer part of the thread /* An exec changes our domain. We are no longer part of the thread
group */ group */
...@@ -1032,13 +1035,50 @@ out: ...@@ -1032,13 +1035,50 @@ out:
EXPORT_SYMBOL(flush_old_exec); EXPORT_SYMBOL(flush_old_exec);
/*
* install the new credentials for this executable
*/
void install_exec_creds(struct linux_binprm *bprm)
{
security_bprm_committing_creds(bprm);
commit_creds(bprm->cred);
bprm->cred = NULL;
/* cred_exec_mutex must be held at least to this point to prevent
* ptrace_attach() from altering our determination of the task's
* credentials; any time after this it may be unlocked */
security_bprm_committed_creds(bprm);
}
EXPORT_SYMBOL(install_exec_creds);
/*
* determine how safe it is to execute the proposed program
* - the caller must hold current->cred_exec_mutex to protect against
* PTRACE_ATTACH
*/
void check_unsafe_exec(struct linux_binprm *bprm)
{
struct task_struct *p = current;
bprm->unsafe = tracehook_unsafe_exec(p);
if (atomic_read(&p->fs->count) > 1 ||
atomic_read(&p->files->count) > 1 ||
atomic_read(&p->sighand->count) > 1)
bprm->unsafe |= LSM_UNSAFE_SHARE;
}
/* /*
* Fill the binprm structure from the inode. * Fill the binprm structure from the inode.
* Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
*
* This may be called multiple times for binary chains (scripts for example).
*/ */
int prepare_binprm(struct linux_binprm *bprm) int prepare_binprm(struct linux_binprm *bprm)
{ {
int mode; umode_t mode;
struct inode * inode = bprm->file->f_path.dentry->d_inode; struct inode * inode = bprm->file->f_path.dentry->d_inode;
int retval; int retval;
...@@ -1046,14 +1086,15 @@ int prepare_binprm(struct linux_binprm *bprm) ...@@ -1046,14 +1086,15 @@ int prepare_binprm(struct linux_binprm *bprm)
if (bprm->file->f_op == NULL) if (bprm->file->f_op == NULL)
return -EACCES; return -EACCES;
bprm->e_uid = current_euid(); /* clear any previous set[ug]id data from a previous binary */
bprm->e_gid = current_egid(); bprm->cred->euid = current_euid();
bprm->cred->egid = current_egid();
if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) { if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
/* Set-uid? */ /* Set-uid? */
if (mode & S_ISUID) { if (mode & S_ISUID) {
current->personality &= ~PER_CLEAR_ON_SETID; bprm->per_clear |= PER_CLEAR_ON_SETID;
bprm->e_uid = inode->i_uid; bprm->cred->euid = inode->i_uid;
} }
/* Set-gid? */ /* Set-gid? */
...@@ -1063,50 +1104,23 @@ int prepare_binprm(struct linux_binprm *bprm) ...@@ -1063,50 +1104,23 @@ int prepare_binprm(struct linux_binprm *bprm)
* executable. * executable.
*/ */
if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
current->personality &= ~PER_CLEAR_ON_SETID; bprm->per_clear |= PER_CLEAR_ON_SETID;
bprm->e_gid = inode->i_gid; bprm->cred->egid = inode->i_gid;
} }
} }
/* fill in binprm security blob */ /* fill in binprm security blob */
retval = security_bprm_set(bprm); retval = security_bprm_set_creds(bprm);
if (retval) if (retval)
return retval; return retval;
bprm->cred_prepared = 1;
memset(bprm->buf,0,BINPRM_BUF_SIZE); memset(bprm->buf, 0, BINPRM_BUF_SIZE);
return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE); return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
} }
EXPORT_SYMBOL(prepare_binprm); EXPORT_SYMBOL(prepare_binprm);
static int unsafe_exec(struct task_struct *p)
{
int unsafe = tracehook_unsafe_exec(p);
if (atomic_read(&p->fs->count) > 1 ||
atomic_read(&p->files->count) > 1 ||
atomic_read(&p->sighand->count) > 1)
unsafe |= LSM_UNSAFE_SHARE;
return unsafe;
}
void compute_creds(struct linux_binprm *bprm)
{
int unsafe;
if (bprm->e_uid != current_uid())
current->pdeath_signal = 0;
exec_keys(current);
task_lock(current);
unsafe = unsafe_exec(current);
security_bprm_apply_creds(bprm, unsafe);
task_unlock(current);
security_bprm_post_apply_creds(bprm);
}
EXPORT_SYMBOL(compute_creds);
/* /*
* Arguments are '\0' separated strings found at the location bprm->p * Arguments are '\0' separated strings found at the location bprm->p
* points to; chop off the first by relocating brpm->p to right after * points to; chop off the first by relocating brpm->p to right after
...@@ -1259,6 +1273,8 @@ EXPORT_SYMBOL(search_binary_handler); ...@@ -1259,6 +1273,8 @@ EXPORT_SYMBOL(search_binary_handler);
void free_bprm(struct linux_binprm *bprm) void free_bprm(struct linux_binprm *bprm)
{ {
free_arg_pages(bprm); free_arg_pages(bprm);
if (bprm->cred)
abort_creds(bprm->cred);
kfree(bprm); kfree(bprm);
} }
...@@ -1284,10 +1300,20 @@ int do_execve(char * filename, ...@@ -1284,10 +1300,20 @@ int do_execve(char * filename,
if (!bprm) if (!bprm)
goto out_files; goto out_files;
retval = mutex_lock_interruptible(&current->cred_exec_mutex);
if (retval < 0)
goto out_free;
retval = -ENOMEM;
bprm->cred = prepare_exec_creds();
if (!bprm->cred)
goto out_unlock;
check_unsafe_exec(bprm);
file = open_exec(filename); file = open_exec(filename);
retval = PTR_ERR(file); retval = PTR_ERR(file);
if (IS_ERR(file)) if (IS_ERR(file))
goto out_kfree; goto out_unlock;
sched_exec(); sched_exec();
...@@ -1301,14 +1327,10 @@ int do_execve(char * filename, ...@@ -1301,14 +1327,10 @@ int do_execve(char * filename,
bprm->argc = count(argv, MAX_ARG_STRINGS); bprm->argc = count(argv, MAX_ARG_STRINGS);
if ((retval = bprm->argc) < 0) if ((retval = bprm->argc) < 0)
goto out_mm; goto out;
bprm->envc = count(envp, MAX_ARG_STRINGS); bprm->envc = count(envp, MAX_ARG_STRINGS);
if ((retval = bprm->envc) < 0) if ((retval = bprm->envc) < 0)
goto out_mm;
retval = security_bprm_alloc(bprm);
if (retval)
goto out; goto out;
retval = prepare_binprm(bprm); retval = prepare_binprm(bprm);
...@@ -1330,21 +1352,18 @@ int do_execve(char * filename, ...@@ -1330,21 +1352,18 @@ int do_execve(char * filename,
current->flags &= ~PF_KTHREAD; current->flags &= ~PF_KTHREAD;
retval = search_binary_handler(bprm,regs); retval = search_binary_handler(bprm,regs);
if (retval >= 0) { if (retval < 0)
/* execve success */ goto out;
security_bprm_free(bprm);
acct_update_integrals(current);
free_bprm(bprm);
if (displaced)
put_files_struct(displaced);
return retval;
}
out: /* execve succeeded */
if (bprm->security) mutex_unlock(&current->cred_exec_mutex);
security_bprm_free(bprm); acct_update_integrals(current);
free_bprm(bprm);
if (displaced)
put_files_struct(displaced);
return retval;
out_mm: out:
if (bprm->mm) if (bprm->mm)
mmput (bprm->mm); mmput (bprm->mm);
...@@ -1353,7 +1372,11 @@ out_file: ...@@ -1353,7 +1372,11 @@ out_file:
allow_write_access(bprm->file); allow_write_access(bprm->file);
fput(bprm->file); fput(bprm->file);
} }
out_kfree:
out_unlock:
mutex_unlock(&current->cred_exec_mutex);
out_free:
free_bprm(bprm); free_bprm(bprm);
out_files: out_files:
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
*/ */
struct super_block; struct super_block;
struct linux_binprm;
/* /*
* block_dev.c * block_dev.c
...@@ -39,6 +40,11 @@ static inline int sb_is_blkdev_sb(struct super_block *sb) ...@@ -39,6 +40,11 @@ static inline int sb_is_blkdev_sb(struct super_block *sb)
*/ */
extern void __init chrdev_init(void); extern void __init chrdev_init(void);
/*
* exec.c
*/
extern void check_unsafe_exec(struct linux_binprm *);
/* /*
* namespace.c * namespace.c
*/ */
......
...@@ -508,22 +508,6 @@ static inline int audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) ...@@ -508,22 +508,6 @@ static inline int audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
return 0; return 0;
} }
/*
* ieieeeeee, an audit function without a return code!
*
* This function might fail! I decided that it didn't matter. We are too late
* to fail the syscall and the information isn't REQUIRED for any purpose. It's
* just nice to have. We should be able to look at past audit logs to figure
* out this process's current cap set along with the fcaps from the PATH record
* and use that to come up with the final set. Yeah, its ugly, but all the info
* is still in the audit log. So I'm not going to bother mentioning we failed
* if we couldn't allocate memory.
*
* If someone changes their mind they could create the aux record earlier and
* then search here and use that earlier allocation. But I don't wanna.
*
* -Eric
*/
static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm, static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
const struct cred *new, const struct cred *new,
const struct cred *old) const struct cred *old)
......
...@@ -35,16 +35,20 @@ struct linux_binprm{ ...@@ -35,16 +35,20 @@ struct linux_binprm{
struct mm_struct *mm; struct mm_struct *mm;
unsigned long p; /* current top of mem */ unsigned long p; /* current top of mem */
unsigned int sh_bang:1, unsigned int sh_bang:1,
misc_bang:1; misc_bang:1,
cred_prepared:1,/* true if creds already prepared (multiple
* preps happen for interpreters) */
cap_effective:1;/* true if has elevated effective capabilities,
* false if not; except for init which inherits
* its parent's caps anyway */
#ifdef __alpha__ #ifdef __alpha__
unsigned int taso:1; unsigned int taso:1;
#endif #endif
unsigned int recursion_depth; unsigned int recursion_depth;
struct file * file; struct file * file;
int e_uid, e_gid; struct cred *cred; /* new credentials */
kernel_cap_t cap_post_exec_permitted; int unsafe; /* how unsafe this exec is (mask of LSM_UNSAFE_*) */
bool cap_effective; unsigned int per_clear; /* bits to clear in current->personality */
void *security;
int argc, envc; int argc, envc;
char * filename; /* Name of binary as seen by procps */ char * filename; /* Name of binary as seen by procps */
char * interp; /* Name of the binary really executed. Most char * interp; /* Name of the binary really executed. Most
...@@ -101,7 +105,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm, ...@@ -101,7 +105,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm,
int executable_stack); int executable_stack);
extern int bprm_mm_init(struct linux_binprm *bprm); extern int bprm_mm_init(struct linux_binprm *bprm);
extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm); extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm);
extern void compute_creds(struct linux_binprm *binprm); extern void install_exec_creds(struct linux_binprm *bprm);
extern int do_coredump(long signr, int exit_code, struct pt_regs * regs); extern int do_coredump(long signr, int exit_code, struct pt_regs * regs);
extern int set_binfmt(struct linux_binfmt *new); extern int set_binfmt(struct linux_binfmt *new);
extern void free_bprm(struct linux_binprm *); extern void free_bprm(struct linux_binprm *);
......
...@@ -84,8 +84,6 @@ struct thread_group_cred { ...@@ -84,8 +84,6 @@ 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
/* /*
...@@ -144,6 +142,7 @@ struct cred { ...@@ -144,6 +142,7 @@ struct cred {
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_creds(void);
extern struct cred *prepare_exec_creds(void);
extern struct cred *prepare_usermodehelper_creds(void); extern struct cred *prepare_usermodehelper_creds(void);
extern int commit_creds(struct cred *); extern int commit_creds(struct cred *);
extern void abort_creds(struct cred *); extern void abort_creds(struct cred *);
......
...@@ -278,7 +278,6 @@ extern ctl_table key_sysctls[]; ...@@ -278,7 +278,6 @@ extern ctl_table key_sysctls[];
* the userspace interface * the userspace interface
*/ */
extern int install_thread_keyring_to_cred(struct cred *cred); extern int install_thread_keyring_to_cred(struct cred *cred);
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);
...@@ -294,7 +293,6 @@ extern void key_init(void); ...@@ -294,7 +293,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 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)
#define key_init() do { } while(0) #define key_init() do { } while(0)
......
...@@ -57,8 +57,7 @@ extern int cap_capset(struct cred *new, const struct cred *old, ...@@ -57,8 +57,7 @@ extern int cap_capset(struct cred *new, const struct cred *old,
const kernel_cap_t *effective, 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);
extern int cap_bprm_set_security(struct linux_binprm *bprm); extern int cap_bprm_set_creds(struct linux_binprm *bprm);
extern int cap_bprm_apply_creds(struct linux_binprm *bprm, int unsafe);
extern int cap_bprm_secureexec(struct linux_binprm *bprm); extern int cap_bprm_secureexec(struct linux_binprm *bprm);
extern int cap_inode_setxattr(struct dentry *dentry, const char *name, extern int cap_inode_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags); const void *value, size_t size, int flags);
...@@ -110,7 +109,7 @@ extern unsigned long mmap_min_addr; ...@@ -110,7 +109,7 @@ extern unsigned long mmap_min_addr;
struct sched_param; struct sched_param;
struct request_sock; struct request_sock;
/* bprm_apply_creds unsafe reasons */ /* bprm->unsafe reasons */
#define LSM_UNSAFE_SHARE 1 #define LSM_UNSAFE_SHARE 1
#define LSM_UNSAFE_PTRACE 2 #define LSM_UNSAFE_PTRACE 2
#define LSM_UNSAFE_PTRACE_CAP 4 #define LSM_UNSAFE_PTRACE_CAP 4
...@@ -154,36 +153,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts) ...@@ -154,36 +153,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
* *
* Security hooks for program execution operations. * Security hooks for program execution operations.
* *
* @bprm_alloc_security: * @bprm_set_creds:
* Allocate and attach a security structure to the @bprm->security field.
* The security field is initialized to NULL when the bprm structure is
* allocated.
* @bprm contains the linux_binprm structure to be modified.
* Return 0 if operation was successful.
* @bprm_free_security:
* @bprm contains the linux_binprm structure to be modified.
* Deallocate and clear the @bprm->security field.
* @bprm_apply_creds:
* Compute and set the security attributes of a process being transformed
* by an execve operation based on the old attributes (current->security)
* and the information saved in @bprm->security by the set_security hook.
* Since this function may return an error, in which case the process will
* be killed. However, it can leave the security attributes of the
* process unchanged if an access failure occurs at this point.
* bprm_apply_creds is called under task_lock. @unsafe indicates various
* reasons why it may be unsafe to change security state.
* @bprm contains the linux_binprm structure.
* @bprm_post_apply_creds:
* Runs after bprm_apply_creds with the task_lock dropped, so that
* functions which cannot be called safely under the task_lock can
* be used. This hook is a good place to perform state changes on
* the process such as closing open file descriptors to which access
* is no longer granted if the attributes were changed.
* Note that a security module might need to save state between
* bprm_apply_creds and bprm_post_apply_creds to store the decision
* on whether the process may proceed.
* @bprm contains the linux_binprm structure.
* @bprm_set_security:
* Save security information in the bprm->security field, typically based * Save security information in the bprm->security field, typically based
* on information about the bprm->file, for later use by the apply_creds * on information about the bprm->file, for later use by the apply_creds
* hook. This hook may also optionally check permissions (e.g. for * hook. This hook may also optionally check permissions (e.g. for
...@@ -196,15 +166,30 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts) ...@@ -196,15 +166,30 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
* @bprm contains the linux_binprm structure. * @bprm contains the linux_binprm structure.
* Return 0 if the hook is successful and permission is granted. * Return 0 if the hook is successful and permission is granted.
* @bprm_check_security: * @bprm_check_security:
* This hook mediates the point when a search for a binary handler will * This hook mediates the point when a search for a binary handler will
* begin. It allows a check the @bprm->security value which is set in * begin. It allows a check the @bprm->security value which is set in the
* the preceding set_security call. The primary difference from * preceding set_creds call. The primary difference from set_creds is
* set_security is that the argv list and envp list are reliably * that the argv list and envp list are reliably available in @bprm. This
* available in @bprm. This hook may be called multiple times * hook may be called multiple times during a single execve; and in each
* during a single execve; and in each pass set_security is called * pass set_creds is called first.
* first.
* @bprm contains the linux_binprm structure. * @bprm contains the linux_binprm structure.
* Return 0 if the hook is successful and permission is granted. * Return 0 if the hook is successful and permission is granted.
* @bprm_committing_creds:
* Prepare to install the new security attributes of a process being
* transformed by an execve operation, based on the old credentials
* pointed to by @current->cred and the information set in @bprm->cred by
* the bprm_set_creds hook. @bprm points to the linux_binprm structure.
* This hook is a good place to perform state changes on the process such
* as closing open file descriptors to which access will no longer be
* granted when the attributes are changed. This is called immediately
* before commit_creds().
* @bprm_committed_creds:
* Tidy up after the installation of the new security attributes of a
* process being transformed by an execve operation. The new credentials
* have, by this point, been set to @current->cred. @bprm points to the
* linux_binprm structure. This hook is a good place to perform state
* changes on the process such as clearing out non-inheritable signal
* state. This is called immediately after commit_creds().
* @bprm_secureexec: * @bprm_secureexec:
* Return a boolean value (0 or 1) indicating whether a "secure exec" * Return a boolean value (0 or 1) indicating whether a "secure exec"
* is required. The flag is passed in the auxiliary table * is required. The flag is passed in the auxiliary table
...@@ -1301,13 +1286,11 @@ struct security_operations { ...@@ -1301,13 +1286,11 @@ struct security_operations {
int (*settime) (struct timespec *ts, struct timezone *tz); int (*settime) (struct timespec *ts, struct timezone *tz);
int (*vm_enough_memory) (struct mm_struct *mm, long pages); int (*vm_enough_memory) (struct mm_struct *mm, long pages);
int (*bprm_alloc_security) (struct linux_binprm *bprm); int (*bprm_set_creds) (struct linux_binprm *bprm);
void (*bprm_free_security) (struct linux_binprm *bprm);
int (*bprm_apply_creds) (struct linux_binprm *bprm, int unsafe);
void (*bprm_post_apply_creds) (struct linux_binprm *bprm);
int (*bprm_set_security) (struct linux_binprm *bprm);
int (*bprm_check_security) (struct linux_binprm *bprm); int (*bprm_check_security) (struct linux_binprm *bprm);
int (*bprm_secureexec) (struct linux_binprm *bprm); int (*bprm_secureexec) (struct linux_binprm *bprm);
void (*bprm_committing_creds) (struct linux_binprm *bprm);
void (*bprm_committed_creds) (struct linux_binprm *bprm);
int (*sb_alloc_security) (struct super_block *sb); int (*sb_alloc_security) (struct super_block *sb);
void (*sb_free_security) (struct super_block *sb); void (*sb_free_security) (struct super_block *sb);
...@@ -1569,12 +1552,10 @@ int security_settime(struct timespec *ts, struct timezone *tz); ...@@ -1569,12 +1552,10 @@ int security_settime(struct timespec *ts, struct timezone *tz);
int security_vm_enough_memory(long pages); int security_vm_enough_memory(long pages);
int security_vm_enough_memory_mm(struct mm_struct *mm, long pages); int security_vm_enough_memory_mm(struct mm_struct *mm, long pages);
int security_vm_enough_memory_kern(long pages); int security_vm_enough_memory_kern(long pages);
int security_bprm_alloc(struct linux_binprm *bprm); int security_bprm_set_creds(struct linux_binprm *bprm);
void security_bprm_free(struct linux_binprm *bprm);
int security_bprm_apply_creds(struct linux_binprm *bprm, int unsafe);
void security_bprm_post_apply_creds(struct linux_binprm *bprm);
int security_bprm_set(struct linux_binprm *bprm);
int security_bprm_check(struct linux_binprm *bprm); int security_bprm_check(struct linux_binprm *bprm);
void security_bprm_committing_creds(struct linux_binprm *bprm);
void security_bprm_committed_creds(struct linux_binprm *bprm);
int security_bprm_secureexec(struct linux_binprm *bprm); int security_bprm_secureexec(struct linux_binprm *bprm);
int security_sb_alloc(struct super_block *sb); int security_sb_alloc(struct super_block *sb);
void security_sb_free(struct super_block *sb); void security_sb_free(struct super_block *sb);
...@@ -1812,32 +1793,22 @@ static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages) ...@@ -1812,32 +1793,22 @@ static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
return cap_vm_enough_memory(mm, pages); return cap_vm_enough_memory(mm, pages);
} }
static inline int security_bprm_alloc(struct linux_binprm *bprm) static inline int security_bprm_set_creds(struct linux_binprm *bprm)
{
return 0;
}
static inline void security_bprm_free(struct linux_binprm *bprm)
{ }
static inline int security_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
{ {
return cap_bprm_apply_creds(bprm, unsafe); return cap_bprm_set_creds(bprm);
} }
static inline void security_bprm_post_apply_creds(struct linux_binprm *bprm) static inline int security_bprm_check(struct linux_binprm *bprm)
{ {
return; return 0;
} }
static inline int security_bprm_set(struct linux_binprm *bprm) static inline void security_bprm_committing_creds(struct linux_binprm *bprm)
{ {
return cap_bprm_set_security(bprm);
} }
static inline int security_bprm_check(struct linux_binprm *bprm) static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
{ {
return 0;
} }
static inline int security_bprm_secureexec(struct linux_binprm *bprm) static inline int security_bprm_secureexec(struct linux_binprm *bprm)
......
...@@ -68,7 +68,7 @@ static void release_tgcred_rcu(struct rcu_head *rcu) ...@@ -68,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.
*/ */
void release_tgcred(struct cred *cred) static 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;
...@@ -163,6 +163,50 @@ error: ...@@ -163,6 +163,50 @@ error:
} }
EXPORT_SYMBOL(prepare_creds); EXPORT_SYMBOL(prepare_creds);
/*
* Prepare credentials for current to perform an execve()
* - The caller must hold current->cred_exec_mutex
*/
struct cred *prepare_exec_creds(void)
{
struct thread_group_cred *tgcred = NULL;
struct cred *new;
#ifdef CONFIG_KEYS
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
if (!tgcred)
return NULL;
#endif
new = prepare_creds();
if (!new) {
kfree(tgcred);
return new;
}
#ifdef CONFIG_KEYS
/* newly exec'd tasks don't get a thread keyring */
key_put(new->thread_keyring);
new->thread_keyring = NULL;
/* create a new per-thread-group creds for all this set of threads to
* share */
memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
atomic_set(&tgcred->usage, 1);
spin_lock_init(&tgcred->lock);
/* inherit the session keyring; new process keyring */
key_get(tgcred->session_keyring);
tgcred->process_keyring = NULL;
release_tgcred(new);
new->tgcred = tgcred;
#endif
return new;
}
/* /*
* prepare new credentials for the usermode helper dispatcher * prepare new credentials for the usermode helper dispatcher
*/ */
......
...@@ -32,24 +32,19 @@ static int cap_quota_on(struct dentry *dentry) ...@@ -32,24 +32,19 @@ static int cap_quota_on(struct dentry *dentry)
return 0; return 0;
} }
static int cap_bprm_alloc_security(struct linux_binprm *bprm) static int cap_bprm_check_security (struct linux_binprm *bprm)
{ {
return 0; return 0;
} }
static void cap_bprm_free_security(struct linux_binprm *bprm) static void cap_bprm_committing_creds(struct linux_binprm *bprm)
{ {
} }
static void cap_bprm_post_apply_creds(struct linux_binprm *bprm) static void cap_bprm_committed_creds(struct linux_binprm *bprm)
{ {
} }
static int cap_bprm_check_security(struct linux_binprm *bprm)
{
return 0;
}
static int cap_sb_alloc_security(struct super_block *sb) static int cap_sb_alloc_security(struct super_block *sb)
{ {
return 0; return 0;
...@@ -827,11 +822,9 @@ void security_fixup_ops(struct security_operations *ops) ...@@ -827,11 +822,9 @@ void security_fixup_ops(struct security_operations *ops)
set_to_cap_if_null(ops, syslog); set_to_cap_if_null(ops, syslog);
set_to_cap_if_null(ops, settime); set_to_cap_if_null(ops, settime);
set_to_cap_if_null(ops, vm_enough_memory); set_to_cap_if_null(ops, vm_enough_memory);
set_to_cap_if_null(ops, bprm_alloc_security); set_to_cap_if_null(ops, bprm_set_creds);
set_to_cap_if_null(ops, bprm_free_security); set_to_cap_if_null(ops, bprm_committing_creds);
set_to_cap_if_null(ops, bprm_apply_creds); set_to_cap_if_null(ops, bprm_committed_creds);
set_to_cap_if_null(ops, bprm_post_apply_creds);
set_to_cap_if_null(ops, bprm_set_security);
set_to_cap_if_null(ops, bprm_check_security); set_to_cap_if_null(ops, bprm_check_security);
set_to_cap_if_null(ops, bprm_secureexec); set_to_cap_if_null(ops, bprm_secureexec);
set_to_cap_if_null(ops, sb_alloc_security); set_to_cap_if_null(ops, sb_alloc_security);
......
...@@ -167,7 +167,7 @@ int cap_capset(struct cred *new, ...@@ -167,7 +167,7 @@ int cap_capset(struct cred *new,
static inline void bprm_clear_caps(struct linux_binprm *bprm) static inline void bprm_clear_caps(struct linux_binprm *bprm)
{ {
cap_clear(bprm->cap_post_exec_permitted); cap_clear(bprm->cred->cap_permitted);
bprm->cap_effective = false; bprm->cap_effective = false;
} }
...@@ -198,15 +198,15 @@ int cap_inode_killpriv(struct dentry *dentry) ...@@ -198,15 +198,15 @@ int cap_inode_killpriv(struct dentry *dentry)
} }
static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
struct linux_binprm *bprm) struct linux_binprm *bprm,
bool *effective)
{ {
struct cred *new = bprm->cred;
unsigned i; unsigned i;
int ret = 0; int ret = 0;
if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
bprm->cap_effective = true; *effective = true;
else
bprm->cap_effective = false;
CAP_FOR_EACH_U32(i) { CAP_FOR_EACH_U32(i) {
__u32 permitted = caps->permitted.cap[i]; __u32 permitted = caps->permitted.cap[i];
...@@ -215,16 +215,13 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, ...@@ -215,16 +215,13 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
/* /*
* pP' = (X & fP) | (pI & fI) * pP' = (X & fP) | (pI & fI)
*/ */
bprm->cap_post_exec_permitted.cap[i] = new->cap_permitted.cap[i] =
(current->cred->cap_bset.cap[i] & permitted) | (new->cap_bset.cap[i] & permitted) |
(current->cred->cap_inheritable.cap[i] & inheritable); (new->cap_inheritable.cap[i] & inheritable);
if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) { if (permitted & ~new->cap_permitted.cap[i])
/* /* insufficient to execute correctly */
* insufficient to execute correctly
*/
ret = -EPERM; ret = -EPERM;
}
} }
/* /*
...@@ -232,7 +229,7 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, ...@@ -232,7 +229,7 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
* do not have enough capabilities, we return an error if they are * do not have enough capabilities, we return an error if they are
* missing some "forced" (aka file-permitted) capabilities. * missing some "forced" (aka file-permitted) capabilities.
*/ */
return bprm->cap_effective ? ret : 0; return *effective ? ret : 0;
} }
int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
...@@ -250,10 +247,9 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data ...@@ -250,10 +247,9 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
XATTR_CAPS_SZ); XATTR_CAPS_SZ);
if (size == -ENODATA || size == -EOPNOTSUPP) { if (size == -ENODATA || size == -EOPNOTSUPP)
/* no data, that's ok */ /* no data, that's ok */
return -ENODATA; return -ENODATA;
}
if (size < 0) if (size < 0)
return size; return size;
...@@ -262,7 +258,7 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data ...@@ -262,7 +258,7 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc); cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
switch ((magic_etc & VFS_CAP_REVISION_MASK)) { switch (magic_etc & VFS_CAP_REVISION_MASK) {
case VFS_CAP_REVISION_1: case VFS_CAP_REVISION_1:
if (size != XATTR_CAPS_SZ_1) if (size != XATTR_CAPS_SZ_1)
return -EINVAL; return -EINVAL;
...@@ -283,11 +279,12 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data ...@@ -283,11 +279,12 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
} }
return 0; return 0;
} }
/* Locate any VFS capabilities: */ /* Locate any VFS capabilities: */
static int get_file_caps(struct linux_binprm *bprm) static int get_file_caps(struct linux_binprm *bprm, bool *effective)
{ {
struct dentry *dentry; struct dentry *dentry;
int rc = 0; int rc = 0;
...@@ -313,7 +310,10 @@ static int get_file_caps(struct linux_binprm *bprm) ...@@ -313,7 +310,10 @@ static int get_file_caps(struct linux_binprm *bprm)
goto out; goto out;
} }
rc = bprm_caps_from_vfs_caps(&vcaps, bprm); rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
if (rc == -EINVAL)
printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
__func__, rc, bprm->filename);
out: out:
dput(dentry); dput(dentry);
...@@ -334,18 +334,27 @@ int cap_inode_killpriv(struct dentry *dentry) ...@@ -334,18 +334,27 @@ int cap_inode_killpriv(struct dentry *dentry)
return 0; return 0;
} }
static inline int get_file_caps(struct linux_binprm *bprm) static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
{ {
bprm_clear_caps(bprm); bprm_clear_caps(bprm);
return 0; return 0;
} }
#endif #endif
int cap_bprm_set_security (struct linux_binprm *bprm) /*
* set up the new credentials for an exec'd task
*/
int cap_bprm_set_creds(struct linux_binprm *bprm)
{ {
const struct cred *old = current_cred();
struct cred *new = bprm->cred;
bool effective;
int ret; int ret;
ret = get_file_caps(bprm); effective = false;
ret = get_file_caps(bprm, &effective);
if (ret < 0)
return ret;
if (!issecure(SECURE_NOROOT)) { if (!issecure(SECURE_NOROOT)) {
/* /*
...@@ -353,63 +362,47 @@ int cap_bprm_set_security (struct linux_binprm *bprm) ...@@ -353,63 +362,47 @@ int cap_bprm_set_security (struct linux_binprm *bprm)
* executables under compatibility mode, we override the * executables under compatibility mode, we override the
* capability sets for the file. * capability sets for the file.
* *
* If only the real uid is 0, we do not set the effective * If only the real uid is 0, we do not set the effective bit.
* bit.
*/ */
if (bprm->e_uid == 0 || current_uid() == 0) { if (new->euid == 0 || new->uid == 0) {
/* pP' = (cap_bset & ~0) | (pI & ~0) */ /* pP' = (cap_bset & ~0) | (pI & ~0) */
bprm->cap_post_exec_permitted = cap_combine( new->cap_permitted = cap_combine(old->cap_bset,
current->cred->cap_bset, old->cap_inheritable);
current->cred->cap_inheritable);
bprm->cap_effective = (bprm->e_uid == 0);
ret = 0;
} }
if (new->euid == 0)
effective = true;
} }
return ret; /* Don't let someone trace a set[ug]id/setpcap binary with the revised
} * credentials unless they have the appropriate permit
*/
int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) if ((new->euid != old->uid ||
{ new->egid != old->gid ||
const struct cred *old = current_cred(); !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
struct cred *new; bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
/* downgrade; they get no more than they had, and maybe less */
new = prepare_creds(); if (!capable(CAP_SETUID)) {
if (!new) new->euid = new->uid;
return -ENOMEM; new->egid = new->gid;
if (bprm->e_uid != old->uid || bprm->e_gid != old->gid ||
!cap_issubset(bprm->cap_post_exec_permitted,
old->cap_permitted)) {
set_dumpable(current->mm, suid_dumpable);
current->pdeath_signal = 0;
if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
if (!capable(CAP_SETUID)) {
bprm->e_uid = old->uid;
bprm->e_gid = old->gid;
}
if (cap_limit_ptraced_target()) {
bprm->cap_post_exec_permitted = cap_intersect(
bprm->cap_post_exec_permitted,
new->cap_permitted);
}
} }
if (cap_limit_ptraced_target())
new->cap_permitted = cap_intersect(new->cap_permitted,
old->cap_permitted);
} }
new->suid = new->euid = new->fsuid = bprm->e_uid; new->suid = new->fsuid = new->euid;
new->sgid = new->egid = new->fsgid = bprm->e_gid; new->sgid = new->fsgid = new->egid;
/* For init, we want to retain the capabilities set /* For init, we want to retain the capabilities set in the initial
* in the init_task struct. Thus we skip the usual * task. Thus we skip the usual capability rules
* capability rules */ */
if (!is_global_init(current)) { if (!is_global_init(current)) {
new->cap_permitted = bprm->cap_post_exec_permitted; if (effective)
if (bprm->cap_effective) new->cap_effective = new->cap_permitted;
new->cap_effective = bprm->cap_post_exec_permitted;
else else
cap_clear(new->cap_effective); cap_clear(new->cap_effective);
} }
bprm->cap_effective = effective;
/* /*
* Audit candidate if current->cap_effective is set * Audit candidate if current->cap_effective is set
...@@ -425,23 +418,31 @@ int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) ...@@ -425,23 +418,31 @@ int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
*/ */
if (!cap_isclear(new->cap_effective)) { if (!cap_isclear(new->cap_effective)) {
if (!cap_issubset(CAP_FULL_SET, new->cap_effective) || if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
bprm->e_uid != 0 || new->uid != 0 || new->euid != 0 || new->uid != 0 ||
issecure(SECURE_NOROOT)) issecure(SECURE_NOROOT)) {
audit_log_bprm_fcaps(bprm, new, old); ret = audit_log_bprm_fcaps(bprm, new, old);
if (ret < 0)
return ret;
}
} }
new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
return commit_creds(new); return 0;
} }
int cap_bprm_secureexec (struct linux_binprm *bprm) /*
* determine whether a secure execution is required
* - the creds have been committed at this point, and are no longer available
* through bprm
*/
int cap_bprm_secureexec(struct linux_binprm *bprm)
{ {
const struct cred *cred = current_cred(); const struct cred *cred = current_cred();
if (cred->uid != 0) { if (cred->uid != 0) {
if (bprm->cap_effective) if (bprm->cap_effective)
return 1; return 1;
if (!cap_isclear(bprm->cap_post_exec_permitted)) if (!cap_isclear(cred->cap_permitted))
return 1; return 1;
} }
...@@ -477,7 +478,7 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name) ...@@ -477,7 +478,7 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
} }
/* moved from kernel/sys.c. */ /* moved from kernel/sys.c. */
/* /*
* cap_emulate_setxuid() fixes the effective / permitted capabilities of * cap_emulate_setxuid() fixes the effective / permitted capabilities of
* a process after a call to setuid, setreuid, or setresuid. * a process after a call to setuid, setreuid, or setresuid.
* *
...@@ -491,10 +492,10 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name) ...@@ -491,10 +492,10 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
* 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
* capabilities are set to the permitted capabilities. * capabilities are set to the permitted capabilities.
* *
* fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
* never happen. * never happen.
* *
* -astor * -astor
* *
* cevans - New behaviour, Oct '99 * cevans - New behaviour, Oct '99
* A process may, via prctl(), elect to keep its capabilities when it * A process may, via prctl(), elect to keep its capabilities when it
...@@ -751,4 +752,3 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages) ...@@ -751,4 +752,3 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages)
cap_sys_admin = 1; cap_sys_admin = 1;
return __vm_enough_memory(mm, pages, cap_sys_admin); return __vm_enough_memory(mm, pages, cap_sys_admin);
} }
...@@ -274,48 +274,6 @@ static int install_session_keyring(struct key *keyring) ...@@ -274,48 +274,6 @@ static int install_session_keyring(struct key *keyring)
return commit_creds(new); return commit_creds(new);
} }
/*****************************************************************************/
/*
* deal with execve()
*/
int exec_keys(struct task_struct *tsk)
{
struct thread_group_cred *tgcred = NULL;
struct cred *new;
#ifdef CONFIG_KEYS
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
if (!tgcred)
return -ENOMEM;
#endif
new = prepare_creds();
if (new < 0)
return -ENOMEM;
/* newly exec'd tasks don't get a thread keyring */
key_put(new->thread_keyring);
new->thread_keyring = NULL;
/* create a new per-thread-group creds for all this set of threads to
* share */
memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
atomic_set(&tgcred->usage, 1);
spin_lock_init(&tgcred->lock);
/* inherit the session keyring; new process keyring */
key_get(tgcred->session_keyring);
tgcred->process_keyring = NULL;
release_tgcred(new);
new->tgcred = tgcred;
commit_creds(new);
return 0;
} /* end exec_keys() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* the filesystem user ID changed * the filesystem user ID changed
......
...@@ -55,9 +55,9 @@ static int rootplug_bprm_check_security (struct linux_binprm *bprm) ...@@ -55,9 +55,9 @@ static int rootplug_bprm_check_security (struct linux_binprm *bprm)
struct usb_device *dev; struct usb_device *dev;
root_dbg("file %s, e_uid = %d, e_gid = %d\n", root_dbg("file %s, e_uid = %d, e_gid = %d\n",
bprm->filename, bprm->e_uid, bprm->e_gid); bprm->filename, bprm->cred->euid, bprm->cred->egid);
if (bprm->e_gid == 0) { if (bprm->cred->egid == 0) {
dev = usb_find_device(vendor_id, product_id); dev = usb_find_device(vendor_id, product_id);
if (!dev) { if (!dev) {
root_dbg("e_gid = 0, and device not found, " root_dbg("e_gid = 0, and device not found, "
...@@ -75,15 +75,12 @@ static struct security_operations rootplug_security_ops = { ...@@ -75,15 +75,12 @@ static struct security_operations rootplug_security_ops = {
.ptrace_may_access = cap_ptrace_may_access, .ptrace_may_access = cap_ptrace_may_access,
.ptrace_traceme = cap_ptrace_traceme, .ptrace_traceme = cap_ptrace_traceme,
.capget = cap_capget, .capget = cap_capget,
.capset_check = cap_capset_check, .capset = cap_capset,
.capset_set = cap_capset_set,
.capable = cap_capable, .capable = cap_capable,
.bprm_apply_creds = cap_bprm_apply_creds, .bprm_set_creds = cap_bprm_set_creds,
.bprm_set_security = cap_bprm_set_security,
.task_post_setuid = cap_task_post_setuid, .task_fix_setuid = cap_task_fix_setuid,
.task_reparent_to_init = cap_task_reparent_to_init,
.task_prctl = cap_task_prctl, .task_prctl = cap_task_prctl,
.bprm_check_security = rootplug_bprm_check_security, .bprm_check_security = rootplug_bprm_check_security,
......
...@@ -213,34 +213,24 @@ int security_vm_enough_memory_kern(long pages) ...@@ -213,34 +213,24 @@ int security_vm_enough_memory_kern(long pages)
return security_ops->vm_enough_memory(current->mm, pages); return security_ops->vm_enough_memory(current->mm, pages);
} }
int security_bprm_alloc(struct linux_binprm *bprm) int security_bprm_set_creds(struct linux_binprm *bprm)
{ {
return security_ops->bprm_alloc_security(bprm); return security_ops->bprm_set_creds(bprm);
} }
void security_bprm_free(struct linux_binprm *bprm) int security_bprm_check(struct linux_binprm *bprm)
{
security_ops->bprm_free_security(bprm);
}
int security_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
{
return security_ops->bprm_apply_creds(bprm, unsafe);
}
void security_bprm_post_apply_creds(struct linux_binprm *bprm)
{ {
security_ops->bprm_post_apply_creds(bprm); return security_ops->bprm_check_security(bprm);
} }
int security_bprm_set(struct linux_binprm *bprm) void security_bprm_committing_creds(struct linux_binprm *bprm)
{ {
return security_ops->bprm_set_security(bprm); return security_ops->bprm_committing_creds(bprm);
} }
int security_bprm_check(struct linux_binprm *bprm) void security_bprm_committed_creds(struct linux_binprm *bprm)
{ {
return security_ops->bprm_check_security(bprm); return security_ops->bprm_committed_creds(bprm);
} }
int security_bprm_secureexec(struct linux_binprm *bprm) int security_bprm_secureexec(struct linux_binprm *bprm)
......
...@@ -2029,59 +2029,45 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) ...@@ -2029,59 +2029,45 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
/* binprm security operations */ /* binprm security operations */
static int selinux_bprm_alloc_security(struct linux_binprm *bprm) static int selinux_bprm_set_creds(struct linux_binprm *bprm)
{ {
struct bprm_security_struct *bsec; const struct task_security_struct *old_tsec;
struct task_security_struct *new_tsec;
bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL);
if (!bsec)
return -ENOMEM;
bsec->sid = SECINITSID_UNLABELED;
bsec->set = 0;
bprm->security = bsec;
return 0;
}
static int selinux_bprm_set_security(struct linux_binprm *bprm)
{
struct task_security_struct *tsec;
struct inode *inode = bprm->file->f_path.dentry->d_inode;
struct inode_security_struct *isec; struct inode_security_struct *isec;
struct bprm_security_struct *bsec;
u32 newsid;
struct avc_audit_data ad; struct avc_audit_data ad;
struct inode *inode = bprm->file->f_path.dentry->d_inode;
int rc; int rc;
rc = secondary_ops->bprm_set_security(bprm); rc = secondary_ops->bprm_set_creds(bprm);
if (rc) if (rc)
return rc; return rc;
bsec = bprm->security; /* SELinux context only depends on initial program or script and not
* the script interpreter */
if (bsec->set) if (bprm->cred_prepared)
return 0; return 0;
tsec = current_security(); old_tsec = current_security();
new_tsec = bprm->cred->security;
isec = inode->i_security; isec = inode->i_security;
/* Default to the current task SID. */ /* Default to the current task SID. */
bsec->sid = tsec->sid; new_tsec->sid = old_tsec->sid;
new_tsec->osid = old_tsec->sid;
/* Reset fs, key, and sock SIDs on execve. */ /* Reset fs, key, and sock SIDs on execve. */
tsec->create_sid = 0; new_tsec->create_sid = 0;
tsec->keycreate_sid = 0; new_tsec->keycreate_sid = 0;
tsec->sockcreate_sid = 0; new_tsec->sockcreate_sid = 0;
if (tsec->exec_sid) { if (old_tsec->exec_sid) {
newsid = tsec->exec_sid; new_tsec->sid = old_tsec->exec_sid;
/* Reset exec SID on execve. */ /* Reset exec SID on execve. */
tsec->exec_sid = 0; new_tsec->exec_sid = 0;
} else { } else {
/* Check for a default transition on this program. */ /* Check for a default transition on this program. */
rc = security_transition_sid(tsec->sid, isec->sid, rc = security_transition_sid(old_tsec->sid, isec->sid,
SECCLASS_PROCESS, &newsid); SECCLASS_PROCESS, &new_tsec->sid);
if (rc) if (rc)
return rc; return rc;
} }
...@@ -2090,33 +2076,63 @@ static int selinux_bprm_set_security(struct linux_binprm *bprm) ...@@ -2090,33 +2076,63 @@ static int selinux_bprm_set_security(struct linux_binprm *bprm)
ad.u.fs.path = bprm->file->f_path; ad.u.fs.path = bprm->file->f_path;
if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
newsid = tsec->sid; new_tsec->sid = old_tsec->sid;
if (tsec->sid == newsid) { if (new_tsec->sid == old_tsec->sid) {
rc = avc_has_perm(tsec->sid, isec->sid, rc = avc_has_perm(old_tsec->sid, isec->sid,
SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
if (rc) if (rc)
return rc; return rc;
} else { } else {
/* Check permissions for the transition. */ /* Check permissions for the transition. */
rc = avc_has_perm(tsec->sid, newsid, rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
if (rc) if (rc)
return rc; return rc;
rc = avc_has_perm(newsid, isec->sid, rc = avc_has_perm(new_tsec->sid, isec->sid,
SECCLASS_FILE, FILE__ENTRYPOINT, &ad); SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
if (rc) if (rc)
return rc; return rc;
/* Clear any possibly unsafe personality bits on exec: */ /* Check for shared state */
current->personality &= ~PER_CLEAR_ON_SETID; if (bprm->unsafe & LSM_UNSAFE_SHARE) {
rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
SECCLASS_PROCESS, PROCESS__SHARE,
NULL);
if (rc)
return -EPERM;
}
/* Make sure that anyone attempting to ptrace over a task that
* changes its SID has the appropriate permit */
if (bprm->unsafe &
(LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
struct task_struct *tracer;
struct task_security_struct *sec;
u32 ptsid = 0;
rcu_read_lock();
tracer = tracehook_tracer_task(current);
if (likely(tracer != NULL)) {
sec = __task_cred(tracer)->security;
ptsid = sec->sid;
}
rcu_read_unlock();
if (ptsid != 0) {
rc = avc_has_perm(ptsid, new_tsec->sid,
SECCLASS_PROCESS,
PROCESS__PTRACE, NULL);
if (rc)
return -EPERM;
}
}
/* Set the security field to the new SID. */ /* Clear any possibly unsafe personality bits on exec: */
bsec->sid = newsid; bprm->per_clear |= PER_CLEAR_ON_SETID;
} }
bsec->set = 1;
return 0; return 0;
} }
...@@ -2125,7 +2141,6 @@ static int selinux_bprm_check_security(struct linux_binprm *bprm) ...@@ -2125,7 +2141,6 @@ static int selinux_bprm_check_security(struct linux_binprm *bprm)
return secondary_ops->bprm_check_security(bprm); return secondary_ops->bprm_check_security(bprm);
} }
static int selinux_bprm_secureexec(struct linux_binprm *bprm) static int selinux_bprm_secureexec(struct linux_binprm *bprm)
{ {
const struct cred *cred = current_cred(); const struct cred *cred = current_cred();
...@@ -2141,19 +2156,13 @@ static int selinux_bprm_secureexec(struct linux_binprm *bprm) ...@@ -2141,19 +2156,13 @@ static int selinux_bprm_secureexec(struct linux_binprm *bprm)
the noatsecure permission is granted between the noatsecure permission is granted between
the two SIDs, i.e. ahp returns 0. */ the two SIDs, i.e. ahp returns 0. */
atsecure = avc_has_perm(osid, sid, atsecure = avc_has_perm(osid, sid,
SECCLASS_PROCESS, SECCLASS_PROCESS,
PROCESS__NOATSECURE, NULL); PROCESS__NOATSECURE, NULL);
} }
return (atsecure || secondary_ops->bprm_secureexec(bprm)); return (atsecure || secondary_ops->bprm_secureexec(bprm));
} }
static void selinux_bprm_free_security(struct linux_binprm *bprm)
{
kfree(bprm->security);
bprm->security = NULL;
}
extern struct vfsmount *selinuxfs_mount; extern struct vfsmount *selinuxfs_mount;
extern struct dentry *selinux_null; extern struct dentry *selinux_null;
...@@ -2252,108 +2261,78 @@ static inline void flush_unauthorized_files(const struct cred *cred, ...@@ -2252,108 +2261,78 @@ static inline void flush_unauthorized_files(const struct cred *cred,
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
} }
static int selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) /*
* Prepare a process for imminent new credential changes due to exec
*/
static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
{ {
struct task_security_struct *tsec; struct task_security_struct *new_tsec;
struct bprm_security_struct *bsec; struct rlimit *rlim, *initrlim;
struct cred *new; int rc, i;
u32 sid;
int rc;
rc = secondary_ops->bprm_apply_creds(bprm, unsafe);
if (rc < 0)
return rc;
new = prepare_creds();
if (!new)
return -ENOMEM;
tsec = new->security; secondary_ops->bprm_committing_creds(bprm);
bsec = bprm->security; new_tsec = bprm->cred->security;
sid = bsec->sid; if (new_tsec->sid == new_tsec->osid)
return;
tsec->osid = tsec->sid;
bsec->unsafe = 0;
if (tsec->sid != sid) {
/* Check for shared state. If not ok, leave SID
unchanged and kill. */
if (unsafe & LSM_UNSAFE_SHARE) {
rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
PROCESS__SHARE, NULL);
if (rc) {
bsec->unsafe = 1;
goto out;
}
}
/* Check for ptracing, and update the task SID if ok. /* Close files for which the new task SID is not authorized. */
Otherwise, leave SID unchanged and kill. */ flush_unauthorized_files(bprm->cred, current->files);
if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
struct task_struct *tracer;
struct task_security_struct *sec;
u32 ptsid = 0;
rcu_read_lock(); /* Always clear parent death signal on SID transitions. */
tracer = tracehook_tracer_task(current); current->pdeath_signal = 0;
if (likely(tracer != NULL)) {
sec = __task_cred(tracer)->security;
ptsid = sec->sid;
}
rcu_read_unlock();
if (ptsid != 0) { /* Check whether the new SID can inherit resource limits from the old
rc = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, * SID. If not, reset all soft limits to the lower of the current
PROCESS__PTRACE, NULL); * task's hard limit and the init task's soft limit.
if (rc) { *
bsec->unsafe = 1; * Note that the setting of hard limits (even to lower them) can be
goto out; * controlled by the setrlimit check. The inclusion of the init task's
} * soft limit into the computation is to avoid resetting soft limits
} * higher than the default soft limit for cases where the default is
* lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
*/
rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
PROCESS__RLIMITINH, NULL);
if (rc) {
for (i = 0; i < RLIM_NLIMITS; i++) {
rlim = current->signal->rlim + i;
initrlim = init_task.signal->rlim + i;
rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
} }
tsec->sid = sid; update_rlimit_cpu(rlim->rlim_cur);
} }
out:
commit_creds(new);
return 0;
} }
/* /*
* called after apply_creds without the task lock held * Clean up the process immediately after the installation of new credentials
* due to exec
*/ */
static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
{ {
const struct cred *cred = current_cred(); const struct task_security_struct *tsec = current_security();
struct task_security_struct *tsec;
struct rlimit *rlim, *initrlim;
struct itimerval itimer; struct itimerval itimer;
struct bprm_security_struct *bsec;
struct sighand_struct *psig; struct sighand_struct *psig;
u32 osid, sid;
int rc, i; int rc, i;
unsigned long flags; unsigned long flags;
tsec = current_security(); secondary_ops->bprm_committed_creds(bprm);
bsec = bprm->security;
if (bsec->unsafe) { osid = tsec->osid;
force_sig_specific(SIGKILL, current); sid = tsec->sid;
return;
} if (sid == osid)
if (tsec->osid == tsec->sid)
return; return;
/* Close files for which the new task SID is not authorized. */ /* Check whether the new SID can inherit signal state from the old SID.
flush_unauthorized_files(cred, current->files); * If not, clear itimers to avoid subsequent signal generation and
* flush and unblock signals.
/* Check whether the new SID can inherit signal state *
from the old SID. If not, clear itimers to avoid * This must occur _after_ the task SID has been updated so that any
subsequent signal generation and flush and unblock * kill done after the flush will be checked against the new SID.
signals. This must occur _after_ the task SID has */
been updated so that any kill done after the flush rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
will be checked against the new SID. */
rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
PROCESS__SIGINH, NULL);
if (rc) { if (rc) {
memset(&itimer, 0, sizeof itimer); memset(&itimer, 0, sizeof itimer);
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
...@@ -2366,32 +2345,8 @@ static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) ...@@ -2366,32 +2345,8 @@ static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm)
spin_unlock_irq(&current->sighand->siglock); spin_unlock_irq(&current->sighand->siglock);
} }
/* Always clear parent death signal on SID transitions. */ /* Wake up the parent if it is waiting so that it can recheck
current->pdeath_signal = 0; * wait permission to the new task SID. */
/* Check whether the new SID can inherit resource limits
from the old SID. If not, reset all soft limits to
the lower of the current task's hard limit and the init
task's soft limit. Note that the setting of hard limits
(even to lower them) can be controlled by the setrlimit
check. The inclusion of the init task's soft limit into
the computation is to avoid resetting soft limits higher
than the default soft limit for cases where the default
is lower than the hard limit, e.g. RLIMIT_CORE or
RLIMIT_STACK.*/
rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
PROCESS__RLIMITINH, NULL);
if (rc) {
for (i = 0; i < RLIM_NLIMITS; i++) {
rlim = current->signal->rlim + i;
initrlim = init_task.signal->rlim+i;
rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
}
update_rlimit_cpu(rlim->rlim_cur);
}
/* Wake up the parent if it is waiting so that it can
recheck wait permission to the new task SID. */
read_lock_irq(&tasklist_lock); read_lock_irq(&tasklist_lock);
psig = current->parent->sighand; psig = current->parent->sighand;
spin_lock_irqsave(&psig->siglock, flags); spin_lock_irqsave(&psig->siglock, flags);
...@@ -5556,12 +5511,10 @@ static struct security_operations selinux_ops = { ...@@ -5556,12 +5511,10 @@ static struct security_operations selinux_ops = {
.netlink_send = selinux_netlink_send, .netlink_send = selinux_netlink_send,
.netlink_recv = selinux_netlink_recv, .netlink_recv = selinux_netlink_recv,
.bprm_alloc_security = selinux_bprm_alloc_security, .bprm_set_creds = selinux_bprm_set_creds,
.bprm_free_security = selinux_bprm_free_security,
.bprm_apply_creds = selinux_bprm_apply_creds,
.bprm_post_apply_creds = selinux_bprm_post_apply_creds,
.bprm_set_security = selinux_bprm_set_security,
.bprm_check_security = selinux_bprm_check_security, .bprm_check_security = selinux_bprm_check_security,
.bprm_committing_creds = selinux_bprm_committing_creds,
.bprm_committed_creds = selinux_bprm_committed_creds,
.bprm_secureexec = selinux_bprm_secureexec, .bprm_secureexec = selinux_bprm_secureexec,
.sb_alloc_security = selinux_sb_alloc_security, .sb_alloc_security = selinux_sb_alloc_security,
......
...@@ -77,17 +77,6 @@ struct ipc_security_struct { ...@@ -77,17 +77,6 @@ struct ipc_security_struct {
u32 sid; /* SID of IPC resource */ u32 sid; /* SID of IPC resource */
}; };
struct bprm_security_struct {
u32 sid; /* SID for transformed process */
unsigned char set;
/*
* unsafe is used to share failure information from bprm_apply_creds()
* to bprm_post_apply_creds().
*/
char unsafe;
};
struct netif_security_struct { struct netif_security_struct {
int ifindex; /* device index */ int ifindex; /* device index */
u32 sid; /* SID for this interface */ u32 sid; /* SID for this interface */
......
...@@ -2596,8 +2596,7 @@ struct security_operations smack_ops = { ...@@ -2596,8 +2596,7 @@ struct security_operations smack_ops = {
.settime = cap_settime, .settime = cap_settime,
.vm_enough_memory = cap_vm_enough_memory, .vm_enough_memory = cap_vm_enough_memory,
.bprm_apply_creds = cap_bprm_apply_creds, .bprm_set_creds = cap_bprm_set_creds,
.bprm_set_security = cap_bprm_set_security,
.bprm_secureexec = cap_bprm_secureexec, .bprm_secureexec = cap_bprm_secureexec,
.sb_alloc_security = smack_sb_alloc_security, .sb_alloc_security = smack_sb_alloc_security,
......
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