1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/magic.h>
16#include <linux/mnt_namespace.h>
17#include <linux/mount.h>
18#include <linux/namei.h>
19#include <linux/nsproxy.h>
20#include <linux/path.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/fs_struct.h>
24
25#include "include/apparmor.h"
26#include "include/path.h"
27#include "include/policy.h"
28
29
30
31static int prepend(char **buffer, int buflen, const char *str, int namelen)
32{
33 buflen -= namelen;
34 if (buflen < 0)
35 return -ENAMETOOLONG;
36 *buffer -= namelen;
37 memcpy(*buffer, str, namelen);
38 return 0;
39}
40
41#define CHROOT_NSCONNECT (PATH_CHROOT_REL | PATH_CHROOT_NSCONNECT)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static int d_namespace_path(struct path *path, char *buf, int buflen,
58 char **name, int flags)
59{
60 char *res;
61 int error = 0;
62 int connected = 1;
63
64 if (path->mnt->mnt_flags & MNT_INTERNAL) {
65
66 res = dentry_path(path->dentry, buf, buflen);
67 *name = res;
68 if (IS_ERR(res)) {
69 *name = buf;
70 return PTR_ERR(res);
71 }
72 if (path->dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
73 strncmp(*name, "/sys/", 5) == 0) {
74
75
76
77 return prepend(name, *name - buf, "/proc", 5);
78 }
79 return 0;
80 }
81
82
83 if (flags & PATH_CHROOT_REL) {
84 struct path root;
85 get_fs_root(current->fs, &root);
86 res = __d_path(path, &root, buf, buflen);
87 if (res && !IS_ERR(res)) {
88
89 *name = res;
90 path_put(&root);
91 goto ok;
92 }
93 path_put(&root);
94 connected = 0;
95 }
96
97 res = d_absolute_path(path, buf, buflen);
98
99 *name = res;
100
101
102
103 if (IS_ERR(res)) {
104 error = PTR_ERR(res);
105 *name = buf;
106 goto out;
107 }
108 if (!our_mnt(path->mnt))
109 connected = 0;
110
111ok:
112
113
114
115
116
117
118 if (d_unlinked(path->dentry) && path->dentry->d_inode &&
119 !(flags & PATH_MEDIATE_DELETED)) {
120 error = -ENOENT;
121 goto out;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 if (!connected) {
136 if (!(flags & PATH_CONNECT_PATH) &&
137 !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
138 our_mnt(path->mnt))) {
139
140
141
142 error = -ESTALE;
143 if (*res == '/')
144 *name = res + 1;
145 }
146 }
147
148out:
149 return error;
150}
151
152
153
154
155
156
157
158
159
160
161
162static int get_name_to_buffer(struct path *path, int flags, char *buffer,
163 int size, char **name)
164{
165 int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
166 int error = d_namespace_path(path, buffer, size - adjust, name, flags);
167
168 if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
169
170
171
172
173 strcpy(&buffer[size - 2], "/");
174
175 return error;
176}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196int aa_get_name(struct path *path, int flags, char **buffer, const char **name)
197{
198 char *buf, *str = NULL;
199 int size = 256;
200 int error;
201
202 *name = NULL;
203 *buffer = NULL;
204 for (;;) {
205
206 buf = kmalloc(size, GFP_KERNEL);
207 if (!buf)
208 return -ENOMEM;
209
210 error = get_name_to_buffer(path, flags, buf, size, &str);
211 if (error != -ENAMETOOLONG)
212 break;
213
214 kfree(buf);
215 size <<= 1;
216 if (size > aa_g_path_max)
217 return -ENAMETOOLONG;
218 }
219 *buffer = buf;
220 *name = str;
221
222 return error;
223}
224