core: add lockless update_rlimit_cpu
Subsystems (3)
PATCH 1/2 core: add lockless update_rlimit_cpu
Quoted 3 lines · Oleg Nesterov
But I dislike the fact the patch uses
tasklist_lock. Can'tlock_task_sighand()work for you? (of course, in this caseupdate_rlimit_cpu()should be updated too).
Yeah, I think so. I didn't know about that, sorry.
Something like these two?
--
Rename update_rlimit_cpu to __update_rlimit_cpu and create update_rlimit_cpu wrapper with sighand lock.
The former is to be used by users who already hold the lock.
--- include/linux/posix-timers.h | 1 + kernel/posix-cpu-timers.c | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
include/linux/posix-timers.h +1 -0
@@ -117,6 +117,7 @@ void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,long clock_nanosleep_restart (struct restart_block *restart_block); +void __update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new);void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new);#endif
kernel/posix-cpu-timers.c +9 -5
@@ -12,17 +12,21 @@/* * Called after updating RLIMIT_CPU to set timer expiration if necessary. */ -void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new) +void __update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new) {cputime_t cputime =secs_to_cputime (rlim_new);struct signal_struct *const sig = task->signal;if (cputime_eq (sig->it[CPUCLOCK_PROF ].expires, cputime_zero) || -cputime_gt (sig->it[CPUCLOCK_PROF ].expires, cputime)) { -spin_lock_irq (&task->sighand->siglock); +cputime_gt (sig->it[CPUCLOCK_PROF ].expires, cputime))set_process_cpu_timer (task,CPUCLOCK_PROF , &cputime,NULL ); -spin_unlock_irq (&task->sighand->siglock); - } +} + +void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new) +{ +spin_lock_irq (&task->sighand->siglock); +__update_rlimit_cpu (task, rlim_new); +spin_unlock_irq (&task->sighand->siglock); }static int check_clock (const clockid_t which_clock)
Signature · 1 line
1.6.3.3
PATCH 2/2 core: allow setrlimit to non-current tasks
Add locking to allow setrlimit accept task parameter other than current.
Namely, lock tasklist_lock for read and check whether the task structure has signal and sighand non-null. Do all the signal processing under that lock still held.
--- kernel/sys.c | 25 ++++++++++++++++++------- 1 files changed, 18 insertions(+), 7 deletions(-)
kernel/sys.c +18 -7
@@ -1240,20 +1240,28 @@ int setrlimit(struct task_struct *tsk, unsigned int resource, struct rlimit *new_rlim) {struct rlimit *old_rlim; +unsigned long flags;int retval;if (new_rlim->rlim_cur > new_rlim->rlim_max)return -EINVAL ; + +if (lock_task_sighand (tsk, &flags) ==NULL ) +return -ESRCH ; old_rlim = tsk->signal->rlim + resource;if ((new_rlim->rlim_max > old_rlim->rlim_max) && - !capable (CAP_SYS_RESOURCE )) -return -EPERM ; -if (resource ==RLIMIT_NOFILE && new_rlim->rlim_max > sysctl_nr_open) -return -EPERM ; + !capable (CAP_SYS_RESOURCE )) { + retval = -EPERM ; + goto unlock; + } +if (resource ==RLIMIT_NOFILE && new_rlim->rlim_max > sysctl_nr_open) { + retval = -EPERM ; + goto unlock; + } retval =security_task_setrlimit (tsk, resource, new_rlim);if (retval) -return retval; + goto unlock;if (resource ==RLIMIT_CPU && new_rlim->rlim_cur ==0 ) {/*
@@ -1281,9 +1289,12 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,if (new_rlim->rlim_cur ==RLIM_INFINITY ) goto out; -update_rlimit_cpu (tsk, new_rlim->rlim_cur); +__update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: -return 0 ; + retval =0 ; +unlock: +unlock_task_sighand (tsk, &flags); +return retval; } SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Signature · 1 line
1.6.3.3
Re: [PATCH 2/2] core: allow setrlimit to non-current tasks
Quoted 3 lines · Jiri Slaby
Namely, lock
tasklist_lockfor read and check whether the task structure has signal and sighand non-null. Do all the signal processing under that lock still held.
The description is incorrect now, indeed...
And I broke mail threads, sorry.
Re: [PATCH 2/2] core: allow setrlimit to non-current tasks
In reply to Jiri SlabyQuoted 5 lines · Jiri Slaby
kernel/sys.c +0
-0@@ -1240,20 +1240,28 @@ int setrlimit(struct task_struct *tsk, unsigned int resource, struct rlimit *new_rlim)
kernel/sys.c int setrlimit(struct task_struct *tsk, unsigned int resource, · line 1250
Quoted 10 lines
kernel/sys.c +4
-0{struct rlimit *old_rlim; +unsigned long flags;int retval;if (new_rlim->rlim_cur > new_rlim->rlim_max)return -EINVAL ; + +if (lock_task_sighand (tsk, &flags) ==NULL ) +return -ESRCH ;
No, sorry, this can't work.
Because we need task_lock() to update rlimits, and ->alloc_lock does not nest under ->siglock.
Looks like we have to use tasklist_lock, but please don't use _irq, and please do not check ->signal != NULL. Perhaps it makes sense to take tasklist only if !same_thread_group(tsk, current) though.
Oh. We really need to make ->signal refcountable.
But there is another minor problem. If we use read_lock(ttasklist), then the write to /proc/application_pid/limits can race with application doing sys_setrlimits().
Nothing bad can happen, but this means that "echo ... > /proc/limits" can be lost. Not good, if admin wants to lower ->rlim_max we should try to ensure this always works.
Oleg.
Re: [PATCH 2/2] core: allow setrlimit to non-current tasks
Quoted 7 lines · Oleg Nesterov
But there is another minor problem. If we use
read_lock(ttasklist), then the write to/proc/application_pid/limitscan race with application doingsys_setrlimits().Nothing bad can happen, but this means that "echo ... >
/proc/limits" can be lost. Not good, if admin wants to lower ->rlim_max we should try to ensure this always works.
Actually, process cpu timer may be set to a wrong value. When
* somebody unrelated holds sighand->siglock
* process one stores rlim_new to rlim and gets stuck on spin_lock(siglock)
* process two does the same
* somebody releases sighand->siglock
* process one continues...
I can't think of anything else than doing all the checks and updates under alloc_lock, introducing coarse grained static mutex in setrlimit to protect it, or some other(I-don't-know-of)/new lock. Any ideas?
Thanks.
Re: [PATCH 2/2] core: allow setrlimit to non-current tasks
Quoted 11 lines · Jiri Slaby
Quoted 7 lines · Oleg Nesterov
But there is another minor problem. If we use
read_lock(ttasklist), then the write to/proc/application_pid/limitscan race with application doingsys_setrlimits().Nothing bad can happen, but this means that "echo ... >
/proc/limits" can be lost. Not good, if admin wants to lower ->rlim_max we should try to ensure this always works.Actually, process cpu timer may be set to a wrong value. When
Yes, I thought about this too. In fact I was going to complain, but then decided this is OK.
Quoted 3 lines
* somebody unrelated holds
sighand->siglock
* process one storesrlim_newto rlim and gets stuck onspin_lock(siglock)
* process two does the same
s/process/thread/. (I am talking about the current code). IOW, if the application is stupid and does setrlimit() from multimple threads at the same time - we can't help, the result is not predictable.
But, unless I missed something I think this case is fine, please see below.
Quoted 2 lines
* somebody releases
sighand->siglock
* process one continues...
Now, it is possible that cputime_expires.xxx_exp does not match ->rlim[RLIMIT_CPU].rlim_cur.
But we don't care. update_rlimit_cpu() must ensure that cputime_expires.xxx_exp is not greater than necessary, nothing else.
Quoted 3 lines
I can't think of anything else than doing all the checks and updates under
alloc_lock, introducing coarse grained static mutex in setrlimit to protect it,
Oh, please don't ;)
Or I missed your point?
But if you mean this series, then yes, I agree. We should try to do something to ensure that at least rlim_max can be always lowered when admin writes to /proc/pid/limits.
Oleg.
Re: [PATCH 2/2] core: allow setrlimit to non-current tasks
Quoted 11 lines · Oleg Nesterov
Quoted 3 lines · Jiri Slaby
I can't think of anything else than doing all the checks and updates under
alloc_lock, introducing coarse grained static mutex in setrlimit to protect it,Oh, please don't ;)
Or I missed your point?
But if you mean this series, then yes, I agree.
Yes, I meant those. But I don't know what do you agree with :).
Quoted 3 lines
We should try to do something
to ensure that at leastrlim_maxcan be always lowered when admin writes to/proc/pid/limits.
Yes, that's what I asked about when I wrote the three options which I was able to think of above. So any other ideas about how to elegantly protect against sys_setrlimit vs. admin+/proc/*/limits race?
Thanks a heap.
PATCH 1/1 core: allow setrlimit to non-current tasks
Just for clarity. The current patch looks like this:
--
Add locking to allow setrlimit accept task parameter other than current.
Namely, lock tasklist_lock for read and check whether the task structure has sighand non-null. Do all the signal processing under that lock still held. We do this only for non-current tasks.
--- kernel/sys.c | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-)
kernel/sys.c +20 -4
@@ -1240,6 +1240,7 @@ int setrlimit(struct task_struct *tsk, unsigned int resource, struct rlimit *new_rlim) {struct rlimit *old_rlim; +unsigned int needs_locking = !same_thread_group (tsk, current);int retval;if (new_rlim->rlim_cur > new_rlim->rlim_max)
@@ -1247,13 +1248,24 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,if (resource ==RLIMIT_NOFILE && new_rlim->rlim_max > sysctl_nr_open)return -EPERM ; +/* optimization: 'current' doesn't need locking, e.g. setrlimit */ +if (needs_locking) { +/* protect tsk->signal and tsk->sighand from disappearing */ +read_lock (&tasklist_lock); +if (!tsk->sighand) { + retval = -ESRCH ; + goto unlock; + } + } old_rlim = tsk->signal->rlim + resource;if ((new_rlim->rlim_max > old_rlim->rlim_max) && - !capable (CAP_SYS_RESOURCE )) -return -EPERM ; + !capable (CAP_SYS_RESOURCE )) { + retval = -EPERM ; + goto unlock; + } retval =security_task_setrlimit (tsk, resource, new_rlim);if (retval) -return retval; + goto unlock;if (resource ==RLIMIT_CPU && new_rlim->rlim_cur ==0 ) {/*
@@ -1283,7 +1295,11 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: -return 0 ; + retval =0 ; +unlock: +if (needs_locking) +read_unlock (&tasklist_lock); +return retval; } SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Signature · 1 line
1.6.3.3
Re: [PATCH] core: allow setrlimit to non-current tasks
kernel/sys.c int setrlimit(struct task_struct *tsk, unsigned int resource, · line 1244
Quoted 7 lines · Jiri Slaby
kernel/sys.c +1
-0@@ -1240,6 +1240,7 @@ int setrlimit(struct task_struct *tsk, unsigned int resource, struct rlimit *new_rlim) {struct rlimit *old_rlim; +unsigned int needs_locking = !same_thread_group (tsk, current);int retval;
Yes, thanks for doing this, imho this optimization is worthwhile.
But I'd suggest you to add this optimization in a separate patch because,
kernel/sys.c int setrlimit(struct task_struct *tsk, unsigned int resource, · line 1257
Quoted 7 lines
kernel/sys.c +7
-0+/* optimization: 'current' doesn't need locking, e.g. setrlimit */ +if (needs_locking) { +/* protect tsk->signal and tsk->sighand from disappearing */ +read_lock (&tasklist_lock); +if (!tsk->sighand) { + retval = -ESRCH ; + goto unlock;
I should have mentioned this before, but it is not that simple.
Even if same_thread_group(tsk, current), we must not trust tsk->sighand, it can be NULL if our subthread is dead. (well, we need ->signal, not ->sighand but this doesn't matter because they disappear simultaneously).
Actually, perhaps same_thread_group() is not needed, perhaps it is enough to avoid tasklist in sys_setrlimit case. So, I think optimization should do:
retval = -ESRCH;
if (tsk != current) {
read_lock(&tasklist_lock);
if (!tsk->sighand)
goto unlock;
}unlock:
if (tsk != current) read_unlock(&tasklist_lock);
Or, if we use same_thread_group(),
needs_locking = !same_thread_group(tsk, current);
if (!needs_locking)
tsk = current;
else {
take tasklist, check ->sighand.
}Oleg.
PATCH v2 1/8 SECURITY: selinux, fix update_rlimit_cpu parameter
OK, thanks, I ended up having this so far.
--
Don't pass rlim_cur member of RLIM_NLIMITS-1=RLIMIT_RTTIME limit to update_rlimit_cpu() in selinux_bprm_committing_creds.
Use proper rlim[RLIMIT_CPU].rlim_cur instead.
--- security/selinux/hooks.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
security/selinux/hooks.c +1 -1
@@ -2360,7 +2360,7 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) initrlim = init_task.signal->rlim + i; rlim->rlim_cur =min (rlim->rlim_max, initrlim->rlim_cur); } -update_rlimit_cpu (rlim->rlim_cur); +update_rlimit_cpu (current->signal->rlim[RLIMIT_CPU ].rlim_cur); } }
Signature · 1 line
1.6.3.3
PATCH v2 2/8 SECURITY: add task_struct to setrlimit
In reply to Oleg NesterovAdd task_struct to task_setrlimit of security_operations to be able to set rlimit of different task than current.
--- include/linux/security.h | 9 ++++++--- kernel/sys.c | 2 +- security/capability.c | 3 ++- security/security.c | 5 +++-- security/selinux/hooks.c | 7 ++++--- 5 files changed, 16 insertions(+), 10 deletions(-)
include/linux/security.h +6 -3
@@ -1520,7 +1520,8 @@ struct security_operations {int (*task_setnice) (struct task_struct *p,int nice);int (*task_setioprio) (struct task_struct *p,int ioprio);int (*task_getioprio) (struct task_struct *p); -int (*task_setrlimit) (unsigned int resource,struct rlimit *new_rlim); +int (*task_setrlimit) (struct task_struct *p,unsigned int resource, +struct rlimit *new_rlim);int (*task_setscheduler) (struct task_struct *p,int policy,struct sched_param *lp);int (*task_getscheduler) (struct task_struct *p);
@@ -1775,7 +1776,8 @@ int security_task_setgroups(struct group_info *group_info);int security_task_setnice (struct task_struct *p,int nice);int security_task_setioprio (struct task_struct *p,int ioprio);int security_task_getioprio (struct task_struct *p); -int security_task_setrlimit (unsigned int resource,struct rlimit *new_rlim); +int security_task_setrlimit (struct task_struct *p,unsigned int resource, +struct rlimit *new_rlim);int security_task_setscheduler (struct task_struct *p,int policy,struct sched_param *lp);int security_task_getscheduler (struct task_struct *p);
@@ -2385,7 +2387,8 @@ static inline int security_task_getioprio(struct task_struct *p)return 0 ; } -static inline int security_task_setrlimit (unsigned int resource, +static inline int security_task_setrlimit (struct task_struct *p, +unsigned int resource,struct rlimit *new_rlim) {return 0 ;
kernel/sys.c +1 -1
@@ -1254,7 +1254,7 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)if (resource ==RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)return -EPERM ; - retval =security_task_setrlimit (resource, &new_rlim); + retval =security_task_setrlimit (current, resource, &new_rlim);if (retval)return retval;
security/capability.c +2 -1
@@ -447,7 +447,8 @@ static int cap_task_getioprio(struct task_struct *p)return 0 ; } -static int cap_task_setrlimit (unsigned int resource,struct rlimit *new_rlim) +static int cap_task_setrlimit (struct task_struct *p,unsigned int resource, +struct rlimit *new_rlim) {return 0 ; }
security/security.c +3 -2
@@ -779,9 +779,10 @@ int security_task_getioprio(struct task_struct *p)return security_ops->task_getioprio (p); } -int security_task_setrlimit (unsigned int resource,struct rlimit *new_rlim) +int security_task_setrlimit (struct task_struct *p,unsigned int resource, +struct rlimit *new_rlim) { -return security_ops->task_setrlimit (resource, new_rlim); +return security_ops->task_setrlimit (p, resource, new_rlim); } int security_task_setscheduler(struct task_struct *p,
security/selinux/hooks.c +4 -3
@@ -3355,16 +3355,17 @@ static int selinux_task_getioprio(struct task_struct *p)return current_has_perm (p,PROCESS__GETSCHED ); } -static int selinux_task_setrlimit (unsigned int resource,struct rlimit *new_rlim) +static int selinux_task_setrlimit (struct task_struct *p,unsigned int resource, +struct rlimit *new_rlim) { -struct rlimit *old_rlim = current->signal->rlim + resource; +struct rlimit *old_rlim = p->signal->rlim + resource;/* Control the ability to change the hard limit (whether lowering or raising it), so that the hard limit can later be used as a safe reset point for the soft limit upon context transitions. See selinux_bprm_committing_creds. */ if (old_rlim->rlim_max != new_rlim->rlim_max) -return current_has_perm (current,PROCESS__SETRLIMIT ); +return current_has_perm (p,PROCESS__SETRLIMIT );return 0 ; }
Signature · 1 line
1.6.3.3
PATCH v2 3/8 core: add task_struct to update_rlimit_cpu
In reply to Oleg NesterovAdd task_struct as a parameter to update_rlimit_cpu to be able to set rlimit_cpu of different task than current.
--- include/linux/posix-timers.h | 2 +- kernel/posix-cpu-timers.c | 10 +++++----- kernel/sys.c | 2 +- security/selinux/hooks.c | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-)
include/linux/posix-timers.h +1 -1
@@ -117,6 +117,6 @@ void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,long clock_nanosleep_restart (struct restart_block *restart_block); -void update_rlimit_cpu (unsigned long rlim_new); +void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new);#endif
kernel/posix-cpu-timers.c +5 -5
@@ -12,16 +12,16 @@/* * Called after updating RLIMIT_CPU to set timer expiration if necessary. */ -void update_rlimit_cpu (unsigned long rlim_new) +void update_rlimit_cpu (struct task_struct *task,unsigned long rlim_new) {cputime_t cputime =secs_to_cputime (rlim_new); -struct signal_struct *const sig = current->signal; +struct signal_struct *const sig = task->signal;if (cputime_eq (sig->it[CPUCLOCK_PROF ].expires, cputime_zero) ||cputime_gt (sig->it[CPUCLOCK_PROF ].expires, cputime)) { -spin_lock_irq (¤t->sighand->siglock); -set_process_cpu_timer (current,CPUCLOCK_PROF , &cputime,NULL ); -spin_unlock_irq (¤t->sighand->siglock); +spin_lock_irq (&task->sighand->siglock); +set_process_cpu_timer (task,CPUCLOCK_PROF , &cputime,NULL ); +spin_unlock_irq (&task->sighand->siglock); } }
kernel/sys.c +1 -1
@@ -1284,7 +1284,7 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)if (new_rlim.rlim_cur ==RLIM_INFINITY ) goto out; -update_rlimit_cpu (new_rlim.rlim_cur); +update_rlimit_cpu (current, new_rlim.rlim_cur); out:return 0 ; }
security/selinux/hooks.c +2 -1
@@ -2360,7 +2360,8 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) initrlim = init_task.signal->rlim + i; rlim->rlim_cur =min (rlim->rlim_max, initrlim->rlim_cur); } -update_rlimit_cpu (current->signal->rlim[RLIMIT_CPU ].rlim_cur); +update_rlimit_cpu (current, + current->signal->rlim[RLIMIT_CPU ].rlim_cur); } }
Signature · 1 line
1.6.3.3
PATCH v2 4/8 sys_setrlimit: make sure ->rlim_max never grows
In reply to Oleg NesterovFrom: Oleg Nesterov <[email protected]>
Mostly preparation for Jiri's changes, but probably makes sense anyway.
sys_setrlimit() checks new_rlim.rlim_max <= old_rlim->rlim_max, but when it takes task_lock() old_rlim->rlim_max can be already lowered. Move this check under task_lock().
Currently this is not important, we can only race with our sub-thread, this means the application is stupid. But when we change the code to allow the update of !current task's limits, it becomes important to make sure ->rlim_max can be lowered "reliably" even if we race with the application doing sys_setrlimit().
--- kernel/sys.c | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-)
kernel/sys.c +8 -7
@@ -1247,10 +1247,6 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)return -EFAULT ;if (new_rlim.rlim_cur > new_rlim.rlim_max)return -EINVAL ; - old_rlim = current->signal->rlim + resource; -if ((new_rlim.rlim_max > old_rlim->rlim_max) && - !capable (CAP_SYS_RESOURCE )) -return -EPERM ;if (resource ==RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)return -EPERM ;
@@ -1268,11 +1264,16 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim) new_rlim.rlim_cur =1 ; } + old_rlim = current->signal->rlim + resource;task_lock (current->group_leader); - *old_rlim = new_rlim; +if ((new_rlim.rlim_max <= old_rlim->rlim_max) || +capable (CAP_SYS_RESOURCE )) + *old_rlim = new_rlim; +else + retval = -EPERM ;task_unlock (current->group_leader); -if (resource !=RLIMIT_CPU ) +if (retval || resource !=RLIMIT_CPU ) goto out;/*
@@ -1286,7 +1287,7 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)update_rlimit_cpu (current, new_rlim.rlim_cur); out: -return 0 ; +return retval; }/*
Signature · 1 line
1.6.3.3
PATCH v2 5/8 core: split sys_setrlimit
In reply to Oleg NesterovCreate setrlimit from sys_setrlimit and declare setrlimit in the resource header. This is to allow rlimits to be changed not only by syscall, but later from proc code too.
--- include/linux/resource.h | 2 ++ kernel/sys.c | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 18 deletions(-)
include/linux/resource.h +2 -0
@@ -71,5 +71,7 @@ struct rlimit {#include <asm/resource.h> int getrusage (struct task_struct *p,int who,struct rusage __user *ru); +int setrlimit (struct task_struct *tsk,unsigned int resource, +struct rlimit *new_rlim);#endif
kernel/sys.c +26 -18
82 lines not on this page · raw message
Signature · 1 line
1.6.3.3
PATCH v2 6/8 core: allow setrlimit to non-current tasks
In reply to Oleg NesterovAdd locking to allow setrlimit accept task parameter other than current.
Namely, lock tasklist_lock for read and check whether the task structure has sighand non-null. Do all the signal processing under that lock still held. We do this only for non-current tasks.
--- kernel/sys.c | 10 +++++++++- 1 files changed, 9 insertions(+), 1 deletions(-)
kernel/sys.c +9 -1
@@ -1247,9 +1247,16 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,if (resource ==RLIMIT_NOFILE && new_rlim->rlim_max > sysctl_nr_open)return -EPERM ; +/* protect tsk->signal and tsk->sighand from disappearing */ +read_lock (&tasklist_lock); +if (!tsk->sighand) { + retval = -ESRCH ; + goto out; + } + retval =security_task_setrlimit (tsk, resource, new_rlim);if (retval) -return retval; + goto out;if (resource ==RLIMIT_CPU && new_rlim->rlim_cur ==0 ) {/*
@@ -1284,6 +1291,7 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: +read_unlock (&tasklist_lock);return retval; }
Signature · 1 line
1.6.3.3
PATCH v2 7/8 core: optimize setrlimit for current task
In reply to Oleg NesterovDon't take tasklist lock for 'current'. It's not needed, since current->sighand/signal can't disappear.
This improves serlimit called especially via sys_setrlimit.
--- kernel/sys.c | 16 ++++++++++------ 1 files changed, 10 insertions(+), 6 deletions(-)
kernel/sys.c +10 -6
@@ -1247,11 +1247,14 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,if (resource ==RLIMIT_NOFILE && new_rlim->rlim_max > sysctl_nr_open)return -EPERM ; -/* protect tsk->signal and tsk->sighand from disappearing */ -read_lock (&tasklist_lock); -if (!tsk->sighand) { - retval = -ESRCH ; - goto out; +/* optimization: 'current' doesn't need locking, e.g. setrlimit */ +if (tsk != current) { +/* protect tsk->signal and tsk->sighand from disappearing */ +read_lock (&tasklist_lock); +if (!tsk->sighand) { + retval = -ESRCH ; + goto out; + } } retval =security_task_setrlimit (tsk, resource, new_rlim);
@@ -1291,7 +1294,8 @@ int setrlimit(struct task_struct *tsk, unsigned int resource,update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: -read_unlock (&tasklist_lock); +if (tsk != current) +read_unlock (&tasklist_lock);return retval; }
Signature · 1 line
1.6.3.3
PATCH v2 8/8 FS: proc, make limits writable
In reply to Oleg NesterovAllow writing strings such as
Max core file size=0:unlimited
to /proc/<pid>/limits to change limits.
--- fs/proc/base.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 76 insertions(+), 2 deletions(-)
fs/proc/base.c +76 -2
@@ -129,6 +129,8 @@ struct pid_entry { NULL, &proc_single_file_operations, \ { .proc_show = show } ) +static ssize_t proc_info_read (struct file * file,char __user * buf, +size_t count,loff_t *ppos);/* * Count the number of hardlinks for the pid_entry table, excluding the . * and .. links.
@@ -522,6 +524,74 @@ static int proc_pid_limits(struct task_struct *task, char *buffer)return count; } +static ssize_t limits_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct task_struct *task =get_proc_task (file->f_path.dentry->d_inode); +char str[32 +1 +16 +1 +16 +1 ], *delim, *next; +struct rlimit new_rlimit; +unsigned int i; +int ret; + +if (!task) { + count = -ESRCH ; + goto out; + } +if (copy_from_user (str, buf,min (count,sizeof (str) -1 ))) { + count = -EFAULT ; + goto put_task; + } + + str[min (count,sizeof (str) -1 )] =0 ; + + delim =strchr (str,'=' ); +if (!delim) { + count = -EINVAL ; + goto put_task; + } + *delim++ =0 ;/* for easy 'str' usage */ + new_rlimit.rlim_cur =simple_strtoul (delim, &next,0 ); +if (*next !=':' ) { +if (strncmp (delim,"unlimited:" ,10 )) { + count = -EINVAL ; + goto put_task; + } + new_rlimit.rlim_cur =RLIM_INFINITY ; + next = delim +9 ;/* move to ':' */ + } + delim = next +1 ; + new_rlimit.rlim_max =simple_strtoul (delim, &next,0 ); +if (*next !=0 ) { +if (strcmp (delim,"unlimited" )) { + count = -EINVAL ; + goto put_task; + } + new_rlimit.rlim_max =RLIM_INFINITY ; + } + +for (i =0 ; i <RLIM_NLIMITS ; i++) +if (!strcmp (str, lnames[i].name)) +break ; +if (i >=RLIM_NLIMITS ) { + count = -EINVAL ; + goto put_task; + } + + ret =setrlimit (task, i, &new_rlimit); +if (ret) + count = ret; + +put_task: +put_task_struct (task); +out: +return count; +} + +static const struct file_operations proc_pid_limits_operations = { + .read = proc_info_read, + .write = limits_write, +}; +#ifdef CONFIG_HAVE_ARCH_TRACEHOOK static int proc_pid_syscall (struct task_struct *task,char *buffer) {
@@ -2501,7 +2571,9 @@ static const struct pid_entry tgid_base_stuff[] = {INF ("auxv" ,S_IRUSR , proc_pid_auxv),ONE ("status" ,S_IRUGO , proc_pid_status),ONE ("personality" ,S_IRUSR , proc_pid_personality), -INF ("limits" ,S_IRUSR , proc_pid_limits), +NOD ("limits" ,S_IFREG |S_IRUSR |S_IWUSR ,NULL , + &proc_pid_limits_operations, + { .proc_read = proc_pid_limits }), #ifdef CONFIG_SCHED_DEBUG REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), #endif
@@ -2836,7 +2908,9 @@ static const struct pid_entry tid_base_stuff[] = {INF ("auxv" ,S_IRUSR , proc_pid_auxv),ONE ("status" ,S_IRUGO , proc_pid_status),ONE ("personality" ,S_IRUSR , proc_pid_personality), -INF ("limits" ,S_IRUSR , proc_pid_limits), +NOD ("limits" ,S_IFREG |S_IRUSR |S_IWUSR ,NULL , + &proc_pid_limits_operations, + { .proc_read = proc_pid_limits }), #ifdef CONFIG_SCHED_DEBUG REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), #endif
Signature · 1 line
1.6.3.3
Re: [PATCH v2 8/8] FS: proc, make limits writable
Quoted 4 lines · Jiri Slaby
Allow writing strings such as
Max core file size=0:unlimited
to /proc/<pid>/limits to change limits.
Can't review the parsing in limits_write() because I don't have enough "C" skills, but otherwise the whole series looks correct to me.
One small nit, just to suggest the further 9/8 cleanup,
fs/proc/base.c static int proc_pid_limits(struct task_struct *task, char *buffer) · line 593
Quoted 4 lines
fs/proc/base.c +4
-0+static const struct file_operations proc_pid_limits_operations = { + .read = proc_info_read, + .write = limits_write, +};
I think it makes sense to tweak proc_pid_limits() a little bit (and rename it), so that we can do
.read = limits_read,
.write = limits_write
Then,
fs/proc/base.c static const struct pid_entry tgid_base_stuff[] = { · line 2573
Quoted 4 lines
fs/proc/base.c +3
-0@@ -2501,7 +2571,9 @@ static const struct pid_entry tgid_base_stuff[] = { +NOD ("limits" ,S_IFREG |S_IRUSR |S_IWUSR ,NULL , + &proc_pid_limits_operations, + { .proc_read = proc_pid_limits }),
We could use
REG("limits", S_IRUSR|S_IWUSR, &proc_pid_limits_operations),
instead, this looks a bit cleaner to me.
But as I said, we can do this later.
And another minor nit (just in case you will re-submit this series for some reason). Perhaps the changelog in 6/8 should mention that we do not do any security checks when tsk != current (without selinux). We assume that either the caller is sys_setrlimit(), or the caller should verify it has rights to change the limits: in case of limits_write() we rely on ->mode = S_IRUSR|S_IWUSR.
Oleg.
Re: [PATCH v2 8/8] FS: proc, make limits writable
Quoted 22 lines · Oleg Nesterov
One small nit, just to suggest the further 9/8 cleanup,
fs/proc/base.c static int proc_pid_limits(struct task_struct *task, char *buffer) · line 593
Quoted 4 lines
fs/proc/base.c +4
-0+static const struct file_operations proc_pid_limits_operations = { + .read = proc_info_read, + .write = limits_write, +};I think it makes sense to tweak
proc_pid_limits()a little bit (and rename it), so that we can do.read =
limits_read,
.write =limits_writeThen,
fs/proc/base.c static const struct pid_entry tgid_base_stuff[] = { · line 2573
Quoted 4 lines
fs/proc/base.c +3
-0@@ -2501,7 +2571,9 @@ static const struct pid_entry tgid_base_stuff[] = { +NOD ("limits" ,S_IFREG |S_IRUSR |S_IWUSR ,NULL , + &proc_pid_limits_operations, + { .proc_read = proc_pid_limits }),We could use
Quoted 3 lines
REG("limits", S_IRUSR|S_IWUSR, &proc_pid_limits_operations),instead, this looks a bit cleaner to me.
Hi again, nobody picked them up yet, I waited till the end of the merge window and now I'll repost.
Did you mean here to do the proc_info_read work (get/put task, alloc buf, simple_read) directly in limits_read?
Quoted 6 lines
And another minor nit (just in case you will re-submit this series for some reason). Perhaps the changelog in 6/8 should mention that we do not do any security checks when tsk != current (without selinux). We assume that either the caller is
sys_setrlimit(), or the caller should verify it has rights to change the limits: in case oflimits_write()we rely on ->mode = S_IRUSR|S_IWUSR.
I did it as a comment by the setrlimit. I think nobody would care about a changelog note ;).
Re: [PATCH v2 8/8] FS: proc, make limits writable
Quoted 26 lines · Jiri Slaby
Quoted 24 lines · Oleg Nesterov
One small nit, just to suggest the further 9/8 cleanup,
fs/proc/base.c static int proc_pid_limits(struct task_struct *task, char *buffer) · line 593
Quoted 4 lines
fs/proc/base.c +4
-0+static const struct file_operations proc_pid_limits_operations = { + .read = proc_info_read, + .write = limits_write, +};I think it makes sense to tweak
proc_pid_limits()a little bit (and rename it), so that we can do.read =
limits_read,
.write =limits_writeThen,
fs/proc/base.c static const struct pid_entry tgid_base_stuff[] = { · line 2573
Quoted 4 lines
fs/proc/base.c +3
-0@@ -2501,7 +2571,9 @@ static const struct pid_entry tgid_base_stuff[] = { +NOD ("limits" ,S_IFREG |S_IRUSR |S_IWUSR ,NULL , + &proc_pid_limits_operations, + { .proc_read = proc_pid_limits }),We could use
REG("limits", S_IRUSR|S_IWUSR, &proc_pid_limits_operations),
Quoted 7 lines
Quoted 1 line
instead, this looks a bit cleaner to me.
Hi again, nobody picked them up yet, I waited till the end of the merge window and now I'll repost.
Did you mean here to do the
proc_info_readwork (get/put task, alloc buf,simple_read) directly inlimits_read?
limits_read() has to do get_proc_task(d_inode), yes. Otherwise I don't see any additional complications, you can use simple_read_from_buffer(), no need to allocate the buffer but of course you can you shouldn't write more than "size_t count" bytes.
But perhaps I missed something, and in any case this is up to you. If you don't like this - please forget.
Quoted 9 lines
Quoted 6 lines
And another minor nit (just in case you will re-submit this series for some reason). Perhaps the changelog in 6/8 should mention that we do not do any security checks when tsk != current (without selinux). We assume that either the caller is
sys_setrlimit(), or the caller should verify it has rights to change the limits: in case oflimits_write()we rely on ->mode = S_IRUSR|S_IWUSR.I did it as a comment by the setrlimit. I think nobody would care about a changelog note ;).
Good ;)
Oleg.
cover sys_setrlimit: make sure ->rlim_max never grows
In reply to Jiri SlabyQuoted 7 lines · Jiri Slaby
Quoted 5 lines · Oleg Nesterov
Quoted 3 lines · Jiri Slaby
I can't think of anything else than doing all the checks and updates under
alloc_lock, introducing coarse grained static mutex in setrlimit to protect it,
Quoted 7 lines
Quoted 5 lines
Oh, please don't ;)
Or I missed your point?
But if you mean this series, then yes, I agree.
Yes, I meant those. But I don't know what do you agree with :).
Not sure what I agree with, but I am glad we seem to agree with each other ;)
Quoted 7 lines
Quoted 3 lines
We should try to do something
to ensure that at leastrlim_maxcan be always lowered when admin writes to/proc/pid/limits.Yes, that's what I asked about when I wrote the three options which I was able to think of above. So any other ideas about how to elegantly protect against
sys_setrlimitvs. admin+/proc/*/limits race?
Perhaps we should start these change with this patch (see the next email) ?
Perhaps, before your changes, we should "fix" sys_setrlimit() first ? Well, the patch (the next email) is not tested... What do you think?
Oleg.
PATCH 1/1 sys_setrlimit: make sure ->rlim_max never grows
In reply to Jiri SlabyMostly preparation for Jiri's changes, but probably makes sense anyway.
sys_setrlimit() checks new_rlim.rlim_max <= old_rlim->rlim_max, but when it takes task_lock() old_rlim->rlim_max can be already lowered. Move this check under task_lock().
Currently this is not important, we can only race with our sub-thread, this means the application is stupid. But when we change the code to allow the update of !current task's limits, it becomes important to make sure ->rlim_max can be lowered "reliably" even if we race with the application doing sys_setrlimit().
---
kernel/sys.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
kernel/sys.c renamed from kernel/sys.c~SETRLIMIT +8 -8
@@ -1247,10 +1247,6 @@ SYSCALL_DEFINE2(setrlimit, unsigned int,return -EFAULT ;if (new_rlim.rlim_cur > new_rlim.rlim_max)return -EINVAL ; - old_rlim = current->signal->rlim + resource; -if ((new_rlim.rlim_max > old_rlim->rlim_max) && - !capable (CAP_SYS_RESOURCE )) -return -EPERM ;if (resource ==RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)return -EPERM ;
@@ -1268,13 +1264,17 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, new_rlim.rlim_cur =1 ; } + old_rlim = current->signal->rlim + resource;task_lock (current->group_leader); - *old_rlim = new_rlim; +if ((new_rlim.rlim_max <= old_rlim->rlim_max) || +capable (CAP_SYS_RESOURCE )) + *old_rlim = new_rlim; +else + retval = -EPERM ;task_unlock (current->group_leader); -if (resource !=RLIMIT_CPU ) +if (retval || resource !=RLIMIT_CPU ) goto out; -/* * RLIMIT_CPU handling. Note that the kernel fails to return an error * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
@@ -1286,7 +1286,7 @@ SYSCALL_DEFINE2(setrlimit, unsigned int,update_rlimit_cpu (new_rlim.rlim_cur); out: -return 0 ; +return retval; }/*