blob: bf38fc1b59b2878b42533c349a37646fb2630e0b [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>
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090010
11/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090012 * tomoyo_encode2 - Encode binary string to ascii string.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090013 *
Tetsuo Handa059d84d2011-09-10 15:23:54 +090014 * @str: String in binary format.
15 * @str_len: Size of @str in byte.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090016 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +090017 * Returns pointer to @str in ascii format on success, NULL otherwise.
18 *
19 * This function uses kzalloc(), so caller must kfree() if this function
20 * didn't return NULL.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090021 */
Tetsuo Handa059d84d2011-09-10 15:23:54 +090022char *tomoyo_encode2(const char *str, int str_len)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090023{
Tetsuo Handa059d84d2011-09-10 15:23:54 +090024 int i;
Tetsuo Handac8c57e82010-06-03 20:36:43 +090025 int len = 0;
26 const char *p = str;
27 char *cp;
28 char *cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090029
Tetsuo Handac8c57e82010-06-03 20:36:43 +090030 if (!p)
31 return NULL;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090032 for (i = 0; i < str_len; i++) {
33 const unsigned char c = p[i];
34
Tetsuo Handac8c57e82010-06-03 20:36:43 +090035 if (c == '\\')
36 len += 2;
37 else if (c > ' ' && c < 127)
38 len++;
39 else
40 len += 4;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090041 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090042 len++;
43 /* Reserve space for appending "/". */
44 cp = kzalloc(len + 10, GFP_NOFS);
45 if (!cp)
46 return NULL;
47 cp0 = cp;
48 p = str;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090049 for (i = 0; i < str_len; i++) {
50 const unsigned char c = p[i];
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090051
Tetsuo Handac8c57e82010-06-03 20:36:43 +090052 if (c == '\\') {
53 *cp++ = '\\';
54 *cp++ = '\\';
55 } else if (c > ' ' && c < 127) {
56 *cp++ = c;
57 } else {
58 *cp++ = '\\';
59 *cp++ = (c >> 6) + '0';
60 *cp++ = ((c >> 3) & 7) + '0';
61 *cp++ = (c & 7) + '0';
Eric W. Biedermana4054b62009-11-20 09:12:22 -080062 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090063 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090064 return cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090065}
66
67/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090068 * tomoyo_encode - Encode binary string to ascii string.
69 *
70 * @str: String in binary format.
71 *
72 * Returns pointer to @str in ascii format on success, NULL otherwise.
73 *
74 * This function uses kzalloc(), so caller must kfree() if this function
75 * didn't return NULL.
76 */
77char *tomoyo_encode(const char *str)
78{
79 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
80}
81
82/**
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090083 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
84 *
85 * @path: Pointer to "struct path".
86 * @buffer: Pointer to buffer to return value in.
87 * @buflen: Sizeof @buffer.
88 *
89 * Returns the buffer on success, an error code otherwise.
90 *
91 * If dentry is a directory, trailing '/' is appended.
92 */
Al Viro22473862015-03-08 19:24:30 -040093static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090094 const int buflen)
95{
96 char *pos = ERR_PTR(-ENOMEM);
Tetsuo Handacdcf6722019-01-24 18:37:35 +090097
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090098 if (buflen >= 256) {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090099 /* go to whatever namespace root we are under */
Al Viro02125a82011-12-05 08:43:34 -0500100 pos = d_absolute_path(path, buffer, buflen - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900101 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000102 struct inode *inode = d_backing_inode(path->dentry);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900103
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900104 if (inode && S_ISDIR(inode->i_mode)) {
105 buffer[buflen - 2] = '/';
106 buffer[buflen - 1] = '\0';
107 }
108 }
109 }
110 return pos;
111}
112
113/**
114 * tomoyo_get_dentry_path - Get the path of a dentry.
115 *
116 * @dentry: Pointer to "struct dentry".
117 * @buffer: Pointer to buffer to return value in.
118 * @buflen: Sizeof @buffer.
119 *
120 * Returns the buffer on success, an error code otherwise.
121 *
122 * If dentry is a directory, trailing '/' is appended.
123 */
124static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
125 const int buflen)
126{
127 char *pos = ERR_PTR(-ENOMEM);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900128
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900129 if (buflen >= 256) {
130 pos = dentry_path_raw(dentry, buffer, buflen - 1);
131 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000132 struct inode *inode = d_backing_inode(dentry);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900133
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900134 if (inode && S_ISDIR(inode->i_mode)) {
135 buffer[buflen - 2] = '/';
136 buffer[buflen - 1] = '\0';
137 }
138 }
139 }
140 return pos;
141}
142
143/**
144 * tomoyo_get_local_path - Get the path of a dentry.
145 *
146 * @dentry: Pointer to "struct dentry".
147 * @buffer: Pointer to buffer to return value in.
148 * @buflen: Sizeof @buffer.
149 *
150 * Returns the buffer on success, an error code otherwise.
151 */
152static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
153 const int buflen)
154{
155 struct super_block *sb = dentry->d_sb;
156 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900157
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900158 if (IS_ERR(pos))
159 return pos;
160 /* Convert from $PID to self if $PID is current thread. */
161 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
162 char *ep;
163 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900164
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900165 if (*ep == '/' && pid && pid ==
166 task_tgid_nr_ns(current, sb->s_fs_info)) {
167 pos = ep - 5;
168 if (pos < buffer)
169 goto out;
170 memmove(pos, "/self", 5);
171 }
172 goto prepend_filesystem_name;
173 }
174 /* Use filesystem name for unnamed devices. */
175 if (!MAJOR(sb->s_dev))
176 goto prepend_filesystem_name;
177 {
David Howellsc6f493d2015-03-17 22:26:22 +0000178 struct inode *inode = d_backing_inode(sb->s_root);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900179
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900180 /*
181 * Use filesystem name if filesystem does not support rename()
182 * operation.
183 */
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200184 if (!inode->i_op->rename)
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900185 goto prepend_filesystem_name;
186 }
187 /* Prepend device name. */
188 {
189 char name[64];
190 int name_len;
191 const dev_t dev = sb->s_dev;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900192
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900193 name[sizeof(name) - 1] = '\0';
194 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
195 MINOR(dev));
196 name_len = strlen(name);
197 pos -= name_len;
198 if (pos < buffer)
199 goto out;
200 memmove(pos, name, name_len);
201 return pos;
202 }
203 /* Prepend filesystem name. */
204prepend_filesystem_name:
205 {
206 const char *name = sb->s_type->name;
207 const int name_len = strlen(name);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900208
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900209 pos -= name_len + 1;
210 if (pos < buffer)
211 goto out;
212 memmove(pos, name, name_len);
213 pos[name_len] = ':';
214 }
215 return pos;
216out:
217 return ERR_PTR(-ENOMEM);
218}
219
220/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900221 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
222 *
223 * @path: Pointer to "struct path".
224 *
225 * Returns the realpath of the given @path on success, NULL otherwise.
226 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900227 * If dentry is a directory, trailing '/' is appended.
228 * Characters out of 0x20 < c < 0x7F range are converted to
229 * \ooo style octal string.
230 * Character \ is converted to \\ string.
231 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900232 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900233 * if these functions didn't return NULL.
234 */
Al Viro22473862015-03-08 19:24:30 -0400235char *tomoyo_realpath_from_path(const struct path *path)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900236{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900237 char *buf = NULL;
238 char *name = NULL;
239 unsigned int buf_len = PAGE_SIZE / 2;
240 struct dentry *dentry = path->dentry;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900241 struct super_block *sb;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900242
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900243 if (!dentry)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900244 return NULL;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900245 sb = dentry->d_sb;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900246 while (1) {
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900247 char *pos;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900248 struct inode *inode;
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900249
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900250 buf_len <<= 1;
251 kfree(buf);
252 buf = kmalloc(buf_len, GFP_NOFS);
253 if (!buf)
254 break;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900255 /* To make sure that pos is '\0' terminated. */
256 buf[buf_len - 1] = '\0';
Tetsuo Handa6f7c4132019-11-25 10:46:51 +0900257 /* For "pipe:[\$]" and "socket:[\$]". */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900258 if (dentry->d_op && dentry->d_op->d_dname) {
259 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900260 goto encode;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900261 }
David Howellsc6f493d2015-03-17 22:26:22 +0000262 inode = d_backing_inode(sb->s_root);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900263 /*
264 * Get local name for filesystems without rename() operation
265 * or dentry without vfsmount.
266 */
Tetsuo Handa8fe7a262014-08-20 14:14:04 +0900267 if (!path->mnt ||
Tetsuo Handa27df4b42019-02-27 23:19:24 +0900268 (!inode->i_op->rename &&
269 !(sb->s_type->fs_flags & FS_REQUIRES_DEV)))
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900270 pos = tomoyo_get_local_path(path->dentry, buf,
271 buf_len - 1);
272 /* Get absolute name for the rest. */
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900273 else {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900274 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900275 /*
276 * Fall back to local name if absolute name is not
277 * available.
278 */
279 if (pos == ERR_PTR(-EINVAL))
280 pos = tomoyo_get_local_path(path->dentry, buf,
281 buf_len - 1);
282 }
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900283encode:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900284 if (IS_ERR(pos))
285 continue;
286 name = tomoyo_encode(pos);
287 break;
288 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900289 kfree(buf);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900290 if (!name)
291 tomoyo_warn_oom(__func__);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900292 return name;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900293}
294
295/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900296 * tomoyo_realpath_nofollow - Get realpath of a pathname.
297 *
298 * @pathname: The pathname to solve.
299 *
300 * Returns the realpath of @pathname on success, NULL otherwise.
301 */
302char *tomoyo_realpath_nofollow(const char *pathname)
303{
Al Viroe24977d2009-04-02 21:17:03 -0400304 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900305
Al Viroe24977d2009-04-02 21:17:03 -0400306 if (pathname && kern_path(pathname, 0, &path) == 0) {
307 char *buf = tomoyo_realpath_from_path(&path);
Tetsuo Handacdcf6722019-01-24 18:37:35 +0900308
Al Viroe24977d2009-04-02 21:17:03 -0400309 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900310 return buf;
311 }
312 return NULL;
313}