2019-11-18 17:05:57

by Prakash Sangappa

[permalink] [raw]
Subject: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces

Allow CAP_SYS_NICE to take effect for processes having effective uid of a
root user from init namespace.

Signed-off-by: Prakash Sangappa <[email protected]>
---
kernel/sched/core.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 7880f4f..628bd46 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4548,6 +4548,8 @@ int can_nice(const struct task_struct *p, const int nice)
int nice_rlim = nice_to_rlimit(nice);

return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
+ (ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
+ uid_eq(current_euid(), GLOBAL_ROOT_UID)) ||
capable(CAP_SYS_NICE));
}

@@ -4784,7 +4786,9 @@ static int __sched_setscheduler(struct task_struct *p,
/*
* Allow unprivileged RT tasks to decrease priority:
*/
- if (user && !capable(CAP_SYS_NICE)) {
+ if (user && !(ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
+ uid_eq(current_euid(), GLOBAL_ROOT_UID)) &&
+ !capable(CAP_SYS_NICE)) {
if (fair_policy(policy)) {
if (attr->sched_nice < task_nice(p) &&
!can_nice(p, attr->sched_nice))
--
2.7.4


2019-11-18 19:32:58

by Jann Horn

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces

On Mon, Nov 18, 2019 at 6:04 PM Prakash Sangappa
<[email protected]> wrote:
> Allow CAP_SYS_NICE to take effect for processes having effective uid of a
> root user from init namespace.
[...]
> @@ -4548,6 +4548,8 @@ int can_nice(const struct task_struct *p, const int nice)
> int nice_rlim = nice_to_rlimit(nice);
>
> return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
> + (ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) ||
> capable(CAP_SYS_NICE));

I very strongly dislike tying such a feature to GLOBAL_ROOT_UID.
Wouldn't it be better to control this through procfs, similar to
uid_map and gid_map? If you really need an escape hatch to become
privileged outside a user namespace, then I'd much prefer a file
"cap_map" that lets someone with appropriate capabilities in the outer
namespace write a bitmask of capabilities that should have effect
outside the container, or something like that. And limit that to bits
where that's sane, like CAP_SYS_NICE.

If we tie features like this to GLOBAL_ROOT_UID, more people are going
to run their containers with GLOBAL_ROOT_UID. Which is a terrible,
terrible idea. GLOBAL_ROOT_UID gives you privilege over all sorts of
files that you shouldn't be able to access, and only things like mount
namespaces and possibly LSMs prevent you from exercising that
privilege. GLOBAL_ROOT_UID should only ever be given to processes that
you trust completely.

2019-11-19 00:53:12

by Prakash Sangappa

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces



On 11/18/2019 11:30 AM, Jann Horn wrote:
> On Mon, Nov 18, 2019 at 6:04 PM Prakash Sangappa
> <[email protected]> wrote:
>> Allow CAP_SYS_NICE to take effect for processes having effective uid of a
>> root user from init namespace.
> [...]
>> @@ -4548,6 +4548,8 @@ int can_nice(const struct task_struct *p, const int nice)
>> int nice_rlim = nice_to_rlimit(nice);
>>
>> return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
>> + (ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
>> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) ||
>> capable(CAP_SYS_NICE));
> I very strongly dislike tying such a feature to GLOBAL_ROOT_UID.
> Wouldn't it be better to control this through procfs, similar to
> uid_map and gid_map? If you really need an escape hatch to become
> privileged outside a user namespace, then I'd much prefer a file
> "cap_map" that lets someone with appropriate capabilities in the outer
> namespace write a bitmask of capabilities that should have effect
> outside the container, or something like that. And limit that to bits
> where that's sane, like CAP_SYS_NICE.

Sounds reasonable. Adding a 'cap_map' file to user namespace, would give
more control. We could allow the capability in 'cap_map' to take effect
only if corresponding capability is enabled for the user inside the user
namespace Ex uid 0. Start with support for CAP_SYS_NICE?


>
> If we tie features like this to GLOBAL_ROOT_UID, more people are going
> to run their containers with GLOBAL_ROOT_UID. Which is a terrible,
> terrible idea. GLOBAL_ROOT_UID gives you privilege over all sorts of
> files that you shouldn't be able to access, and only things like mount
> namespaces and possibly LSMs prevent you from exercising that
> privilege. GLOBAL_ROOT_UID should only ever be given to processes that
> you trust completely.

Agreed.

2019-11-21 21:30:50

by Eric W. Biederman

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces

Prakash Sangappa <[email protected]> writes:

> Allow CAP_SYS_NICE to take effect for processes having effective uid of a
> root user from init namespace.
>
> Signed-off-by: Prakash Sangappa <[email protected]>
> ---
> kernel/sched/core.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 7880f4f..628bd46 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -4548,6 +4548,8 @@ int can_nice(const struct task_struct *p, const int nice)
> int nice_rlim = nice_to_rlimit(nice);
>
> return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
> + (ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) ||
> capable(CAP_SYS_NICE));
> }
>
> @@ -4784,7 +4786,9 @@ static int __sched_setscheduler(struct task_struct *p,
> /*
> * Allow unprivileged RT tasks to decrease priority:
> */
> - if (user && !capable(CAP_SYS_NICE)) {
> + if (user && !(ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) &&
> + !capable(CAP_SYS_NICE)) {
> if (fair_policy(policy)) {
> if (attr->sched_nice < task_nice(p) &&
> !can_nice(p, attr->sched_nice))


I remember looking at this before. I don't remember if I commented.

1) Having GLOBAL_ROOT_UID in a user namespace is A Bad Idea™.
Definitely not something we should make special case for.
That configuration is almost certainly a privilege escalation waiting
to happen.

2) If I read the other thread correctly there was talk about setting the
nice levels of processes in other containers. Ouch!

The only thing I can think that makes any sense at all is to allow
setting the nice levels of the processes in your own container.

I can totally see having a test to see if a processes credentials are
in the caller's user namespace or a child of caller's user namespace
and allowing admin level access if the caller has the appropriate
caps in their user namespace.

But in this case I don't see anything preventing the admin in a
container from using the ordinary nice levels on a task. You are
unlocking the nice levels reserved for the system administrator
for special occassions. I don't see how that makes any sense
to do from inside a container.

The design goal of user namespaces (assuming a non-buggy kernel) is to
ensure user namespaces give a user no more privileges than the user had
before creating a user namespace. In this case you are granting a user
who creates a user namespace the ability to change nice levels on all
process in the system (limited to users whose uid happens to be
GLOBAL_ROOT_UID). But still this is effectively a way to get
CAP_SYS_NICE back if it was dropped.

As a violation of security policy this change simply can not be allowed.
The entire idiom: "ns_capable(__task_cred(p)->user_ns, ...)" is a check
that provides no security.

Eric








2019-11-22 01:48:06

by Prakash Sangappa

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces



On 11/21/19 1:27 PM, [email protected] wrote:
> Prakash Sangappa <[email protected]> writes:
>
>> Allow CAP_SYS_NICE to take effect for processes having effective uid of a
>> root user from init namespace.
>>
>> Signed-off-by: Prakash Sangappa <[email protected]>
>> ---
>> kernel/sched/core.c | 6 +++++-
>> 1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
>> index 7880f4f..628bd46 100644
>> --- a/kernel/sched/core.c
>> +++ b/kernel/sched/core.c
>> @@ -4548,6 +4548,8 @@ int can_nice(const struct task_struct *p, const int nice)
>> int nice_rlim = nice_to_rlimit(nice);
>>
>> return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
>> + (ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
>> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) ||
>> capable(CAP_SYS_NICE));
>> }
>>
>> @@ -4784,7 +4786,9 @@ static int __sched_setscheduler(struct task_struct *p,
>> /*
>> * Allow unprivileged RT tasks to decrease priority:
>> */
>> - if (user && !capable(CAP_SYS_NICE)) {
>> + if (user && !(ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE) &&
>> + uid_eq(current_euid(), GLOBAL_ROOT_UID)) &&
>> + !capable(CAP_SYS_NICE)) {
>> if (fair_policy(policy)) {
>> if (attr->sched_nice < task_nice(p) &&
>> !can_nice(p, attr->sched_nice))
>
> I remember looking at this before. I don't remember if I commented.

Thanks for looking at this.

>
> 1) Having GLOBAL_ROOT_UID in a user namespace is A Bad Idea™.
> Definitely not something we should make special case for.
> That configuration is almost certainly a privilege escalation waiting
> to happen.

Mapping root uid 0(GLOBAL_ROOT_UID) from init namespace into a user
namespace is allowed right now. so the proposal was to extend this to
allow capabilities like CAP_SYS_NICE to take effect which is lacking.

Understand encouraging use of GLOBAL_ROOT_UID for this purpose may not
be a good idea.

We could look at other means to grant such capabilities to user
namespace thru a per process /proc file like 'cap_map' or something as
suggested in the other thread. What do you think about this approach?

Only privileged user in init namespace gets to add an entry to this
file. We need to define if this gets inherited by any nested user
namespaces that get created.



> 2) If I read the other thread correctly there was talk about setting the
> nice levels of processes in other containers. Ouch!

No not in other containers. Only on processes with in the container
which as this capability. The use case is to use it in a container with
user namespace and pid namespace. So no processes from other containers
should be visible. Necessary checks should be added?.


>
> The only thing I can think that makes any sense at all is to allow
> setting the nice levels of the processes in your own container.

Yes that is the intended use.

>
> I can totally see having a test to see if a processes credentials are
> in the caller's user namespace or a child of caller's user namespace
> and allowing admin level access if the caller has the appropriate
> caps in their user namespace.

Ok

> But in this case I don't see anything preventing the admin in a
> container from using the ordinary nice levels on a task. You are
> unlocking the nice levels reserved for the system administrator
> for special occassions. I don't see how that makes any sense
> to do from inside a container.

But this is what seems to be lacking. A container could have some
critical processes running which need to run at a higher priority.

>
> The design goal of user namespaces (assuming a non-buggy kernel) is to
> ensure user namespaces give a user no more privileges than the user had
> before creating a user namespace. In this case you are granting a user
> who creates a user namespace the ability to change nice levels on all
> process in the system (limited to users whose uid happens to be
> GLOBAL_ROOT_UID). But still this is effectively a way to get
> CAP_SYS_NICE back if it was dropped.

Giving privileges to only to those user with root uid from init
namespace inside the user namespace(GLOBAL_ROOT_UID), or if not using
GLOBAL_ROOT_UID, then privilege granted thru the /proc mechanism as
mentioned above.

>
> As a violation of security policy this change simply can not be allowed.
> The entire idiom: "ns_capable(__task_cred(p)->user_ns, ...)" is a check
> that provides no security.

If the effect of allowing such privileges inside user namespace could be
controlled with use of Cgroups, even then would it be a concern?

-Prakash
> Eric
>
>
>
>
>
>
>
>

2020-01-08 21:28:06

by Prakash Sangappa

[permalink] [raw]
Subject: Re: [RESEND RFC PATCH 1/1] Selectively allow CAP_SYS_NICE capability inside user namespaces



On 11/21/2019 05:45 PM, Prakash Sangappa wrote:
>
>
> On 11/21/19 1:27 PM, [email protected] wrote:
>> Prakash Sangappa <[email protected]> writes:
<..>
>> 2) If I read the other thread correctly there was talk about setting the
>> nice levels of processes in other containers. Ouch!
>
> No not in other containers. Only on processes within the container
> which has this capability. The use case is to use it in a container
> with user namespace and pid namespace. So no processes from other
> containers should be visible. Necessary checks should be added?.
>
>
>>
>> The only thing I can think that makes any sense at all is to allow
>> setting the nice levels of the processes in your own container.
>
> Yes that is the intended use.
>
>>
>> I can totally see having a test to see if a processes credentials
>> are
>> in the caller's user namespace or a child of caller's user namespace
>> and allowing admin level access if the caller has the appropriate
>> caps in their user namespace.
>
> Ok
>
>> But in this case I don't see anything preventing the admin in a
>> container from using the ordinary nice levels on a task. You are
>> unlocking the nice levels reserved for the system administrator
>> for special occassions. I don't see how that makes any sense
>> to do from inside a container.
>
> But this is what seems to be lacking. A container could have some
> critical processes running which need to run at a higher priority.

Any comments about this? What would be the recommendation for dealing
with such a requirement?