From: "Bill Rugolsky Jr." <brugolsky@telemetry-investments.com>
To: Pavel Machek <pavel@ucw.cz>
Cc: linux-kernel@vger.kernel.org, Chris Wright <chrisw@osdl.org>,
Jan Knutar <jk-lkml@sci.fi>
Subject: Re: [RFC][PATCH] /proc/<pid>/rlimit
Date: Thu, 20 Jan 2005 21:19:53 -0500 [thread overview]
Message-ID: <20050121021953.GA6797@ti64.telemetry-investments.com> (raw)
In-Reply-To: <20050120144358.GI476@openzaurus.ucw.cz>
On Thu, Jan 20, 2005 at 03:43:58PM +0100, Pavel Machek wrote:
> It would be nice if you could make it "value-per-file". That way,
> it could become writable in future. If "max nice level" ever becomes rlimit,
> this would be very usefull.
Agreed, though write support present difficulties.
My principal concern is that we don't want users changing resource limits
of privileged processes. If we want an ordinary user to be allowed to
change limits, the rules would have to be similar to those allowed for
ptrace(), e.g., no-setuid processes, etc. [With ptrace(), one can of
course attach to the process and invoke the setrlimit() syscall directly].
Additionally, sys_setrlimit() has an LSM hook:
security_task_setrlimit(unsigned int resource, struct rlimit *)
One would need to take account of changing the limit from a different
context. It's a bit of a mess, and outside of the standard API; that's
why I didn't bother.
Anyway, for Jan, here's my incomplete and unmergeable cut-n-paste hack
to implement write on top of my previous patch. Format is as was
suggested by Jan:
<name|[rlimit-]%u> <%u|unlimited> <%u|unlimited>
E.g.,
echo memlock 65536 65536 > /proc/1/rlimit
Writing is limited to root (i.e. CAP_SYS_PTRACE), though see
fs/proc/base.c:may_ptrace_attach() for an idea of how to change that.
-Bill
--- linux-2.6.11-rc1-bk6/fs/proc/base.c.proc-pid-rlimit-write
+++ linux-2.6.11-rc1-bk6/fs/proc/base.c
@@ -23,6 +23,7 @@
#include <linux/init.h>
#include <linux/file.h>
#include <linux/string.h>
+#include <linux/ctype.h>
#include <linux/seq_file.h>
#include <linux/namei.h>
#include <linux/namespace.h>
@@ -127,7 +128,7 @@
E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
- E(PROC_TGID_RLIMIT, "rlimit", S_IFREG|S_IRUGO),
+ E(PROC_TGID_RLIMIT, "rlimit", S_IFREG|S_IRUGO|S_IWUSR),
#ifdef CONFIG_SECURITY
E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
#endif
@@ -153,7 +154,7 @@
E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
- E(PROC_TID_RLIMIT, "rlimit", S_IFREG|S_IRUGO),
+ E(PROC_TID_RLIMIT, "rlimit", S_IFREG|S_IRUGO|S_IWUSR),
#ifdef CONFIG_SECURITY
E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
#endif
@@ -595,9 +596,99 @@
return single_release(inode, file);
}
+static inline char *skip_ws(char *s)
+{
+ while (isspace(*s))
+ s++;
+ return s;
+}
+
+static inline char *find_ws(char *s)
+{
+ while (!isspace(*s) && *s != '\0')
+ s++;
+ return s;
+}
+
+#define MAX_RLIMIT_WRITE 79
+static ssize_t rlimit_write(struct file * file, const char * buf,
+ size_t count, loff_t *ppos)
+{
+ struct task_struct *task = proc_task(file->f_dentry->d_inode);
+ struct rlimit new_rlim, *old_rlim;
+ unsigned int i;
+ char *s, *t, kbuf[MAX_RLIMIT_WRITE+1];
+
+ /* changing resources limits can crash or subvert a process */
+ if (!capable(CAP_SYS_PTRACE) || security_ptrace(current,task))
+ return -ESRCH;
+
+ if (count > MAX_RLIMIT_WRITE)
+ return -EINVAL;
+ if (copy_from_user(&kbuf, buf, count))
+ return -EFAULT;
+ kbuf[MAX_RLIMIT_WRITE] = '\0';
+
+ /* parse the resource id */
+ s = skip_ws(kbuf);
+ t = find_ws(s);
+ if (*t == '\0')
+ return -EINVAL;
+ *t++ = '\0';
+ for (i = 0 ; i < RLIM_NLIMITS ; i++)
+ if (rlim_name[i] && !strcmp(s,rlim_name[i]))
+ break;
+ if (i >= RLIM_NLIMITS) {
+ if (!strncmp(s, "rlimit-",7))
+ s += 7;
+ if (sscanf(s, "%u", &i) != 1 || i >= RLIM_NLIMITS)
+ return -EINVAL;
+ }
+
+ /* parse the soft limit */
+ s = skip_ws(t);
+ t = find_ws(s);
+ if (*t == '\0')
+ return -EINVAL;
+ *t++ = '\0';
+ if (!strcmp(s, "unlimited"))
+ new_rlim.rlim_cur = RLIM_INFINITY;
+ else if (sscanf(s, "%lu", &new_rlim.rlim_cur) != 1)
+ return -EINVAL;
+
+ /* parse the hard limit */
+ s = skip_ws(t);
+ t = find_ws(s);
+ *t = '\0';
+ if (!strcmp(s, "unlimited"))
+ new_rlim.rlim_max = RLIM_INFINITY;
+ else if (sscanf(s, "%lu", &new_rlim.rlim_max) != 1)
+ return -EINVAL;
+
+ /* validate the values; copied from sys_setrlimit() */
+ if (new_rlim.rlim_cur > new_rlim.rlim_max)
+ return -EINVAL;
+ old_rlim = task->signal->rlim + i;
+ if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
+ !capable(CAP_SYS_RESOURCE))
+ return -EPERM;
+ if (i == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
+ return -EPERM;
+
+ /* SECURITY: missing security_task_setrlimit() equivalent */
+
+ /* set the limits */
+ task_lock(task->group_leader);
+ *old_rlim = new_rlim;
+ task_unlock(task->group_leader);
+
+ return count;
+}
+
static struct file_operations proc_rlimit_operations = {
.open = rlimit_open,
.read = seq_read,
+ .write = rlimit_write,
.llseek = seq_lseek,
.release = rlimit_release,
};
prev parent reply other threads:[~2005-01-21 2:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-18 20:44 Bill Rugolsky Jr.
2005-01-19 7:11 ` Chris Wright
2005-01-19 19:31 ` Jan Knutar
2005-01-19 19:38 ` Chris Wright
2005-01-19 20:13 ` Bill Rugolsky Jr.
2005-01-19 20:47 ` Chris Wright
2005-01-20 14:43 ` Pavel Machek
2005-01-21 2:19 ` Bill Rugolsky Jr. [this message]
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=20050121021953.GA6797@ti64.telemetry-investments.com \
--to=brugolsky@telemetry-investments.com \
--cc=chrisw@osdl.org \
--cc=jk-lkml@sci.fi \
--cc=linux-kernel@vger.kernel.org \
--cc=pavel@ucw.cz \
/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®