linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
@ 2017-07-31 23:51 Kees Cook
  2017-07-31 23:51 ` [PATCH v4 01/15] exec: Rename bprm->cred_prepared to called_set_creds Kees Cook
                   ` (15 more replies)
  0 siblings, 16 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

As discussed with Linus and Andy, we need to reset the stack rlimit
before we do memory layouts when execing a privilege-gaining (e.g.
setuid) program. To do this, we need to know the results of the
bprm_secureexec hook before memory layouts. As it turns out, this
can be made _mostly_ trivial by collapsing bprm_secureexec into
bprm_set_creds.

The LSMs using bprm_secureexec nearly always save state between
bprm_set_creds and bprm_secureexec. In the face of multiple calls to
bprm_set_creds (via prepare_binprm() calls from binfmt_script, etc),
all LSMs except commoncap only pay attention to the first call, so
that aligns well with collapsing bprm_secureexec into bprm_set_creds.
The commoncaps, though, needs to check the _last_ bprm_set_creds, so
this series just swaps one bprm flag for another (cap_effective is no
longer needed to save state between bprm_set_creds and bprm_secureexec,
but we do need to keep a separate state, so we add the cap_elevated flag).

Once secureexec is available to setup_new_exec() before the memory
layout, we can add an rlimit sanity-check for setuid execs. (With no
need to clean up since we're past the point of no return.)

Along the way, this fixes comments, renames a variable, and consolidates
dumpability and pdeath_signal clearing, which includes some commit log
archeology to examine the subtle differences between what we had and
what we need.

Several folks have looked at this already (thank you!) but I'd appreciate
any other eyes on this to make sure it isn't broken in some special
way. Looking at the diffstat, even after all my long comments, this is
a net reduction in lines. :)

Given this crosses a bunch of areas, I think this is likely best to go
via the -mm tree, which is where nearly all of my prior exec work has
lived too. It's also after rc2 at this point, so I'd be slightly nervous
to see this land directly in Linus's tree, but I leave that decision up
to Linus. :) Regardless, very little has changed between v3 and v4, so I
think this is ready to go.

Thanks!

-Kees

----------------------------------------------------------------
Kees Cook (15):
      exec: Rename bprm->cred_prepared to called_set_creds
      exec: Correct comments about "point of no return"
      binfmt: Introduce secureexec flag
      apparmor: Refactor to remove bprm_secureexec hook
      selinux: Refactor to remove bprm_secureexec hook
      smack: Refactor to remove bprm_secureexec hook
      commoncap: Refactor to remove bprm_secureexec hook
      commoncap: Move cap_elevated calculation into bprm_set_creds
      LSM: drop bprm_secureexec hook
      exec: Use secureexec for setting dumpability
      exec: Use secureexec for clearing pdeath_signal
      smack: Remove redundant pdeath_signal clearing
      exec: Consolidate dumpability logic
      exec: Use sane stack rlimit under secureexec
      exec: Consolidate pdeath_signal clearing

 fs/binfmt_elf.c                    |  2 +-
 fs/binfmt_elf_fdpic.c              |  2 +-
 fs/binfmt_flat.c                   |  2 +-
 fs/exec.c                          | 56 ++++++++++++++++++++++++++++----------
 include/linux/binfmts.h            | 24 ++++++++++++----
 include/linux/lsm_hooks.h          | 14 ++++------
 include/linux/security.h           |  7 -----
 security/apparmor/domain.c         | 24 ++--------------
 security/apparmor/include/domain.h |  1 -
 security/apparmor/include/file.h   |  3 --
 security/apparmor/lsm.c            |  1 -
 security/commoncap.c               | 50 ++++++++--------------------------
 security/security.c                |  5 ----
 security/selinux/hooks.c           | 26 ++++--------------
 security/smack/smack_lsm.c         | 34 ++---------------------
 security/tomoyo/tomoyo.c           |  2 +-
 16 files changed, 91 insertions(+), 162 deletions(-)

v4:
- add {Ack,Review,Test}ed-bys
- reorder patches to move trivial refactoring to the front
- move secureexec flag set earlier in the series to setup_new_exec(); amluto

v3:
- collapse brpm_secureexec into bprm_set_creds; ebiederm.
- continue to improve various comments

v2:
- fix missed current_security() uses in LSMs.
- research/consolidate dumpability setting logic
- research/consolidate pdeath_signal clearing logic
- split up logical steps a little more for easier review (and bisection)
- fix some old broken comments

^ permalink raw reply	[flat|nested] 36+ messages in thread

* [PATCH v4 01/15] exec: Rename bprm->cred_prepared to called_set_creds
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 02/15] exec: Correct comments about "point of no return" Kees Cook
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, John Johansen, Paul Moore,
	Stephen Smalley, Casey Schaufler, James Morris,
	Eric W. Biederman, Serge E. Hallyn, Tetsuo Handa,
	Andy Lutomirski, Linus Torvalds, linux-fsdevel,
	linux-security-module, linux-kernel

The cred_prepared bprm flag has a misleading name. It has nothing to do
with the bprm_prepare_cred hook, and actually tracks if bprm_set_creds has
been called. Rename this flag and improve its comment.

Cc: David Howells <dhowells@redhat.com>
Cc: John Johansen <john.johansen@canonical.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: Stephen Smalley <sds@tycho.nsa.gov>
Cc: Casey Schaufler <casey@schaufler-ca.com>
Cc: James Morris <james.l.morris@oracle.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: John Johansen <john.johansen@canonical.com>
Acked-by: James Morris <james.l.morris@oracle.com>
Acked-by: Paul Moore <paul@paul-moore.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/binfmt_flat.c           | 2 +-
 fs/exec.c                  | 2 +-
 include/linux/binfmts.h    | 8 ++++++--
 security/apparmor/domain.c | 2 +-
 security/selinux/hooks.c   | 2 +-
 security/smack/smack_lsm.c | 2 +-
 security/tomoyo/tomoyo.c   | 2 +-
 7 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index 2edcefc0a294..a722530cc468 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -885,7 +885,7 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
 	 * as we're past the point of no return and are dealing with shared
 	 * libraries.
 	 */
-	bprm.cred_prepared = 1;
+	bprm.called_set_creds = 1;
 
 	res = prepare_binprm(&bprm);
 
diff --git a/fs/exec.c b/fs/exec.c
index 72934df68471..cedae1620d2f 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1527,7 +1527,7 @@ int prepare_binprm(struct linux_binprm *bprm)
 	retval = security_bprm_set_creds(bprm);
 	if (retval)
 		return retval;
-	bprm->cred_prepared = 1;
+	bprm->called_set_creds = 1;
 
 	memset(bprm->buf, 0, BINPRM_BUF_SIZE);
 	return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
