Skip to contentSkip to conversation

Forbid invocation of kexec_load() outside initial PID namespace

PATCH NAK Daniel P. Berrange 5 participants 11 messages Aug 3, 2012 – Aug 6, 2012 1 message not in the archive kexec

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Forbid invocation of kexec_load() outside initial PID namespace

From: "Daniel P. Berrange" <[email protected]>

The following commit

commit cf3f89214ef6a33fad60856bc5ffd7bb2fc4709b
Author: Daniel Lezcano <[email protected]>
Date: Wed Mar 28 14:42:51 2012 -0700

pidns: add reboot_pid_ns() to handle the reboot syscall

introduced custom handling of the reboot() syscall when invoked from a non-initial PID namespace. The intent was that a process in a container can be allowed to keep CAP_SYS_BOOT and execute reboot() to shutdown/reboot just their private container, rather than the host.

Unfortunately the kexec_load() syscall also relies on the CAP_SYS_BOOT capability. So by allowing a container to keep this capability to safely invoke reboot(), they mistakenly also gain the ability to use kexec_load(). The solution is to make kexec_load() return -EPERM if invoked from a PID namespace that is not the initial namespace

Cc Serge Hallyn <[email protected]>
Cc Daniel Lezcano <[email protected]>
Cc Michael Kerrisk <[email protected]>
Cc "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Cc Tejun Heo <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 kernel/kexec.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/kexec.c +5 -0
index 0668d58..b152bde 100644
@@ -947,6 +947,11 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
 	if (!capable(CAP_SYS_BOOT))
 		return -EPERM;
 
