Skip to contentSkip to conversation

New Syscall: get rlimits of any process

PATCH In review Wieland Gmeiner 4 participants 7 messages Aug 12, 2005 – Aug 21, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 New Syscall: get rlimits of any process

Hi all!

I'm a participant in Googles Summer of Code Program, doing an entry level kernel project suggested by Fedora Core, "Getting and Setting Process Resource Limits out of Process Space".

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_RESOURCE capability or
- the calling process uid equals the uid of the process whose rlimit is being read or
- the calling process uid equals the suid of the process whose rlimit is being read or
- the calling process euid equals the uid of the process whose rlimit is being read or
- the calling process euid equals the suid of the process whose rlimit is being read

See the followup for the writing syscall. Additionally a /proc/pid/rlimits interface for comfortable access is under construction.

Simple programs for testing the syscalls can be found on
http://stud4.tuwien.ac.at/~e8607062/studies/soc/patches/

arch/i386/kernel/syscall_table.S +1 -0
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-vanilla/arch/i386/kernel/syscall_table.S linux-2.6.13-rc6-rlim/arch/i386/kernel/syscall_table.S
@@ -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
include/asm-i386/unistd.h +2 -1
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-vanilla/include/asm-i386/unistd.h linux-2.6.13-rc6-rlim/include/asm-i386/unistd.h
@@ -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
kernel/sys.c +41 -0

47 lines not on this page · raw message

#1 lore raw

PATCH 2/2 New Syscall: set rlimits of any process

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 write access is granted if

- the calling process holds the CAP_SYS_RESOURCE capability or
- the calling process uid equals the uid of the process whose rlimit is being written and
the calling process uid equals the euid of the process whose rlimit is being written
or
- the calling process euid equals the suid of the process whose rlimit is being written

arch/i386/kernel/syscall_table.S +1 -0
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-rlim/arch/i386/kernel/syscall_table.S linux-2.6.13-rc6-get_set_rlim/arch/i386/kernel/syscall_table.S
@@ -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 */
include/asm-i386/unistd.h +2 -1
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-rlim/include/asm-i386/unistd.h linux-2.6.13-rc6-get_set_rlim/include/asm-i386/unistd.h
@@ -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
include/linux/security.h +19 -1
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-rlim/include/linux/security.h linux-2.6.13-rc6-get_set_rlim/include/linux/security.h
@@ -591,7 +591,16 @@ struct swap_info_struct;
  *	@resource contains the resource whose limit is being set.
  *	@new_rlim contains the new limits for @resource.
  *	Return 0 if permission is granted.
- * @task_setscheduler:
+ * @task_setprlimit:
+ *	Check permission before setting the resource limits of process @p
+ *	for @resource to @new_rlim.  The old resource limit values can
+ *	be examined by dereferencing (p->signal->rlim + resource).
+ *	@p contains the task struct of the process whose resource limit is
+ *	being set.
+ *	@resource contains the resource whose limit is being set.
+ *	@new_rlim contains the new limits for @resource.
+ *	Return 0 if permission is granted.
+  @task_setscheduler:
  *	Check permission before setting scheduling policy and/or parameters of
  *	process @p based on @policy and @lp.
  *	@p contains the task_struct for process.
@@ -1157,6 +1166,8 @@ struct security_operations {
 	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_setprlimit) (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);
@@ -1804,6 +1815,13 @@ static inline int security_task_setrlimi
 	return security_ops->task_setrlimit (resource, new_rlim);
 }
 
+static inline int security_task_setprlimit (struct task_struct *p,
+                                            unsigned int resource,
+					    struct rlimit *new_rlim)
+{
+	return security_ops->task_setprlimit (p, resource, new_rlim);
+}
+
 static inline int security_task_setscheduler (struct task_struct *p,
 					      int policy,
 					      struct sched_param *lp)
kernel/sys.c +66 -0

72 lines not on this page · raw message

security/dummy.c +7 -0
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-rlim/security/dummy.c linux-2.6.13-rc6-get_set_rlim/security/dummy.c
@@ -548,6 +548,12 @@ static int dummy_task_setrlimit (unsigne
 	return 0;
 }
 