index 05488da3aee9..3cd98e8bc9dc 100644
--- a/include/linux/binfmts.h
+++ b/include/linux/binfmts.h
@@ -25,8 +25,12 @@ struct linux_binprm {
 	struct mm_struct *mm;
 	unsigned long p; /* current top of mem */
 	unsigned int
-		cred_prepared:1,/* true if creds already prepared (multiple
-				 * preps happen for interpreters) */
+		/*
+		 * True after the bprm_set_creds hook has been called once
+		 * (multiple calls can be made via prepare_binprm() for
+		 * binfmt_script/misc).
+		 */
+		called_set_creds:1,
 		cap_effective:1;/* true if has elevated effective capabilities,
 				 * false if not; except for init which inherits
 				 * its parent's caps anyway */
diff --git a/security/apparmor/domain.c b/security/apparmor/domain.c
index 001e133a3c8c..878407e023e3 100644
--- a/security/apparmor/domain.c
+++ b/security/apparmor/domain.c
@@ -350,7 +350,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
 	const char *name = NULL, *info = NULL;
 	int error = 0;
 
-	if (bprm->cred_prepared)
+	if (bprm->called_set_creds)
 		return 0;
 
 	ctx = cred_ctx(bprm->cred);
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index e67a526d1f30..5d4051541518 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2328,7 +2328,7 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
 
 	/* SELinux context only depends on initial program or script and not
 	 * the script interpreter */
-	if (bprm->cred_prepared)
+	if (bprm->called_set_creds)
 		return 0;
 
 	old_tsec = current_security();
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 658f5d8c7e76..7d4b2e221124 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -917,7 +917,7 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
 	struct superblock_smack *sbsp;
 	int rc;
 
-	if (bprm->cred_prepared)
+	if (bprm->called_set_creds)
 		return 0;
 
 	isp = inode->i_security;
diff --git a/security/tomoyo/tomoyo.c b/security/tomoyo/tomoyo.c
index 130b4fa4f65f..d25b705360e0 100644
--- a/security/tomoyo/tomoyo.c
+++ b/security/tomoyo/tomoyo.c
@@ -76,7 +76,7 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
 	 * Do only if this function is called for the first time of an execve
 	 * operation.
 	 */
-	if (bprm->cred_prepared)
+	if (bprm->called_set_creds)
 		return 0;
 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
 	/*
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 02/15] exec: Correct comments about "point of no return"
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
  2017-07-31 23:51 ` [PATCH v4 01/15] exec: Rename bprm->cred_prepared to called_set_creds Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 03/15] binfmt: Introduce secureexec flag Kees Cook
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W . Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

In commit 221af7f87b97 ("Split 'flush_old_exec' into two functions"),
the comment about the point of no return should have stayed in
flush_old_exec() since it refers to "bprm->mm = NULL;" line, but prior
changes in commits c89681ed7d0e ("remove steal_locks()"), and
fd8328be874f ("sanitize handling of shared descriptor tables in failing
execve()") made it look like it meant the current->sas_ss_sp line instead.

The comment was referring to the fact that once bprm->mm is NULL, all
failures from a binfmt load_binary hook (e.g. load_elf_binary), will
get SEGV raised against current. Move this comment and expand the
explanation a bit, putting it above the assignment this time, and add
details about the true nature of "point of no return" being the call
to flush_old_exec() itself.

This also removes an erroneous commet about when credentials are being
installed. That has its own dedicated function, install_exec_creds(),
which carries a similar (and correct) comment, so remove the bogus comment
where installation is not actually happening.

Cc: David Howells <dhowells@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index cedae1620d2f..90bd5b85814f 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1238,6 +1238,12 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 	perf_event_comm(tsk, exec);
 }
 
+/*
+ * Calling this is the point of no return. None of the failures will be
+ * seen by userspace since either the process is already taking a fatal
+ * signal (via de_thread() or coredump), or will have SEGV raised
+ * (after exec_mmap()) by search_binary_handlers (see below).
+ */
 int flush_old_exec(struct linux_binprm * bprm)
 {
 	int retval;
@@ -1265,7 +1271,13 @@ int flush_old_exec(struct linux_binprm * bprm)
 	if (retval)
 		goto out;
 
-	bprm->mm = NULL;		/* We're using it now */
+	/*
+	 * After clearing bprm->mm (to mark that current is using the
+	 * prepared mm now), we have nothing left of the original
+	 * process. If anything from here on returns an error, the check
+	 * in search_binary_handler() will SEGV current.
+	 */
+	bprm->mm = NULL;
 
 	set_fs(USER_DS);
 	current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
@@ -1312,7 +1324,6 @@ void setup_new_exec(struct linux_binprm * bprm)
 {
 	arch_pick_mmap_layout(current->mm);
 
-	/* This is the point of no return */
 	current->sas_ss_sp = current->sas_ss_size = 0;
 
 	if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
@@ -1330,7 +1341,6 @@ void setup_new_exec(struct linux_binprm * bprm)
 	 */
 	current->mm->task_size = TASK_SIZE;
 
-	/* install the new credentials */
 	if (!uid_eq(bprm->cred->uid, current_euid()) ||
 	    !gid_eq(bprm->cred->gid, current_egid())) {
 		current->pdeath_signal = 0;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 03/15] binfmt: Introduce secureexec flag
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
  2017-07-31 23:51 ` [PATCH v4 01/15] exec: Rename bprm->cred_prepared to called_set_creds Kees Cook
  2017-07-31 23:51 ` [PATCH v4 02/15] exec: Correct comments about "point of no return" Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:23   ` Kees Cook
  2017-08-01  0:44   ` James Morris
  2017-07-31 23:51 ` [PATCH v4 04/15] apparmor: Refactor to remove bprm_secureexec hook Kees Cook
                   ` (12 subsequent siblings)
  15 siblings, 2 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The bprm_secureexec hook can be moved earlier. Right now, it is called
during create_elf_tables(), via load_binary(), via search_binary_handler(),
via exec_binprm(). Nearly all (see exception below) state used by
bprm_secureexec is created during the bprm_set_creds hook, called from
prepare_binprm().

For all LSMs (except commoncaps described next), only the first execution
of bprm_set_creds takes any effect (they all check bprm->called_set_creds
which prepare_binprm() sets after the first call to the bprm_set_creds
hook). However, all these LSMs also only do anything with bprm_secureexec
when they detected a secure state during their first run of bprm_set_creds.
Therefore, it is functionally identical to move the detection into
bprm_set_creds, since the results from secureexec here only need to be
based on the first call to the LSM's bprm_set_creds hook.

The single exception is that the commoncaps secureexec hook also examines
euid/uid and egid/gid differences which are controlled by bprm_fill_uid(),
via prepare_binprm(), which can be called multiple times (e.g.
binfmt_script, binfmt_misc), and may clear the euid/egid for the final
load (i.e. the script interpreter). However, while commoncaps specifically
ignores bprm->cred_prepared, and runs its bprm_set_creds hook each time
prepare_binprm() may get called, it needs to base the secureexec decision
on the final call to bprm_set_creds. As a result, it will need special
handling.

To begin this refactoring, this adds the secureexec flag to the bprm
struct, and calls the secureexec hook during setup_new_exec(). This is
safe since all the cred work is finished (and past the point of no return).
This explicit call will be removed in later patches once the hook has been
removed.

Cc: David Howells <dhowells@redhat.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: John Johansen <john.johansen@canonical.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/binfmt_elf.c         | 2 +-
 fs/binfmt_elf_fdpic.c   | 2 +-
 fs/exec.c               | 2 ++
 include/linux/binfmts.h | 6 ++++++
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 5075fd5c62c8..7f6ec4dac13d 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -254,7 +254,7 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
 	NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
 	NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid));
 	NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid));
- 	NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
+	NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
 	NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
 #ifdef ELF_HWCAP2
 	NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index cf93a4fad012..5aa9199dfb13 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -650,7 +650,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
 	NEW_AUX_ENT(AT_EUID,	(elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
 	NEW_AUX_ENT(AT_GID,	(elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
 	NEW_AUX_ENT(AT_EGID,	(elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
-	NEW_AUX_ENT(AT_SECURE,	security_bprm_secureexec(bprm));
+	NEW_AUX_ENT(AT_SECURE,	bprm->secureexec);
 	NEW_AUX_ENT(AT_EXECFN,	bprm->exec);
 
 #ifdef ARCH_DLINFO
diff --git a/fs/exec.c b/fs/exec.c
index 90bd5b85814f..77244367c773 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1322,6 +1322,8 @@ EXPORT_SYMBOL(would_dump);
 
 void setup_new_exec(struct linux_binprm * bprm)
 {
+	bprm->secureexec |= security_bprm_secureexec(bprm);
+
 	arch_pick_mmap_layout(current->mm);
 
 	current->sas_ss_sp = current->sas_ss_size = 0;
diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
index 3cd98e8bc9dc..6cfd36a27d4e 100644
--- a/include/linux/binfmts.h
+++ b/include/linux/binfmts.h
@@ -34,6 +34,12 @@ struct linux_binprm {
 		cap_effective:1;/* true if has elevated effective capabilities,
 				 * false if not; except for init which inherits
 				 * its parent's caps anyway */
+		/*
+		 * Set by bprm_set_creds hook to indicate a privilege-gaining
+		 * exec has happened. Used to sanitize execution environment
+		 * and to set AT_SECURE auxv for glibc.
+		 */
+		secureexec:1;
 #ifdef __alpha__
 	unsigned int taso:1;
 #endif
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 04/15] apparmor: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (2 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 03/15] binfmt: Introduce secureexec flag Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 05/15] selinux: " Kees Cook
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The AppArmor bprm_secureexec hook can be merged with the bprm_set_creds
hook since it's dealing with the same information, and all of the details
are finalized during the first call to the bprm_set_creds hook via
prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
via bprm->called_set_creds).

Here, all the comments describe how secureexec is actually calculated
during bprm_set_creds, so this actually does it, drops the bprm flag that
was being used internally by AppArmor, and drops the bprm_secureexec hook.

Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: John Johansen <john.johansen@canonical.com>
Reviewed-by: James Morris <james.l.morris@oracle.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 security/apparmor/domain.c         | 22 +---------------------
 security/apparmor/include/domain.h |  1 -
 security/apparmor/include/file.h   |  3 ---
 security/apparmor/lsm.c            |  1 -
 4 files changed, 1 insertion(+), 26 deletions(-)

diff --git a/security/apparmor/domain.c b/security/apparmor/domain.c
index 878407e023e3..1a1b1ec89d9d 100644
--- a/security/apparmor/domain.c
+++ b/security/apparmor/domain.c
@@ -485,14 +485,11 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
 	 *
 	 * Cases 2 and 3 are marked as requiring secure exec
 	 * (unless policy specified "unsafe exec")
-	 *
-	 * bprm->unsafe is used to cache the AA_X_UNSAFE permission
-	 * to avoid having to recompute in secureexec
 	 */
 	if (!(perms.xindex & AA_X_UNSAFE)) {
 		AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
 			 name, new_profile->base.hname);
-		bprm->unsafe |= AA_SECURE_X_NEEDED;
+		bprm->secureexec = 1;
 	}
 apply:
 	/* when transitioning profiles clear unsafe personality bits */
@@ -521,23 +518,6 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
 }
 
 /**
- * apparmor_bprm_secureexec - determine if secureexec is needed
- * @bprm: binprm for exec  (NOT NULL)
- *
- * Returns: %1 if secureexec is needed else %0
- */
-int apparmor_bprm_secureexec(struct linux_binprm *bprm)
-{
-	/* the decision to use secure exec is computed in set_creds
-	 * and stored in bprm->unsafe.
-	 */
-	if (bprm->unsafe & AA_SECURE_X_NEEDED)
-		return 1;
-
-	return 0;
-}
-
-/**
  * apparmor_bprm_committing_creds - do task cleanup on committing new creds
  * @bprm: binprm for the exec  (NOT NULL)
  */
diff --git a/security/apparmor/include/domain.h b/security/apparmor/include/domain.h
index 30544729878a..2495af293587 100644
--- a/security/apparmor/include/domain.h
+++ b/security/apparmor/include/domain.h
@@ -24,7 +24,6 @@ struct aa_domain {
 };
 
 int apparmor_bprm_set_creds(struct linux_binprm *bprm);
-int apparmor_bprm_secureexec(struct linux_binprm *bprm);
 void apparmor_bprm_committing_creds(struct linux_binprm *bprm);
 void apparmor_bprm_committed_creds(struct linux_binprm *bprm);
 
diff --git a/security/apparmor/include/file.h b/security/apparmor/include/file.h
index 38f821bf49b6..076ac4501d97 100644
--- a/security/apparmor/include/file.h
+++ b/security/apparmor/include/file.h
@@ -66,9 +66,6 @@ struct path;
 #define AA_X_INHERIT		0x4000
 #define AA_X_UNCONFINED		0x8000
 
-/* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */
-#define AA_SECURE_X_NEEDED	0x8000
-
 /* need to make conditional which ones are being set */
 struct path_cond {
 	kuid_t uid;
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index 8f3c0f7aca5a..291c7126350f 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -624,7 +624,6 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_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_secureexec, apparmor_bprm_secureexec),
 
 	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
 };
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 05/15] selinux: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (3 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 04/15] apparmor: Refactor to remove bprm_secureexec hook Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:45   ` James Morris
  2017-08-01 13:24   ` Andy Lutomirski
  2017-07-31 23:51 ` [PATCH v4 06/15] smack: " Kees Cook
                   ` (10 subsequent siblings)
  15 siblings, 2 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Stephen Smalley, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The SELinux bprm_secureexec hook can be merged with the bprm_set_creds
hook since it's dealing with the same information, and all of the details
are finalized during the first call to the bprm_set_creds hook via
prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
via bprm->called_set_creds).

