Re: [PATCH 2/4] security: filesystem capabilities bugfix2

Previous thread: [PATCH 1/4] security: filesystem capabilities bugfix1 by Andrew G. Morgan on Thursday, June 26, 2008 - 1:47 am. (6 messages)

Next thread: [PATCH 3/4] security: filesystem capabilities refactor kernel code by Andrew G. Morgan on Thursday, June 26, 2008 - 1:49 am. (3 messages)
From: Andrew G. Morgan
Date: Thursday, June 26, 2008 - 1:48 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bugfix for strace, and CAP_SETPCAP, in the case that filesystem
capabilities are supported.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFIY1fr+bHCR3gb8jsRAph7AKDOlmeveIpQs1jhIs0TJxjCdMAS5ACgsml6
7UYR+FZpW2XdmG8PkiZzemU=
=+Ko+
-----END PGP SIGNATURE-----
From: Serge E. Hallyn
Date: Friday, June 27, 2008 - 1:59 pm

If I understand this right, then LSM_UNSAFE_PTRACE_CAP will only be set
if the tracer didn't have CAP_SYS_PTRACE.  So this seems sane to me.


--

From: David Howells
Date: Monday, June 30, 2008 - 7:53 am

Erm...  Firstly:

	int ptrace_attach(struct task_struct *task)
	{
	...
		if (capable(CAP_SYS_PTRACE))
			task->ptrace |= PT_PTRACE_CAP;
	...
	}

Then:

	static int unsafe_exec(struct task_struct *p)
	{
		int unsafe = 0;
		if (p->ptrace & PT_PTRACED) {
			if (p->ptrace & PT_PTRACE_CAP)
				unsafe |= LSM_UNSAFE_PTRACE_CAP;
			else
				unsafe |= LSM_UNSAFE_PTRACE;
		}
		if (atomic_read(&p->fs->count) > 1 ||
		    atomic_read(&p->files->count) > 1 ||
		    atomic_read(&p->sighand->count) > 1)
			unsafe |= LSM_UNSAFE_SHARE;

		return unsafe;
	}

So LSM_UNSAFE_PTRACE_CAP will only be set if the tracer _does_ have
CAP_SYS_PTRACE.  That will be irrelevant, however, if any of fs, files or
sighand are shared.

And finally:

	void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
	{
	...
		if (bprm->e_uid != current->uid ||
		    bprm->e_gid != current->gid ||
		    !cap_issubset (new_permitted, current->cap_permitted)) {
	...
			if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
				if (!capable(CAP_SETUID)) {
					bprm->e_uid = current->uid;
					bprm->e_gid = current->gid;
				}
				if (!capable (CAP_SETPCAP)) {
					new_permitted = cap_intersect(
						new_permitted,
						current->cap_permitted);
				}
			}
	...
	}

So if it's a 'set-privilege' binary, then if the tracer _doesn't_ have
CAP_SYS_PTRACE, we look at downgrading the privileges of the process.

Without Andrew's patch, we only downgrade the capabilities if we don't have
CAP_SETPCAP (and aren't sharing inheritables).

With Andrew's patch, capabilities are downgraded regardless of whether we have
CAP_SETPCAP or not.  I guess that means that if you're tracing a binary whose
filecaps say that it wants CAP_SETPCAP, then it retains CAP_SETPCAP.

I wonder if the tracing task should be examined here, and any capability the
tracer isn't permitted should be denied the process doing the exec.

Anyway, in my commoncap.c prettification patch, I've dressed the limiter
function up as ...
From: Serge E. Hallyn
Date: Monday, June 30, 2008 - 11:53 am

I don't understand where that last sentence comes from.  Why would it

That sounds reasonable on its own, but it opens up a dangerous ability
for the partially-privileged tracer to manipulate the capability set for
the traced task.

Note that (as of recently) we do not allow the execution of a file with
partial privileges in its pE', precisely because it is dangerous to
allow pick-and-choosing of capabilities in a capability-unaware binary.

So frankly I wonder whether the existing downgrade is really safe...

--

From: David Howells
Date: Monday, June 30, 2008 - 12:10 pm

It seems I missed a bit out.  It should've read:

	I guess that means that if you're tracing a binary that has
	CAP_SETPCAP already, and whose filecaps say that it wants CAP_SETPCAP,
	then it retains CAP_SETPCAP.

If the debugger has CAP_SYS_PTRACE, then it can attach to a binary that has

Does it, though?  It would only reduce the capabilities of the inferior
process; it wouldn't allow the inferior process or the debugger to get
additional capabilities, apart from what's available under CAP_SETPCAP.

David
--

From: Serge E. Hallyn
Date: Monday, June 30, 2008 - 12:49 pm

And the uids won't change unless capable(CAP_SETUID)...  so I think
you're right, it does sound safe.

thanks,
-serge
--

From: David Howells
Date: Friday, June 27, 2008 - 4:04 pm

Calling it cap_limit_ptraced_target() would probably be better.

David
--

From: Andrew G. Morgan
Date: Sunday, June 29, 2008 - 10:41 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Howells wrote:
| Andrew G. Morgan <morgan@kernel.org> wrote:
|
|> +static inline int cap_limit_straced_target(void) { return 1; }
|
| Calling it cap_limit_ptraced_target() would probably be better.

You are right. Fix-up patch attached.

Cheers

Andrew
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFIaHH8+bHCR3gb8jsRAgKHAJ4uu7Xd6PZObvIwzvJPSA6lGBpY1QCfUb20
bw1vY5cX8qW7UBMel6qgi5I=
=iacs
-----END PGP SIGNATURE-----
From: David Howells
Date: Monday, June 30, 2008 - 2:45 am

Acked-by: David Howells <dhowells@redhat.com>
--

Previous thread: [PATCH 1/4] security: filesystem capabilities bugfix1 by Andrew G. Morgan on Thursday, June 26, 2008 - 1:47 am. (6 messages)

Next thread: [PATCH 3/4] security: filesystem capabilities refactor kernel code by Andrew G. Morgan on Thursday, June 26, 2008 - 1:49 am. (3 messages)