2 * security/tomoyo/realpath.c
4 * Pathname calculation functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/types.h>
10 #include <linux/mount.h>
11 #include <linux/mnt_namespace.h>
12 #include <linux/fs_struct.h>
13 #include <linux/magic.h>
14 #include <linux/slab.h>
17 #include "../../fs/internal.h"
20 * tomoyo_encode: Convert binary string to ascii string.
22 * @str: String in binary format.
24 * Returns pointer to @str in ascii format on success, NULL otherwise.
26 * This function uses kzalloc(), so caller must kfree() if this function
29 char *tomoyo_encode(const char *str)
39 const unsigned char c = *p++;
42 else if (c > ' ' && c < 127)
48 /* Reserve space for appending "/". */
49 cp = kzalloc(len + 10, GFP_NOFS);
55 const unsigned char c = *p++;
60 } else if (c > ' ' && c < 127) {
64 *cp++ = (c >> 6) + '0';
65 *cp++ = ((c >> 3) & 7) + '0';
66 *cp++ = (c & 7) + '0';
73 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
75 * @path: Pointer to "struct path".
76 * @buffer: Pointer to buffer to return value in.
77 * @buflen: Sizeof @buffer.
79 * Returns the buffer on success, an error code otherwise.
81 * If dentry is a directory, trailing '/' is appended.
83 static char *tomoyo_get_absolute_path(struct path *path, char * const buffer,
86 char *pos = ERR_PTR(-ENOMEM);
88 struct path ns_root = { };
89 /* go to whatever namespace root we are under */
90 pos = __d_path(path, &ns_root, buffer, buflen - 1);
91 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
92 struct inode *inode = path->dentry->d_inode;
93 if (inode && S_ISDIR(inode->i_mode)) {
94 buffer[buflen - 2] = '/';
95 buffer[buflen - 1] = '\0';
103 * tomoyo_get_dentry_path - Get the path of a dentry.
105 * @dentry: Pointer to "struct dentry".
106 * @buffer: Pointer to buffer to return value in.
107 * @buflen: Sizeof @buffer.
109 * Returns the buffer on success, an error code otherwise.
111 * If dentry is a directory, trailing '/' is appended.
113 static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
116 char *pos = ERR_PTR(-ENOMEM);
118 pos = dentry_path_raw(dentry, buffer, buflen - 1);
119 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
120 struct inode *inode = dentry->d_inode;
121 if (inode && S_ISDIR(inode->i_mode)) {
122 buffer[buflen - 2] = '/';
123 buffer[buflen - 1] = '\0';
131 * tomoyo_get_local_path - Get the path of a dentry.
133 * @dentry: Pointer to "struct dentry".
134 * @buffer: Pointer to buffer to return value in.
135 * @buflen: Sizeof @buffer.
137 * Returns the buffer on success, an error code otherwise.
139 static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
142 struct super_block *sb = dentry->d_sb;
143 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
146 /* Convert from $PID to self if $PID is current thread. */
147 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
149 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
150 if (*ep == '/' && pid && pid ==
151 task_tgid_nr_ns(current, sb->s_fs_info)) {
155 memmove(pos, "/self", 5);
157 goto prepend_filesystem_name;
159 /* Use filesystem name for unnamed devices. */
160 if (!MAJOR(sb->s_dev))
161 goto prepend_filesystem_name;
163 struct inode *inode = sb->s_root->d_inode;
165 * Use filesystem name if filesystem does not support rename()
168 if (inode->i_op && !inode->i_op->rename)
169 goto prepend_filesystem_name;
171 /* Prepend device name. */
175 const dev_t dev = sb->s_dev;
176 name[sizeof(name) - 1] = '\0';
177 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
179 name_len = strlen(name);
183 memmove(pos, name, name_len);
186 /* Prepend filesystem name. */
187 prepend_filesystem_name:
189 const char *name = sb->s_type->name;
190 const int name_len = strlen(name);
194 memmove(pos, name, name_len);
199 return ERR_PTR(-ENOMEM);
203 * tomoyo_get_socket_name - Get the name of a socket.
205 * @path: Pointer to "struct path".
206 * @buffer: Pointer to buffer to return value in.
207 * @buflen: Sizeof @buffer.
209 * Returns the buffer.
211 static char *tomoyo_get_socket_name(struct path *path, char * const buffer,
214 struct inode *inode = path->dentry->d_inode;
215 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
216 struct sock *sk = sock ? sock->sk : NULL;
218 snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
219 "protocol=%u]", sk->sk_family, sk->sk_type,
222 snprintf(buffer, buflen, "socket:[unknown]");
228 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
230 * @path: Pointer to "struct path".
232 * Returns the realpath of the given @path on success, NULL otherwise.
234 * If dentry is a directory, trailing '/' is appended.
235 * Characters out of 0x20 < c < 0x7F range are converted to
236 * \ooo style octal string.
237 * Character \ is converted to \\ string.
239 * These functions use kzalloc(), so the caller must call kfree()
240 * if these functions didn't return NULL.
242 char *tomoyo_realpath_from_path(struct path *path)
246 unsigned int buf_len = PAGE_SIZE / 2;
247 struct dentry *dentry = path->dentry;
248 struct super_block *sb;
257 buf = kmalloc(buf_len, GFP_NOFS);
260 /* To make sure that pos is '\0' terminated. */
261 buf[buf_len - 1] = '\0';
262 /* Get better name for socket. */
263 if (sb->s_magic == SOCKFS_MAGIC) {
264 pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
267 /* For "pipe:[\$]". */
268 if (dentry->d_op && dentry->d_op->d_dname) {
269 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
272 inode = sb->s_root->d_inode;
274 * Get local name for filesystems without rename() operation
275 * or dentry without vfsmount.
277 if (!path->mnt || (inode->i_op && !inode->i_op->rename))
278 pos = tomoyo_get_local_path(path->dentry, buf,
280 /* Get absolute name for the rest. */
282 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
286 name = tomoyo_encode(pos);
291 tomoyo_warn_oom(__func__);
296 * tomoyo_realpath_nofollow - Get realpath of a pathname.
298 * @pathname: The pathname to solve.
300 * Returns the realpath of @pathname on success, NULL otherwise.
302 char *tomoyo_realpath_nofollow(const char *pathname)
306 if (pathname && kern_path(pathname, 0, &path) == 0) {
307 char *buf = tomoyo_realpath_from_path(&path);