Here, the test can just happen at the end of the bprm_set_creds hook,
and the bprm_secureexec hook can be dropped.

Cc: Stephen Smalley <sds@tycho.nsa.gov>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Paul Moore <paul@paul-moore.com>
Tested-by: Paul Moore <paul@paul-moore.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 security/selinux/hooks.c | 24 +++++-------------------
 1 file changed, 5 insertions(+), 19 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 5d4051541518..edbc1c76964e 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2414,30 +2414,17 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
 
 		/* Clear any possibly unsafe personality bits on exec: */
 		bprm->per_clear |= PER_CLEAR_ON_SETID;
-	}
-
-	return 0;
-}
-
-static int selinux_bprm_secureexec(struct linux_binprm *bprm)
-{
-	const struct task_security_struct *tsec = current_security();
-	u32 sid, osid;
-	int atsecure = 0;
-
-	sid = tsec->sid;
-	osid = tsec->osid;
 
-	if (osid != sid) {
 		/* Enable secure mode for SIDs transitions unless
 		   the noatsecure permission is granted between
 		   the two SIDs, i.e. ahp returns 0. */
-		atsecure = avc_has_perm(osid, sid,
-					SECCLASS_PROCESS,
-					PROCESS__NOATSECURE, NULL);
+		rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
+				  SECCLASS_PROCESS, PROCESS__NOATSECURE,
+				  NULL);
+		bprm->secureexec |= !!rc;
 	}
 
-	return !!atsecure;
+	return 0;
 }
 
 static int match_file(const void *p, struct file *file, unsigned fd)
@@ -6152,7 +6139,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_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_secureexec, selinux_bprm_secureexec),
 
 	LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
 	LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 06/15] smack: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (4 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 05/15] selinux: " Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:46   ` James Morris
  2017-08-01 15:24   ` Casey Schaufler
  2017-07-31 23:51 ` [PATCH v4 07/15] commoncap: " Kees Cook
                   ` (9 subsequent siblings)
  15 siblings, 2 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Casey Schaufler, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The Smack bprm_secureexec hook can be merged with the bprm_set_creds
hook since it's dealing with the same information, and all of the details
are finalized during the first call to the bprm_set_creds hook via
prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
via bprm->called_set_creds).

Here, the test can just happen at the end of the bprm_set_creds hook,
and the bprm_secureexec hook can be dropped.

Cc: Casey Schaufler <casey@schaufler-ca.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 security/smack/smack_lsm.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 7d4b2e221124..4f1967be3d20 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -950,6 +950,10 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
 	bsp->smk_task = isp->smk_task;
 	bprm->per_clear |= PER_CLEAR_ON_SETID;
 
+	/* Decide if this is a secure exec. */
+	if (bsp->smk_task != bsp->smk_forked)
+		bprm->secureexec = 1;
+
 	return 0;
 }
 
@@ -967,22 +971,6 @@ static void smack_bprm_committing_creds(struct linux_binprm *bprm)
 		current->pdeath_signal = 0;
 }
 
-/**
- * smack_bprm_secureexec - Return the decision to use secureexec.
- * @bprm: binprm for exec
- *
- * Returns 0 on success.
- */
-static int smack_bprm_secureexec(struct linux_binprm *bprm)
-{
-	struct task_smack *tsp = current_security();
-
-	if (tsp->smk_task != tsp->smk_forked)
-		return 1;
-
-	return 0;
-}
-
 /*
  * Inode hooks
  */
@@ -4646,7 +4634,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
 
 	LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
 	LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
-	LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
 
 	LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
 	LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 07/15] commoncap: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (5 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 06/15] smack: " Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds Kees Cook
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Andy Lutomirski, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The commoncap implementation of the bprm_secureexec hook is the only LSM
that depends on the final call to its bprm_set_creds hook (since it may
be called for multiple files, it ignores bprm->called_set_creds). As a
result, it cannot safely _clear_ bprm->secureexec since other LSMs may
have set it. Instead, remove the bprm_secureexec hook by introducing a
new flag to bprm specific to commoncap: cap_elevated. This is similar to
cap_effective, but that is used for a specific subset of elevated
privileges, and exists solely to track state from bprm_set_creds to
bprm_secureexec. As such, it will be removed in the next patch.

Here, set the new bprm->cap_elevated flag when setuid/setgid has happened
from bprm_fill_uid() or fscapabilities have been prepared. This temporarily
moves the bprm_secureexec hook to a static inline. The helper will be
removed in the next patch; this makes the step easier to review and bisect,
since this does not introduce any changes to inputs nor outputs to the
"elevated privileges" calculation.

The new flag is merged with the bprm->secureexec flag in setup_new_exec()
since this marks the end of any further prepare_binprm() calls.

Cc: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Andy Lutomirski <luto@kernel.org>
Acked-by: James Morris <james.l.morris@oracle.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c               |  7 +++++++
 include/linux/binfmts.h |  7 +++++++
 security/commoncap.c    | 12 ++++++++----
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 77244367c773..3762aef8b49e 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1324,6 +1324,13 @@ void setup_new_exec(struct linux_binprm * bprm)
 {
 	bprm->secureexec |= security_bprm_secureexec(bprm);
 
+	/*
+	 * Once here, prepare_binrpm() will not be called any more, so
+	 * the final state of setuid/setgid/fscaps can be merged into the
+	 * secureexec flag.
+	 */
+	bprm->secureexec |= bprm->cap_elevated;
+
 	arch_pick_mmap_layout(current->mm);
 
 	current->sas_ss_sp = current->sas_ss_size = 0;
diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
index 6cfd36a27d4e..ebc4030fc6bb 100644
--- a/include/linux/binfmts.h
+++ b/include/linux/binfmts.h
@@ -35,6 +35,13 @@ struct linux_binprm {
 				 * false if not; except for init which inherits
 				 * its parent's caps anyway */
 		/*
+		 * 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,
+		/*
 		 * Set by bprm_set_creds hook to indicate a privilege-gaining
 		 * exec has happened. Used to sanitize execution environment
 		 * and to set AT_SECURE auxv for glibc.
diff --git a/security/commoncap.c b/security/commoncap.c
index 7abebd782d5e..abb6050c8083 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -481,6 +481,8 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
 	return rc;
 }
 
+static int is_secureexec(struct linux_binprm *bprm);
+
 /**
  * cap_bprm_set_creds - Set up the proposed credentials for execve().
  * @bprm: The execution parameters, including the proposed creds
@@ -614,11 +616,14 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
 	if (WARN_ON(!cap_ambient_invariant_ok(new)))
 		return -EPERM;
 
+	/* Check for privilege-elevated exec. */
+	bprm->cap_elevated = is_secureexec(bprm);
+
 	return 0;
 }
 
 /**
- * cap_bprm_secureexec - Determine whether a secure execution is required
+ * is_secureexec - Determine whether a secure execution is required
  * @bprm: The execution parameters
  *
  * Determine whether a secure execution is required, return 1 if it is, and 0
@@ -627,9 +632,9 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
  * The credentials have been committed by this point, and so are no longer
  * available through @bprm->cred.
  */
-int cap_bprm_secureexec(struct linux_binprm *bprm)
+static int is_secureexec(struct linux_binprm *bprm)
 {
-	const struct cred *cred = current_cred();
+	const struct cred *cred = bprm->cred;
 	kuid_t root_uid = make_kuid(cred->user_ns, 0);
 
 	if (!uid_eq(cred->uid, root_uid)) {
@@ -1079,7 +1084,6 @@ struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(capget, cap_capget),
 	LSM_HOOK_INIT(capset, cap_capset),
 	LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
-	LSM_HOOK_INIT(bprm_secureexec, cap_bprm_secureexec),
 	LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
 	LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
 	LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (6 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 07/15] commoncap: " Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01 13:46   ` Andy Lutomirski
  2017-07-31 23:51 ` [PATCH v4 09/15] LSM: drop bprm_secureexec hook Kees Cook
                   ` (7 subsequent siblings)
  15 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Andy Lutomirski, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

