2009-01-06 03:22:34

by Tetsuo Handa

[permalink] [raw]
Subject: [PATCH] Introduce d_realpath().

Al, thank you very much for processing security_path_*() patch.
Now James is reviewing our patch and he suggested
following parts need your acknowledgments.
Will you review and ACK for introducing a variant of d_path()?

--------------------
Subject: Introduce d_realpath().

To remove factors that make pathname based access control difficult
(e.g. symbolic links, "..", "//", chroot() etc.), a variant of d_path()
which traverses up to the root of the namespace is needed.

This patch introduces d_realpath(), a variant of d_path().
While d_path() stops traversing at current->fs->root,
d_realpath() doesn't stop traversiong at current->fs->root.

Three differences compared to d_path().
(1) Ignores current process's root directory.
(2) Trailing '/' is added if the pathname refers to a directory.
(3) /proc/PID/ is represented as /proc/self/ if PID equals current->tgid.

Signed-off-by: Kentaro Takeda <[email protected]>
Signed-off-by: Tetsuo Handa <[email protected]>
Signed-off-by: Toshiharu Harada <[email protected]>
---
fs/dcache.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/dcache.h | 1
2 files changed, 85 insertions(+)

--- linux-2.6.28-mm1.orig/fs/dcache.c
+++ linux-2.6.28-mm1/fs/dcache.c
@@ -32,6 +32,7 @@
#include <linux/seqlock.h>
#include <linux/swap.h>
#include <linux/bootmem.h>
+#include <linux/magic.h>
#include "internal.h"


@@ -1978,6 +1979,89 @@ Elong:
}

/**
+ * d_realpath - Get the realpath of a dentry.
+ *
+ * @path: Pointer to "struct path".
+ * @buffer: Pointer to buffer to return value in.
+ * @buflen: Sizeof @buffer.
+ *
+ * Returns pointer to the realpath on success, an error code othersize.
+ *
+ * If @path is a directory, trailing '/' is appended.
+ * /proc/PID/ is replaced by /proc/self/ if PID == task_tgid_nr_ns(current).
+ */
+char *d_realpath(struct path *path, char *buffer, int buflen)
+{
+ struct dentry *dentry = path->dentry;
+ struct vfsmount *vfsmnt = path->mnt;
+ char *end = buffer + buflen;
+
+ spin_lock(&dcache_lock);
+ spin_lock(&vfsmount_lock);
+ if (buflen < 1 || prepend(&end, &buflen, "", 1))
+ goto Elong;
+ /*
+ * Exception: Add trailing '/' for directory.
+ */
+ if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode) &&
+ prepend(&end, &buflen, "/", 1))
+ goto Elong;
+ for (;;) {
+ struct dentry *parent;
+ const char *name;
+ int name_len;
+ unsigned long pid;
+
+ if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
+ /* Global root? */
+ if (vfsmnt->mnt_parent == vfsmnt)
+ break;
+ dentry = vfsmnt->mnt_mountpoint;
+ vfsmnt = vfsmnt->mnt_parent;
+ continue;
+ }
+ parent = dentry->d_parent;
+ prefetch(parent);
+ /*
+ * Exception: Use /proc/self/ rather than /proc/PID/
+ * for current process.
+ */
+ name = dentry->d_name.name;
+ name_len = dentry->d_name.len;
+ if (IS_ROOT(parent) &&
+ parent->d_sb->s_magic == PROC_SUPER_MAGIC &&
+ !strict_strtoul(name, 10, &pid)) {
+ const pid_t tgid
+ = task_tgid_nr_ns(current,
+ dentry->d_sb->s_fs_info);
+ if (tgid && (pid_t) pid == tgid) {
+ name = "self";
+ name_len = 4;
+ }
+ }
+ if (prepend(&end, &buflen, name, name_len))
+ goto Elong;
+ if (prepend(&end, &buflen, "/", 1))
+ goto Elong;
+ dentry = parent;
+ }
+ if (*end == '/') {
+ /* hit the slash */
+ buflen++;
+ end++;
+ }
+ if (prepend_name(&end, &buflen, &dentry->d_name))
+ goto Elong;
+ out:
+ spin_unlock(&vfsmount_lock);
+ spin_unlock(&dcache_lock);
+ return end;
+ Elong:
+ end = ERR_PTR(-ENAMETOOLONG);
+ goto out;
+}
+
+/**
* d_path - return the path of a dentry
* @path: path to report
* @buf: buffer to return value in
--- linux-2.6.28-mm1.orig/include/linux/dcache.h
+++ linux-2.6.28-mm1/include/linux/dcache.h
@@ -305,6 +305,7 @@ extern char *dynamic_dname(struct dentry
extern char *__d_path(const struct path *path, struct path *root, char *, int);
extern char *d_path(const struct path *, char *, int);
extern char *dentry_path(struct dentry *, char *, int);
+extern char *d_realpath(struct path *, char *, int);

/* Allocation counts.. */

