blob: a97b275ca3afe476a4ca0d865ef332d3f45aa688 [file] [log] [blame]
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09001/*
2 * security/tomoyo/realpath.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09005 */
6
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09007#include "common.h"
Al Virod10577a2011-12-07 13:06:11 -05008#include <linux/magic.h>
Kentaro Takedac73bd6d2009-02-05 17:18:12 +09009
10/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090011 * tomoyo_encode2 - Encode binary string to ascii string.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090012 *
Tetsuo Handa059d84d2011-09-10 15:23:54 +090013 * @str: String in binary format.
14 * @str_len: Size of @str in byte.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090015 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +090016 * Returns pointer to @str in ascii format on success, NULL otherwise.
17 *
18 * This function uses kzalloc(), so caller must kfree() if this function
19 * didn't return NULL.
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090020 */
Tetsuo Handa059d84d2011-09-10 15:23:54 +090021char *tomoyo_encode2(const char *str, int str_len)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090022{
Tetsuo Handa059d84d2011-09-10 15:23:54 +090023 int i;
Tetsuo Handac8c57e82010-06-03 20:36:43 +090024 int len = 0;
25 const char *p = str;
26 char *cp;
27 char *cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090028
Tetsuo Handac8c57e82010-06-03 20:36:43 +090029 if (!p)
30 return NULL;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090031 for (i = 0; i < str_len; i++) {
32 const unsigned char c = p[i];
33
Tetsuo Handac8c57e82010-06-03 20:36:43 +090034 if (c == '\\')
35 len += 2;
36 else if (c > ' ' && c < 127)
37 len++;
38 else
39 len += 4;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090040 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090041 len++;
42 /* Reserve space for appending "/". */
43 cp = kzalloc(len + 10, GFP_NOFS);
44 if (!cp)
45 return NULL;
46 cp0 = cp;
47 p = str;
Tetsuo Handa059d84d2011-09-10 15:23:54 +090048 for (i = 0; i < str_len; i++) {
49 const unsigned char c = p[i];
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090050
Tetsuo Handac8c57e82010-06-03 20:36:43 +090051 if (c == '\\') {
52 *cp++ = '\\';
53 *cp++ = '\\';
54 } else if (c > ' ' && c < 127) {
55 *cp++ = c;
56 } else {
57 *cp++ = '\\';
58 *cp++ = (c >> 6) + '0';
59 *cp++ = ((c >> 3) & 7) + '0';
60 *cp++ = (c & 7) + '0';
Eric W. Biedermana4054b62009-11-20 09:12:22 -080061 }
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090062 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +090063 return cp0;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +090064}
65
66/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090067 * tomoyo_encode - Encode binary string to ascii string.
68 *
69 * @str: String in binary format.
70 *
71 * Returns pointer to @str in ascii format on success, NULL otherwise.
72 *
73 * This function uses kzalloc(), so caller must kfree() if this function
74 * didn't return NULL.
75 */
76char *tomoyo_encode(const char *str)
77{
78 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
79}
80
81/**
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090082 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
83 *
84 * @path: Pointer to "struct path".
85 * @buffer: Pointer to buffer to return value in.
86 * @buflen: Sizeof @buffer.
87 *
88 * Returns the buffer on success, an error code otherwise.
89 *
90 * If dentry is a directory, trailing '/' is appended.
91 */
Al Viro22473862015-03-08 19:24:30 -040092static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090093 const int buflen)
94{
95 char *pos = ERR_PTR(-ENOMEM);
96 if (buflen >= 256) {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090097 /* go to whatever namespace root we are under */
Al Viro02125a82011-12-05 08:43:34 -050098 pos = d_absolute_path(path, buffer, buflen - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +090099 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000100 struct inode *inode = d_backing_inode(path->dentry);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900101 if (inode && S_ISDIR(inode->i_mode)) {
102 buffer[buflen - 2] = '/';
103 buffer[buflen - 1] = '\0';
104 }
105 }
106 }
107 return pos;
108}
109
110/**
111 * tomoyo_get_dentry_path - Get the path of a dentry.
112 *
113 * @dentry: Pointer to "struct dentry".
114 * @buffer: Pointer to buffer to return value in.
115 * @buflen: Sizeof @buffer.
116 *
117 * Returns the buffer on success, an error code otherwise.
118 *
119 * If dentry is a directory, trailing '/' is appended.
120 */
121static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
122 const int buflen)
123{
124 char *pos = ERR_PTR(-ENOMEM);
125 if (buflen >= 256) {
126 pos = dentry_path_raw(dentry, buffer, buflen - 1);
127 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
David Howellsc6f493d2015-03-17 22:26:22 +0000128 struct inode *inode = d_backing_inode(dentry);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900129 if (inode && S_ISDIR(inode->i_mode)) {
130 buffer[buflen - 2] = '/';
131 buffer[buflen - 1] = '\0';
132 }
133 }
134 }
135 return pos;
136}
137
138/**
139 * tomoyo_get_local_path - Get the path of a dentry.
140 *
141 * @dentry: Pointer to "struct dentry".
142 * @buffer: Pointer to buffer to return value in.
143 * @buflen: Sizeof @buffer.
144 *
145 * Returns the buffer on success, an error code otherwise.
146 */
147static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
148 const int buflen)
149{
150 struct super_block *sb = dentry->d_sb;
151 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
152 if (IS_ERR(pos))
153 return pos;
154 /* Convert from $PID to self if $PID is current thread. */
155 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
156 char *ep;
157 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
158 if (*ep == '/' && pid && pid ==
159 task_tgid_nr_ns(current, sb->s_fs_info)) {
160 pos = ep - 5;
161 if (pos < buffer)
162 goto out;
163 memmove(pos, "/self", 5);
164 }
165 goto prepend_filesystem_name;
166 }
167 /* Use filesystem name for unnamed devices. */
168 if (!MAJOR(sb->s_dev))
169 goto prepend_filesystem_name;
170 {
David Howellsc6f493d2015-03-17 22:26:22 +0000171 struct inode *inode = d_backing_inode(sb->s_root);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900172 /*
173 * Use filesystem name if filesystem does not support rename()
174 * operation.
175 */
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200176 if (!inode->i_op->rename)
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900177 goto prepend_filesystem_name;
178 }
179 /* Prepend device name. */
180 {
181 char name[64];
182 int name_len;
183 const dev_t dev = sb->s_dev;
184 name[sizeof(name) - 1] = '\0';
185 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
186 MINOR(dev));
187 name_len = strlen(name);
188 pos -= name_len;
189 if (pos < buffer)
190 goto out;
191 memmove(pos, name, name_len);
192 return pos;
193 }
194 /* Prepend filesystem name. */
195prepend_filesystem_name:
196 {
197 const char *name = sb->s_type->name;
198 const int name_len = strlen(name);
199 pos -= name_len + 1;
200 if (pos < buffer)
201 goto out;
202 memmove(pos, name, name_len);
203 pos[name_len] = ':';
204 }
205 return pos;
206out:
207 return ERR_PTR(-ENOMEM);
208}
209
210/**
211 * tomoyo_get_socket_name - Get the name of a socket.
212 *
213 * @path: Pointer to "struct path".
214 * @buffer: Pointer to buffer to return value in.
215 * @buflen: Sizeof @buffer.
216 *
217 * Returns the buffer.
218 */
Al Viro22473862015-03-08 19:24:30 -0400219static char *tomoyo_get_socket_name(const struct path *path, char * const buffer,
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900220 const int buflen)
221{
David Howellsc6f493d2015-03-17 22:26:22 +0000222 struct inode *inode = d_backing_inode(path->dentry);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900223 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
224 struct sock *sk = sock ? sock->sk : NULL;
225 if (sk) {
226 snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
227 "protocol=%u]", sk->sk_family, sk->sk_type,
228 sk->sk_protocol);
229 } else {
230 snprintf(buffer, buflen, "socket:[unknown]");
231 }
232 return buffer;
233}
234
235/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900236 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
237 *
238 * @path: Pointer to "struct path".
239 *
240 * Returns the realpath of the given @path on success, NULL otherwise.
241 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900242 * If dentry is a directory, trailing '/' is appended.
243 * Characters out of 0x20 < c < 0x7F range are converted to
244 * \ooo style octal string.
245 * Character \ is converted to \\ string.
246 *
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900247 * These functions use kzalloc(), so the caller must call kfree()
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900248 * if these functions didn't return NULL.
249 */
Al Viro22473862015-03-08 19:24:30 -0400250char *tomoyo_realpath_from_path(const struct path *path)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900251{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900252 char *buf = NULL;
253 char *name = NULL;
254 unsigned int buf_len = PAGE_SIZE / 2;
255 struct dentry *dentry = path->dentry;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900256 struct super_block *sb;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900257 if (!dentry)
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900258 return NULL;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900259 sb = dentry->d_sb;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900260 while (1) {
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900261 char *pos;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900262 struct inode *inode;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900263 buf_len <<= 1;
264 kfree(buf);
265 buf = kmalloc(buf_len, GFP_NOFS);
266 if (!buf)
267 break;
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900268 /* To make sure that pos is '\0' terminated. */
269 buf[buf_len - 1] = '\0';
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900270 /* Get better name for socket. */
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900271 if (sb->s_magic == SOCKFS_MAGIC) {
272 pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
273 goto encode;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900274 }
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900275 /* For "pipe:[\$]". */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900276 if (dentry->d_op && dentry->d_op->d_dname) {
277 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900278 goto encode;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900279 }
David Howellsc6f493d2015-03-17 22:26:22 +0000280 inode = d_backing_inode(sb->s_root);
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900281 /*
282 * Get local name for filesystems without rename() operation
283 * or dentry without vfsmount.
284 */
Tetsuo Handa8fe7a262014-08-20 14:14:04 +0900285 if (!path->mnt ||
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200286 (!inode->i_op->rename))
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900287 pos = tomoyo_get_local_path(path->dentry, buf,
288 buf_len - 1);
289 /* Get absolute name for the rest. */
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900290 else {
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900291 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
Tetsuo Handa1418a3e2011-12-08 21:24:06 +0900292 /*
293 * Fall back to local name if absolute name is not
294 * available.
295 */
296 if (pos == ERR_PTR(-EINVAL))
297 pos = tomoyo_get_local_path(path->dentry, buf,
298 buf_len - 1);
299 }
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900300encode:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900301 if (IS_ERR(pos))
302 continue;
303 name = tomoyo_encode(pos);
304 break;
305 }
Tetsuo Handa8e2d39a2010-01-26 20:45:27 +0900306 kfree(buf);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900307 if (!name)
308 tomoyo_warn_oom(__func__);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900309 return name;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900310}
311
312/**
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900313 * tomoyo_realpath_nofollow - Get realpath of a pathname.
314 *
315 * @pathname: The pathname to solve.
316 *
317 * Returns the realpath of @pathname on success, NULL otherwise.
318 */
319char *tomoyo_realpath_nofollow(const char *pathname)
320{
Al Viroe24977d2009-04-02 21:17:03 -0400321 struct path path;
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900322
Al Viroe24977d2009-04-02 21:17:03 -0400323 if (pathname && kern_path(pathname, 0, &path) == 0) {
324 char *buf = tomoyo_realpath_from_path(&path);
325 path_put(&path);
Kentaro Takedac73bd6d2009-02-05 17:18:12 +0900326 return buf;
327 }
328 return NULL;
329}