+static int dummy_task_setprlimit (struct task_struct *p, unsigned int resource,
+                                  struct rlimit *new_rlim)
+{
+	return 0;
+}
+
 static int dummy_task_setscheduler (struct task_struct *p, int policy,
 				    struct sched_param *lp)
 {
@@ -937,6 +943,7 @@ void security_fixup_ops (struct security
 	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_setprlimit);
 	set_to_dummy_if_null(ops, task_setscheduler);
 	set_to_dummy_if_null(ops, task_getscheduler);
 	set_to_dummy_if_null(ops, task_wait);
security/selinux/hooks.c +20 -0
diff -uprN -X linux-2.6.13-rc6-vanilla/Documentation/dontdiff linux-2.6.13-rc6-rlim/security/selinux/hooks.c linux-2.6.13-rc6-get_set_rlim/security/selinux/hooks.c
@@ -2722,6 +2722,26 @@ static int selinux_task_setrlimit(unsign
 	return 0;
 }
 
+static int selinux_task_setprlimit(struct task_struct *p, unsigned int resource,
+                                  struct rlimit *new_rlim)
+{
+	struct rlimit *old_rlim = p->signal->rlim + resource;
+	int rc;
+
+	rc = secondary_ops->task_setprlimit(p, resource, new_rlim);
+	if (rc)
+		return rc;
+
+	/* 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)
+		return task_has_perm(current, p, PROCESS__SETRLIMIT);
+
+	return 0;
+}
+
 static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
 {
 	return task_has_perm(current, p, PROCESS__SETSCHED);
#2 lore raw
In reply to Wieland Gmeiner

arch/i386/kernel/syscall_table.S ENTRY(sys_call_table) · line 297

Quoted 5 lines · Wieland Gmeiner
arch/i386/kernel/syscall_table.S +1 -0
@@ -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

Please follow the proper kernel coding style when writing new kernel code...

thanks,

greg k-h

#3 lore raw
Quoted 9 lines · Greg KH

arch/i386/kernel/syscall_table.S ENTRY(sys_call_table) · line 297

Quoted 5 lines · Wieland Gmeiner
arch/i386/kernel/syscall_table.S +1 -0
@@ -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

Please follow the proper kernel coding style when writing new kernel code...

Hm, Documentation/CodingStyle suggests using descriptive names, so something like getrlimit(...)/getrlimit_per_process(pid_t pid, ...) would be more appropriate?

I thought getrlimit(...)/getprlimit(pid_t pid, ...) would be a good choice as getgid(void)/getpgid(pid_t pid) already exists in Linux which have the same naming scheme.

Or would something like
getrlimit/getrlimitpid (like wait(void)/waitpid(pid)) or getrlimit/getrlimit2 (like getpgrp(void)/getpgrp2(pid) in HP-UX) be preferred?

What would you suggest?

Thanks,
Wieland

#4 lore raw
Quoted 19 lines · Wieland Gmeiner
Quoted 9 lines · Greg KH

arch/i386/kernel/syscall_table.S ENTRY(sys_call_table) · line 297

Quoted 5 lines · Wieland Gmeiner
arch/i386/kernel/syscall_table.S +1 -0
@@ -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

Please follow the proper kernel coding style when writing new kernel code...

Hm, Documentation/CodingStyle suggests using descriptive names, so something like getrlimit(...)/getrlimit_per_process(pid_t pid, ...) would be more appropriate?

I thought getrlimit(...)/getprlimit(pid_t pid, ...) would be a good choice as getgid(void)/getpgid(pid_t pid) already exists in Linux which have the same naming scheme.

Quoted 6 lines

Or would something like
getrlimit/getrlimitpid (like wait(void)/waitpid(pid)) or getrlimit/getrlimit2 (like getpgrp(void)/getpgrp2(pid) in HP-UX) be preferred?

What would you suggest?

You use tabs instead of spaces :)

thanks,

greg k-h

#5 lore raw
In reply to Wieland Gmeiner
Quoted 13 lines · Wieland Gmeiner
Quoted 11 lines · Greg KH

arch/i386/kernel/syscall_table.S ENTRY(sys_call_table) · line 297

Quoted 6 lines · Wieland Gmeiner
arch/i386/kernel/syscall_table.S +1 -0
@@ -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

Please follow the proper kernel coding style when writing new kernel code...

Quoted 3 lines

Hm, Documentation/CodingStyle suggests using descriptive names, so something like getrlimit(...)/getrlimit_per_process(pid_t pid, ...) would be more appropriate?

I think he was commenting more on the code indentation and braces placement than any naming issue. There was also a good guide to kernel whitespace posted to the LKML a week or so ago, please check the archives and review that as well.

I have one small comment on something you stated in your original mail:

Quoted 12 lines

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_RESOURCE capability or
- the calling process uid equals the uid of the process whose rlimit is being read or
- the calling process uid equals the suid of the process whose rlimit is being read or
- the calling process euid equals the uid of the process whose rlimit is being read or
- the calling process euid equals the suid of the process whose rlimit is being read

I suggest that you revise this list to the following:

Quoted 3 lines

If the calling process can ptrace the target process, then allow rlimits to be read and written such that the hard limits may not be raised unless one of the two processes possesses the CAP_SYS_RESOURCE capability

ptrace implies the ability to execute arbitrary code in the given process, which means that even without this new function the calling process theoretically could obtain and set rlimits for that process anyways, subject to its own CAP_SYS_RESOURCE capability. Such a situation would guarantee that there are no new security holes, and would limit the number of inter-process access rules which kernel developers need to understand. I believe some simple Googling and grepping through the kernel code should reveal the necessary ptrace-related process checks.

Cheers,
Kyle Moffett

--
There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.
-- C.A.R. Hoare

#6 lore raw
In reply to Wieland Gmeiner
Quoted 22 lines · Wieland Gmeiner

+asmlinkage long sys_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_nounlock;
+
+ if (pid < 0)
+ goto out_nounlock;
+
+ retval = -ESRCH;
+ if (pid == 0) {
+ p = current;
+ } else {
+ read_lock(&tasklist_lock);
+ p = find_task_by_pid(pid);
+ }
+ if (p) {
+ retval = -EPERM;

Quoted 8 lines

+ if ((current->euid ^ p->suid) && (current->euid ^ p->uid) &&
+ (current->uid ^ p->suid) && (current->uid ^ p->uid) &&
+ !capable(CAP_SYS_RESOURCE))
+ goto out_unlock;
+

 +                task_lock(p->group_leader);
 +                value = p->signal->rlim[resource];
 +                task_unlock(p->group_leader);

There isn't much point in taking task_lock() here. The value can change after the lock has been dropped anyway.

Quoted 1 line

+ retval = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;

It's not legal to perform copy_*_user() (which sleeps) inside read_lock(), write_lock(), spin_lock(), preempt_diable() or, really, local_irq_disable().

Quoted 6 lines

+ }
+ if (pid == 0)
+ goto out_nounlock;
+
+out_unlock:
+ read_unlock(&tasklist_lock);