blob: df479898041657a618a5bd86f2ed579ff82adda7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09002/*
3 * security/tomoyo/realpath.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09006 */
7
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09008#include "common.h"
Al Virod10577a2011-12-07 13:06:11 -05009#include <linux/magic.h>
Alexey Gladkovc59f4152020-04-23 22:03:10 +020010#include <linux/proc_fs.h>
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090011
12/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090013 * tomoyo_encode2 - Encode binary string to ascii string.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090014 *
Tetsuo Handa059d84d2011-09-10 15:23:54 +090015 * @str: String in binary format.
16 * @str_len: Size of @str in byte.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090017 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +090018 * Returns pointer to @str in ascii format on success, NULL otherwise.
19 *
20 * This function uses kzalloc(), so caller must kfree() if this function
21 * didn't return NULL.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090022 */
Tetsuo Handa059d84d2011-09-10 15:23:54 +090023char *tomoyo_encode2(const char *str, int str_len)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090024{
Tetsuo Handa059d84d2011-09-10 15:23:54 +090025 int i;
Tetsuo Handac8c57e82010-06-03 20:36:43 +090026 int len = 0;
27 const char *p = str;
28 char *cp;
29 char *cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090030
Tetsuo Handac8c57e82010-06-03 20:36:43 +090031 if (!p)
32 return NULL;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090033 for (i = 0; i < str_len; i++) {
34 const unsigned char c = p[i];
35
Tetsuo Handac8c57e82010-06-03 20:36:43 +090036 if (c == '\\')
37 len += 2;
38 else if (c > ' ' && c < 127)
39 len++;
40 else
41 len += 4;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090042 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090043 len++;
44 /* Reserve space for appending "/". */
45 cp = kzalloc(len + 10, GFP_NOFS);
46 if (!cp)
47 return NULL;
48 cp0 = cp;
49 p = str;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090050 for (i = 0; i < str_len; i++) {
51 const unsigned char c = p[i];
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090052
Tetsuo Handac8c57e82010-06-03 20:36:43 +090053 if (c == '\\') {
54 *cp++ = '\\';
55 *cp++ = '\\';
56 } else if (c > ' ' && c < 127) {
57 *cp++ = c;
58 } else {
59 *cp++ = '\\';
60 *cp++ = (c >> 6) + '0';
61 *cp++ = ((c >> 3) & 7) + '0';
62 *cp++ = (c & 7) + '0';
Eric W. Biedermana4054b62009-11-20 09:12:22 -080063 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090064 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090065 return cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090066}
67
68/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090069 * tomoyo_encode - Encode binary string to ascii string.
70 *
71 * @str: String in binary format.
72 *
73 * Returns pointer to @str in ascii format on success, NULL otherwise.
74 *
75 * This function uses kzalloc(), so caller must kfree() if this function
76 * didn't return NULL.
77 */
78char *tomoyo_encode(const char *str)
79{
80 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
81}
82
83/**
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090084 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
85 *
86 * @path: Pointer to "struct path".
87 * @buffer: Pointer to buffer to return value in.
88 * @buflen: Sizeof @buffer.
89 *
90 * Returns the buffer on success, an error code otherwise.
91 *
92 * If dentry is a directory, trailing '/' is appended.
93 */
Al Viro22473862015-03-08 19:24:30 -040094static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090095 const int buflen)
96{
97 char *pos = ERR_PTR(-ENOMEM);
Tetsuo Handacdcf6722019-01-24 18:37:35 +090098
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090099 if (buflen >= 256) {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900100 /* go to whatever namespace root we are under */
Al Viro02125a82011-12-05 08:43:34 -0500101 pos = d_absolute_path(path, buffer, buflen - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900102 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000103 struct inode *inode = d_backing_inode(path->dentry);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900104
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900105 if (inode && S_ISDIR(inode->i_mode)) {
106 buffer[buflen - 2] = '/';
107 buffer[buflen - 1] = '\0';
108 }
109 }
110 }
111 return pos;
112}
113
114/**
115 * tomoyo_get_dentry_path - Get the path of a dentry.
116 *
117 * @dentry: Pointer to "struct dentry".
118 * @buffer: Pointer to buffer to return value in.
119 * @buflen: Sizeof @buffer.
120 *
121 * Returns the buffer on success, an error code otherwise.
122 *
123 * If dentry is a directory, trailing '/' is appended.
124 */
125static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
126 const int buflen)
127{
128 char *pos = ERR_PTR(-ENOMEM);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900129
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900130 if (buflen >= 256) {
131 pos = dentry_path_raw(dentry, buffer, buflen - 1);
132 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000133 struct inode *inode = d_backing_inode(dentry);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900134
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900135 if (inode && S_ISDIR(inode->i_mode)) {
136 buffer[buflen - 2] = '/';
137 buffer[buflen - 1] = '\0';
138 }
139 }
140 }
141 return pos;
142}
143
144/**
145 * tomoyo_get_local_path - Get the path of a dentry.
146 *
147 * @dentry: Pointer to "struct dentry".
148 * @buffer: Pointer to buffer to return value in.
149 * @buflen: Sizeof @buffer.
150 *
151 * Returns the buffer on success, an error code otherwise.
152 */
153static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
154 const int buflen)
155{
156 struct super_block *sb = dentry->d_sb;
157 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900158
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900159 if (IS_ERR(pos))
160 return pos;
161 /* Convert from $PID to self if $PID is current thread. */
162 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
163 char *ep;
164 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
Alexey Gladkov9d78ede2020-05-18 20:07:38 +0200165 struct pid_namespace *proc_pidns = proc_pid_ns(sb);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900166
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900167 if (*ep == '/' && pid && pid ==
Alexey Gladkovc59f4152020-04-23 22:03:10 +0200168 task_tgid_nr_ns(current, proc_pidns)) {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900169 pos = ep - 5;
170 if (pos < buffer)
171 goto out;
172 memmove(pos, "/self", 5);
173 }
174 goto prepend_filesystem_name;
175 }
176 /* Use filesystem name for unnamed devices. */
177 if (!MAJOR(sb->s_dev))
178 goto prepend_filesystem_name;
179 {
David Howellsc6f493d2015-03-17 22:26:22 +0000180 struct inode *inode = d_backing_inode(sb->s_root);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900181
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900182 /*
183 * Use filesystem name if filesystem does not support rename()
184 * operation.
185 */
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200186 if (!inode->i_op->rename)
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900187 goto prepend_filesystem_name;
188 }
189 /* Prepend device name. */
190 {
191 char name[64];
192 int name_len;
193 const dev_t dev = sb->s_dev;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900194
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900195 name[sizeof(name) - 1] = '\0';
196 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
197 MINOR(dev));
198 name_len = strlen(name);
199 pos -= name_len;
200 if (pos < buffer)
201 goto out;
202 memmove(pos, name, name_len);
203 return pos;
204 }
205 /* Prepend filesystem name. */
206prepend_filesystem_name:
207 {
208 const char *name = sb->s_type->name;
209 const int name_len = strlen(name);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900210
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900211 pos -= name_len + 1;
212 if (pos < buffer)
213 goto out;
214 memmove(pos, name, name_len);
215 pos[name_len] = ':';
216 }
217 return pos;
218out:
219 return ERR_PTR(-ENOMEM);
220}
221
222/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900223 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
224 *
225 * @path: Pointer to "struct path".
226 *
227 * Returns the realpath of the given @path on success, NULL otherwise.
228 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900229 * 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.
233 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900234 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900235 * if these functions didn't return NULL.
236 */
Al Viro22473862015-03-08 19:24:30 -0400237char *tomoyo_realpath_from_path(const struct path *path)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900238{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900239 char *buf = NULL;
240 char *name = NULL;
241 unsigned int buf_len = PAGE_SIZE / 2;
242 struct dentry *dentry = path->dentry;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900243 struct super_block *sb;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900244
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900245 if (!dentry)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900246 return NULL;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900247 sb = dentry->d_sb;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900248 while (1) {
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900249 char *pos;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900250 struct inode *inode;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900251
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900252 buf_len <<= 1;
253 kfree(buf);
254 buf = kmalloc(buf_len, GFP_NOFS);
255 if (!buf)
256 break;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900257 /* To make sure that pos is '\0' terminated. */
258 buf[buf_len - 1] = '\0';
Tetsuo Handa6f7c4132019-11-25 10:46:51 +0900259 /* For "pipe:[\$]" and "socket:[\$]". */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900260 if (dentry->d_op && dentry->d_op->d_dname) {
261 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900262 goto encode;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900263 }
David Howellsc6f493d2015-03-17 22:26:22 +0000264 inode = d_backing_inode(sb->s_root);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900265 /*
266 * Get local name for filesystems without rename() operation
267 * or dentry without vfsmount.
268 */
Tetsuo Handa8fe7a262014-08-20 14:14:04 +0900269 if (!path->mnt ||
Tetsuo Handa27df4b42019-02-27 23:19:24 +0900270 (!inode->i_op->rename &&
271 !(sb->s_type->fs_flags & FS_REQUIRES_DEV)))
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900272 pos = tomoyo_get_local_path(path->dentry, buf,
273 buf_len - 1);
274 /* Get absolute name for the rest. */
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900275 else {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900276 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900277 /*
278 * Fall back to local name if absolute name is not
279 * available.
280 */
281 if (pos == ERR_PTR(-EINVAL))
282 pos = tomoyo_get_local_path(path->dentry, buf,
283 buf_len - 1);
284 }
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900285encode:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900286 if (IS_ERR(pos))
287 continue;
288 name = tomoyo_encode(pos);
289 break;
290 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900291 kfree(buf);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900292 if (!name)
293 tomoyo_warn_oom(__func__);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900294 return name;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900295}
296
297/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900298 * tomoyo_realpath_nofollow - Get realpath of a pathname.
299 *
300 * @pathname: The pathname to solve.
301 *
302 * Returns the realpath of @pathname on success, NULL otherwise.
303 */
304char *tomoyo_realpath_nofollow(const char *pathname)
305{
Al Viroe24977d2009-04-02 21:17:03 -0400306 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900307
Al Viroe24977d2009-04-02 21:17:03 -0400308 if (pathname && kern_path(pathname, 0, &path) == 0) {
309 char *buf = tomoyo_realpath_from_path(&path);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900310
Al Viroe24977d2009-04-02 21:17:03 -0400311 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900312 return buf;
313 }
314 return NULL;
315}