2 * security/tomoyo/realpath.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/types.h>
8 #include <linux/mount.h>
9 #include <linux/mnt_namespace.h>
10 #include <linux/fs_struct.h>
11 #include <linux/magic.h>
12 #include <linux/slab.h>
15 #include "../../fs/internal.h"
18 * tomoyo_encode: Convert binary string to ascii string.
20 * @str: String in binary format.
22 * Returns pointer to @str in ascii format on success, NULL otherwise.
24 * This function uses kzalloc(), so caller must kfree() if this function
27 char *tomoyo_encode(const char *str)
37 const unsigned char c = *p++;
40 else if (c > ' ' && c < 127)
46 /* Reserve space for appending "/". */
47 cp = kzalloc(len + 10, GFP_NOFS);
53 const unsigned char c = *p++;
58 } else if (c > ' ' && c < 127) {
62 *cp++ = (c >> 6) + '0';
63 *cp++ = ((c >> 3) & 7) + '0';
64 *cp++ = (c & 7) + '0';
71 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
73 * @path: Pointer to "struct path".
74 * @buffer: Pointer to buffer to return value in.
75 * @buflen: Sizeof @buffer.
77 * Returns the buffer on success, an error code otherwise.
79 * If dentry is a directory, trailing '/' is appended.
81 static char *tomoyo_get_absolute_path(struct path *path, char * const buffer,
84 char *pos = ERR_PTR(-ENOMEM);
86 struct path ns_root = { };
87 /* go to whatever namespace root we are under */
88 pos = __d_path(path, &ns_root, buffer, buflen - 1);
89 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
90 struct inode *inode = path->dentry->d_inode;
91 if (inode && S_ISDIR(inode->i_mode)) {
92 buffer[buflen - 2] = '/';
93 buffer[buflen - 1] = '\0';
101 * tomoyo_get_dentry_path - Get the path of a dentry.
103 * @dentry: Pointer to "struct dentry".
104 * @buffer: Pointer to buffer to return value in.
105 * @buflen: Sizeof @buffer.
107 * Returns the buffer on success, an error code otherwise.
109 * If dentry is a directory, trailing '/' is appended.
111 static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
114 char *pos = ERR_PTR(-ENOMEM);
116 pos = dentry_path_raw(dentry, buffer, buflen - 1);
117 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
118 struct inode *inode = dentry->d_inode;
119 if (inode && S_ISDIR(inode->i_mode)) {
120 buffer[buflen - 2] = '/';
121 buffer[buflen - 1] = '\0';
129 * tomoyo_get_local_path - Get the path of a dentry.
131 * @dentry: Pointer to "struct dentry".
132 * @buffer: Pointer to buffer to return value in.
133 * @buflen: Sizeof @buffer.
135 * Returns the buffer on success, an error code otherwise.
137 static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
140 struct super_block *sb = dentry->d_sb;
141 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
144 /* Convert from $PID to self if $PID is current thread. */
145 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
147 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
148 if (*ep == '/' && pid && pid ==
149 task_tgid_nr_ns(current, sb->s_fs_info)) {
153 memmove(pos, "/self", 5);
155 goto prepend_filesystem_name;
157 /* Use filesystem name for unnamed devices. */
158 if (!MAJOR(sb->s_dev))
159 goto prepend_filesystem_name;
161 struct inode *inode = sb->s_root->d_inode;
163 * Use filesystem name if filesystem does not support rename()
166 if (inode->i_op && !inode->i_op->rename)
167 goto prepend_filesystem_name;
169 /* Prepend device name. */
173 const dev_t dev = sb->s_dev;
174 name[sizeof(name) - 1] = '\0';
175 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
177 name_len = strlen(name);
181 memmove(pos, name, name_len);
184 /* Prepend filesystem name. */
185 prepend_filesystem_name:
187 const char *name = sb->s_type->name;
188 const int name_len = strlen(name);
192 memmove(pos, name, name_len);
197 return ERR_PTR(-ENOMEM);
201 * tomoyo_get_socket_name - Get the name of a socket.
203 * @path: Pointer to "struct path".
204 * @buffer: Pointer to buffer to return value in.
205 * @buflen: Sizeof @buffer.
207 * Returns the buffer.
209 static char *tomoyo_get_socket_name(struct path *path, char * const buffer,
212 struct inode *inode = path->dentry->d_inode;
213 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
214 struct sock *sk = sock ? sock->sk : NULL;
216 snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
217 "protocol=%u]", sk->sk_family, sk->sk_type,
220 snprintf(buffer, buflen, "socket:[unknown]");
226 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
228 * @path: Pointer to "struct path".
230 * Returns the realpath of the given @path on success, NULL otherwise.
232 * If dentry is a directory, trailing '/' is appended.
233 * Characters out of 0x20 < c < 0x7F range are converted to
234 * \ooo style octal string.
235 * Character \ is converted to \\ string.
237 * These functions use kzalloc(), so the caller must call kfree()
238 * if these functions didn't return NULL.
240 char *tomoyo_realpath_from_path(struct path *path)
244 unsigned int buf_len = PAGE_SIZE / 2;
245 struct dentry *dentry = path->dentry;
246 struct super_block *sb;
255 buf = kmalloc(buf_len, GFP_NOFS);
258 /* To make sure that pos is '\0' terminated. */
259 buf[buf_len - 1] = '\0';
260 /* Get better name for socket. */
261 if (sb->s_magic == SOCKFS_MAGIC) {
262 pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
265 /* For "pipe:[\$]". */
266 if (dentry->d_op && dentry->d_op->d_dname) {
267 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
270 inode = sb->s_root->d_inode;
272 * Get local name for filesystems without rename() operation
273 * or dentry without vfsmount.
275 if (!path->mnt || (inode->i_op && !inode->i_op->rename))
276 pos = tomoyo_get_local_path(path->dentry, buf,
278 /* Get absolute name for the rest. */
280 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
284 name = tomoyo_encode(pos);
289 tomoyo_warn_oom(__func__);
294 * tomoyo_realpath_nofollow - Get realpath of a pathname.
296 * @pathname: The pathname to solve.
298 * Returns the realpath of @pathname on success, NULL otherwise.
300 char *tomoyo_realpath_nofollow(const char *pathname)
304 if (pathname && kern_path(pathname, 0, &path) == 0) {
305 char *buf = tomoyo_realpath_from_path(&path);