+	/* Processes in containers must not be allowed to load a new
+	 * kernel, even if they have CAP_SYS_BOOT */
+	if (task_active_pid_ns(current) != &init_pid_ns)
+		return -EPERM;
+
 	/*
 	 * Verify we have a legal set of flags
 	 * This leaves us room for future extensions.
Signature · 1 line
1.7.11.2
#1 lore raw
Quoted 23 lines · Daniel P. Berrange

From: "Daniel P. Berrange" <[email protected]>

The following commit

commit cf3f89214ef6a33fad60856bc5ffd7bb2fc4709b
Author: Daniel Lezcano <[email protected]>
Date: Wed Mar 28 14:42:51 2012 -0700

pidns: add reboot_pid_ns() to handle the reboot syscall

introduced custom handling of the reboot() syscall when invoked from a non-initial PID namespace. The intent was that a process in a container can be allowed to keep CAP_SYS_BOOT and execute reboot() to shutdown/reboot just their private container, rather than the host.

Unfortunately the kexec_load() syscall also relies on the CAP_SYS_BOOT capability. So by allowing a container to keep this capability to safely invoke reboot(), they mistakenly also gain the ability to use kexec_load(). The solution is to make kexec_load() return -EPERM if invoked from a PID namespace that is not the initial namespace

Quoted 7 lines
Cc Serge Hallyn <[email protected]>
Cc Daniel Lezcano <[email protected]>
Cc Michael Kerrisk <[email protected]>
Cc "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Cc Tejun Heo <[email protected]>
Cc Oleg Nesterov <[email protected]>

Why is this commit not marked for -stable?

Signature · 2 lines
Thanks,
//richard
#3 lore raw
Quoted 3 lines · Eric W. Biederman

The solution is to use user namespaces and to only test ns_capable on the magic reboot path.

For the 3.7 timeframe that should be a realistic solution.

Hmm, that would imply that if LXC wants to allow reboot()/CAP_SYS_BOOT they will be forced to use CLONE_NEWUSER. I was rather looking for a way to allow the container to keep CAP_SYS_BOOT, without also mandating use of user namespaces.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#4 lore raw
Quoted 11 lines · Daniel P. Berrange
Quoted 1 line · Eric W. Biederman

The solution is to use user namespaces and to only test ns_capable on

the magic reboot path.

Quoted 2 lines

For the 3.7 timeframe that should be a realistic solution.

Hmm, that would imply that if LXC wants to allow reboot()/CAP_SYS_BOOT they will be forced to use CLONE_NEWUSER. I was rather looking for a way
to allow the container to keep CAP_SYS_BOOT, without also mandating use of user namespaces.

If we remove the use of CAP_SYS_BOOT on the container reboot path perhaps.

But you have hit one small issue in the huge pile of issues why giving contaners capabilities is generally a bad idea.

This is the reason I have been insisting on a reasonable version of user namespaces for a long time.

When the security issues become important it is time for user namespaces. That is their purpose.

Eric

#6 lore raw
In reply to a message not in the archive

Eric,

during the container reboot discussion, the agreement was reached that rebooting for real fron non-init pid ns is not safe. Restarting userspace (in pidns caller owns) is. I argue the same reasoning supports this.

I haven't had a chance to review the patch, but the idea gets my ack. I'll look at the patch asap.

I'm also fine with splitting cap_sys_boot into a user and system caps. The former would only be needed targeted to the userns of the init pid, while the latter would be required to init_user_ns. Then containers could safely be given cap_sys_restart or whatever, but not cap_sys_boot which authorizes kexec and machine reset/poweroff.

----- Original message -----

Quoted 30 lines
Quoted 11 lines · Daniel P. Berrange
Quoted 1 line · Eric W. Biederman

The solution is to use user namespaces and to only test ns_capable on

the magic reboot path.

Quoted 2 lines

For the 3.7 timeframe that should be a realistic solution.

Hmm, that would imply that if LXC wants to allow reboot()/CAP_SYS_BOOT they will be forced to use CLONE_NEWUSER. I was rather looking for a way
to allow the container to keep CAP_SYS_BOOT, without also mandating use of user namespaces.

If we remove the use of CAP_SYS_BOOT on the container reboot path perhaps.

But you have hit one small issue in the huge pile of issues why giving contaners capabilities is generally a bad idea.

This is the reason I have been insisting on a reasonable version of user namespaces for a long time.

When the security issues become important it is time for user namespaces.    That is their purpose.

Eric

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

#7 lore raw
In reply to Eric W. Biederman

Eric,

during the container reboot discussion, the agreement was reached that rebooting for real fron non-init pid ns is not safe. Restarting userspace (in pidns caller owns) is. I argue the same reasoning supports this.

I haven't had a chance to review the patch, but the idea gets my ack. I'll look at the patch asap.

I'm also fine with splitting cap_sys_boot into a user and system caps. The former would only be needed targeted to the userns of the init pid, while the latter would be required to init_user_ns. Then containers could safely be given cap_sys_restart or whatever, but not cap_sys_boot which authorizes kexec and machine reset/poweroff.

----- Original message -----

Quoted 35 lines
Quoted 11 lines · Daniel P. Berrange
Quoted 1 line · Eric W. Biederman

The solution is to use user namespaces and to only test ns_capable on

the magic reboot path.

Quoted 2 lines

For the 3.7 timeframe that should be a realistic solution.

Hmm, that would imply that if LXC wants to allow reboot()/CAP_SYS_BOOT they will be forced to use CLONE_NEWUSER. I was rather looking for a way
to allow the container to keep CAP_SYS_BOOT, without also mandating use of user namespaces.

If we remove the use of CAP_SYS_BOOT on the container reboot path perhaps.

But you have hit one small issue in the huge pile of issues why giving contaners capabilities is generally a bad idea.

This is the reason I have been insisting on a reasonable version of user namespaces for a long time.

When the security issues become important it is time for user namespaces.    That is their purpose.

Eric

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Acked-by Serge Hallyn on v1 1/1
#9 lore raw
In reply to Daniel P. Berrange
Quoted 23 lines · Daniel P. Berrange

From: "Daniel P. Berrange" <[email protected]>

The following commit

commit cf3f89214ef6a33fad60856bc5ffd7bb2fc4709b
Author: Daniel Lezcano <[email protected]>
Date: Wed Mar 28 14:42:51 2012 -0700

pidns: add reboot_pid_ns() to handle the reboot syscall

introduced custom handling of the reboot() syscall when invoked from a non-initial PID namespace. The intent was that a process in a container can be allowed to keep CAP_SYS_BOOT and execute reboot() to shutdown/reboot just their private container, rather than the host.

Unfortunately the kexec_load() syscall also relies on the CAP_SYS_BOOT capability. So by allowing a container to keep this capability to safely invoke reboot(), they mistakenly also gain the ability to use kexec_load(). The solution is to make kexec_load() return -EPERM if invoked from a PID namespace that is not the initial namespace

Quoted 2 lines

(Please see my previous email explaining why I believe the pidns is an appropriate check)

Quoted 33 lines
Cc Daniel Lezcano <[email protected]>
Cc Michael Kerrisk <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 kernel/kexec.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/kexec.c +5 -0
index 0668d58..b152bde 100644
@@ -947,6 +947,11 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
 	if (!capable(CAP_SYS_BOOT))
 		return -EPERM;
 
+	/* Processes in containers must not be allowed to load a new
+	 * kernel, even if they have CAP_SYS_BOOT */
+	if (task_active_pid_ns(current) != &init_pid_ns)
+		return -EPERM;
+
 	/*
 	 * Verify we have a legal set of flags
 	 * This leaves us room for future extensions.
Signature · 7 lines
1.7.11.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Nacked-by NAK
#10 lore raw
Quoted 24 lines · Serge E. Hallyn
Quoted 23 lines · Daniel P. Berrange

From: "Daniel P. Berrange" <[email protected]>

The following commit

commit cf3f89214ef6a33fad60856bc5ffd7bb2fc4709b
Author: Daniel Lezcano <[email protected]>
Date: Wed Mar 28 14:42:51 2012 -0700

pidns: add reboot_pid_ns() to handle the reboot syscall

introduced custom handling of the reboot() syscall when invoked from a non-initial PID namespace. The intent was that a process in a container can be allowed to keep CAP_SYS_BOOT and execute reboot() to shutdown/reboot just their private container, rather than the host.

Unfortunately the kexec_load() syscall also relies on the CAP_SYS_BOOT capability. So by allowing a container to keep this capability to safely invoke reboot(), they mistakenly also gain the ability to use kexec_load(). The solution is to make kexec_load() return -EPERM if invoked from a PID namespace that is not the initial namespace

Quoted 7 lines
Quoted 2 lines

(Please see my previous email explaining why I believe the pidns is an appropriate check)

Serge as to your objects.

If we define kexec_load in terms of the pid namespace then something makes sense, but the error should be EINVAL, or something of that sort.

That is what we did with reboot. We defined reboot in terms of the pid namespace.

Not defining kexec_load in terms of the pid namespace and then returning EPERM because having we happen to have CAP_SYS_BOOT for other reasons is semantically horrible.

At the end of the day the effect is the same, but I think it matters a great deal in how we think about things.

We have CAP_SYS_BOOT in the initial user namespace. We do have permission to make the system call.

So I continue to see this patch the way it is current constructed as broken.

Eric

#8 lore raw
In reply to Serge Hallyn
Quoted 7 lines · Serge Hallyn

Eric,

during the container reboot discussion, the agreement was reached that rebooting for real fron non-init pid ns is not safe. Restarting userspace (in pidns caller owns) is. I argue the same reasoning supports this.

I haven't had a chance to review the patch, but the idea gets my ack. I'll look at the patch asap.

I'm also fine with splitting cap_sys_boot into a user and system caps. The former would only be needed targeted to the userns of the init pid, while the latter would be required to init_user_ns. Then containers could safely be given cap_sys_restart or whatever, but not cap_sys_boot which authorizes kexec and machine reset/poweroff.

Splitting the cap up into CAP_RESTART (restart /sbin/init) and CAP_BOOT (reboot hardware or kexec kernel) has the advantage that the capabilities each remain simpler to parse, no 'in this context it means that'.

#11 lore raw
In reply to Eric W. Biederman
Quoted 31 lines · Eric W. Biederman
Quoted 29 lines · Serge E. Hallyn
Quoted 23 lines · Daniel P. Berrange

From: "Daniel P. Berrange" <[email protected]>

The following commit

commit cf3f89214ef6a33fad60856bc5ffd7bb2fc4709b
Author: Daniel Lezcano <[email protected]>
Date: Wed Mar 28 14:42:51 2012 -0700

pidns: add reboot_pid_ns() to handle the reboot syscall

introduced custom handling of the reboot() syscall when invoked from a non-initial PID namespace. The intent was that a process in a container can be allowed to keep CAP_SYS_BOOT and execute reboot() to shutdown/reboot just their private container, rather than the host.

Unfortunately the kexec_load() syscall also relies on the CAP_SYS_BOOT capability. So by allowing a container to keep this capability to safely invoke reboot(), they mistakenly also gain the ability to use kexec_load(). The solution is to make kexec_load() return -EPERM if invoked from a PID namespace that is not the initial namespace

Quoted 2 lines
Quoted 7 lines
Quoted 2 lines

(Please see my previous email explaining why I believe the pidns is an appropriate check)

Serge as to your objects.

If we define kexec_load in terms of the pid namespace then something makes sense, but the error should be EINVAL, or something of that sort.

Makes sense.

Quoted 10 lines

That is what we did with reboot. We defined reboot in terms of the pid namespace.

Not defining kexec_load in terms of the pid namespace and then returning EPERM because having we happen to have CAP_SYS_BOOT for other reasons is semantically horrible.

At the end of the day the effect is the same, but I think it matters a great deal in how we think about things.

Quoted 7 lines

We have CAP_SYS_BOOT in the initial user namespace. We do have permission to make the system call.

So I continue to see this patch the way it is current constructed as broken.

I do also prefer splitting the capability. Michael Kerrisk, do you have any good suggestions for better names than CAP_RESTART (for killing or restarting /sbin/init) and CAP_BOOT (for kexec and/or hardware resets)? Maybe CAP_RESTART_USER and CAP_RESTART_HW? (CAP_SYS_BOOT being an alias for both for backward compatibility)