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>
10 #include <linux/proc_fs.h>
13 * tomoyo_encode2 - Encode binary string to ascii string.
15 * @str: String in binary format.
16 * @str_len: Size of @str in byte.
18 * Returns pointer to @str in ascii format on success, NULL otherwise.
20 * This function uses kzalloc(), so caller must kfree() if this function
23 char *tomoyo_encode2(const char *str, int str_len)
33 for (i = 0; i < str_len; i++) {
34 const unsigned char c = p[i];
38 else if (c > ' ' && c < 127)
44 /* Reserve space for appending "/". */
45 cp = kzalloc(len + 10, GFP_NOFS);
50 for (i = 0; i < str_len; i++) {
51 const unsigned char c = p[i];
56 } else if (c > ' ' && c < 127) {
60 *cp++ = (c >> 6) + '0';
61 *cp++ = ((c >> 3) & 7) + '0';
62 *cp++ = (c & 7) + '0';
69 * tomoyo_encode - Encode binary string to ascii string.
71 * @str: String in binary format.
73 * Returns pointer to @str in ascii format on success, NULL otherwise.
75 * This function uses kzalloc(), so caller must kfree() if this function
78 char *tomoyo_encode(const char *str)
80 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
84 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
86 * @path: Pointer to "struct path".
87 * @buffer: Pointer to buffer to return value in.
88 * @buflen: Sizeof @buffer.
90 * Returns the buffer on success, an error code otherwise.
92 * If dentry is a directory, trailing '/' is appended.
94 static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
97 char *pos = ERR_PTR(-ENOMEM);
100 /* go to whatever namespace root we are under */
101 pos = d_absolute_path(path, buffer, buflen - 1);
102 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
103 struct inode *inode = d_backing_inode(path->dentry);
105 if (inode && S_ISDIR(inode->i_mode)) {
106 buffer[buflen - 2] = '/';
107 buffer[buflen - 1] = '\0';
115 * tomoyo_get_dentry_path - Get the path of a dentry.
117 * @dentry: Pointer to "struct dentry".
118 * @buffer: Pointer to buffer to return value in.
119 * @buflen: Sizeof @buffer.
121 * Returns the buffer on success, an error code otherwise.
123 * If dentry is a directory, trailing '/' is appended.
125 static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
128 char *pos = ERR_PTR(-ENOMEM);
131 pos = dentry_path_raw(dentry, buffer, buflen - 1);
132 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
133 struct inode *inode = d_backing_inode(dentry);
135 if (inode && S_ISDIR(inode->i_mode)) {
136 buffer[buflen - 2] = '/';
137 buffer[buflen - 1] = '\0';
145 * tomoyo_get_local_path - Get the path of a dentry.
147 * @dentry: Pointer to "struct dentry".
148 * @buffer: Pointer to buffer to return value in.
149 * @buflen: Sizeof @buffer.
151 * Returns the buffer on success, an error code otherwise.
153 static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
156 struct super_block *sb = dentry->d_sb;
157 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
161 /* Convert from $PID to self if $PID is current thread. */
162 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
164 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
165 struct pid_namespace *proc_pidns = proc_pid_ns(sb);
167 if (*ep == '/' && pid && pid ==
168 task_tgid_nr_ns(current, proc_pidns)) {
172 memmove(pos, "/self", 5);
174 goto prepend_filesystem_name;
176 /* Use filesystem name for unnamed devices. */
177 if (!MAJOR(sb->s_dev))
178 goto prepend_filesystem_name;
180 struct inode *inode = d_backing_inode(sb->s_root);
183 * Use filesystem name if filesystem does not support rename()
186 if (!inode->i_op->rename)
187 goto prepend_filesystem_name;
189 /* Prepend device name. */
193 const dev_t dev = sb->s_dev;
195 name[sizeof(name) - 1] = '\0';
196 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
198 name_len = strlen(name);
202 memmove(pos, name, name_len);
205 /* Prepend filesystem name. */
206 prepend_filesystem_name:
208 const char *name = sb->s_type->name;
209 const int name_len = strlen(name);
214 memmove(pos, name, name_len);
219 return ERR_PTR(-ENOMEM);
223 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
225 * @path: Pointer to "struct path".
227 * Returns the realpath of the given @path on success, NULL otherwise.
229 * If dentry is a directory, trailing '/' is appended.
230 * Characters out of 0x20 < c < 0x7F range are converted to
231 * \ooo style octal string.
232 * Character \ is converted to \\ string.
234 * These functions use kzalloc(), so the caller must call kfree()
235 * if these functions didn't return NULL.
237 char *tomoyo_realpath_from_path(const struct path *path)
241 unsigned int buf_len = PAGE_SIZE / 2;
242 struct dentry *dentry = path->dentry;
243 struct super_block *sb;
254 buf = kmalloc(buf_len, GFP_NOFS);
257 /* To make sure that pos is '\0' terminated. */
258 buf[buf_len - 1] = '\0';
259 /* For "pipe:[\$]" and "socket:[\$]". */
260 if (dentry->d_op && dentry->d_op->d_dname) {
261 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
264 inode = d_backing_inode(sb->s_root);
266 * Get local name for filesystems without rename() operation
267 * or dentry without vfsmount.
270 (!inode->i_op->rename &&
271 !(sb->s_type->fs_flags & FS_REQUIRES_DEV)))
272 pos = tomoyo_get_local_path(path->dentry, buf,
274 /* Get absolute name for the rest. */
276 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
278 * Fall back to local name if absolute name is not
281 if (pos == ERR_PTR(-EINVAL))
282 pos = tomoyo_get_local_path(path->dentry, buf,
288 name = tomoyo_encode(pos);
293 tomoyo_warn_oom(__func__);
298 * tomoyo_realpath_nofollow - Get realpath of a pathname.
300 * @pathname: The pathname to solve.
302 * Returns the realpath of @pathname on success, NULL otherwise.
304 char *tomoyo_realpath_nofollow(const char *pathname)
308 if (pathname && kern_path(pathname, 0, &path) == 0) {
309 char *buf = tomoyo_realpath_from_path(&path);