* [PATCH 2.6.13-rc7 1/2] New Syscall: get rlimits of any process (reworked)
@ 2005-08-26 3:34 Wieland Gmeiner
2005-08-26 3:39 ` [PATCH 2.6.13-rc7 2/2] New Syscall: set " Wieland Gmeiner
2005-08-31 12:46 ` [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate) Wieland Gmeiner
0 siblings, 2 replies; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-26 3:34 UTC (permalink / raw)
To: linux-kernel; +Cc: Elliot Lee
Hi all!
First I would like to thank everyone who commented on my code.
I understand that this won't go into mainline but nevertheless I would
like to work on it further as it is a great learning experience to me.
I incorporated the changes suggested to me by this list (at least I hope
so), any comments highly appreciated.
Thanks,
Wieland
Rationale: Currently resource usage limits (rlimits) can only be
set inside a process space, or inherited from the parent process.
It would be useful to allow adjusting resource limits for running
processes, e.g. tuning the resource usage of daemon processes under
changing workloads without restarting them.
Implementation: This patch provides a new syscall getprlimit() for
reading a given process resource limits for i386. Its implementation
follows closely the getrlimit syscall. It is given a pid as an
additional argument. If the given pid equals zero the current process
rlimits are read and the behaviour resembles the behaviour of
getrlimit. Otherwise some checking on the validity of the given pid is
done and if the given process is found access is granted if
- the calling process holds the CAP_SYS_PTRACE capability or
- the calling process uid equals the uid, euid, suid of the target
process and the calling process gid equals the gid, egid, sgid of
the target process.
(This resembles the behaviour of the ptrace system call.)
See the followup for the writing syscall.
Simple programs for testing the syscalls can be found on
http://stud4.tuwien.ac.at/~e8607062/studies/soc/patches/
Signed-off-by: Wieland Gmeiner <e8607062@student.tuwien.ac.at>
---
arch/i386/kernel/syscall_table.S | 1
include/asm-i386/unistd.h | 3 -
include/linux/security.h | 25 +++++++-----
kernel/sys.c | 81 ++++++++++++++++++++++++++++++++++-----
security/dummy.c | 5 +-
security/selinux/hooks.c | 17 +++++---
6 files changed, 105 insertions(+), 27 deletions(-)
diff -puN arch/i386/kernel/syscall_table.S~getprlimit arch/i386/kernel/syscall_table.S
--- linux-2.6.13-rc7/arch/i386/kernel/syscall_table.S~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/arch/i386/kernel/syscall_table.S 2005-08-26 05:01:46.000000000 +0200
@@ -294,3 +294,4 @@ ENTRY(sys_call_table)
.long sys_inotify_init
.long sys_inotify_add_watch
.long sys_inotify_rm_watch
+ .long sys_getprlimit
diff -puN include/asm-i386/unistd.h~getprlimit include/asm-i386/unistd.h
--- linux-2.6.13-rc7/include/asm-i386/unistd.h~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/include/asm-i386/unistd.h 2005-08-26 05:01:46.000000000 +0200
@@ -299,8 +299,9 @@
#define __NR_inotify_init 291
#define __NR_inotify_add_watch 292
#define __NR_inotify_rm_watch 293
+#define __NR_getprlimit 294
-#define NR_syscalls 294
+#define NR_syscalls 295
/*
* user-visible error numbers are in the range -1 - -128: see
diff -puN include/linux/security.h~getprlimit include/linux/security.h
--- linux-2.6.13-rc7/include/linux/security.h~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/include/linux/security.h 2005-08-26 05:01:46.000000000 +0200
@@ -584,10 +584,12 @@ struct swap_info_struct;
* @p contains the task_struct of process.
* @nice contains the new nice value.
* Return 0 if permission is granted.
- * @task_setrlimit:
- * Check permission before setting the resource limits of the current
- * process for @resource to @new_rlim. The old resource limit values can
- * be examined by dereferencing (current->signal->rlim + resource).
+ * @task_rlimit:
+ * Check permission before reading the resource limits of the process @p
+ * for @resource or setting the limits to @new_rlim. The old resource
+ * limit values can be examined by dereferencing
+ * (p->signal->rlim + resource).
+ * @p contains the task_struct for the process.
* @resource contains the resource whose limit is being set.
* @new_rlim contains the new limits for @resource.
* Return 0 if permission is granted.
@@ -1156,7 +1158,8 @@ struct security_operations {
int (*task_getsid) (struct task_struct * p);
int (*task_setgroups) (struct group_info *group_info);
int (*task_setnice) (struct task_struct * p, int nice);
- int (*task_setrlimit) (unsigned int resource, struct rlimit * new_rlim);
+ int (*task_rlimit) (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);
@@ -1798,10 +1801,11 @@ static inline int security_task_setnice
return security_ops->task_setnice (p, nice);
}
-static inline int security_task_setrlimit (unsigned int resource,
- struct rlimit *new_rlim)
+static inline int security_task_rlimit (struct task_struct *p,
+ unsigned int resource,
+ struct rlimit *new_rlim)
{
- return security_ops->task_setrlimit (resource, new_rlim);
+ return security_ops->task_rlimit (p, resource, new_rlim);
}
static inline int security_task_setscheduler (struct task_struct *p,
@@ -2447,8 +2451,9 @@ static inline int security_task_setnice
return 0;
}
-static inline int security_task_setrlimit (unsigned int resource,
- struct rlimit *new_rlim)
+static inline int security_task_rlimit (struct task_struct *p,
+ unsigned int resource,
+ struct rlimit *new_rlim)
{
return 0;
}
diff -puN kernel/sys.c~getprlimit kernel/sys.c
--- linux-2.6.13-rc7/kernel/sys.c~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/kernel/sys.c 2005-08-26 05:06:24.000000000 +0200
@@ -1524,17 +1524,80 @@ asmlinkage long sys_setdomainname(char _
return errno;
}
-asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
+/*
+ * get/setprlimit()
+ *
+ * As ptrace implies the ability to execute arbitrary code in the given
+ * process, which means that the calling process could obtain and set
+ * rlimits for that process without getprlimit/setprlimit anyways,
+ * we use the same permission checks as ptrace.
+ */
+static inline int prlim_check_perm(task_t *task)
+{
+ return ((current->uid == task->euid) &&
+ (current->uid == task->suid) &&
+ (current->uid == task->uid) &&
+ (current->gid == task->egid) &&
+ (current->gid == task->sgid) &&
+ (current->gid == task->gid)) || capable(CAP_SYS_PTRACE);
+}
+
+static inline long rlim_do_getprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
{
+ struct rlimit value;
+ task_t *p;
+ int retval = -EINVAL;
+
if (resource >= RLIM_NLIMITS)
- return -EINVAL;
- else {
- struct rlimit value;
- task_lock(current->group_leader);
- value = current->signal->rlim[resource];
- task_unlock(current->group_leader);
- return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
+ goto out;
+
+ if (pid < 0)
+ goto out;
+
+ retval = -ESRCH;
+ read_lock(&tasklist_lock);
+ if (!pid) {
+ p = current;
+ } else {
+ p = find_task_by_pid(pid);
}
+ if (p) {
+ retval = -EPERM;
+ if (pid && !prlim_check_perm(p))
+ goto unlock_out;
+ if (pid) {
+ retval = security_task_rlimit(p, resource, 0);
+ if (retval)
+ goto unlock_out;
+ }
+ task_lock(p->group_leader);
+ value = p->signal->rlim[resource];
+ task_unlock(p->group_leader);
+ } else
+ goto unlock_out;
+
+ read_unlock(&tasklist_lock);
+
+ retval = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
+
+out:
+ return retval;
+
+unlock_out:
+ read_unlock(&tasklist_lock);
+ return retval;
+}
+
+asmlinkage long sys_getprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ return rlim_do_getprlimit(pid, resource, rlim);
+}
+
+asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
+{
+ return rlim_do_getprlimit(0, resource, rlim);
}
#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
@@ -1579,7 +1642,7 @@ asmlinkage long sys_setrlimit(unsigned i
if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
return -EPERM;
- retval = security_task_setrlimit(resource, &new_rlim);
+ retval = security_task_rlimit(0, resource, &new_rlim);
if (retval)
return retval;
diff -puN security/dummy.c~getprlimit security/dummy.c
--- linux-2.6.13-rc7/security/dummy.c~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/security/dummy.c 2005-08-26 05:01:46.000000000 +0200
@@ -543,7 +543,8 @@ static int dummy_task_setnice (struct ta
return 0;
}
-static int dummy_task_setrlimit (unsigned int resource, struct rlimit *new_rlim)
+static int dummy_task_rlimit (struct task_struct *p, unsigned int resource,
+ struct rlimit *new_rlim)
{
return 0;
}
@@ -936,7 +937,7 @@ void security_fixup_ops (struct security
set_to_dummy_if_null(ops, task_getsid);
set_to_dummy_if_null(ops, task_setgroups);
set_to_dummy_if_null(ops, task_setnice);
- set_to_dummy_if_null(ops, task_setrlimit);
+ set_to_dummy_if_null(ops, task_rlimit);
set_to_dummy_if_null(ops, task_setscheduler);
set_to_dummy_if_null(ops, task_getscheduler);
set_to_dummy_if_null(ops, task_wait);
diff -puN security/selinux/hooks.c~getprlimit security/selinux/hooks.c
--- linux-2.6.13-rc7/security/selinux/hooks.c~getprlimit 2005-08-26 05:01:17.000000000 +0200
+++ linux-2.6.13-rc7-wieland/security/selinux/hooks.c 2005-08-26 05:01:46.000000000 +0200
@@ -2703,20 +2703,27 @@ static int selinux_task_setnice(struct t
return task_has_perm(current,p, PROCESS__SETSCHED);
}
-static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
+static int selinux_task_rlimit(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;
int rc;
- rc = secondary_ops->task_setrlimit(resource, new_rlim);
+ if (!new_rlim)
+ rc = secondary_ops->task_rlimit(p, resource, 0);
+ else
+ rc = secondary_ops->task_rlimit(0, resource, new_rlim);
if (rc)
return rc;
+ if (!new_rlim)
+ return task_has_perm(current, p, PROCESS__PTRACE);
/* 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_apply_creds. */
- if (old_rlim->rlim_max != new_rlim->rlim_max)
+ else if (p == current && old_rlim->rlim_max != new_rlim->rlim_max)
return task_has_perm(current, current, PROCESS__SETRLIMIT);
return 0;
@@ -4349,7 +4356,7 @@ static struct security_operations selinu
.task_getsid = selinux_task_getsid,
.task_setgroups = selinux_task_setgroups,
.task_setnice = selinux_task_setnice,
- .task_setrlimit = selinux_task_setrlimit,
+ .task_rlimit = selinux_task_rlimit,
.task_setscheduler = selinux_task_setscheduler,
.task_getscheduler = selinux_task_getscheduler,
.task_kill = selinux_task_kill,
_
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2.6.13-rc7 2/2] New Syscall: set rlimits of any process (reworked)
2005-08-26 3:34 [PATCH 2.6.13-rc7 1/2] New Syscall: get rlimits of any process (reworked) Wieland Gmeiner
@ 2005-08-26 3:39 ` Wieland Gmeiner
2005-08-26 15:34 ` Alan Cox
2005-08-31 12:46 ` [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate) Wieland Gmeiner
1 sibling, 1 reply; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-26 3:39 UTC (permalink / raw)
To: linux-kernel; +Cc: Elliot Lee
This is the second of two patches, it implements the setprlimit()
syscall.
Implementation: This patch provides a new syscall setprlimit() for
writing a given process resource limits for i386. Its implementation
follows closely the setrlimit syscall. It is given a pid as an
additional argument. If the given pid equals zero the current process
rlimits are written and the behaviour resembles the behaviour of
setrlimit. Otherwise some checking on the validity of the given pid is
done and if the given process is found access is granted if
- the calling process holds the CAP_SYS_PTRACE capability or
- the calling process uid equals the uid, euid, suid of the target
process and the calling process gid equals the gid, egid, sgid of
the target process.
(This resembles the behaviour of the ptrace system call.)
Simple programs for testing the syscalls can be found on
http://stud4.tuwien.ac.at/~e8607062/studies/soc/patches/
Signed-off-by: Wieland Gmeiner <e8607062@student.tuwien.ac.at>
---
arch/i386/kernel/syscall_table.S | 1
include/asm-i386/unistd.h | 3 -
kernel/sys.c | 114 ++++++++++++++++++++++++---------------
security/selinux/hooks.c | 14 +++-
4 files changed, 85 insertions(+), 47 deletions(-)
diff -puN arch/i386/kernel/syscall_table.S~setprlimit arch/i386/kernel/syscall_table.S
--- linux-2.6.13-rc7/arch/i386/kernel/syscall_table.S~setprlimit 2005-08-26 05:09:13.000000000 +0200
+++ linux-2.6.13-rc7-wieland/arch/i386/kernel/syscall_table.S 2005-08-26 05:09:34.000000000 +0200
@@ -295,3 +295,4 @@ ENTRY(sys_call_table)
.long sys_inotify_add_watch
.long sys_inotify_rm_watch
.long sys_getprlimit
+ .long sys_setprlimit /* 295 */
diff -puN include/asm-i386/unistd.h~setprlimit include/asm-i386/unistd.h
--- linux-2.6.13-rc7/include/asm-i386/unistd.h~setprlimit 2005-08-26 05:09:13.000000000 +0200
+++ linux-2.6.13-rc7-wieland/include/asm-i386/unistd.h 2005-08-26 05:09:34.000000000 +0200
@@ -300,8 +300,9 @@
#define __NR_inotify_add_watch 292
#define __NR_inotify_rm_watch 293
#define __NR_getprlimit 294
+#define __NR_setprlimit 295
-#define NR_syscalls 295
+#define NR_syscalls 296
/*
* user-visible error numbers are in the range -1 - -128: see
diff -puN kernel/sys.c~setprlimit kernel/sys.c
--- linux-2.6.13-rc7/kernel/sys.c~setprlimit 2005-08-26 05:09:13.000000000 +0200
+++ linux-2.6.13-rc7-wieland/kernel/sys.c 2005-08-26 05:09:34.000000000 +0200
@@ -1600,6 +1600,78 @@ asmlinkage long sys_getrlimit(unsigned i
return rlim_do_getprlimit(0, resource, rlim);
}
+static inline long rlim_do_setprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ struct rlimit new_rlim, *old_rlim;
+ int retval;
+ task_t *p;
+
+ if (resource >= RLIM_NLIMITS)
+ return -EINVAL;
+ if (pid < 0)
+ return -EINVAL;
+ if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
+ return -EFAULT;
+ if (new_rlim.rlim_cur > new_rlim.rlim_max)
+ return -EINVAL;
+
+ retval = -ESRCH;
+ read_lock(&tasklist_lock);
+ if (pid == 0) {
+ p = current;
+ } else {
+ p = find_task_by_pid(pid);
+ }
+ if (p) {
+ retval = -EPERM;
+ if (pid && !prlim_check_perm(p))
+ goto out;
+
+ old_rlim = p->signal->rlim + resource;
+ if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
+ !capable(CAP_SYS_RESOURCE))
+ goto out;
+ if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
+ goto out;
+
+ retval = security_task_rlimit(p, resource, &new_rlim);
+ if (retval)
+ goto out;
+
+ task_lock(p->group_leader);
+ *old_rlim = new_rlim;
+ task_unlock(p->group_leader);
+
+ if (resource == RLIMIT_CPU &&
+ new_rlim.rlim_cur != RLIM_INFINITY &&
+ (cputime_eq(p->signal->it_prof_expires, cputime_zero) ||
+ new_rlim.rlim_cur <= cputime_to_secs(
+ p->signal->it_prof_expires))) {
+ cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
+ spin_lock_irq(&p->sighand->siglock);
+ set_process_cpu_timer(p, CPUCLOCK_PROF,
+ &cputime, NULL);
+ spin_unlock_irq(&p->sighand->siglock);
+ }
+ }
+
+out:
+ read_unlock(&tasklist_lock);
+ return retval;
+}
+
+asmlinkage long sys_setprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ return rlim_do_setprlimit(pid, resource, rlim);
+}
+
+asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
+{
+ return rlim_do_setprlimit(0, resource, rlim);
+}
+
#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
/*
@@ -1624,48 +1696,6 @@ asmlinkage long sys_old_getrlimit(unsign
#endif
-asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
-{
- struct rlimit new_rlim, *old_rlim;
- int retval;
-
- if (resource >= RLIM_NLIMITS)
- return -EINVAL;
- if(copy_from_user(&new_rlim, rlim, sizeof(*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 > NR_OPEN)
- return -EPERM;
-
- retval = security_task_rlimit(0, resource, &new_rlim);
- if (retval)
- return retval;
-
- task_lock(current->group_leader);
- *old_rlim = new_rlim;
- task_unlock(current->group_leader);
-
- if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
- (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
- new_rlim.rlim_cur <= cputime_to_secs(
- current->signal->it_prof_expires))) {
- cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
- read_lock(&tasklist_lock);
- spin_lock_irq(¤t->sighand->siglock);
- set_process_cpu_timer(current, CPUCLOCK_PROF,
- &cputime, NULL);
- spin_unlock_irq(¤t->sighand->siglock);
- read_unlock(&tasklist_lock);
- }
-
- return 0;
-}
-
/*
* It would make sense to put struct rusage in the task_struct,
* except that would make the task_struct be *really big*. After
diff -puN security/selinux/hooks.c~setprlimit security/selinux/hooks.c
--- linux-2.6.13-rc7/security/selinux/hooks.c~setprlimit 2005-08-26 05:09:13.000000000 +0200
+++ linux-2.6.13-rc7-wieland/security/selinux/hooks.c 2005-08-26 05:09:34.000000000 +0200
@@ -2710,21 +2710,27 @@ static int selinux_task_rlimit(struct ta
struct rlimit *old_rlim = p->signal->rlim + resource;
int rc;
+ /* getprlimit */
if (!new_rlim)
rc = secondary_ops->task_rlimit(p, resource, 0);
- else
+ /* setrlimit */
+ else if (p == current)
rc = secondary_ops->task_rlimit(0, resource, new_rlim);
+ /* setprlimit */
+ else
+ rc = secondary_ops->task_rlimit(p, resource, new_rlim);
if (rc)
return rc;
- if (!new_rlim)
- return task_has_perm(current, p, PROCESS__PTRACE);
/* 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_apply_creds. */
- else if (p == current && old_rlim->rlim_max != new_rlim->rlim_max)
+ if (p == current && new_rlim &&
+ old_rlim->rlim_max != new_rlim->rlim_max)
return task_has_perm(current, current, PROCESS__SETRLIMIT);
+ else
+ return task_has_perm(current, p, PROCESS__PTRACE);
return 0;
}
_
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2.6.13-rc7 2/2] New Syscall: set rlimits of any process (reworked)
2005-08-26 3:39 ` [PATCH 2.6.13-rc7 2/2] New Syscall: set " Wieland Gmeiner
@ 2005-08-26 15:34 ` Alan Cox
2005-08-28 21:39 ` Wieland Gmeiner
0 siblings, 1 reply; 7+ messages in thread
From: Alan Cox @ 2005-08-26 15:34 UTC (permalink / raw)
To: e8607062; +Cc: linux-kernel, Elliot Lee
On Gwe, 2005-08-26 at 05:39 +0200, Wieland Gmeiner wrote:
> This is the second of two patches, it implements the setprlimit()
> syscall.
>
> Implementation: This patch provides a new syscall setprlimit() for
> writing a given process resource limits for i386. Its implementation
> follows closely the setrlimit syscall. It is given a pid as an
While looking at this have you considered 64bit rlimits on a 32bit box.
If a new API is going to be added it would be a good time to fix the
fact that some limits should be 64bit nowdays and have
setrlimit() existing legacy/standards API
setprlimit64() with size fixed and ability to specify process
Any thoughts on this ?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2.6.13-rc7 2/2] New Syscall: set rlimits of any process (reworked)
2005-08-26 15:34 ` Alan Cox
@ 2005-08-28 21:39 ` Wieland Gmeiner
0 siblings, 0 replies; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-28 21:39 UTC (permalink / raw)
To: Alan Cox; +Cc: linux-kernel, Elliot Lee
On Fri, 2005-08-26 at 16:34 +0100, Alan Cox wrote:
> On Gwe, 2005-08-26 at 05:39 +0200, Wieland Gmeiner wrote:
> > This is the second of two patches, it implements the setprlimit()
> > syscall.
> >
> > Implementation: This patch provides a new syscall setprlimit() for
> > writing a given process resource limits for i386. Its implementation
> > follows closely the setrlimit syscall. It is given a pid as an
>
>
> While looking at this have you considered 64bit rlimits on a 32bit box.
> If a new API is going to be added it would be a good time to fix the
> fact that some limits should be 64bit nowdays and have
>
> setrlimit() existing legacy/standards API
> setprlimit64() with size fixed and ability to specify process
>
> Any thoughts on this ?
Not yet, but thanks for the hint.
Thanks,
Wieland
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate)
2005-08-26 3:34 [PATCH 2.6.13-rc7 1/2] New Syscall: get rlimits of any process (reworked) Wieland Gmeiner
2005-08-26 3:39 ` [PATCH 2.6.13-rc7 2/2] New Syscall: set " Wieland Gmeiner
@ 2005-08-31 12:46 ` Wieland Gmeiner
2005-08-31 12:51 ` [PATCH 2.6.13 1/2] " Wieland Gmeiner
2005-08-31 12:52 ` [PATCH 2.6.13 2/2] " Wieland Gmeiner
1 sibling, 2 replies; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-31 12:46 UTC (permalink / raw)
To: linux-kernel; +Cc: Elliot Lee
Hi all!
Just for the logs, the getprlimit/setprlimit system call for the
2.6.13 plus a man page in case sombody cares.
Again my request to those who reviewed my first try, maybe you could
have a look at this if locking is better now, I do not break SELinux
anymore etc. so I can shift to the 64 Bit reimplementation Alan
suggested without introducing the same bugs again.
Thanks,
Wieland
The man page for this version of get/setprlimit():
Signed-off-by: Wieland Gmeiner <e8607062@student.tuwien.ac.at>
---------------------------------------------------------------------
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
.\" Copyright (c) 2002 Michael Kerrisk
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified by Michael Haardt <michael@moria.de>
.\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1996-01-13 by Arnt Gulbrandsen <agulbra@troll.no>
.\" Modified 1996-01-22 by aeb, following a remark by
.\" Tigran Aivazian <tigran@sco.com>
.\" Modified 1996-04-14 by aeb, following a remark by
.\" Robert Bihlmeyer <robbe@orcus.ping.at>
.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 2001-05-04 by aeb, following a remark by
.\" Havard Lygre <hklygre@online.no>
.\" Modified 2001-04-17 by Michael Kerrisk <mtk-manpages@gmx.net>
.\" Modified 2002-06-13 by Michael Kerrisk <mtk-manpages@gmx.net>
.\" Added note on non-standard behaviour when SIGCHLD is ignored.
.\" Modified 2002-07-09 by Michael Kerrisk <mtk-manpages@gmx.net>
.\" Enhanced descriptions of 'resource' values for [gs]etrlimit()
.\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
.\" Modified 2004-03-26 by aeb, added RLIMIT_AS
.\" Modified 2004-06-16 by Michael Kerrisk <mtk-manpages@gmx.net>
.\" Added notes on CAP_SYS_RESOURCE
.\"
.\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
.\" coverage of getrusage(2), has been split, so that the latter
.\" is now covered in its own getrusage.2.
.\"
.\" Modified 2004-11-16, mtk: A few other minor changes
.\" Modified 2004-11-23, mtk
.\" Added notes on RLIMIT_MEMLOCK, RLIMIT_NPROC, and RLIMIT_RSS
.\" to "CONFORMING TO"
.\" Modified 2004-11-25, mtk
.\" Rewrote discussion on RLIMIT_MEMLOCK to incorporate kernel
.\" 2.6.9 changes.
.\" Added note on RLIMIT_CPU error in older kernels
.\" 2004-11-03, mtk
.\" Added RLIMIT_SIGPENDING
.\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
.\"
.\" Modified 2005-08-30 by Wieland Gmeiner <e8607062@student.tuwien.ac.at>
.\" Adapted to describe getprlimit() and setprlimit().
.\"
.TH GETPRLIMIT 2 2005-08-30 "Linux" "Linux Programmer's Manual"
.SH NAME
getprlimit, setprlimit \- get/set resource limits of a process
.SH SYNOPSIS
.B #include <sys/time.h>
.br
.B #include <sys/resource.h>
.sp
.BI "int getprlimit(pid_t " pid ", int " resource ", struct rlimit *" rlim );
.br
.BI "int setprlimit(pid_t " pid ", int " resource ", const struct rlimit *" rlim );
.SH DESCRIPTION
.BR getprlimit ()
and
.BR setprlimit ()
respectively get and set resource limits of the process specified by
.IR pid .
If
.I pid
is zero, the process ID of the current process is used. Otherwise the
user IDs and group IDs of the calling process must match user IDs and group IDs
of the process with process ID
.I pid
or the calling process must hold the
.B CAP_SYS_PTRACE
capability.
Each resource has an associated soft and hard limit, as defined by the
.B rlimit
structure (the
.I rlim
argument to both
.BR getprlimit "() and " setprlimit ()):
.PP
.in +0.5i
.nf
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};
.fi
.in -0.5i
The soft limit is the value that the kernel enforces for the
corresponding resource.
The hard limit acts as a ceiling for the soft limit:
an unprivileged process may only set the soft limit to a value in the
range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
A privileged process (under Linux: one with the
.B CAP_SYS_RESOURCE
capability) may make arbitrary changes to either limit value.
.PP
The value
.B RLIM_INFINITY
denotes no limit on a resource (both in the structure returned by
.BR getprlimit ()
and in the structure passed to
.BR setprlimit ()).
.PP
.I resource
must be one of:
.TP
.B RLIMIT_AS
The maximum size of the process's virtual memory (address space) in bytes.
.\" since 2.0.27 / 2.1.12
This limit affects calls to
.BR brk (2),
.BR mmap (2)
and
.BR mremap (2),
which fail with the error
.B ENOMEM
upon exceeding this limit. Also automatic stack expansion will fail
(and generate a
.B SIGSEGV
that kills the process if no alternate stack
has been made available via
.BR sigaltstack (2)).
Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
either this limit is at most 2 GiB, or this resource is unlimited.
.TP
.B RLIMIT_CORE
Maximum size of
.I core
file. When 0 no core dump files are created.
When non-zero, larger dumps are truncated to this size.
.TP
.B RLIMIT_CPU
CPU time limit in seconds.
When the process reaches the soft limit, it is sent a
.B SIGXCPU
signal.
The default action for this signal is to terminate the process.
However, the signal can be caught, and the handler can return control to
the main program.
If the process continues to consume CPU time, it will be sent
.B SIGXCPU
once per second until the hard limit is reached, at which time
it is sent
.BR SIGKILL .
(This latter point describes Linux 2.2 through 2.6 behaviour.
Implementations vary in how they treat processes which continue to
consume CPU time after reaching the soft limit.
Portable applications that need to catch this signal should
perform an orderly termination upon first receipt of
.BR SIGXCPU .)
.TP
.B RLIMIT_DATA
The maximum size of the process's data segment (initialized data,
uninitialized data, and heap).
This limit affects calls to
.BR brk "() and " sbrk (),
which fail with the error
.B ENOMEM
upon encountering the soft limit of this resource.
.TP
.B RLIMIT_FSIZE
The maximum size of files that the process may create.
Attempts to extend a file beyond this limit result in delivery of a
.B SIGXFSZ
signal.
By default, this signal terminates a process, but a process can
catch this signal instead, in which case the relevant system call (e.g.,
.BR write "(), " truncate ())
fails with the error
.BR EFBIG .
.TP
.BR RLIMIT_LOCKS " (Early Linux 2.4 only)"
.\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
A limit on the combined number of
.BR flock ()
locks and
.BR fcntl()
leases that this process may establish.
.TP
.B RLIMIT_MEMLOCK
The maximum number of bytes of memory that may be locked
into RAM.
In effect this limit is rounded down to the nearest multiple
of the system page size.
This limit affects
.BR mlock "(2) and " mlockall (2)
and the
.BR mmap (2)
.B MAP_LOCKED
operation.
Since Linux 2.6.9 it also affects the
.BR shmctl (2)
.B SHM_LOCK
operation, where it sets a maximum on the total bytes in
shared memory segments (see
.BR shmget (2))
that may be locked by the real user ID of the calling process.
The
.BR shmctl (2)
.B SHM_LOCK
locks are accounted for separately from the per-process memory
locks established by
.BR mlock "(2), " mlockall (2),
and
.BR mmap (2)
.BR MAP_LOCKED ;
a process can lock bytes up to this limit in each of these
two categories.
In Linux kernels before 2.6.9, this limit controlled the amount of
memory that could be locked by a privileged process.
Since Linux 2.6.9, no limits are placed on the amount of memory
that a privileged process may lock, and this limit instead governs
the amount of memory that an unprivileged process may lock.
.TP
.BR RLIMIT_MSGQUEUE " (Since Linux 2.6.8)"
Specifies the limit on the number of bytes that can be allocated
for POSIX message queues for the real user ID of the calling process.
This limit is enforced for
.BR mq_open (3).
.\" FIXME there is no mq_open.3 page yet
Each message queue that the user creates counts (until it is removed)
against this limit according to the formula:
.nf
bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
attr.mq_maxmsg * attr.mq_msgsize)
.fi
where
.I attr
is the
.I mq_attr
structure specified as the fourth argument to
.BR mq_open ().
The first addend in the formula, which includes
.I "sizeof(struct msg_msg *)"
(4 bytes on Linux/x86), ensures that the user cannot
create an unlimited number of zero-length messages (such messages
nevertheless each consume some system memory for bookkeeping overhead).
.TP
.B RLIMIT_NOFILE
Specifies a value one greater than the maximum file descriptor number
that can be opened by this process.
Attempts
.RB ( open "(), " pipe "(), " dup "(), etc.)"
to exceed this limit yield the error
.BR EMFILE .
.TP
.B RLIMIT_NPROC
The maximum number of processes that can be created for the real user
ID of the calling process.
Upon encountering this limit,
.BR fork ()
fails with the error
.BR EAGAIN .
.TP
.B RLIMIT_RSS
Specifies the limit (in pages) of the process's resident set
(the number of virtual pages resident in RAM).
This limit only has effect in Linux 2.4.x, x < 30, and there only
affects calls to
.BR madvise ()
specifying
.BR MADV_WILLNEED .
.\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
.\" talk of making it do something has surfaced from time to time in LKML
.\" -- MTK, Jul 05
.TP
.BR RLIMIT_SIGPENDING " (Since Linux 2.6.8)"
Specifies the limit on the number of signals
that may be queued for the real user ID of the calling process.
Both standard and real-time signals are counted for the purpose of
checking this limit.
However, the limit is only enforced for
.BR sigqueue (2);
it is always possible to use
.BR kill (2)
to queue one instance of any of the signals that are not already
queued to the process.
.\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
.\" that was present in kernels <= 2.6.7. MTK Dec 04
.TP
.B RLIMIT_STACK
The maximum size of the process stack, in bytes.
Upon reaching this limit, a
.B SIGSEGV
signal is generated.
To handle this signal, a process must employ an alternate signal stack
.RB ( sigaltstack (2)).
.PP
.B RLIMIT_OFILE
is the BSD name for
.BR RLIMIT_NOFILE .
.SH "RETURN VALUE"
On success, zero is returned. On error, \-1 is returned, and
.I errno
is set appropriately.
.SH ERRORS
.TP
.B ESRCH
.I pid
is not valid.
.TP
.B EFAULT
.I rlim
points outside the accessible address space.
.TP
.B EINVAL
.I resource
is not valid.
.TP
.B EPERM
An unprivileged process tried to use \fBsetprlimit()\fP to
increase a soft or hard limit above the current hard limit; the
.B CAP_SYS_RESOURCE
capability is required to do this.
Or, the process tried to use \fBsetprlimit()\fP to increase
the soft or hard RLIMIT_NOFILE limit above the current kernel
maximum (NR_OPEN). Or, the process tried to read or alter a
resource limit of a process he is not privileged to access: User IDs
and group IDs must match or the
.B CAP_SYS_PTRACE
capability is required to do this.
.SH BUGS
In older Linux kernels, the
.B SIGXCPU
and
.B SIGKILL
signals delivered when a process encountered the soft and hard
.B RLIMIT_CPU
limits were delivered one (CPU) second later than they should have been.
This was fixed in kernel 2.6.8.
.SH "CONFORMING TO"
.BR getprlimit ()
and
.BR setprlimit ()
are Linux specific.
.BR RLIMIT_MEMLOCK
and
.BR RLIMIT_NPROC
derive from BSD and are not specified in POSIX.1-2001;
they are present on the BSDs and Linux, but on few other implementations.
.BR RLIMIT_RSS
derives from BSD and is not specified in POSIX.1-2001;
it is nevertheless present on most implementations.
.B RLIMIT_SIGPENDING
and
.B RLIMIT_MSGQUEUE
are Linux specific.
.SH "SEE ALSO"
.BR getrlimit (2),
.BR setrlimit (2),
.BR dup (2),
.BR fcntl (2),
.BR fork (2),
.BR getrusage (2),
.BR mlock (2),
.BR mmap (2),
.BR open (2),
.BR quotactl (2),
.BR sbrk (2),
.BR shmctl (2),
.BR sigqueue (2),
.BR malloc (3),
.BR ulimit (3),
.BR capabilities (7),
.BR signal (7)
---------------------------------------------------------------------
and for setprlimit():
---------------------------------------------------------------------
.so man2/getprlimit.2
---------------------------------------------------------------------
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2.6.13 1/2] New Syscall: get/set rlimits of any process (udate)
2005-08-31 12:46 ` [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate) Wieland Gmeiner
@ 2005-08-31 12:51 ` Wieland Gmeiner
2005-08-31 12:52 ` [PATCH 2.6.13 2/2] " Wieland Gmeiner
1 sibling, 0 replies; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-31 12:51 UTC (permalink / raw)
To: linux-kernel; +Cc: Elliot Lee
The patch for the getprlimit() syscall:
Signed-off-by: Wieland Gmeiner <e8607062@student.tuwien.ac.at>
---
arch/i386/kernel/syscall_table.S | 1
include/asm-i386/unistd.h | 3 -
include/linux/security.h | 25 ++++++-----
kernel/sys.c | 85 +++++++++++++++++++++++++++++++++------
security/dummy.c | 5 +-
security/selinux/hooks.c | 17 +++++--
6 files changed, 107 insertions(+), 29 deletions(-)
diff -puN arch/i386/kernel/syscall_table.S~getprlimit arch/i386/kernel/syscall_table.S
--- linux-2.6.13/arch/i386/kernel/syscall_table.S~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/arch/i386/kernel/syscall_table.S 2005-08-31 02:29:57.000000000 +0200
@@ -294,3 +294,4 @@ ENTRY(sys_call_table)
.long sys_inotify_init
.long sys_inotify_add_watch
.long sys_inotify_rm_watch
+ .long sys_getprlimit
diff -puN include/asm-i386/unistd.h~getprlimit include/asm-i386/unistd.h
--- linux-2.6.13/include/asm-i386/unistd.h~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/include/asm-i386/unistd.h 2005-08-31 02:30:43.000000000 +0200
@@ -299,8 +299,9 @@
#define __NR_inotify_init 291
#define __NR_inotify_add_watch 292
#define __NR_inotify_rm_watch 293
+#define __NR_getprlimit 294
-#define NR_syscalls 294
+#define NR_syscalls 295
/*
* user-visible error numbers are in the range -1 - -128: see
diff -puN include/linux/security.h~getprlimit include/linux/security.h
--- linux-2.6.13/include/linux/security.h~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/include/linux/security.h 2005-08-31 02:37:52.000000000 +0200
@@ -584,10 +584,12 @@ struct swap_info_struct;
* @p contains the task_struct of process.
* @nice contains the new nice value.
* Return 0 if permission is granted.
- * @task_setrlimit:
- * Check permission before setting the resource limits of the current
- * process for @resource to @new_rlim. The old resource limit values can
- * be examined by dereferencing (current->signal->rlim + resource).
+ * @task_rlimit:
+ * Check permission before reading the resource limits of the process @p
+ * for @resource or setting the limits to @new_rlim. The old resource
+ * limit values can be examined by dereferencing
+ * (p->signal->rlim + resource).
+ * @p contains the task_struct for the process.
* @resource contains the resource whose limit is being set.
* @new_rlim contains the new limits for @resource.
* Return 0 if permission is granted.
@@ -1156,7 +1158,8 @@ struct security_operations {
int (*task_getsid) (struct task_struct * p);
int (*task_setgroups) (struct group_info *group_info);
int (*task_setnice) (struct task_struct * p, int nice);
- int (*task_setrlimit) (unsigned int resource, struct rlimit * new_rlim);
+ int (*task_rlimit) (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);
@@ -1798,10 +1801,11 @@ static inline int security_task_setnice
return security_ops->task_setnice (p, nice);
}
-static inline int security_task_setrlimit (unsigned int resource,
- struct rlimit *new_rlim)
+static inline int security_task_rlimit (struct task_struct *p,
+ unsigned int resource,
+ struct rlimit *new_rlim)
{
- return security_ops->task_setrlimit (resource, new_rlim);
+ return security_ops->task_rlimit (p, resource, new_rlim);
}
static inline int security_task_setscheduler (struct task_struct *p,
@@ -2447,8 +2451,9 @@ static inline int security_task_setnice
return 0;
}
-static inline int security_task_setrlimit (unsigned int resource,
- struct rlimit *new_rlim)
+static inline int security_task_rlimit (struct task_struct *p,
+ unsigned int resource,
+ struct rlimit *new_rlim)
{
return 0;
}
diff -puN kernel/sys.c~getprlimit kernel/sys.c
--- linux-2.6.13/kernel/sys.c~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/kernel/sys.c 2005-08-31 02:42:12.000000000 +0200
@@ -1524,17 +1524,80 @@ asmlinkage long sys_setdomainname(char _
return errno;
}
-asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
+/*
+ * get/setprlimit()
+ *
+ * As ptrace implies the ability to execute arbitrary code in the given
+ * process, which means that the calling process could obtain and set
+ * rlimits for that process without getprlimit/setprlimit anyways,
+ * we use the same permission checks as ptrace.
+ */
+static inline int prlim_check_perm(task_t *task)
{
- if (resource >= RLIM_NLIMITS)
- return -EINVAL;
- else {
- struct rlimit value;
- task_lock(current->group_leader);
- value = current->signal->rlim[resource];
- task_unlock(current->group_leader);
- return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
+ return ((current->uid == task->euid) &&
+ (current->uid == task->suid) &&
+ (current->uid == task->uid) &&
+ (current->gid == task->egid) &&
+ (current->gid == task->sgid) &&
+ (current->gid == task->gid)) || capable(CAP_SYS_PTRACE);
+}
+
+static inline long rlim_do_getprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ struct rlimit value;
+ task_t *p;
+ int retval = -EINVAL;
+
+ if (resource >= RLIM_NLIMITS)
+ goto out;
+
+ if (pid < 0)
+ goto out;
+
+ retval = -ESRCH;
+ read_lock(&tasklist_lock);
+ if (!pid) {
+ p = current;
+ } else {
+ p = find_task_by_pid(pid);
}
+ if (p) {
+ retval = -EPERM;
+ if (pid && !prlim_check_perm(p))
+ goto unlock_out;
+ if (pid) {
+ retval = security_task_rlimit(p, resource, 0);
+ if (retval)
+ goto unlock_out;
+ }
+ task_lock(p->group_leader);
+ value = p->signal->rlim[resource];
+ task_unlock(p->group_leader);
+ } else
+ goto unlock_out;
+
+ read_unlock(&tasklist_lock);
+
+ retval = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
+
+out:
+ return retval;
+
+unlock_out:
+ read_unlock(&tasklist_lock);
+ return retval;
+}
+
+asmlinkage long sys_getprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ return rlim_do_getprlimit(pid, resource, rlim);
+}
+
+asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
+{
+ return rlim_do_getprlimit(0, resource, rlim);
}
#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
@@ -1542,7 +1605,7 @@ asmlinkage long sys_getrlimit(unsigned i
/*
* Back compatibility for getrlimit. Needed for some apps.
*/
-
+
asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
{
struct rlimit x;
@@ -1579,7 +1642,7 @@ asmlinkage long sys_setrlimit(unsigned i
if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
return -EPERM;
- retval = security_task_setrlimit(resource, &new_rlim);
+ retval = security_task_rlimit(0, resource, &new_rlim);
if (retval)
return retval;
diff -puN security/dummy.c~getprlimit security/dummy.c
--- linux-2.6.13/security/dummy.c~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/security/dummy.c 2005-08-31 02:44:06.000000000 +0200
@@ -543,7 +543,8 @@ static int dummy_task_setnice (struct ta
return 0;
}
-static int dummy_task_setrlimit (unsigned int resource, struct rlimit *new_rlim)
+static int dummy_task_rlimit (struct task_struct *p, unsigned int resource,
+ struct rlimit *new_rlim)
{
return 0;
}
@@ -936,7 +937,7 @@ void security_fixup_ops (struct security
set_to_dummy_if_null(ops, task_getsid);
set_to_dummy_if_null(ops, task_setgroups);
set_to_dummy_if_null(ops, task_setnice);
- set_to_dummy_if_null(ops, task_setrlimit);
+ set_to_dummy_if_null(ops, task_rlimit);
set_to_dummy_if_null(ops, task_setscheduler);
set_to_dummy_if_null(ops, task_getscheduler);
set_to_dummy_if_null(ops, task_wait);
diff -puN security/selinux/hooks.c~getprlimit security/selinux/hooks.c
--- linux-2.6.13/security/selinux/hooks.c~getprlimit 2005-08-31 02:09:48.000000000 +0200
+++ linux-2.6.13-wieland/security/selinux/hooks.c 2005-08-31 02:47:10.000000000 +0200
@@ -2703,20 +2703,27 @@ static int selinux_task_setnice(struct t
return task_has_perm(current,p, PROCESS__SETSCHED);
}
-static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
+static int selinux_task_rlimit(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;
int rc;
- rc = secondary_ops->task_setrlimit(resource, new_rlim);
+ if (!new_rlim)
+ rc = secondary_ops->task_rlimit(p, resource, 0);
+ else
+ rc = secondary_ops->task_rlimit(0, resource, new_rlim);
if (rc)
return rc;
+ if (!new_rlim)
+ return task_has_perm(current, p, PROCESS__PTRACE);
/* 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_apply_creds. */
- if (old_rlim->rlim_max != new_rlim->rlim_max)
+ else if (p == current && old_rlim->rlim_max != new_rlim->rlim_max)
return task_has_perm(current, current, PROCESS__SETRLIMIT);
return 0;
@@ -4349,7 +4356,7 @@ static struct security_operations selinu
.task_getsid = selinux_task_getsid,
.task_setgroups = selinux_task_setgroups,
.task_setnice = selinux_task_setnice,
- .task_setrlimit = selinux_task_setrlimit,
+ .task_rlimit = selinux_task_rlimit,
.task_setscheduler = selinux_task_setscheduler,
.task_getscheduler = selinux_task_getscheduler,
.task_kill = selinux_task_kill,
_
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2.6.13 2/2] New Syscall: get/set rlimits of any process (udate)
2005-08-31 12:46 ` [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate) Wieland Gmeiner
2005-08-31 12:51 ` [PATCH 2.6.13 1/2] " Wieland Gmeiner
@ 2005-08-31 12:52 ` Wieland Gmeiner
1 sibling, 0 replies; 7+ messages in thread
From: Wieland Gmeiner @ 2005-08-31 12:52 UTC (permalink / raw)
To: linux-kernel; +Cc: Elliot Lee
The patch for the setprlimit() syscall:
Signed-off-by: Wieland Gmeiner <e8607062@student.tuwien.ac.at>
---
arch/i386/kernel/syscall_table.S | 1
include/asm-i386/unistd.h | 3 -
kernel/sys.c | 114 ++++++++++++++++++++++++---------------
security/selinux/hooks.c | 14 +++-
4 files changed, 85 insertions(+), 47 deletions(-)
diff -puN arch/i386/kernel/syscall_table.S~setprlimit arch/i386/kernel/syscall_table.S
--- linux-2.6.13/arch/i386/kernel/syscall_table.S~setprlimit 2005-08-31 02:56:22.000000000 +0200
+++ linux-2.6.13-wieland/arch/i386/kernel/syscall_table.S 2005-08-31 02:58:31.000000000 +0200
@@ -295,3 +295,4 @@ ENTRY(sys_call_table)
.long sys_inotify_add_watch
.long sys_inotify_rm_watch
.long sys_getprlimit
+ .long sys_setprlimit /* 295 */
diff -puN include/asm-i386/unistd.h~setprlimit include/asm-i386/unistd.h
--- linux-2.6.13/include/asm-i386/unistd.h~setprlimit 2005-08-31 02:56:22.000000000 +0200
+++ linux-2.6.13-wieland/include/asm-i386/unistd.h 2005-08-31 02:59:16.000000000 +0200
@@ -300,8 +300,9 @@
#define __NR_inotify_add_watch 292
#define __NR_inotify_rm_watch 293
#define __NR_getprlimit 294
+#define __NR_setprlimit 295
-#define NR_syscalls 295
+#define NR_syscalls 296
/*
* user-visible error numbers are in the range -1 - -128: see
diff -puN kernel/sys.c~setprlimit kernel/sys.c
--- linux-2.6.13/kernel/sys.c~setprlimit 2005-08-31 02:56:22.000000000 +0200
+++ linux-2.6.13-wieland/kernel/sys.c 2005-08-31 03:02:47.000000000 +0200
@@ -1600,6 +1600,78 @@ asmlinkage long sys_getrlimit(unsigned i
return rlim_do_getprlimit(0, resource, rlim);
}
+static inline long rlim_do_setprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ struct rlimit new_rlim, *old_rlim;
+ int retval;
+ task_t *p;
+
+ if (resource >= RLIM_NLIMITS)
+ return -EINVAL;
+ if (pid < 0)
+ return -EINVAL;
+ if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
+ return -EFAULT;
+ if (new_rlim.rlim_cur > new_rlim.rlim_max)
+ return -EINVAL;
+
+ retval = -ESRCH;
+ read_lock(&tasklist_lock);
+ if (pid == 0) {
+ p = current;
+ } else {
+ p = find_task_by_pid(pid);
+ }
+ if (p) {
+ retval = -EPERM;
+ if (pid && !prlim_check_perm(p))
+ goto out;
+
+ old_rlim = p->signal->rlim + resource;
+ if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
+ !capable(CAP_SYS_RESOURCE))
+ goto out;
+ if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
+ goto out;
+
+ retval = security_task_rlimit(p, resource, &new_rlim);
+ if (retval)
+ goto out;
+
+ task_lock(p->group_leader);
+ *old_rlim = new_rlim;
+ task_unlock(p->group_leader);
+
+ if (resource == RLIMIT_CPU &&
+ new_rlim.rlim_cur != RLIM_INFINITY &&
+ (cputime_eq(p->signal->it_prof_expires, cputime_zero) ||
+ new_rlim.rlim_cur <= cputime_to_secs(
+ p->signal->it_prof_expires))) {
+ cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
+ spin_lock_irq(&p->sighand->siglock);
+ set_process_cpu_timer(p, CPUCLOCK_PROF,
+ &cputime, NULL);
+ spin_unlock_irq(&p->sighand->siglock);
+ }
+ }
+
+out:
+ read_unlock(&tasklist_lock);
+ return retval;
+}
+
+asmlinkage long sys_setprlimit(pid_t pid, unsigned int resource,
+ struct rlimit __user *rlim)
+{
+ return rlim_do_setprlimit(pid, resource, rlim);
+}
+
+asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
+{
+ return rlim_do_setprlimit(0, resource, rlim);
+}
+
#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
/*
@@ -1624,48 +1696,6 @@ asmlinkage long sys_old_getrlimit(unsign
#endif
-asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
-{
- struct rlimit new_rlim, *old_rlim;
- int retval;
-
- if (resource >= RLIM_NLIMITS)
- return -EINVAL;
- if(copy_from_user(&new_rlim, rlim, sizeof(*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 > NR_OPEN)
- return -EPERM;
-
- retval = security_task_rlimit(0, resource, &new_rlim);
- if (retval)
- return retval;
-
- task_lock(current->group_leader);
- *old_rlim = new_rlim;
- task_unlock(current->group_leader);
-
- if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
- (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
- new_rlim.rlim_cur <= cputime_to_secs(
- current->signal->it_prof_expires))) {
- cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
- read_lock(&tasklist_lock);
- spin_lock_irq(¤t->sighand->siglock);
- set_process_cpu_timer(current, CPUCLOCK_PROF,
- &cputime, NULL);
- spin_unlock_irq(¤t->sighand->siglock);
- read_unlock(&tasklist_lock);
- }
-
- return 0;
-}
-
/*
* It would make sense to put struct rusage in the task_struct,
* except that would make the task_struct be *really big*. After
diff -puN security/selinux/hooks.c~setprlimit security/selinux/hooks.c
--- linux-2.6.13/security/selinux/hooks.c~setprlimit 2005-08-31 02:56:22.000000000 +0200
+++ linux-2.6.13-wieland/security/selinux/hooks.c 2005-08-31 03:07:22.000000000 +0200
@@ -2710,21 +2710,27 @@ static int selinux_task_rlimit(struct ta
struct rlimit *old_rlim = p->signal->rlim + resource;
int rc;
+ /* getprlimit */
if (!new_rlim)
rc = secondary_ops->task_rlimit(p, resource, 0);
- else
+ /* setrlimit */
+ else if (p == current)
rc = secondary_ops->task_rlimit(0, resource, new_rlim);
+ /* setprlimit */
+ else
+ rc = secondary_ops->task_rlimit(p, resource, new_rlim);
if (rc)
return rc;
- if (!new_rlim)
- return task_has_perm(current, p, PROCESS__PTRACE);
/* 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_apply_creds. */
- else if (p == current && old_rlim->rlim_max != new_rlim->rlim_max)
+ if (p == current && new_rlim &&
+ old_rlim->rlim_max != new_rlim->rlim_max)
return task_has_perm(current, current, PROCESS__SETRLIMIT);
+ else
+ return task_has_perm(current, p, PROCESS__PTRACE);
return 0;
}
_
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2005-08-31 12:52 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-26 3:34 [PATCH 2.6.13-rc7 1/2] New Syscall: get rlimits of any process (reworked) Wieland Gmeiner
2005-08-26 3:39 ` [PATCH 2.6.13-rc7 2/2] New Syscall: set " Wieland Gmeiner
2005-08-26 15:34 ` Alan Cox
2005-08-28 21:39 ` Wieland Gmeiner
2005-08-31 12:46 ` [PATCH 2.6.13 0/2] New Syscall: get/set rlimits of any process (udate) Wieland Gmeiner
2005-08-31 12:51 ` [PATCH 2.6.13 1/2] " Wieland Gmeiner
2005-08-31 12:52 ` [PATCH 2.6.13 2/2] " Wieland Gmeiner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox