9p: close ACL leaks
[deliverable/linux.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"
76381a42 24#include "v9fs.h"
5ffc0cb3 25#include "v9fs_vfs.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 61 v9ses = v9fs_inode2v9ses(inode);
e782ef71
VJJ
62 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
63 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42
AK
64 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
65 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
66 return 0;
67 }
85ff872d
AK
68 /* get the default/access acl values and cache them */
69 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
70 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
71
72 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
73 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
74 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
85ff872d
AK
75 } else
76 retval = -EIO;
77
c61fa0d6
VJJ
78 if (!IS_ERR(dacl))
79 posix_acl_release(dacl);
80
81 if (!IS_ERR(pacl))
82 posix_acl_release(pacl);
83
85ff872d
AK
84 return retval;
85}
86
87static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
88{
89 struct posix_acl *acl;
90 /*
91 * 9p Always cache the acl value when
92 * instantiating the inode (v9fs_inode_from_fid)
93 */
94 acl = get_cached_acl(inode, type);
95 BUG_ON(acl == ACL_NOT_CACHED);
96 return acl;
97}
98
7e40145e 99int v9fs_check_acl(struct inode *inode, int mask)
85ff872d 100{
76381a42
AK
101 struct posix_acl *acl;
102 struct v9fs_session_info *v9ses;
103
9c2c7039 104 if (mask & MAY_NOT_BLOCK)
b74c79e9
NP
105 return -ECHILD;
106
76381a42 107 v9ses = v9fs_inode2v9ses(inode);
e782ef71
VJJ
108 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
109 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42 110 /*
e782ef71 111 * On access = client and acl = on mode get the acl
76381a42
AK
112 * values from the server
113 */
114 return 0;
115 }
116 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
85ff872d
AK
117
118 if (IS_ERR(acl))
119 return PTR_ERR(acl);
120 if (acl) {
121 int error = posix_acl_permission(inode, acl, mask);
122 posix_acl_release(acl);
123 return error;
124 }
125 return -EAGAIN;
126}
7a4566b0 127
6e8dc555
AK
128static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
129{
130 int retval;
131 char *name;
132 size_t size;
133 void *buffer;
134 struct inode *inode = dentry->d_inode;
135
136 set_cached_acl(inode, type, acl);
d344b0fb
VJJ
137
138 if (!acl)
139 return 0;
140
6e8dc555
AK
141 /* Set a setxattr request to server */
142 size = posix_acl_xattr_size(acl->a_count);
143 buffer = kmalloc(size, GFP_KERNEL);
144 if (!buffer)
145 return -ENOMEM;
146 retval = posix_acl_to_xattr(acl, buffer, size);
147 if (retval < 0)
148 goto err_free_out;
149 switch (type) {
150 case ACL_TYPE_ACCESS:
151 name = POSIX_ACL_XATTR_ACCESS;
152 break;
153 case ACL_TYPE_DEFAULT:
154 name = POSIX_ACL_XATTR_DEFAULT;
155 break;
156 default:
157 BUG();
158 }
159 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
160err_free_out:
161 kfree(buffer);
162 return retval;
163}
164
165int v9fs_acl_chmod(struct dentry *dentry)
166{
167 int retval = 0;
168 struct posix_acl *acl, *clone;
169 struct inode *inode = dentry->d_inode;
170
171 if (S_ISLNK(inode->i_mode))
172 return -EOPNOTSUPP;
173 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
174 if (acl) {
175 clone = posix_acl_clone(acl, GFP_KERNEL);
176 posix_acl_release(acl);
177 if (!clone)
178 return -ENOMEM;
179 retval = posix_acl_chmod_masq(clone, inode->i_mode);
180 if (!retval)
181 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
182 posix_acl_release(clone);
183 }
184 return retval;
185}
186
ad77dbce 187int v9fs_set_create_acl(struct dentry *dentry,
1ec95bf3 188 struct posix_acl **dpacl, struct posix_acl **pacl)
ad77dbce 189{
1ec95bf3
AV
190 if (dentry) {
191 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
192 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
193 }
194 posix_acl_release(*dpacl);
195 posix_acl_release(*pacl);
196 *dpacl = *pacl = NULL;
ad77dbce
AK
197 return 0;
198}
199
200int v9fs_acl_mode(struct inode *dir, mode_t *modep,
201 struct posix_acl **dpacl, struct posix_acl **pacl)
202{
203 int retval = 0;
204 mode_t mode = *modep;
205 struct posix_acl *acl = NULL;
206
207 if (!S_ISLNK(mode)) {
208 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
209 if (IS_ERR(acl))
210 return PTR_ERR(acl);
211 if (!acl)
212 mode &= ~current_umask();
213 }
214 if (acl) {
215 struct posix_acl *clone;
216
217 if (S_ISDIR(mode))
1ec95bf3 218 *dpacl = posix_acl_dup(acl);
ad77dbce 219 clone = posix_acl_clone(acl, GFP_NOFS);
1ec95bf3 220 posix_acl_release(acl);
ad77dbce 221 if (!clone)
1ec95bf3 222 return -ENOMEM;
ad77dbce
AK
223
224 retval = posix_acl_create_masq(clone, &mode);
225 if (retval < 0) {
226 posix_acl_release(clone);
227 goto cleanup;
228 }
229 if (retval > 0)
230 *pacl = clone;
1ec95bf3
AV
231 else
232 posix_acl_release(clone);
ad77dbce
AK
233 }
234 *modep = mode;
235 return 0;
236cleanup:
ad77dbce
AK
237 return retval;
238
239}
240
76381a42
AK
241static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
242 void *buffer, size_t size, int type)
243{
244 char *full_name;
245
246 switch (type) {
247 case ACL_TYPE_ACCESS:
248 full_name = POSIX_ACL_XATTR_ACCESS;
249 break;
250 case ACL_TYPE_DEFAULT:
251 full_name = POSIX_ACL_XATTR_DEFAULT;
252 break;
253 default:
254 BUG();
255 }
256 return v9fs_xattr_get(dentry, full_name, buffer, size);
257}
258
7a4566b0
AK
259static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
260 void *buffer, size_t size, int type)
261{
76381a42 262 struct v9fs_session_info *v9ses;
7a4566b0
AK
263 struct posix_acl *acl;
264 int error;
265
266 if (strcmp(name, "") != 0)
267 return -EINVAL;
268
42869c8a 269 v9ses = v9fs_dentry2v9ses(dentry);
76381a42
AK
270 /*
271 * We allow set/get/list of acl when access=client is not specified
272 */
273 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
274 return v9fs_remote_get_acl(dentry, name, buffer, size, type);
275
7a4566b0
AK
276 acl = v9fs_get_cached_acl(dentry->d_inode, type);
277 if (IS_ERR(acl))
278 return PTR_ERR(acl);
279 if (acl == NULL)
280 return -ENODATA;
281 error = posix_acl_to_xattr(acl, buffer, size);
282 posix_acl_release(acl);
283
284 return error;
285}
286
76381a42
AK
287static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
288 const void *value, size_t size,
289 int flags, int type)
290{
291 char *full_name;
292
293 switch (type) {
294 case ACL_TYPE_ACCESS:
295 full_name = POSIX_ACL_XATTR_ACCESS;
296 break;
297 case ACL_TYPE_DEFAULT:
298 full_name = POSIX_ACL_XATTR_DEFAULT;
299 break;
300 default:
301 BUG();
302 }
303 return v9fs_xattr_set(dentry, full_name, value, size, flags);
304}
305
306
7a4566b0
AK
307static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
308 const void *value, size_t size,
309 int flags, int type)
310{
22d8dcdf
AK
311 int retval;
312 struct posix_acl *acl;
76381a42 313 struct v9fs_session_info *v9ses;
22d8dcdf
AK
314 struct inode *inode = dentry->d_inode;
315
316 if (strcmp(name, "") != 0)
317 return -EINVAL;
76381a42 318
42869c8a 319 v9ses = v9fs_dentry2v9ses(dentry);
76381a42
AK
320 /*
321 * set the attribute on the remote. Without even looking at the
322 * xattr value. We leave it to the server to validate
323 */
324 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
325 return v9fs_remote_set_acl(dentry, name,
326 value, size, flags, type);
327
22d8dcdf
AK
328 if (S_ISLNK(inode->i_mode))
329 return -EOPNOTSUPP;
2e149670 330 if (!inode_owner_or_capable(inode))
22d8dcdf
AK
331 return -EPERM;
332 if (value) {
333 /* update the cached acl value */
334 acl = posix_acl_from_xattr(value, size);
335 if (IS_ERR(acl))
336 return PTR_ERR(acl);
337 else if (acl) {
338 retval = posix_acl_valid(acl);
339 if (retval)
340 goto err_out;
341 }
342 } else
343 acl = NULL;
344
345 switch (type) {
346 case ACL_TYPE_ACCESS:
347 name = POSIX_ACL_XATTR_ACCESS;
348 if (acl) {
349 mode_t mode = inode->i_mode;
350 retval = posix_acl_equiv_mode(acl, &mode);
351 if (retval < 0)
352 goto err_out;
353 else {
354 struct iattr iattr;
355 if (retval == 0) {
356 /*
357 * ACL can be represented
358 * by the mode bits. So don't
359 * update ACL.
360 */
361 acl = NULL;
362 value = NULL;
363 size = 0;
364 }
365 /* Updte the mode bits */
366 iattr.ia_mode = ((mode & S_IALLUGO) |
367 (inode->i_mode & ~S_IALLUGO));
368 iattr.ia_valid = ATTR_MODE;
369 /* FIXME should we update ctime ?
370 * What is the following setxattr update the
371 * mode ?
372 */
373 v9fs_vfs_setattr_dotl(dentry, &iattr);
374 }
375 }
376 break;
377 case ACL_TYPE_DEFAULT:
378 name = POSIX_ACL_XATTR_DEFAULT;
379 if (!S_ISDIR(inode->i_mode)) {
6f81c115 380 retval = acl ? -EINVAL : 0;
22d8dcdf
AK
381 goto err_out;
382 }
383 break;
384 default:
385 BUG();
386 }
387 retval = v9fs_xattr_set(dentry, name, value, size, flags);
388 if (!retval)
389 set_cached_acl(inode, type, acl);
390err_out:
391 posix_acl_release(acl);
392 return retval;
7a4566b0
AK
393}
394
395const struct xattr_handler v9fs_xattr_acl_access_handler = {
396 .prefix = POSIX_ACL_XATTR_ACCESS,
397 .flags = ACL_TYPE_ACCESS,
398 .get = v9fs_xattr_get_acl,
399 .set = v9fs_xattr_set_acl,
400};
401
402const struct xattr_handler v9fs_xattr_acl_default_handler = {
403 .prefix = POSIX_ACL_XATTR_DEFAULT,
404 .flags = ACL_TYPE_DEFAULT,
405 .get = v9fs_xattr_get_acl,
406 .set = v9fs_xattr_set_acl,
407};
This page took 0.171606 seconds and 5 git commands to generate.