mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: jirislaby@gmail.com
Cc: mingo@elte.hu, nhorman@tuxdriver.com, sfr@canb.auug.org.au,
	linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
	marcin.slusarz@gmail.com, tglx@linutronix.de, mingo@redhat.com,
	hpa@zytor.com, torvalds@linux-foundation.org, oleg@redhat.com,
	James Morris <jmorris@namei.org>,
	Heiko Carstens <heiko.carstens@de.ibm.com>
Subject: [PATCH v3 24/27] core: implement getprlimit and setprlimit syscalls
Date: Sat, 28 Nov 2009 00:06:04 +0100	[thread overview]
Message-ID: <1259363167-9347-24-git-send-email-jslaby@suse.cz> (raw)
In-Reply-To: <1259363167-9347-1-git-send-email-jslaby@suse.cz>

This patch adds the code to support the sys_setprlimit and set_getprlimit
syscalls which modify the rlim values of a selected process.

All rlim users now either use newly added accessors or doesn't need them
due to
- locking
- a process was just forked and nobody else knows about it yet (and
  nobody can't thus read/write limits)

Based on Neil's work. Thank him.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
Cc: James Morris <jmorris@namei.org>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ingo Molnar <mingo@elte.hu>
---
 include/linux/syscalls.h |    4 ++
 kernel/sys.c             |   86 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index a990ace..6fd7ba6 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -702,11 +702,15 @@ asmlinkage long sys_newuname(struct new_utsname __user *name);
 
 asmlinkage long sys_getrlimit(unsigned int resource,
 				struct rlimit __user *rlim);
+asmlinkage long sys_getprlimit(pid_t pid, unsigned int resource,
+				struct rlimit __user *rlim);
 #if defined(COMPAT_RLIM_OLD_INFINITY) || !(defined(CONFIG_IA64))
 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim);
 #endif
 asmlinkage long sys_setrlimit(unsigned int resource,
 				struct rlimit __user *rlim);
+asmlinkage long sys_setprlimit(pid_t pid, unsigned int resource,
+				struct rlimit __user *rlim);
 asmlinkage long sys_getrusage(int who, struct rusage __user *ru);
 asmlinkage long sys_umask(int mask);
 
diff --git a/kernel/sys.c b/kernel/sys.c
index c4065c5..626148f 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1212,6 +1212,61 @@ SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
 	}
 }
 
