1
2
3
4
5
6
7
8#include "common.h"
9#include <linux/magic.h>
10
11
12
13
14
15
16
17
18
19
20
21
22char *tomoyo_encode2(const char *str, int str_len)
23{
24 int i;
25 int len = 0;
26 const char *p = str;
27 char *cp;
28 char *cp0;
29
30 if (!p)
31 return NULL;
32 for (i = 0; i < str_len; i++) {
33 const unsigned char c = p[i];
34
35 if (c == '\\')
36 len += 2;
37 else if (c > ' ' && c < 127)
38 len++;
39 else
40 len += 4;
41 }
42 len++;
43
44 cp = kzalloc(len + 10, GFP_NOFS);
45 if (!cp)
46 return NULL;
47 cp0 = cp;
48 p = str;
49 for (i = 0; i < str_len; i++) {
50 const unsigned char c = p[i];
51
52 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';
62 }
63 }
64 return cp0;
65}
66
67
68
69
70
71
72
73
74
75
76
77char *tomoyo_encode(const char *str)
78{
79 return str ? tomoyo_encode2(str, strlen(str)) : NULL;
80}
81
82
83
84
85
86
87
88
89
90
91
92
93static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer,
94 const int buflen)
95{
96 char *pos = ERR_PTR(-ENOMEM);
97 if (buflen >= 256) {
98
99 pos = d_absolute_path(path, buffer, buflen - 1);
100 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
101 struct inode *inode = d_backing_inode(path->dentry);
102 if (inode && S_ISDIR(inode->i_mode)) {
103 buffer[buflen - 2] = '/';
104 buffer[buflen - 1] = '\0';
105 }
106 }
107 }
108 return pos;
109}
110
111
112
113
114
115
116
117
118
119
120
121
122static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
123 const int buflen)
124{
125 char *pos = ERR_PTR(-ENOMEM);
126 if (buflen >= 256) {
127 pos = dentry_path_raw(dentry, buffer, buflen - 1);
128 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
129 struct inode *inode = d_backing_inode(dentry);
130 if (inode && S_ISDIR(inode->i_mode)) {
131 buffer[buflen - 2] = '/';
132 buffer[buflen - 1] = '\0';
133 }
134 }
135 }
136 return pos;
137}
138
139
140
141
142
143
144
145
146
147
148static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
149 const int buflen)
150{
151 struct super_block *sb = dentry->d_sb;
152 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
153 if (IS_ERR(pos))
154 return pos;
155
156 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
157 char *ep;
158 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
159 if (*ep == '/' && pid && pid ==
160 task_tgid_nr_ns(current, sb->s_fs_info)) {
161 pos = ep - 5;
162 if (pos < buffer)
163 goto out;
164 memmove(pos, "/self", 5);
165 }
166 goto prepend_filesystem_name;
167 }
168
169 if (!MAJOR(sb->s_dev))
170 goto prepend_filesystem_name;
171 {
172 struct inode *inode = d_backing_inode(sb->s_root);
173
174
175
176
177 if (!inode->i_op->rename)
178 goto prepend_filesystem_name;
179 }
180
181 {
182 char name[64];
183 int name_len;
184 const dev_t dev = sb->s_dev;
185 name[sizeof(name) - 1] = '\0';
186 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
187 MINOR(dev));
188 name_len = strlen(name);
189 pos -= name_len;
190 if (pos < buffer)
191 goto out;
192 memmove(pos, name, name_len);
193 return pos;
194 }
195
196prepend_filesystem_name:
197 {
198 const char *name = sb->s_type->name;
199 const int name_len = strlen(name);
200 pos -= name_len + 1;
201 if (pos < buffer)
202 goto out;
203 memmove(pos, name, name_len);
204 pos[name_len] = ':';
205 }
206 return pos;
207out:
208 return ERR_PTR(-ENOMEM);
209}
210
211
212
213
214
215
216
217
218
219
220static char *tomoyo_get_socket_name(const struct path *path, char * const buffer,
221 const int buflen)
222{
223 struct inode *inode = d_backing_inode(path->dentry);
224 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
225 struct sock *sk = sock ? sock->sk : NULL;
226 if (sk) {
227 snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
228 "protocol=%u]", sk->sk_family, sk->sk_type,
229 sk->sk_protocol);
230 } else {
231 snprintf(buffer, buflen, "socket:[unknown]");
232 }
233 return buffer;
234}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251char *tomoyo_realpath_from_path(const struct path *path)
252{
253 char *buf = NULL;
254 char *name = NULL;
255 unsigned int buf_len = PAGE_SIZE / 2;
256 struct dentry *dentry = path->dentry;
257 struct super_block *sb;
258 if (!dentry)
259 return NULL;
260 sb = dentry->d_sb;
261 while (1) {
262 char *pos;
263 struct inode *inode;
264 buf_len <<= 1;
265 kfree(buf);
266 buf = kmalloc(buf_len, GFP_NOFS);
267 if (!buf)
268 break;
269
270 buf[buf_len - 1] = '\0';
271
272 if (sb->s_magic == SOCKFS_MAGIC) {
273 pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
274 goto encode;
275 }
276
277 if (dentry->d_op && dentry->d_op->d_dname) {
278 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
279 goto encode;
280 }
281 inode = d_backing_inode(sb->s_root);
282
283
284
285
286 if (!path->mnt ||
287 (!inode->i_op->rename))
288 pos = tomoyo_get_local_path(path->dentry, buf,
289 buf_len - 1);
290
291 else {
292 pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
293
294
295
296
297 if (pos == ERR_PTR(-EINVAL))
298 pos = tomoyo_get_local_path(path->dentry, buf,
299 buf_len - 1);
300 }
301encode:
302 if (IS_ERR(pos))
303 continue;
304 name = tomoyo_encode(pos);
305 break;
306 }
307 kfree(buf);
308 if (!name)
309 tomoyo_warn_oom(__func__);
310 return name;
311}
312
313
314
315
316
317
318
319
320char *tomoyo_realpath_nofollow(const char *pathname)
321{
322 struct path path;
323
324 if (pathname && kern_path(pathname, 0, &path) == 0) {
325 char *buf = tomoyo_realpath_from_path(&path);
326 path_put(&path);
327 return buf;
328 }
329 return NULL;
330}
331