Instead of a separate function, open-code the cap_elevated test, which
lets us entirely remove bprm->cap_effective (to use the local "effective"
variable instead), and more accurately examine euid/egid changes via the
existing local "is_setid".

The following LTP tests were run to validate the changes:

	# ./runltp -f syscalls -s cap
	# ./runltp -f securebits
	# ./runltp -f cap_bounds
	# ./runltp -f filecaps

All kernel selftests for capabilities and exec continue to pass as well.

Cc: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: James Morris <james.l.morris@oracle.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 include/linux/binfmts.h |  3 ---
 security/commoncap.c    | 52 ++++++++++---------------------------------------
 2 files changed, 10 insertions(+), 45 deletions(-)

diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
index ebc4030fc6bb..b2c1563091e5 100644
--- a/include/linux/binfmts.h
+++ b/include/linux/binfmts.h
@@ -31,9 +31,6 @@ struct linux_binprm {
 		 * binfmt_script/misc).
 		 */
 		called_set_creds:1,
-		cap_effective:1;/* true if has elevated effective capabilities,
-				 * false if not; except for init which inherits
-				 * its parent's caps anyway */
 		/*
 		 * True if most recent call to the commoncaps bprm_set_creds
 		 * hook (due to multiple prepare_binprm() calls from the
diff --git a/security/commoncap.c b/security/commoncap.c
index abb6050c8083..d8e26fb9781d 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -285,15 +285,6 @@ int cap_capset(struct cred *new,
 	return 0;
 }
 
-/*
- * Clear proposed capability sets for execve().
- */
-static inline void bprm_clear_caps(struct linux_binprm *bprm)
-{
-	cap_clear(bprm->cred->cap_permitted);
-	bprm->cap_effective = false;
-}
-
 /**
  * cap_inode_need_killpriv - Determine if inode change affects privileges
  * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
@@ -443,7 +434,7 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
 	int rc = 0;
 	struct cpu_vfs_cap_data vcaps;
 
-	bprm_clear_caps(bprm);
+	cap_clear(bprm->cred->cap_permitted);
 
 	if (!file_caps_enabled)
 		return 0;
@@ -476,13 +467,11 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
 
 out:
 	if (rc)
-		bprm_clear_caps(bprm);
+		cap_clear(bprm->cred->cap_permitted);
 
 	return rc;
 }
 
-static int is_secureexec(struct linux_binprm *bprm);
-
 /**
  * cap_bprm_set_creds - Set up the proposed credentials for execve().
  * @bprm: The execution parameters, including the proposed creds
@@ -587,8 +576,6 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
 	if (WARN_ON(!cap_ambient_invariant_ok(new)))
 		return -EPERM;
 
-	bprm->cap_effective = effective;
-
 	/*
 	 * Audit candidate if current->cap_effective is set
 	 *
@@ -617,35 +604,16 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
 		return -EPERM;
 
 	/* Check for privilege-elevated exec. */