--------------------

Andrew Morton suggested me to save "parent->d_sb" and
avoid "->s_magic == PROC_SUPER_MAGIC" comparison.
But I think it is difficult because "parent->d_sb" is not
single-valued. http://lkml.org/lkml/2008/11/17/18

Regards.


2009-01-06 20:35:56

by Miklos Szeredi

[permalink] [raw]
Subject: Re: [PATCH] Introduce d_realpath().

On Tue, 06 Jan 2009, Tetsuo Handa wrote:
> Al, thank you very much for processing security_path_*() patch.
> Now James is reviewing our patch and he suggested
> following parts need your acknowledgments.
> Will you review and ACK for introducing a variant of d_path()?
>
> --------------------
> Subject: Introduce d_realpath().
>
> To remove factors that make pathname based access control difficult
> (e.g. symbolic links, "..", "//", chroot() etc.), a variant of d_path()
> which traverses up to the root of the namespace is needed.
>
> This patch introduces d_realpath(), a variant of d_path().
> While d_path() stops traversing at current->fs->root,
> d_realpath() doesn't stop traversiong at current->fs->root.
>
> Three differences compared to d_path().
> (1) Ignores current process's root directory.

I'd suggest calling __d_path() and passing in the namespace root
instead of the process root. That would be a lot simpler and result
in less code duplication.

> (2) Trailing '/' is added if the pathname refers to a directory.

Caller can do this.

> (3) /proc/PID/ is represented as /proc/self/ if PID equals current->tgid.

This too. Such hacks really don't belong in generic VFS functions.

Thanks,
Miklos

2009-01-07 06:32:40

by Tetsuo Handa

[permalink] [raw]
Subject: Re: [PATCH] Introduce d_realpath().

Miklos Szeredi wrote:
> > Three differences compared to d_path().
> > (1) Ignores current process's root directory.
>
> I'd suggest calling __d_path() and passing in the namespace root
> instead of the process root. That would be a lot simpler and result
> in less code duplication.
>
Yes, that'll be possible. But I won't do so. See (3).

> > (2) Trailing '/' is added if the pathname refers to a directory.
>
> Caller can do this.
>
Yes. But I'd like to add trailing '/' here.

Appending trailing '/' have to worry about buffer size, but removing trailing
'/' needn't to. Thus, I think it is more convenient for callers that trailing
'/' is automatically added.

> > (3) /proc/PID/ is represented as /proc/self/ if PID equals current->tgid.
>
> This too. Such hacks really don't belong in generic VFS functions.
>
No.

How can the caller of __d_path() detect the /PID/ part and convert
to /self/ from returned string? You might think that I can use
strstr(returned_path, "/proc/") and strict_strtoul() .

The procfs is mounted on /proc/ by convention, but that is not guaranteed.
Some systems might mount procfs on /proc2/ , /p/ or /var/tmp/proc/100/proc/ .

Also, a pathname like /var/tmp/proc/100/file1.txt is possible.
In this case, /var/tmp/proc/ is not the mount point of the procfs.

Thus, it is too late for the caller of __d_path() to detect the /PID/ part.

If the caller of __d_path() traverses {dentry,mount} tree for checking
dentry->d_sb->s_magic == PROC_SUPER_MAGIC, the reason to call __d_path()
disappears.

Thus, I need to detect /proc/PID/ and convert it to /proc/self/ in
d_realpath().