1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/realpath.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
9 #include <linux/magic.h>
12 * tomoyo_encode2 - Encode binary string to ascii string.
14 * @str: String in binary format.
15 * @str_len: Size of @str in byte.
17 * Returns pointer to @str in ascii format on success, NULL otherwise.
19 * This function uses kzalloc(), so caller must kfree() if this function
22 char *tomoyo_encode2(const char *str, int str_len)
32 for (i = 0; i < str_len; i++) {
33 const unsigned char c = p[i];
37 else if (c > ' ' && c < 127)
43 /* Reserve space for appending "/". */
44 cp = kzalloc(len + 10, GFP_NOFS);
49 for (i = 0; i < str_len; i++) {
50 const unsigned char c = p[i];
55 } else if (c > ' ' && c < 127) {
59 *cp++ = (c >> 6) + '0';
60 *cp++ = ((c >> 3) & 7) + '0';
61 *cp++ = (c & 7) + '0';
68 * tomoyo_encode - Encode binary string to ascii string.
70 * @str: String in binary format.
72 * Returns pointer to @str in ascii format on success, NULL otherwise.
74 * This function uses kzalloc(), so caller must kfree() if this function
77 char *tomoyo_encode(const char *str)
79 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
83 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
85 * @path: Pointer to "struct path".
86 * @buffer: Pointer to buffer to return value in.
87 * @buflen: Sizeof @buffer.
89 * Returns the buffer on success, an error code otherwise.
91 * If dentry is a directory, trailing '/' is appended.
93 static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
96 char *pos = ERR_PTR(-ENOMEM);
99 /* go to whatever namespace root we are under */
100 pos = d_absolute_path(path, buffer, buflen - 1);
101 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
102 struct inode *inode = d_backing_inode(path->dentry);
104 if (inode && S_ISDIR(inode->i_mode)) {
105 buffer[buflen - 2] = '/';
106 buffer[buflen - 1] = '\0';
114 * tomoyo_get_dentry_path - Get the path of a dentry.
116 * @dentry: Pointer to "struct dentry".
117 * @buffer: Pointer to buffer to return value in.
118 * @buflen: Sizeof @buffer.
120 * Returns the buffer on success, an error code otherwise.
122 * If dentry is a directory, trailing '/' is appended.
124 static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
127 char *pos = ERR_PTR(-ENOMEM);
130 pos = dentry_path_raw(dentry, buffer, buflen - 1);
131 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
132 struct inode *inode = d_backing_inode(dentry);
134 if (inode && S_ISDIR(inode->i_mode)) {
135 buffer[buflen - 2] = '/';
136 buffer[buflen - 1] = '\0';
144 * tomoyo_get_local_path - Get the path of a dentry.
146 * @dentry: Pointer to "struct dentry".
147 * @buffer: Pointer to buffer to return value in.
148 * @buflen: Sizeof @buffer.
150 * Returns the buffer on success, an error code otherwise.
152 static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
155 struct super_block *sb = dentry->d_sb;
156 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
160 /* Convert from $PID to self if $PID is current thread. */
161 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
163 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
165 if (*ep == '/' && pid && pid ==
166 task_tgid_nr_ns(current, sb->s_fs_info)) {
170 memmove(pos, "/self", 5);
172 goto prepend_filesystem_name;
174 /* Use filesystem name for unnamed devices. */
175 if (!MAJOR(sb->s_dev))
176 goto prepend_filesystem_name;
178 struct inode *inode = d_backing_inode(sb->s_root);
181 * Use filesystem name if filesystem does not support rename()
184 if (!inode->i_op->rename)
185 goto prepend_filesystem_name;
187 /* Prepend device name. */
191 const dev_t dev = sb->s_dev;
193 name[sizeof(name) - 1] = '\0';
194 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
196 name_len = strlen(name);
200 memmove(pos, name, name_len);
203 /* Prepend filesystem name. */
204 prepend_filesystem_name:
206 const char *name = sb->s_type->name;
207 const int name_len = strlen(name);
212 memmove(pos, name, name_len);
217 return ERR_PTR(-ENOMEM);
221 * tomoyo_get_socket_name - Get the name of a socket.
223 * @path: Pointer to "struct path".
224 * @buffer: Pointer to buffer to return value in.
225 * @buflen: Sizeof @buffer.
227 * Returns the buffer.
229 static char *tomoyo_get_socket_name(const struct path *path, char * const buffer,
232 struct inode *inode = d_backing_inode(path->dentry);
233 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
234 struct sock *sk = sock ? sock->sk : NULL;
237 snprintf(buffer, buflen, "socket:[family=%u:type=%u:protocol=%u]",
238 sk->sk_family, sk->sk_type, sk->sk_protocol);
240 snprintf(buffer, buflen, "socket:[unknown]");
246 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
248 * @path: Pointer to "struct path".
250 * Returns the realpath of the given @path on success, NULL otherwise.
252 * If dentry is a directory, trailing '/' is appended.
253 * Characters out of 0x20 < c < 0x7F range are converted to
254 * \ooo style octal string.
255 * Character \ is converted to \\ string.
257 * These functions use kzalloc(), so the caller must call kfree()
258 * if these functions didn't return NULL.
260 char *tomoyo_realpath_from_path(const struct path *path)
264 unsigned int buf_len = PAGE_SIZE / 2;
265 struct dentry *dentry = path->dentry;
266 struct super_block *sb;
277 buf = kmalloc(buf_len, GFP_NOFS);
280 /* To make sure that pos is '\0' terminated. */
281 buf[buf_len - 1] = '\0';
282 /* Get better name for socket. */
283 if (sb->s_magic == SOCKFS_MAGIC) {
284 pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
287 /* For "pipe:[\$]". */
288 if (dentry->d_op && dentry->d_op->d_dname) {
289 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
292 inode = d_backing_inode(sb->s_root);
294 * Get local name for filesystems without rename() operation
295 * or dentry without vfsmount.
298 (!inode->i_op->rename))
299 pos = tomoyo_get_local_path(path->dentry, buf,
301 /* Get absolute name for the rest. */
303 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
305 * Fall back to local name if absolute name is not
308 if (pos == ERR_PTR(-EINVAL))
309 pos = tomoyo_get_local_path(path->dentry, buf,
315 name = tomoyo_encode(pos);
320 tomoyo_warn_oom(__func__);
325 * tomoyo_realpath_nofollow - Get realpath of a pathname.
327 * @pathname: The pathname to solve.
329 * Returns the realpath of @pathname on success, NULL otherwise.
331 char *tomoyo_realpath_nofollow(const char *pathname)
335 if (pathname && kern_path(pathname, 0, &path) == 0) {
336 char *buf = tomoyo_realpath_from_path(&path);