]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/9p/acl.c
[fs/9p] Plug potential acl leak
[mirror_ubuntu-zesty-kernel.git] / fs / 9p / acl.c
CommitLineData
85ff872d
AK
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <net/9p/9p.h>
18#include <net/9p/client.h>
19#include <linux/slab.h>
22d8dcdf 20#include <linux/sched.h>
85ff872d
AK
21#include <linux/posix_acl_xattr.h>
22#include "xattr.h"
23#include "acl.h"
22d8dcdf 24#include "v9fs_vfs.h"
76381a42 25#include "v9fs.h"
85ff872d
AK
26
27static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
28{
29 ssize_t size;
30 void *value = NULL;
009ca389 31 struct posix_acl *acl = NULL;
85ff872d
AK
32
33 size = v9fs_fid_xattr_get(fid, name, NULL, 0);
34 if (size > 0) {
35 value = kzalloc(size, GFP_NOFS);
36 if (!value)
37 return ERR_PTR(-ENOMEM);
38 size = v9fs_fid_xattr_get(fid, name, value, size);
39 if (size > 0) {
40 acl = posix_acl_from_xattr(value, size);
41 if (IS_ERR(acl))
42 goto err_out;
43 }
44 } else if (size == -ENODATA || size == 0 ||
45 size == -ENOSYS || size == -EOPNOTSUPP) {
46 acl = NULL;
47 } else
48 acl = ERR_PTR(-EIO);
49
50err_out:
51 kfree(value);
52 return acl;
53}
54
55int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
56{
57 int retval = 0;
58 struct posix_acl *pacl, *dacl;
76381a42 59 struct v9fs_session_info *v9ses;
85ff872d 60
76381a42
AK
61 v9ses = v9fs_inode2v9ses(inode);
62 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
63 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
64 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
65 return 0;
66 }
85ff872d
AK
67 /* get the default/access acl values and cache them */
68 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
69 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
70
71 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
72 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
73 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
85ff872d
AK
74 } else
75 retval = -EIO;
76
c61fa0d6
VJJ
77 if (!IS_ERR(dacl))
78 posix_acl_release(dacl);
79
80 if (!IS_ERR(pacl))
81 posix_acl_release(pacl);
82
85ff872d
AK
83 return retval;
84}
85
86static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
87{
88 struct posix_acl *acl;
89 /*
90 * 9p Always cache the acl value when
91 * instantiating the inode (v9fs_inode_from_fid)
92 */
93 acl = get_cached_acl(inode, type);
94 BUG_ON(acl == ACL_NOT_CACHED);
95 return acl;
96}
97
b74c79e9 98int v9fs_check_acl(struct inode *inode, int mask, unsigned int flags)
85ff872d 99{
76381a42
AK
100 struct posix_acl *acl;
101 struct v9fs_session_info *v9ses;
102
b74c79e9
NP
103 if (flags & IPERM_FLAG_RCU)
104 return -ECHILD;
105
76381a42
AK
106 v9ses = v9fs_inode2v9ses(inode);
107 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
108 /*
109 * On access = client mode get the acl
110 * values from the server
111 */
112 return 0;
113 }
114 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
85ff872d
AK
115
116 if (IS_ERR(acl))
117 return PTR_ERR(acl);
118 if (acl) {
119 int error = posix_acl_permission(inode, acl, mask);
120 posix_acl_release(acl);
121 return error;
122 }
123 return -EAGAIN;
124}
7a4566b0 125
6e8dc555
AK
126static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
127{
128 int retval;
129 char *name;
130 size_t size;
131 void *buffer;
132 struct inode *inode = dentry->d_inode;
133
134 set_cached_acl(inode, type, acl);
135 /* Set a setxattr request to server */
136 size = posix_acl_xattr_size(acl->a_count);
137 buffer = kmalloc(size, GFP_KERNEL);
138 if (!buffer)
139 return -ENOMEM;
140 retval = posix_acl_to_xattr(acl, buffer, size);
141 if (retval < 0)
142 goto err_free_out;
143 switch (type) {
144 case ACL_TYPE_ACCESS:
145 name = POSIX_ACL_XATTR_ACCESS;
146 break;
147 case ACL_TYPE_DEFAULT:
148 name = POSIX_ACL_XATTR_DEFAULT;
149 break;
150 default:
151 BUG();
152 }
153 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
154err_free_out:
155 kfree(buffer);
156 return retval;
157}
158
159int v9fs_acl_chmod(struct dentry *dentry)
160{
161 int retval = 0;
162 struct posix_acl *acl, *clone;
163 struct inode *inode = dentry->d_inode;
164
165 if (S_ISLNK(inode->i_mode))
166 return -EOPNOTSUPP;
167 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
168 if (acl) {
169 clone = posix_acl_clone(acl, GFP_KERNEL);
170 posix_acl_release(acl);
171 if (!clone)
172 return -ENOMEM;
173 retval = posix_acl_chmod_masq(clone, inode->i_mode);
174 if (!retval)
175 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
176 posix_acl_release(clone);
177 }
178 return retval;
179}
180
ad77dbce
AK
181int v9fs_set_create_acl(struct dentry *dentry,
182 struct posix_acl *dpacl, struct posix_acl *pacl)
183{
184 if (dpacl)
185 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
186 if (pacl)
187 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
188 posix_acl_release(dpacl);
189 posix_acl_release(pacl);
190 return 0;
191}
192
193int v9fs_acl_mode(struct inode *dir, mode_t *modep,
194 struct posix_acl **dpacl, struct posix_acl **pacl)
195{
196 int retval = 0;
197 mode_t mode = *modep;
198 struct posix_acl *acl = NULL;
199
200 if (!S_ISLNK(mode)) {
201 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
202 if (IS_ERR(acl))
203 return PTR_ERR(acl);
204 if (!acl)
205 mode &= ~current_umask();
206 }
207 if (acl) {
208 struct posix_acl *clone;
209
210 if (S_ISDIR(mode))
211 *dpacl = acl;
212 clone = posix_acl_clone(acl, GFP_NOFS);
213 retval = -ENOMEM;
214 if (!clone)
215 goto cleanup;
216
217 retval = posix_acl_create_masq(clone, &mode);
218 if (retval < 0) {
219 posix_acl_release(clone);
220 goto cleanup;
221 }
222 if (retval > 0)
223 *pacl = clone;
224 }
225 *modep = mode;
226 return 0;
227cleanup:
228 posix_acl_release(acl);
229 return retval;
230
231}
232
76381a42
AK
233static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
234 void *buffer, size_t size, int type)
235{
236 char *full_name;
237
238 switch (type) {
239 case ACL_TYPE_ACCESS:
240 full_name = POSIX_ACL_XATTR_ACCESS;
241 break;
242 case ACL_TYPE_DEFAULT:
243 full_name = POSIX_ACL_XATTR_DEFAULT;
244 break;
245 default:
246 BUG();
247 }
248 return v9fs_xattr_get(dentry, full_name, buffer, size);
249}
250
7a4566b0
AK
251static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
252 void *buffer, size_t size, int type)
253{
76381a42 254 struct v9fs_session_info *v9ses;
7a4566b0
AK
255 struct posix_acl *acl;
256 int error;
257
258 if (strcmp(name, "") != 0)
259 return -EINVAL;
260
76381a42
AK
261 v9ses = v9fs_inode2v9ses(dentry->d_inode);
262 /*
263 * We allow set/get/list of acl when access=client is not specified
264 */
265 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
266 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
267
7a4566b0
AK
268 acl = v9fs_get_cached_acl(dentry->d_inode, type);
269 if (IS_ERR(acl))
270 return PTR_ERR(acl);
271 if (acl == NULL)
272 return -ENODATA;
273 error = posix_acl_to_xattr(acl, buffer, size);
274 posix_acl_release(acl);
275
276 return error;
277}
278
76381a42
AK
279static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
280 const void *value, size_t size,
281 int flags, int type)
282{
283 char *full_name;
284
285 switch (type) {
286 case ACL_TYPE_ACCESS:
287 full_name = POSIX_ACL_XATTR_ACCESS;
288 break;
289 case ACL_TYPE_DEFAULT:
290 full_name = POSIX_ACL_XATTR_DEFAULT;
291 break;
292 default:
293 BUG();
294 }
295 return v9fs_xattr_set(dentry, full_name, value, size, flags);
296}
297
298
7a4566b0
AK
299static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
300 const void *value, size_t size,
301 int flags, int type)
302{
22d8dcdf
AK
303 int retval;
304 struct posix_acl *acl;
76381a42 305 struct v9fs_session_info *v9ses;
22d8dcdf
AK
306 struct inode *inode = dentry->d_inode;
307
308 if (strcmp(name, "") != 0)
309 return -EINVAL;
76381a42
AK
310
311 v9ses = v9fs_inode2v9ses(dentry->d_inode);
312 /*
313 * set the attribute on the remote. Without even looking at the
314 * xattr value. We leave it to the server to validate
315 */
316 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
317 return v9fs_remote_set_acl(dentry, name,
318 value, size, flags, type);
319
22d8dcdf
AK
320 if (S_ISLNK(inode->i_mode))
321 return -EOPNOTSUPP;
322 if (!is_owner_or_cap(inode))
323 return -EPERM;
324 if (value) {
325 /* update the cached acl value */
326 acl = posix_acl_from_xattr(value, size);
327 if (IS_ERR(acl))
328 return PTR_ERR(acl);
329 else if (acl) {
330 retval = posix_acl_valid(acl);
331 if (retval)
332 goto err_out;
333 }
334 } else
335 acl = NULL;
336
337 switch (type) {
338 case ACL_TYPE_ACCESS:
339 name = POSIX_ACL_XATTR_ACCESS;
340 if (acl) {
341 mode_t mode = inode->i_mode;
342 retval = posix_acl_equiv_mode(acl, &mode);
343 if (retval < 0)
344 goto err_out;
345 else {
346 struct iattr iattr;
347 if (retval == 0) {
348 /*
349 * ACL can be represented
350 * by the mode bits. So don't
351 * update ACL.
352 */
353 acl = NULL;
354 value = NULL;
355 size = 0;
356 }
357 /* Updte the mode bits */
358 iattr.ia_mode = ((mode & S_IALLUGO) |
359 (inode->i_mode & ~S_IALLUGO));
360 iattr.ia_valid = ATTR_MODE;
361 /* FIXME should we update ctime ?
362 * What is the following setxattr update the
363 * mode ?
364 */
365 v9fs_vfs_setattr_dotl(dentry, &iattr);
366 }
367 }
368 break;
369 case ACL_TYPE_DEFAULT:
370 name = POSIX_ACL_XATTR_DEFAULT;
371 if (!S_ISDIR(inode->i_mode)) {
6f81c115 372 retval = acl ? -EINVAL : 0;
22d8dcdf
AK
373 goto err_out;
374 }
375 break;
376 default:
377 BUG();
378 }
379 retval = v9fs_xattr_set(dentry, name, value, size, flags);
380 if (!retval)
381 set_cached_acl(inode, type, acl);
382err_out:
383 posix_acl_release(acl);
384 return retval;
7a4566b0
AK
385}
386
387const struct xattr_handler v9fs_xattr_acl_access_handler = {
388 .prefix = POSIX_ACL_XATTR_ACCESS,
389 .flags = ACL_TYPE_ACCESS,
390 .get = v9fs_xattr_get_acl,
391 .set = v9fs_xattr_set_acl,
392};
393
394const struct xattr_handler v9fs_xattr_acl_default_handler = {
395 .prefix = POSIX_ACL_XATTR_DEFAULT,
396 .flags = ACL_TYPE_DEFAULT,
397 .get = v9fs_xattr_get_acl,
398 .set = v9fs_xattr_set_acl,
399};