Commit b8bff599 authored by Eric W. Biederman's avatar Eric W. Biederman

exec: Factor security_bprm_creds_for_exec out of security_bprm_set_creds

Today security_bprm_set_creds has several implementations:
apparmor_bprm_set_creds, cap_bprm_set_creds, selinux_bprm_set_creds,
smack_bprm_set_creds, and tomoyo_bprm_set_creds.

Except for cap_bprm_set_creds they all test bprm->called_set_creds and
return immediately if it is true.  The function cap_bprm_set_creds
ignores bprm->calld_sed_creds entirely.

Create a new LSM hook security_bprm_creds_for_exec that is called just
before prepare_binprm in __do_execve_file, resulting in a LSM hook
that is called exactly once for the entire of exec.  Modify the bits
of security_bprm_set_creds that only want to be called once per exec
into security_bprm_creds_for_exec, leaving only cap_bprm_set_creds
behind.

Remove bprm->called_set_creds all of it's former users have been moved
to security_bprm_creds_for_exec.

Add or upate comments a appropriate to bring them up to date and
to reflect this change.

Link: https://lkml.kernel.org/r/87v9kszrzh.fsf_-_@x220.int.ebiederm.orgAcked-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Acked-by: Casey Schaufler <casey@schaufler-ca.com> # For the LSM and Smack bits
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
parent 87b047d2
...@@ -1640,7 +1640,6 @@ int prepare_binprm(struct linux_binprm *bprm) ...@@ -1640,7 +1640,6 @@ int prepare_binprm(struct linux_binprm *bprm)
retval = security_bprm_set_creds(bprm); retval = security_bprm_set_creds(bprm);
if (retval) if (retval)
return retval; return retval;
bprm->called_set_creds = 1;
memset(bprm->buf, 0, BINPRM_BUF_SIZE); memset(bprm->buf, 0, BINPRM_BUF_SIZE);
return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos); return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
...@@ -1855,6 +1854,11 @@ static int __do_execve_file(int fd, struct filename *filename, ...@@ -1855,6 +1854,11 @@ static int __do_execve_file(int fd, struct filename *filename,
if (retval < 0) if (retval < 0)
goto out; goto out;
/* Set the unchanging part of bprm->cred */
retval = security_bprm_creds_for_exec(bprm);
if (retval)
goto out;
retval = prepare_binprm(bprm); retval = prepare_binprm(bprm);
if (retval < 0) if (retval < 0)
goto out; goto out;
......
...@@ -27,22 +27,14 @@ struct linux_binprm { ...@@ -27,22 +27,14 @@ struct linux_binprm {
unsigned long argmin; /* rlimit marker for copy_strings() */ unsigned long argmin; /* rlimit marker for copy_strings() */
unsigned int unsigned int
/* /*
* True after the bprm_set_creds hook has been called once * True if most recent call to cap_bprm_set_creds
* (multiple calls can be made via prepare_binprm() for * resulted in elevated privileges.
* binfmt_script/misc).
*/
called_set_creds:1,
/*
* True if most recent call to the commoncaps bprm_set_creds
* hook (due to multiple prepare_binprm() calls from the
* binfmt_script/misc handlers) resulted in elevated
* privileges.
*/ */
cap_elevated:1, cap_elevated:1,
/* /*
* Set by bprm_set_creds hook to indicate a privilege-gaining * Set by bprm_creds_for_exec hook to indicate a
* exec has happened. Used to sanitize execution environment * privilege-gaining exec has happened. Used to set
* and to set AT_SECURE auxv for glibc. * AT_SECURE auxv for glibc.
*/ */
secureexec:1, secureexec:1,
/* /*
......
...@@ -49,6 +49,7 @@ LSM_HOOK(int, 0, syslog, int type) ...@@ -49,6 +49,7 @@ LSM_HOOK(int, 0, syslog, int type)
LSM_HOOK(int, 0, settime, const struct timespec64 *ts, LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
const struct timezone *tz) const struct timezone *tz)
LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages) LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
LSM_HOOK(int, 0, bprm_set_creds, struct linux_binprm *bprm) LSM_HOOK(int, 0, bprm_set_creds, struct linux_binprm *bprm)
LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm) LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm) LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
......
...@@ -34,40 +34,46 @@ ...@@ -34,40 +34,46 @@
* *
* Security hooks for program execution operations. * Security hooks for program execution operations.
* *
* @bprm_creds_for_exec:
* If the setup in prepare_exec_creds did not setup @bprm->cred->security
* properly for executing @bprm->file, update the LSM's portion of
* @bprm->cred->security to be what commit_creds needs to install for the
* new program. This hook may also optionally check permissions
* (e.g. for transitions between security domains).
* The hook must set @bprm->secureexec to 1 if AT_SECURE should be set to
* request libc enable secure mode.
* @bprm contains the linux_binprm structure.
* Return 0 if the hook is successful and permission is granted.
* @bprm_set_creds: * @bprm_set_creds:
* Save security information in the bprm->security field, typically based * Assuming that the relevant bits of @bprm->cred->security have been
* on information about the bprm->file, for later use by the apply_creds * previously set, examine @bprm->file and regenerate them. This is
* hook. This hook may also optionally check permissions (e.g. for * so that the credentials derived from the interpreter the code is
* actually going to run are used rather than credentials derived
* from a script. This done because the interpreter binary needs to
* reopen script, and may end up opening something completely different.
* This hook may also optionally check permissions (e.g. for
* transitions between security domains). * transitions between security domains).
* This hook may be called multiple times during a single execve, e.g. for * The hook must set @bprm->cap_elevated to 1 if AT_SECURE should be set to
* interpreters. The hook can tell whether it has already been called by * request libc enable secure mode.
* checking to see if @bprm->security is non-NULL. If so, then the hook
* may decide either to retain the security information saved earlier or
* to replace it. The hook must set @bprm->secureexec to 1 if a "secure
* exec" has happened as a result of this hook call. The flag is used to
* indicate the need for a sanitized execution environment, and is also
* passed in the ELF auxiliary table on the initial stack to indicate
* whether libc should enable secure mode.
* @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 the * begin. It allows a check against the @bprm->cred->security value
* preceding set_creds call. The primary difference from set_creds is * which was set in the preceding creds_for_exec call. The argv list and
* that the argv list and envp list are reliably available in @bprm. This * envp list are reliably available in @bprm. This hook may be called
* hook may be called multiple times during a single execve; and in each * multiple times during a single execve.
* pass set_creds is called 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: * @bprm_committing_creds:
* Prepare to install the new security attributes of a process being * Prepare to install the new security attributes of a process being
* transformed by an execve operation, based on the old credentials * transformed by an execve operation, based on the old credentials
* pointed to by @current->cred and the information set in @bprm->cred by * 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. * the bprm_creds_for_exec hook. @bprm points to the linux_binprm
* This hook is a good place to perform state changes on the process such * structure. This hook is a good place to perform state changes on the
* as closing open file descriptors to which access will no longer be * process such as closing open file descriptors to which access will no
* granted when the attributes are changed. This is called immediately * longer be granted when the attributes are changed. This is called
* before commit_creds(). * immediately before commit_creds().
* @bprm_committed_creds: * @bprm_committed_creds:
* Tidy up after the installation of the new security attributes of a * Tidy up after the installation of the new security attributes of a
* process being transformed by an execve operation. The new credentials * process being transformed by an execve operation. The new credentials
......
...@@ -276,6 +276,7 @@ int security_quota_on(struct dentry *dentry); ...@@ -276,6 +276,7 @@ int security_quota_on(struct dentry *dentry);
int security_syslog(int type); int security_syslog(int type);
int security_settime64(const struct timespec64 *ts, const struct timezone *tz); int security_settime64(const struct timespec64 *ts, const struct timezone *tz);
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_bprm_creds_for_exec(struct linux_binprm *bprm);
int security_bprm_set_creds(struct linux_binprm *bprm); int security_bprm_set_creds(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_committing_creds(struct linux_binprm *bprm);
...@@ -569,6 +570,11 @@ static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages) ...@@ -569,6 +570,11 @@ static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages)); return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages));
} }
static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
{
return 0;
}
static inline int security_bprm_set_creds(struct linux_binprm *bprm) static inline int security_bprm_set_creds(struct linux_binprm *bprm)
{ {
return cap_bprm_set_creds(bprm); return cap_bprm_set_creds(bprm);
......
...@@ -854,14 +854,14 @@ static struct aa_label *handle_onexec(struct aa_label *label, ...@@ -854,14 +854,14 @@ static struct aa_label *handle_onexec(struct aa_label *label,
} }
/** /**
* apparmor_bprm_set_creds - set the new creds on the bprm struct * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
* @bprm: binprm for the exec (NOT NULL) * @bprm: binprm for the exec (NOT NULL)
* *
* Returns: %0 or error on failure * Returns: %0 or error on failure
* *
* TODO: once the other paths are done see if we can't refactor into a fn * TODO: once the other paths are done see if we can't refactor into a fn
*/ */
int apparmor_bprm_set_creds(struct linux_binprm *bprm) int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
{ {
struct aa_task_ctx *ctx; struct aa_task_ctx *ctx;
struct aa_label *label, *new = NULL; struct aa_label *label, *new = NULL;
...@@ -875,9 +875,6 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm) ...@@ -875,9 +875,6 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
file_inode(bprm->file)->i_mode file_inode(bprm->file)->i_mode
}; };
if (bprm->called_set_creds)
return 0;
ctx = task_ctx(current); ctx = task_ctx(current);
AA_BUG(!cred_label(bprm->cred)); AA_BUG(!cred_label(bprm->cred));
AA_BUG(!ctx); AA_BUG(!ctx);
......
...@@ -30,7 +30,7 @@ struct aa_domain { ...@@ -30,7 +30,7 @@ struct aa_domain {
struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex, struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
const char **name); const char **name);
int apparmor_bprm_set_creds(struct linux_binprm *bprm); int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm);
void aa_free_domain_entries(struct aa_domain *domain); void aa_free_domain_entries(struct aa_domain *domain);
int aa_change_hat(const char *hats[], int count, u64 token, int flags); int aa_change_hat(const char *hats[], int count, u64 token, int flags);
......
...@@ -1232,7 +1232,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { ...@@ -1232,7 +1232,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds), LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
......
...@@ -823,6 +823,11 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages) ...@@ -823,6 +823,11 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
return __vm_enough_memory(mm, pages, cap_sys_admin); return __vm_enough_memory(mm, pages, cap_sys_admin);
} }
int security_bprm_creds_for_exec(struct linux_binprm *bprm)
{
return call_int_hook(bprm_creds_for_exec, 0, bprm);
}
int security_bprm_set_creds(struct linux_binprm *bprm) int security_bprm_set_creds(struct linux_binprm *bprm)
{ {
return call_int_hook(bprm_set_creds, 0, bprm); return call_int_hook(bprm_set_creds, 0, bprm);
......
...@@ -2286,7 +2286,7 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm, ...@@ -2286,7 +2286,7 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm,
return -EACCES; return -EACCES;
} }
static int selinux_bprm_set_creds(struct linux_binprm *bprm) static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
{ {
const struct task_security_struct *old_tsec; const struct task_security_struct *old_tsec;
struct task_security_struct *new_tsec; struct task_security_struct *new_tsec;
...@@ -2297,8 +2297,6 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm) ...@@ -2297,8 +2297,6 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
/* SELinux context only depends on initial program or script and not /* SELinux context only depends on initial program or script and not
* the script interpreter */ * the script interpreter */
if (bprm->called_set_creds)
return 0;
old_tsec = selinux_cred(current_cred()); old_tsec = selinux_cred(current_cred());
new_tsec = selinux_cred(bprm->cred); new_tsec = selinux_cred(bprm->cred);
...@@ -6385,7 +6383,7 @@ static int selinux_setprocattr(const char *name, void *value, size_t size) ...@@ -6385,7 +6383,7 @@ static int selinux_setprocattr(const char *name, void *value, size_t size)
/* Permission checking based on the specified context is /* Permission checking based on the specified context is
performed during the actual operation (execve, performed during the actual operation (execve,
open/mkdir/...), when we know the full context of the open/mkdir/...), when we know the full context of the
operation. See selinux_bprm_set_creds for the execve operation. See selinux_bprm_creds_for_exec for the execve
checks and may_create for the file creation checks. The checks and may_create for the file creation checks. The
operation will then fail if the context is not permitted. */ operation will then fail if the context is not permitted. */
tsec = selinux_cred(new); tsec = selinux_cred(new);
...@@ -6914,7 +6912,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { ...@@ -6914,7 +6912,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
LSM_HOOK_INIT(netlink_send, selinux_netlink_send), LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
......
...@@ -891,12 +891,12 @@ static int smack_sb_statfs(struct dentry *dentry) ...@@ -891,12 +891,12 @@ static int smack_sb_statfs(struct dentry *dentry)
*/ */
/** /**
* smack_bprm_set_creds - set creds for exec * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
* @bprm: the exec information * @bprm: the exec information
* *
* Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
*/ */
static int smack_bprm_set_creds(struct linux_binprm *bprm) static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
{ {
struct inode *inode = file_inode(bprm->file); struct inode *inode = file_inode(bprm->file);
struct task_smack *bsp = smack_cred(bprm->cred); struct task_smack *bsp = smack_cred(bprm->cred);
...@@ -904,9 +904,6 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm) ...@@ -904,9 +904,6 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
struct superblock_smack *sbsp; struct superblock_smack *sbsp;
int rc; int rc;
if (bprm->called_set_creds)
return 0;
isp = smack_inode(inode); isp = smack_inode(inode);
if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
return 0; return 0;
...@@ -4598,7 +4595,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { ...@@ -4598,7 +4595,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts), LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds), LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
......
...@@ -63,20 +63,14 @@ static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm) ...@@ -63,20 +63,14 @@ static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
/** /**
* tomoyo_bprm_set_creds - Target for security_bprm_set_creds(). * tomoyo_bprm_for_exec - Target for security_bprm_creds_for_exec().
* *
* @bprm: Pointer to "struct linux_binprm". * @bprm: Pointer to "struct linux_binprm".
* *
* Returns 0. * Returns 0.
*/ */
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm)
{ {
/*
* Do only if this function is called for the first time of an execve
* operation.
*/
if (bprm->called_set_creds)
return 0;
/* /*
* Load policy if /sbin/tomoyo-init exists and /sbin/init is requested * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
* for the first time. * for the first time.
...@@ -539,7 +533,7 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { ...@@ -539,7 +533,7 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc), LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
LSM_HOOK_INIT(task_free, tomoyo_task_free), LSM_HOOK_INIT(task_free, tomoyo_task_free),
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds), LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
#endif #endif
LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
......
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