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