* [PATCH 1/2] proc: add and remove /proc entry create checks
@ 2014-07-08 21:40 Alexey Dobriyan
2014-07-08 21:45 ` [PATCH 2/2] proc: faster /proc/$PID lookup Alexey Dobriyan
0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2014-07-08 21:40 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, viro
* remove proc_create(NULL, ...) check, let it oops
* warn about proc_create("", ...) and proc_create("very very long name", ...)
proc code keeps length as u8, no 256+ name length possible
* warn about proc_create("123", ...)
/proc/$PID and /proc/misc namespaces are separate things,
but dumb module might create funky a-la $PID entry.
* remove post mortem strchr('/') check
Triggering it implies either strchr() is buggy or memory corruption.
It should be VFS check anyway.
In reality, none of these checks will ever trigger,
it is preparation for the next patch.
Based on patch from Al Viro.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
fs/proc/base.c | 4 ++--
fs/proc/fd.c | 2 +-
fs/proc/generic.c | 30 +++++++++++++++---------------
fs/proc/internal.h | 6 +++---
4 files changed, 21 insertions(+), 21 deletions(-)
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2785,7 +2785,7 @@ struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsign
unsigned tgid;
struct pid_namespace *ns;
- tgid = name_to_int(dentry);
+ tgid = name_to_int(&dentry->d_name);
if (tgid == ~0U)
goto out;
@@ -3033,7 +3033,7 @@ static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry
if (!leader)
goto out_no_task;
- tid = name_to_int(dentry);
+ tid = name_to_int(&dentry->d_name);
if (tid == ~0U)
goto out;
--- a/fs/proc/fd.c
+++ b/fs/proc/fd.c
@@ -206,7 +206,7 @@ static struct dentry *proc_lookupfd_common(struct inode *dir,
{
struct task_struct *task = get_proc_task(dir);
int result = -ENOENT;
- unsigned fd = name_to_int(dentry);
+ unsigned fd = name_to_int(&dentry->d_name);
if (!task)
goto out_no_task;
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -330,28 +330,28 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
nlink_t nlink)
{
struct proc_dir_entry *ent = NULL;
- const char *fn = name;
- unsigned int len;
-
- /* make sure name is valid */
- if (!name || !strlen(name))
- goto out;
+ const char *fn;
+ struct qstr qstr;
if (xlate_proc_name(name, parent, &fn) != 0)
goto out;
+ qstr.name = fn;
+ qstr.len = strlen(fn);
+ if (qstr.len == 0 || qstr.len >= 256) {
+ WARN(1, "name len %u\n", qstr.len);
+ return NULL;
+ }
+ if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
+ WARN(1, "create '/proc/%s' by hand\n", qstr.name);
+ return NULL;
+ }
- /* At this point there must not be any '/' characters beyond *fn */
- if (strchr(fn, '/'))
- goto out;
-
- len = strlen(fn);
-
- ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
+ ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
if (!ent)
goto out;
- memcpy(ent->name, fn, len + 1);
- ent->namelen = len;
+ memcpy(ent->name, fn, qstr.len + 1);
+ ent->namelen = qstr.len;
ent->mode = mode;
ent->nlink = nlink;
atomic_set(&ent->count, 1);
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -112,10 +112,10 @@ static inline int task_dumpable(struct task_struct *task)
return 0;
}
-static inline unsigned name_to_int(struct dentry *dentry)
+static inline unsigned name_to_int(const struct qstr *qstr)
{
- const char *name = dentry->d_name.name;
- int len = dentry->d_name.len;
+ const char *name = qstr->name;
+ int len = qstr->len;
unsigned n = 0;
if (len > 1 && *name == '0')
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] proc: faster /proc/$PID lookup
2014-07-08 21:40 [PATCH 1/2] proc: add and remove /proc entry create checks Alexey Dobriyan
@ 2014-07-08 21:45 ` Alexey Dobriyan
2014-07-08 21:50 ` Alexey Dobriyan
0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2014-07-08 21:45 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, viro
Currently lookup for /proc/$PID first goes through spinlock and
whole list of misc /proc entries only to confirm that, yes,
/proc/42 can not possibly match random proc entry.
List is is several dozens entries long (52 entries on my setup).
None of this is necessary.
Try to convert dentry name to integer first.
If it works, it must be /proc/$PID.
If it doesn't, it must be random proc entry.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
fs/proc/base.c | 2 +-
fs/proc/root.c | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2780,7 +2780,7 @@ out:
struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
{
- int result = 0;
+ int result = -ENOENT;
struct task_struct *task;
unsigned tgid;
struct pid_namespace *ns;
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -199,10 +199,10 @@ static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct
static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
{
- if (!proc_lookup(dir, dentry, flags))
+ if (!proc_pid_lookup(dir, dentry, flags))
return NULL;
- return proc_pid_lookup(dir, dentry, flags);
+ return proc_lookup(dir, dentry, flags);
}
static int proc_root_readdir(struct file *file, struct dir_context *ctx)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 2/2] proc: faster /proc/$PID lookup
2014-07-08 21:45 ` [PATCH 2/2] proc: faster /proc/$PID lookup Alexey Dobriyan
@ 2014-07-08 21:50 ` Alexey Dobriyan
0 siblings, 0 replies; 3+ messages in thread
From: Alexey Dobriyan @ 2014-07-08 21:50 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, viro
On Wed, Jul 09, 2014 at 12:45:57AM +0300, Alexey Dobriyan wrote:
> Currently lookup for /proc/$PID first goes through spinlock and
> whole list of misc /proc entries only to confirm that, yes,
> /proc/42 can not possibly match random proc entry.
>
> List is is several dozens entries long (52 entries on my setup).
>
> None of this is necessary.
>
> Try to convert dentry name to integer first.
> If it works, it must be /proc/$PID.
> If it doesn't, it must be random proc entry.
Recycled from Al Viro's patch as well!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-07-08 21:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-08 21:40 [PATCH 1/2] proc: add and remove /proc entry create checks Alexey Dobriyan
2014-07-08 21:45 ` [PATCH 2/2] proc: faster /proc/$PID lookup Alexey Dobriyan
2014-07-08 21:50 ` Alexey Dobriyan
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®