-	bprm->cap_elevated = is_secureexec(bprm);
-
-	return 0;
-}
-
-/**
- * is_secureexec - Determine whether a secure execution is required
- * @bprm: The execution parameters
- *
- * Determine whether a secure execution is required, return 1 if it is, and 0
- * if it is not.
- *
- * The credentials have been committed by this point, and so are no longer
- * available through @bprm->cred.
- */
-static int is_secureexec(struct linux_binprm *bprm)
-{
-	const struct cred *cred = bprm->cred;
-	kuid_t root_uid = make_kuid(cred->user_ns, 0);
-
-	if (!uid_eq(cred->uid, root_uid)) {
-		if (bprm->cap_effective)
-			return 1;
-		if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
-			return 1;
+	bprm->cap_elevated = 0;
+	if (is_setid) {
+		bprm->cap_elevated = 1;
+	} else if (!uid_eq(new->uid, root_uid)) {
+		if (effective ||
+		    !cap_issubset(new->cap_permitted, new->cap_ambient))
+			bprm->cap_elevated = 1;
 	}
 
-	return (!uid_eq(cred->euid, cred->uid) ||
-		!gid_eq(cred->egid, cred->gid));
+	return 0;
 }
 
 /**
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 09/15] LSM: drop bprm_secureexec hook
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (7 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 10/15] exec: Use secureexec for setting dumpability Kees Cook
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Eric W . Biederman, David Howells, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

This removes the bprm_secureexec hook since the logic has been folded into
the bprm_set_creds hook for all LSMs now.

Cc: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: John Johansen <john.johansen@canonical.com>
Acked-by: James Morris <james.l.morris@oracle.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c                 |  2 --
 include/linux/lsm_hooks.h | 14 +++++---------
 include/linux/security.h  |  7 -------
 security/security.c       |  5 -----
 4 files changed, 5 insertions(+), 23 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 3762aef8b49e..f0e8d25eba9f 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1322,8 +1322,6 @@ EXPORT_SYMBOL(would_dump);
 
 void setup_new_exec(struct linux_binprm * bprm)
 {
-	bprm->secureexec |= security_bprm_secureexec(bprm);
-
 	/*
 	 * Once here, prepare_binrpm() will not be called any more, so
 	 * the final state of setuid/setgid/fscaps can be merged into the
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 080f34e66017..2ddc1c7e8923 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -40,7 +40,11 @@
  *	interpreters.  The hook can tell whether it has already been called by
  *	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.
+ *	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.
  *	Return 0 if the hook is successful and permission is granted.
  * @bprm_check_security:
@@ -68,12 +72,6 @@
  *	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:
- *	Return a boolean value (0 or 1) indicating whether a "secure exec"
- *	is required.  The flag is passed in the auxiliary table
- *	on the initial stack to the ELF interpreter to indicate whether libc
- *	should enable secure mode.
- *	@bprm contains the linux_binprm structure.
  *
  * Security hooks for filesystem operations.
  *
@@ -1368,7 +1366,6 @@ union security_list_options {
 
 	int (*bprm_set_creds)(struct linux_binprm *bprm);
 	int (*bprm_check_security)(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);
 
@@ -1680,7 +1677,6 @@ struct security_hook_heads {
 	struct list_head vm_enough_memory;
 	struct list_head bprm_set_creds;
 	struct list_head bprm_check_security;
-	struct list_head bprm_secureexec;
 	struct list_head bprm_committing_creds;
 	struct list_head bprm_committed_creds;
 	struct list_head sb_alloc_security;
diff --git a/include/linux/security.h b/include/linux/security.h
index af675b576645..133c41bb666d 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -80,7 +80,6 @@ extern int cap_capset(struct cred *new, const struct cred *old,
 		      const kernel_cap_t *inheritable,
 		      const kernel_cap_t *permitted);
 extern int cap_bprm_set_creds(struct linux_binprm *bprm);
-extern int cap_bprm_secureexec(struct linux_binprm *bprm);
 extern int cap_inode_setxattr(struct dentry *dentry, const char *name,
 			      const void *value, size_t size, int flags);
 extern int cap_inode_removexattr(struct dentry *dentry, const char *name);
@@ -223,7 +222,6 @@ int security_bprm_set_creds(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_sb_alloc(struct super_block *sb);
 void security_sb_free(struct super_block *sb);
 int security_sb_copy_data(char *orig, char *copy);
@@ -515,11 +513,6 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
-static inline int security_bprm_secureexec(struct linux_binprm *bprm)
-{
-	return cap_bprm_secureexec(bprm);
-}
-
 static inline int security_sb_alloc(struct super_block *sb)
 {
 	return 0;
diff --git a/security/security.c b/security/security.c
index b9fea3999cf8..750b83186869 100644
--- a/security/security.c
+++ b/security/security.c
@@ -311,11 +311,6 @@ void security_bprm_committed_creds(struct linux_binprm *bprm)
 	call_void_hook(bprm_committed_creds, bprm);
 }
 
-int security_bprm_secureexec(struct linux_binprm *bprm)
-{
-	return call_int_hook(bprm_secureexec, 0, bprm);
-}
-
 int security_sb_alloc(struct super_block *sb)
 {
 	return call_int_hook(sb_alloc_security, 0, sb);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 10/15] exec: Use secureexec for setting dumpability
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (8 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 09/15] LSM: drop bprm_secureexec hook Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:48   ` James Morris
  2017-07-31 23:51 ` [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal Kees Cook
                   ` (5 subsequent siblings)
  15 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

The examination of "current" to decide dumpability is wrong. This was a
check of and euid/uid (or egid/gid) mismatch in the existing process,
not the newly created one. This appears to stretch back into even the
"history.git" tree. Luckily, dumpability is later set in commit_creds().
In earlier kernel versions before creds existed, similar checks also
existed late in the exec flow, covering up the mistake as far back as I
could find.

Note that because the commit_creds() check examines differences of euid,
uid, egid, gid, and capabilities between the old and new creds, it would
look like the setup_new_exec() dumpability test could be entirely removed.
However, the secureexec test may cover a different set of tests (specific
to the LSMs) than what commit_creds() checks for. So, fix this test to
use secureexec (the removed euid tests are redundant to the commoncap
secureexec checks now).

Cc: David Howells <dhowells@redhat.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/exec.c b/fs/exec.c
index f0e8d25eba9f..f9997ea6414e 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1333,7 +1333,7 @@ void setup_new_exec(struct linux_binprm * bprm)
 
 	current->sas_ss_sp = current->sas_ss_size = 0;
 
-	if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
+	if (!bprm->secureexec)
 		set_dumpable(current->mm, SUID_DUMP_USER);
 	else
 		set_dumpable(current->mm, suid_dumpable);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (9 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 10/15] exec: Use secureexec for setting dumpability Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:50   ` James Morris
  2017-07-31 23:51 ` [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing Kees Cook
                   ` (4 subsequent siblings)
  15 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

Like dumpability, clearing pdeath_signal happens both in setup_new_exec()
and later in commit_creds(). The test in setup_new_exec() is different
from all other privilege comparisons, though: it is checking the new cred
(bprm) uid vs the old cred (current) euid. This appears to be a bug,
introduced by commit a6f76f23d297 ("CRED: Make execve() take advantage of
copy-on-write credentials"):

-       if (bprm->e_uid != current_euid() ||
-           bprm->e_gid != current_egid()) {
-               set_dumpable(current->mm, suid_dumpable);
+       if (bprm->cred->uid != current_euid() ||
+           bprm->cred->gid != current_egid()) {

It was bprm euid vs current euid (and egids), but the effective got
dropped. Nothing in the exec flow changes bprm->cred->uid (nor gid).
The call traces are:

	prepare_bprm_creds()
	    prepare_exec_creds()
	        prepare_creds()
	            memcpy(new_creds, old_creds, ...)
	            security_prepare_creds() (unimplemented by commoncap)
	...
	prepare_binprm()
	    bprm_fill_uid()
	        resets euid/egid to current euid/egid
	        sets euid/egid on bprm based on set*id file bits
	    security_bprm_set_creds()
		cap_bprm_set_creds()
		        handle all caps-based manipulations

so this test is effectively a test of current_uid() vs current_euid(),
which is wrong, just like the prior dumpability tests were wrong.

The commit log says "Clear pdeath_signal and set dumpable on
certain circumstances that may not be covered by commit_creds()." This
may be meaning the earlier old euid vs new euid (and egid) test that
got changed.

Luckily, as with dumpability, this is all masked by commit_creds()
which performs old/new euid and egid tests and clears pdeath_signal.

And again, like dumpability, we should include LSM secureexec logic for
pdeath_signal clearing. For example, Smack goes out of its way to clear
pdeath_signal when it finds a secureexec condition.

Cc: David Howells <dhowells@redhat.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index f9997ea6414e..708a72f93320 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1348,8 +1348,7 @@ void setup_new_exec(struct linux_binprm * bprm)
 	 */
 	current->mm->task_size = TASK_SIZE;
 
-	if (!uid_eq(bprm->cred->uid, current_euid()) ||
-	    !gid_eq(bprm->cred->gid, current_egid())) {
+	if (bprm->secureexec) {
 		current->pdeath_signal = 0;
 	} else {
 		if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (10 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:50   ` James Morris
  2017-08-01 15:24   ` Casey Schaufler
  2017-07-31 23:51 ` [PATCH v4 13/15] exec: Consolidate dumpability logic Kees Cook
                   ` (3 subsequent siblings)
  15 siblings, 2 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Casey Schaufler, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

This removes the redundant pdeath_signal clearing in Smack: the check in
smack_bprm_committing_creds() matches the check in smack_bprm_set_creds()
(which used to be in the now-removed smack_bprm_securexec() hook) and
since secureexec is now being checked for clearing pdeath_signal, this
is redundant to the common exec code.

Cc: Casey Schaufler <casey@schaufler-ca.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 security/smack/smack_lsm.c | 15 ---------------
 1 file changed, 15 deletions(-)

diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 4f1967be3d20..4e33c650b224 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -957,20 +957,6 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
 	return 0;
 }
 
-/**
- * smack_bprm_committing_creds - Prepare to install the new credentials
- * from bprm.
- *
- * @bprm: binprm for exec
- */
-static void smack_bprm_committing_creds(struct linux_binprm *bprm)
-{
-	struct task_smack *bsp = bprm->cred->security;
-
-	if (bsp->smk_task != bsp->smk_forked)
-		current->pdeath_signal = 0;
-}
-
 /*
  * Inode hooks
  */
@@ -4633,7 +4619,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
 
 	LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
-	LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
 
 	LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
 	LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 13/15] exec: Consolidate dumpability logic
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (11 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 14/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

Since it's already valid to set dumpability in the early part of
setup_new_exec(), we can consolidate the logic into a single place.
The BINPRM_FLAGS_ENFORCE_NONDUMP is set during would_dump() calls
before setup_new_exec(), so its test is safe to move as well.

Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 708a72f93320..3ae8a40c587b 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1333,10 +1333,12 @@ void setup_new_exec(struct linux_binprm * bprm)
 
 	current->sas_ss_sp = current->sas_ss_size = 0;
 
-	if (!bprm->secureexec)
-		set_dumpable(current->mm, SUID_DUMP_USER);
-	else
+	/* Figure out dumpability. */
+	if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP ||
+	    bprm->secureexec)
 		set_dumpable(current->mm, suid_dumpable);
+	else
+		set_dumpable(current->mm, SUID_DUMP_USER);
 
 	arch_setup_new_exec();
 	perf_event_exec();
@@ -1350,9 +1352,6 @@ void setup_new_exec(struct linux_binprm * bprm)
 
 	if (bprm->secureexec) {
 		current->pdeath_signal = 0;
-	} else {
-		if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
-			set_dumpable(current->mm, suid_dumpable);
 	}
 
 	/* An exec changes our domain. We are no longer part of the thread
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 14/15] exec: Use sane stack rlimit under secureexec
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (12 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 13/15] exec: Consolidate dumpability logic Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-07-31 23:51 ` [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing Kees Cook
  2017-08-01  0:34 ` [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
  15 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Linus Torvalds, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, linux-kernel

For a secureexec, before memory layout selection has happened, reset the
stack rlimit to something sane to avoid the caller having control over
the resulting layouts.

$ ulimit -s
8192
$ ulimit -s unlimited
$ /bin/sh -c 'ulimit -s'
unlimited
$ sudo /bin/sh -c 'ulimit -s'
8192

Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: James Morris <james.l.morris@oracle.com>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/fs/exec.c b/fs/exec.c
index 3ae8a40c587b..cddd2a2cbc1f 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1329,6 +1329,18 @@ void setup_new_exec(struct linux_binprm * bprm)
 	 */
 	bprm->secureexec |= bprm->cap_elevated;
 
+	if (bprm->secureexec) {
+		/*
+		 * For secureexec, reset the stack limit to sane default to
+		 * avoid bad behavior from the prior rlimits. This has to
+		 * happen before arch_pick_mmap_layout(), which examines
+		 * RLIMIT_STACK, but after the point of no return to avoid
+		 * needing to clean up the change on failure.
+		 */
+		if (current->signal->rlim[RLIMIT_STACK].rlim_cur > _STK_LIM)
+			current->signal->rlim[RLIMIT_STACK].rlim_cur = _STK_LIM;
+	}
+
 	arch_pick_mmap_layout(current->mm);
 
 	current->sas_ss_sp = current->sas_ss_size = 0;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (13 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 14/15] exec: Use sane stack rlimit under secureexec Kees Cook
@ 2017-07-31 23:51 ` Kees Cook
  2017-08-01  0:52   ` James Morris
  2017-08-01  0:34 ` [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
  15 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-07-31 23:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

Instead of an additional secureexec check for pdeath_signal, just move it
up into the initial secureexec test. Neither perf nor arch code touches
pdeath_signal, so the relocation shouldn't change anything.

Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
---
 fs/exec.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index cddd2a2cbc1f..5a19912a4f53 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1330,6 +1330,9 @@ void setup_new_exec(struct linux_binprm * bprm)
 	bprm->secureexec |= bprm->cap_elevated;
 
 	if (bprm->secureexec) {
+		/* Make sure parent cannot signal privileged process. */
+		current->pdeath_signal = 0;
+
 		/*
 		 * For secureexec, reset the stack limit to sane default to
 		 * avoid bad behavior from the prior rlimits. This has to
@@ -1362,10 +1365,6 @@ void setup_new_exec(struct linux_binprm * bprm)
 	 */
 	current->mm->task_size = TASK_SIZE;
 
-	if (bprm->secureexec) {
-		current->pdeath_signal = 0;
-	}
-
 	/* An exec changes our domain. We are no longer part of the thread
 	   group */
 	current->self_exec_id++;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 03/15] binfmt: Introduce secureexec flag
  2017-07-31 23:51 ` [PATCH v4 03/15] binfmt: Introduce secureexec flag Kees Cook
@ 2017-08-01  0:23   ` Kees Cook
  2017-08-01  0:44   ` James Morris
  1 sibling, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-08-01  0:23 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, LKML

On Mon, Jul 31, 2017 at 4:51 PM, Kees Cook <keescook@chromium.org> wrote:
> diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
> index 3cd98e8bc9dc..6cfd36a27d4e 100644
> --- a/include/linux/binfmts.h
> +++ b/include/linux/binfmts.h
> @@ -34,6 +34,12 @@ struct linux_binprm {
>                 cap_effective:1;/* true if has elevated effective capabilities,
>                                  * false if not; except for init which inherits
>                                  * its parent's caps anyway */
> +               /*
> +                * Set by bprm_set_creds hook to indicate a privilege-gaining
> +                * exec has happened. Used to sanitize execution environment
> +                * and to set AT_SECURE auxv for glibc.
> +                */
> +               secureexec:1;
>  #ifdef __alpha__
>         unsigned int taso:1;
>  #endif

Grrr. git rebase messed me up. (; vs , in variable list.) I will send
a v5 and double-check the per-patch builds. Bleh.

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
                   ` (14 preceding siblings ...)
  2017-07-31 23:51 ` [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing Kees Cook
@ 2017-08-01  0:34 ` Kees Cook
  2017-08-01  0:54   ` James Morris
  15 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-08-01  0:34 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, LKML

Ugh, please ignore this v4. There's a typo that snuck in. I'll send a v5 soon...

-Kees

On Mon, Jul 31, 2017 at 4:51 PM, Kees Cook <keescook@chromium.org> wrote:
> As discussed with Linus and Andy, we need to reset the stack rlimit
> before we do memory layouts when execing a privilege-gaining (e.g.
> setuid) program. To do this, we need to know the results of the
> bprm_secureexec hook before memory layouts. As it turns out, this
> can be made _mostly_ trivial by collapsing bprm_secureexec into
> bprm_set_creds.
>
> The LSMs using bprm_secureexec nearly always save state between
> bprm_set_creds and bprm_secureexec. In the face of multiple calls to
> bprm_set_creds (via prepare_binprm() calls from binfmt_script, etc),
> all LSMs except commoncap only pay attention to the first call, so
> that aligns well with collapsing bprm_secureexec into bprm_set_creds.
> The commoncaps, though, needs to check the _last_ bprm_set_creds, so
> this series just swaps one bprm flag for another (cap_effective is no
> longer needed to save state between bprm_set_creds and bprm_secureexec,
> but we do need to keep a separate state, so we add the cap_elevated flag).
>
> Once secureexec is available to setup_new_exec() before the memory
> layout, we can add an rlimit sanity-check for setuid execs. (With no
> need to clean up since we're past the point of no return.)
>
> Along the way, this fixes comments, renames a variable, and consolidates
> dumpability and pdeath_signal clearing, which includes some commit log
> archeology to examine the subtle differences between what we had and
> what we need.
>
> Several folks have looked at this already (thank you!) but I'd appreciate
> any other eyes on this to make sure it isn't broken in some special
> way. Looking at the diffstat, even after all my long comments, this is
> a net reduction in lines. :)
>
> Given this crosses a bunch of areas, I think this is likely best to go
> via the -mm tree, which is where nearly all of my prior exec work has
> lived too. It's also after rc2 at this point, so I'd be slightly nervous
> to see this land directly in Linus's tree, but I leave that decision up
> to Linus. :) Regardless, very little has changed between v3 and v4, so I
> think this is ready to go.
>
> Thanks!
>
> -Kees
>
> ----------------------------------------------------------------
> Kees Cook (15):
>       exec: Rename bprm->cred_prepared to called_set_creds
>       exec: Correct comments about "point of no return"
>       binfmt: Introduce secureexec flag
>       apparmor: Refactor to remove bprm_secureexec hook
>       selinux: Refactor to remove bprm_secureexec hook
>       smack: Refactor to remove bprm_secureexec hook
>       commoncap: Refactor to remove bprm_secureexec hook
>       commoncap: Move cap_elevated calculation into bprm_set_creds
>       LSM: drop bprm_secureexec hook
>       exec: Use secureexec for setting dumpability
>       exec: Use secureexec for clearing pdeath_signal
>       smack: Remove redundant pdeath_signal clearing
>       exec: Consolidate dumpability logic
>       exec: Use sane stack rlimit under secureexec
>       exec: Consolidate pdeath_signal clearing
>
>  fs/binfmt_elf.c                    |  2 +-
>  fs/binfmt_elf_fdpic.c              |  2 +-
>  fs/binfmt_flat.c                   |  2 +-
>  fs/exec.c                          | 56 ++++++++++++++++++++++++++++----------
>  include/linux/binfmts.h            | 24 ++++++++++++----
>  include/linux/lsm_hooks.h          | 14 ++++------
>  include/linux/security.h           |  7 -----
>  security/apparmor/domain.c         | 24 ++--------------
>  security/apparmor/include/domain.h |  1 -
>  security/apparmor/include/file.h   |  3 --
>  security/apparmor/lsm.c            |  1 -
>  security/commoncap.c               | 50 ++++++++--------------------------
>  security/security.c                |  5 ----
>  security/selinux/hooks.c           | 26 ++++--------------
>  security/smack/smack_lsm.c         | 34 ++---------------------
>  security/tomoyo/tomoyo.c           |  2 +-
>  16 files changed, 91 insertions(+), 162 deletions(-)
>
> v4:
> - add {Ack,Review,Test}ed-bys
> - reorder patches to move trivial refactoring to the front
> - move secureexec flag set earlier in the series to setup_new_exec(); amluto
>
> v3:
> - collapse brpm_secureexec into bprm_set_creds; ebiederm.
> - continue to improve various comments
>
> v2:
> - fix missed current_security() uses in LSMs.
> - research/consolidate dumpability setting logic
> - research/consolidate pdeath_signal clearing logic
> - split up logical steps a little more for easier review (and bisection)
> - fix some old broken comments
>
>



-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 03/15] binfmt: Introduce secureexec flag
  2017-07-31 23:51 ` [PATCH v4 03/15] binfmt: Introduce secureexec flag Kees Cook
  2017-08-01  0:23   ` Kees Cook
@ 2017-08-01  0:44   ` James Morris
  1 sibling, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:44 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> Cc: David Howells <dhowells@redhat.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Reviewed-by: John Johansen <john.johansen@canonical.com>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  fs/binfmt_elf.c         | 2 +-
>  fs/binfmt_elf_fdpic.c   | 2 +-
>  fs/exec.c               | 2 ++
>  include/linux/binfmts.h | 6 ++++++
>  4 files changed, 10 insertions(+), 2 deletions(-)
> 


Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 05/15] selinux: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 ` [PATCH v4 05/15] selinux: " Kees Cook
@ 2017-08-01  0:45   ` James Morris
  2017-08-01 13:24   ` Andy Lutomirski
  1 sibling, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:45 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Stephen Smalley, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> The SELinux bprm_secureexec hook can be merged with the bprm_set_creds
> hook since it's dealing with the same information, and all of the details
> are finalized during the first call to the bprm_set_creds hook via
> prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
> via bprm->called_set_creds).
> 
> Here, the test can just happen at the end of the bprm_set_creds hook,
> and the bprm_secureexec hook can be dropped.
> 
> Cc: Stephen Smalley <sds@tycho.nsa.gov>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Paul Moore <paul@paul-moore.com>
> Tested-by: Paul Moore <paul@paul-moore.com>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  security/selinux/hooks.c | 24 +++++-------------------
>  1 file changed, 5 insertions(+), 19 deletions(-)


Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 06/15] smack: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 ` [PATCH v4 06/15] smack: " Kees Cook
@ 2017-08-01  0:46   ` James Morris
  2017-08-01 15:24   ` Casey Schaufler
  1 sibling, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:46 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Casey Schaufler, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> The Smack bprm_secureexec hook can be merged with the bprm_set_creds
> hook since it's dealing with the same information, and all of the details
> are finalized during the first call to the bprm_set_creds hook via
> prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
> via bprm->called_set_creds).
> 
> Here, the test can just happen at the end of the bprm_set_creds hook,
> and the bprm_secureexec hook can be dropped.
> 
> Cc: Casey Schaufler <casey@schaufler-ca.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  security/smack/smack_lsm.c | 21 ++++-----------------
>  1 file changed, 4 insertions(+), 17 deletions(-)


Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 10/15] exec: Use secureexec for setting dumpability
  2017-07-31 23:51 ` [PATCH v4 10/15] exec: Use secureexec for setting dumpability Kees Cook