+static int check_prlimit_permission(struct task_struct *task)
+{
+	const struct cred *cred = current_cred(), *tcred;
+	int ret = 0;
+
+	rcu_read_lock();
+	tcred = __task_cred(task);
+	if ((cred->uid != tcred->euid ||
+	     cred->uid != tcred->suid ||
+	     cred->uid != tcred->uid  ||
+	     cred->gid != tcred->egid ||
+	     cred->gid != tcred->sgid ||
+	     cred->gid != tcred->gid) &&
+	     !capable(CAP_SYS_RESOURCE)) {
+		ret = -EPERM;
+	}
+	rcu_read_unlock();
+	return ret;
+}
+
+SYSCALL_DEFINE3(getprlimit, pid_t, pid, unsigned int, resource,
+		struct rlimit __user *, rlim)
+{
+	struct rlimit val;
+	struct task_struct *tsk;
+	int ret;
+
+	if (resource >= RLIM_NLIMITS)
+		return -EINVAL;
+
+	read_lock(&tasklist_lock);
+
+	tsk = find_task_by_vpid(pid);
+	if (!tsk || !tsk->sighand) {
+		ret = -ESRCH;
+		goto err_unlock;
+	}
+
+	ret = check_prlimit_permission(tsk);
+	if (ret)
+		goto err_unlock;
+
+	task_lock(tsk->group_leader);
+	val = tsk->signal->rlim[resource];
+	task_unlock(tsk->group_leader);
+
+	read_unlock(&tasklist_lock);
+
+	return copy_to_user(rlim, &val, sizeof(*rlim)) ? -EFAULT : 0;
+err_unlock:
+	read_unlock(&tasklist_lock);
+	return ret;
+}
+
+
 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
 
 /*
@@ -1310,6 +1365,37 @@ SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
 	return do_setrlimit(current, resource, &new_rlim);
 }
 
+SYSCALL_DEFINE3(setprlimit, pid_t, pid, unsigned int, resource,
+		struct rlimit __user *, rlim)
+{
+	struct task_struct *tsk;
+	struct rlimit new_rlim;
+	int ret;
+
+	if (resource >= RLIM_NLIMITS)
+		return -EINVAL;
+
+	if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
+		return -EFAULT;
+
+	rcu_read_lock();
+	tsk = find_task_by_vpid(pid);
+	if (!tsk) {
+		rcu_read_unlock();
+		return -ESRCH;
+	}
+	get_task_struct(tsk);
+	rcu_read_unlock();
+
+	ret = check_prlimit_permission(tsk);
+	if (!ret)
+		ret = do_setrlimit(tsk, resource, &new_rlim);
+
+	put_task_struct(tsk);
+
+	return ret;
+}
+
 /*
  * It would make sense to put struct rusage in the task_struct,
  * except that would make the task_struct be *really big*.  After
-- 
1.6.5.3


  parent reply	other threads:[~2009-11-27 23:07 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-27 23:05 [PATCH v3 01/27] SECURITY: selinux, fix update_rlimit_cpu parameter Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 02/27] SECURITY: add task_struct to setrlimit Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 03/27] core: add task_struct to update_rlimit_cpu Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 04/27] sys_setrlimit: make sure ->rlim_max never grows Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 05/27] core: split sys_setrlimit Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 06/27] core: allow setrlimit to non-current tasks Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 07/27] core: optimize setrlimit for current task Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 08/27] FS: proc, switch limits reading to fops Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 09/27] FS: proc, make limits writable Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 10/27] core: do security check under task_lock Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 11/27] core: rename setrlimit to do_setrlimit Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 12/27] resource: move kernel functions inside __KERNEL__ Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 13/27] core: posix-cpu-timers, cleanup rlimits usage Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 14/27] resource: add helpers for fetching rlimits Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 15/27] IA64: use helpers for rlimits Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 16/27] PPC: " Jiri Slaby
2009-11-28  0:19   ` Benjamin Herrenschmidt
2009-11-28  8:47     ` Jiri Slaby
2009-11-28 21:16       ` Benjamin Herrenschmidt
2009-11-29 11:06         ` Stephen Rothwell
2009-11-27 23:05 ` [PATCH v3 17/27] S390: " Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 18/27] SPARC: " Jiri Slaby
2009-11-27 23:05 ` [PATCH v3 19/27] X86: " Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 20/27] FS: " Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 21/27] MM: " Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 22/27] core: " Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 23/27] misc: " Jiri Slaby
2009-12-09 23:59   ` Roland Dreier
2009-11-27 23:06 ` Jiri Slaby [this message]
2009-11-27 23:06 ` [PATCH v3 25/27] unistd: add __NR_[get|set]prlimit syscall numbers Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 26/27] COMPAT: add get/put_compat_rlimit Jiri Slaby
2009-11-27 23:06 ` [PATCH v3 27/27] x86: add ia32 compat prlimit syscalls Jiri Slaby
2009-11-27 23:41 ` [PATCH v3 00/27] writable limits Jiri Slaby
2009-11-28  7:28   ` Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1259363167-9347-24-git-send-email-jslaby@suse.cz \
    --to=jslaby@suse.cz \
    --cc=akpm@linux-foundation.org \
    --cc=heiko.carstens@de.ibm.com \
    --cc=hpa@zytor.com \
    --cc=jirislaby@gmail.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcin.slusarz@gmail.com \
    --cc=mingo@elte.hu \
    --cc=mingo@redhat.com \
    --cc=nhorman@tuxdriver.com \
    --cc=oleg@redhat.com \
    --cc=sfr@canb.auug.org.au \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®