@ 2017-08-01  0:48   ` James Morris
  0 siblings, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:48 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> The examination of "current" to decide dumpability is wrong. This was a
> check of and euid/uid (or egid/gid) mismatch in the existing process,
> not the newly created one. This appears to stretch back into even the
> "history.git" tree. Luckily, dumpability is later set in commit_creds().
> In earlier kernel versions before creds existed, similar checks also
> existed late in the exec flow, covering up the mistake as far back as I
> could find.
> 
> Note that because the commit_creds() check examines differences of euid,
> uid, egid, gid, and capabilities between the old and new creds, it would
> look like the setup_new_exec() dumpability test could be entirely removed.
> However, the secureexec test may cover a different set of tests (specific
> to the LSMs) than what commit_creds() checks for. So, fix this test to
> use secureexec (the removed euid tests are redundant to the commoncap
> secureexec checks now).
> 
> Cc: David Howells <dhowells@redhat.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  fs/exec.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal
  2017-07-31 23:51 ` [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal Kees Cook
@ 2017-08-01  0:50   ` James Morris
  0 siblings, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:50 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> Cc: David Howells <dhowells@redhat.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  fs/exec.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)


Reviewed-by: James Morris <james.l.morris@oracle.com>

-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing
  2017-07-31 23:51 ` [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing Kees Cook
@ 2017-08-01  0:50   ` James Morris
  2017-08-01 15:24   ` Casey Schaufler
  1 sibling, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:50 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Casey Schaufler, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> This removes the redundant pdeath_signal clearing in Smack: the check in
> smack_bprm_committing_creds() matches the check in smack_bprm_set_creds()
> (which used to be in the now-removed smack_bprm_securexec() hook) and
> since secureexec is now being checked for clearing pdeath_signal, this
> is redundant to the common exec code.
> 
> Cc: Casey Schaufler <casey@schaufler-ca.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>
> ---
>  security/smack/smack_lsm.c | 15 ---------------
>  1 file changed, 15 deletions(-)


Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing
  2017-07-31 23:51 ` [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing Kees Cook
@ 2017-08-01  0:52   ` James Morris
  0 siblings, 0 replies; 36+ messages in thread
From: James Morris @ 2017-08-01  0:52 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, linux-kernel

On Mon, 31 Jul 2017, Kees Cook wrote:

> Instead of an additional secureexec check for pdeath_signal, just move it
> up into the initial secureexec test. Neither perf nor arch code touches
> pdeath_signal, so the relocation shouldn't change anything.
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>


Reviewed-by: James Morris <james.l.morris@oracle.com>


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01  0:34 ` [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
@ 2017-08-01  0:54   ` James Morris
  2017-08-01  3:03     ` Kees Cook
  0 siblings, 1 reply; 36+ messages in thread
From: James Morris @ 2017-08-01  0:54 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, LKML

On Mon, 31 Jul 2017, Kees Cook wrote:

> Ugh, please ignore this v4. There's a typo that snuck in. I'll send a v5 soon...

Aside from that, it's looking good. This touches a lot of stuff in 
security/ so it may make sense for it to go in via my tree.


-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01  0:54   ` James Morris
@ 2017-08-01  3:03     ` Kees Cook
  2017-08-01  5:11       ` Linus Torvalds
  0 siblings, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-08-01  3:03 UTC (permalink / raw)
  To: James Morris
  Cc: Andrew Morton, David Howells, Eric W. Biederman, John Johansen,
	Serge E. Hallyn, Paul Moore, Stephen Smalley, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	linux-fsdevel, linux-security-module, LKML

On Mon, Jul 31, 2017 at 5:54 PM, James Morris <jmorris@namei.org> wrote:
> On Mon, 31 Jul 2017, Kees Cook wrote:
>
>> Ugh, please ignore this v4. There's a typo that snuck in. I'll send a v5 soon...
>
> Aside from that, it's looking good. This touches a lot of stuff in
> security/ so it may make sense for it to go in via my tree.

Yeah, I'm open to whatever. It's not clear where it should go, but if
you want to take it and Linus doesn't want it "early", that works for
me. Linus, Andrew, thoughts?

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01  3:03     ` Kees Cook
@ 2017-08-01  5:11       ` Linus Torvalds
  2017-08-01  5:14         ` Linus Torvalds
  2017-08-01 15:04         ` Kees Cook
  0 siblings, 2 replies; 36+ messages in thread
From: Linus Torvalds @ 2017-08-01  5:11 UTC (permalink / raw)
  To: Kees Cook
  Cc: James Morris, Andrew Morton, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, LKML

On Mon, Jul 31, 2017 at 8:03 PM, Kees Cook <keescook@chromium.org> wrote:
>
> Yeah, I'm open to whatever. It's not clear where it should go, but if
> you want to take it and Linus doesn't want it "early", that works for
> me. Linus, Andrew, thoughts?

I'd actually like this to go in separately from all the other security stuff.

And I just checked this on a separate branch, just because I wanted to
see what the overall diff was. There's a conflict with apparmor
already - the resolution looks fairly straightforward, but considering
the area this touches, it would probably be good that Kees keeps this
branch and verifies things like that.

                    Linus

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01  5:11       ` Linus Torvalds
@ 2017-08-01  5:14         ` Linus Torvalds
  2017-08-01 15:04         ` Kees Cook
  1 sibling, 0 replies; 36+ messages in thread
From: Linus Torvalds @ 2017-08-01  5:14 UTC (permalink / raw)
  To: Kees Cook
  Cc: James Morris, Andrew Morton, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, LKML

On Mon, Jul 31, 2017 at 10:11 PM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> And I just checked this on a separate branch, just because I wanted to
> see what the overall diff was. There's a conflict [..]

Side note: the overall patch looks fine to me. I like how it removes
complexity and code. I didn't test it (other than test *building* the
merge), so take that for what it's worth, but at least the patches
look sensible both on an individual level and as an end result.

                   Linus

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 05/15] selinux: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 ` [PATCH v4 05/15] selinux: " Kees Cook
  2017-08-01  0:45   ` James Morris
@ 2017-08-01 13:24   ` Andy Lutomirski
  1 sibling, 0 replies; 36+ messages in thread
From: Andy Lutomirski @ 2017-08-01 13:24 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Stephen Smalley, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Casey Schaufler,
	Tetsuo Handa, James Morris, Andy Lutomirski, Linus Torvalds,
	Linux FS Devel, LSM List, linux-kernel

On Mon, Jul 31, 2017 at 4:51 PM, Kees Cook <keescook@chromium.org> wrote:
> The SELinux bprm_secureexec hook can be merged with the bprm_set_creds
> hook since it's dealing with the same information, and all of the details
> are finalized during the first call to the bprm_set_creds hook via
> prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
> via bprm->called_set_creds).
>
> Here, the test can just happen at the end of the bprm_set_creds hook,
> and the bprm_secureexec hook can be dropped.
>
> Cc: Stephen Smalley <sds@tycho.nsa.gov>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Paul Moore <paul@paul-moore.com>
> Tested-by: Paul Moore <paul@paul-moore.com>
> Acked-by: Serge Hallyn <serge@hallyn.com>

Reviewed-by: Andy Lutomirski <luto@kernel.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds
  2017-07-31 23:51 ` [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds Kees Cook
@ 2017-08-01 13:46   ` Andy Lutomirski
  0 siblings, 0 replies; 36+ messages in thread
From: Andy Lutomirski @ 2017-08-01 13:46 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Andy Lutomirski, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Linus Torvalds,
	Linux FS Devel, LSM List, linux-kernel

On Mon, Jul 31, 2017 at 4:51 PM, Kees Cook <keescook@chromium.org> wrote:
> Instead of a separate function, open-code the cap_elevated test, which
> lets us entirely remove bprm->cap_effective (to use the local "effective"
> variable instead), and more accurately examine euid/egid changes via the
> existing local "is_setid".
>
> The following LTP tests were run to validate the changes:
>
>         # ./runltp -f syscalls -s cap
>         # ./runltp -f securebits
>         # ./runltp -f cap_bounds
>         # ./runltp -f filecaps
>
> All kernel selftests for capabilities and exec continue to pass as well.
>
> Cc: Andy Lutomirski <luto@kernel.org>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Reviewed-by: James Morris <james.l.morris@oracle.com>
> Acked-by: Serge Hallyn <serge@hallyn.com>

Reviewed-by: Andy Lutomirski <luto@kernel.org>

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01  5:11       ` Linus Torvalds
  2017-08-01  5:14         ` Linus Torvalds
@ 2017-08-01 15:04         ` Kees Cook
  2017-08-01 20:19           ` Linus Torvalds
  1 sibling, 1 reply; 36+ messages in thread
From: Kees Cook @ 2017-08-01 15:04 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: James Morris, Andrew Morton, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, LKML

On Mon, Jul 31, 2017 at 10:11 PM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
> On Mon, Jul 31, 2017 at 8:03 PM, Kees Cook <keescook@chromium.org> wrote:
>>
>> Yeah, I'm open to whatever. It's not clear where it should go, but if
>> you want to take it and Linus doesn't want it "early", that works for
>> me. Linus, Andrew, thoughts?
>
> I'd actually like this to go in separately from all the other security stuff.
>
> And I just checked this on a separate branch, just because I wanted to
> see what the overall diff was. There's a conflict with apparmor
> already - the resolution looks fairly straightforward, but considering
> the area this touches, it would probably be good that Kees keeps this
> branch and verifies things like that.

Do you want me to carry this for -next and send it as a distinct pull
request for v4.14?

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing
  2017-07-31 23:51 ` [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing Kees Cook
  2017-08-01  0:50   ` James Morris
@ 2017-08-01 15:24   ` Casey Schaufler
  1 sibling, 0 replies; 36+ messages in thread
From: Casey Schaufler @ 2017-08-01 15:24 UTC (permalink / raw)
  To: Kees Cook, Andrew Morton
  Cc: David Howells, Eric W. Biederman, John Johansen, Serge E. Hallyn,
	Paul Moore, Stephen Smalley, Tetsuo Handa, James Morris,
	Andy Lutomirski, Linus Torvalds, linux-fsdevel,
	linux-security-module, linux-kernel

On 7/31/2017 4:51 PM, Kees Cook wrote:
> This removes the redundant pdeath_signal clearing in Smack: the check in
> smack_bprm_committing_creds() matches the check in smack_bprm_set_creds()
> (which used to be in the now-removed smack_bprm_securexec() hook) and
> since secureexec is now being checked for clearing pdeath_signal, this
> is redundant to the common exec code.
>
> Cc: Casey Schaufler <casey@schaufler-ca.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>

Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>

> ---
>  security/smack/smack_lsm.c | 15 ---------------
>  1 file changed, 15 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 4f1967be3d20..4e33c650b224 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -957,20 +957,6 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
>  	return 0;
>  }
>  
> -/**
> - * smack_bprm_committing_creds - Prepare to install the new credentials
> - * from bprm.
> - *
> - * @bprm: binprm for exec
> - */
> -static void smack_bprm_committing_creds(struct linux_binprm *bprm)
> -{
> -	struct task_smack *bsp = bprm->cred->security;
> -
> -	if (bsp->smk_task != bsp->smk_forked)
> -		current->pdeath_signal = 0;
> -}
> -
>  /*
>   * Inode hooks
>   */
> @@ -4633,7 +4619,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
>  	LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
>  
>  	LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
> -	LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
>  
>  	LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
>  	LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 06/15] smack: Refactor to remove bprm_secureexec hook
  2017-07-31 23:51 ` [PATCH v4 06/15] smack: " Kees Cook
  2017-08-01  0:46   ` James Morris
@ 2017-08-01 15:24   ` Casey Schaufler
  1 sibling, 0 replies; 36+ messages in thread
From: Casey Schaufler @ 2017-08-01 15:24 UTC (permalink / raw)
  To: Kees Cook, Andrew Morton
  Cc: David Howells, Eric W. Biederman, John Johansen, Serge E. Hallyn,
	Paul Moore, Stephen Smalley, Tetsuo Handa, James Morris,
	Andy Lutomirski, Linus Torvalds, linux-fsdevel,
	linux-security-module, linux-kernel

On 7/31/2017 4:51 PM, Kees Cook wrote:
> The Smack bprm_secureexec hook can be merged with the bprm_set_creds
> hook since it's dealing with the same information, and all of the details
> are finalized during the first call to the bprm_set_creds hook via
> prepare_binprm() (subsequent calls due to binfmt_script, etc, are ignored
> via bprm->called_set_creds).
>
> Here, the test can just happen at the end of the bprm_set_creds hook,
> and the bprm_secureexec hook can be dropped.
>
> Cc: Casey Schaufler <casey@schaufler-ca.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Acked-by: Serge Hallyn <serge@hallyn.com>

Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>

> ---
>  security/smack/smack_lsm.c | 21 ++++-----------------
>  1 file changed, 4 insertions(+), 17 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 7d4b2e221124..4f1967be3d20 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -950,6 +950,10 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm)
>  	bsp->smk_task = isp->smk_task;
>  	bprm->per_clear |= PER_CLEAR_ON_SETID;
>  
> +	/* Decide if this is a secure exec. */
> +	if (bsp->smk_task != bsp->smk_forked)
> +		bprm->secureexec = 1;
> +
>  	return 0;
>  }
>  
> @@ -967,22 +971,6 @@ static void smack_bprm_committing_creds(struct linux_binprm *bprm)
>  		current->pdeath_signal = 0;
>  }
>  
> -/**
> - * smack_bprm_secureexec - Return the decision to use secureexec.
> - * @bprm: binprm for exec
> - *
> - * Returns 0 on success.
> - */
> -static int smack_bprm_secureexec(struct linux_binprm *bprm)
> -{
> -	struct task_smack *tsp = current_security();
> -
> -	if (tsp->smk_task != tsp->smk_forked)
> -		return 1;
> -
> -	return 0;
> -}
> -
>  /*
>   * Inode hooks
>   */
> @@ -4646,7 +4634,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
>  
>  	LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
>  	LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
> -	LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
>  
>  	LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
>  	LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01 15:04         ` Kees Cook
@ 2017-08-01 20:19           ` Linus Torvalds
  2017-08-01 21:04             ` Kees Cook
  0 siblings, 1 reply; 36+ messages in thread
From: Linus Torvalds @ 2017-08-01 20:19 UTC (permalink / raw)
  To: Kees Cook
  Cc: James Morris, Andrew Morton, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, LKML

On Tue, Aug 1, 2017 at 8:04 AM, Kees Cook <keescook@chromium.org> wrote:
>
> Do you want me to carry this for -next and send it as a distinct pull
> request for v4.14?

Yes, I think that would be preferred. I consider this a "execve()"
cleanup/change with implications for the security models rather than
the other way around, so I'd rather keep it separate, and you already
have a few other git trees so I think it makes sense to just treat it
as another of your git pulls next merge window.

                 Linus

^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec
  2017-08-01 20:19           ` Linus Torvalds
@ 2017-08-01 21:04             ` Kees Cook
  0 siblings, 0 replies; 36+ messages in thread
From: Kees Cook @ 2017-08-01 21:04 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: James Morris, Andrew Morton, David Howells, Eric W. Biederman,
	John Johansen, Serge E. Hallyn, Paul Moore, Stephen Smalley,
	Casey Schaufler, Tetsuo Handa, James Morris, Andy Lutomirski,
	linux-fsdevel, linux-security-module, LKML

On Tue, Aug 1, 2017 at 1:19 PM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
> On Tue, Aug 1, 2017 at 8:04 AM, Kees Cook <keescook@chromium.org> wrote:
>>
>> Do you want me to carry this for -next and send it as a distinct pull
>> request for v4.14?
>
> Yes, I think that would be preferred. I consider this a "execve()"
> cleanup/change with implications for the security models rather than
> the other way around, so I'd rather keep it separate, and you already
> have a few other git trees so I think it makes sense to just treat it
> as another of your git pulls next merge window.

Okay, sounds good. Thanks!

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2017-08-01 21:04 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-31 23:51 [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
2017-07-31 23:51 ` [PATCH v4 01/15] exec: Rename bprm->cred_prepared to called_set_creds Kees Cook
2017-07-31 23:51 ` [PATCH v4 02/15] exec: Correct comments about "point of no return" Kees Cook
2017-07-31 23:51 ` [PATCH v4 03/15] binfmt: Introduce secureexec flag Kees Cook
2017-08-01  0:23   ` Kees Cook
2017-08-01  0:44   ` James Morris
2017-07-31 23:51 ` [PATCH v4 04/15] apparmor: Refactor to remove bprm_secureexec hook Kees Cook
2017-07-31 23:51 ` [PATCH v4 05/15] selinux: " Kees Cook
2017-08-01  0:45   ` James Morris
2017-08-01 13:24   ` Andy Lutomirski
2017-07-31 23:51 ` [PATCH v4 06/15] smack: " Kees Cook
2017-08-01  0:46   ` James Morris
2017-08-01 15:24   ` Casey Schaufler
2017-07-31 23:51 ` [PATCH v4 07/15] commoncap: " Kees Cook
2017-07-31 23:51 ` [PATCH v4 08/15] commoncap: Move cap_elevated calculation into bprm_set_creds Kees Cook
2017-08-01 13:46   ` Andy Lutomirski
2017-07-31 23:51 ` [PATCH v4 09/15] LSM: drop bprm_secureexec hook Kees Cook
2017-07-31 23:51 ` [PATCH v4 10/15] exec: Use secureexec for setting dumpability Kees Cook
2017-08-01  0:48   ` James Morris
2017-07-31 23:51 ` [PATCH v4 11/15] exec: Use secureexec for clearing pdeath_signal Kees Cook
2017-08-01  0:50   ` James Morris
2017-07-31 23:51 ` [PATCH v4 12/15] smack: Remove redundant pdeath_signal clearing Kees Cook
2017-08-01  0:50   ` James Morris
2017-08-01 15:24   ` Casey Schaufler
2017-07-31 23:51 ` [PATCH v4 13/15] exec: Consolidate dumpability logic Kees Cook
2017-07-31 23:51 ` [PATCH v4 14/15] exec: Use sane stack rlimit under secureexec Kees Cook
2017-07-31 23:51 ` [PATCH v4 15/15] exec: Consolidate pdeath_signal clearing Kees Cook
2017-08-01  0:52   ` James Morris
2017-08-01  0:34 ` [PATCH v4 00/15] exec: Use sane stack rlimit under secureexec Kees Cook
2017-08-01  0:54   ` James Morris
2017-08-01  3:03     ` Kees Cook
2017-08-01  5:11       ` Linus Torvalds
2017-08-01  5:14         ` Linus Torvalds
2017-08-01 15:04         ` Kees Cook
2017-08-01 20:19           ` Linus Torvalds
2017-08-01 21:04             ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).