[JFFS2] Fix ACL vs. mode handling.
[safe/jmp/linux-2.6] / fs / jffs2 / acl.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2006  NEC Corporation
5  *
6  * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>
17 #include <linux/crc32.h>
18 #include <linux/jffs2.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl_xattr.h>
21 #include <linux/mtd/mtd.h>
22 #include "nodelist.h"
23
24 static size_t jffs2_acl_size(int count)
25 {
26         if (count <= 4) {
27                 return sizeof(struct jffs2_acl_header)
28                        + count * sizeof(struct jffs2_acl_entry_short);
29         } else {
30                 return sizeof(struct jffs2_acl_header)
31                        + 4 * sizeof(struct jffs2_acl_entry_short)
32                        + (count - 4) * sizeof(struct jffs2_acl_entry);
33         }
34 }
35
36 static int jffs2_acl_count(size_t size)
37 {
38         size_t s;
39
40         size -= sizeof(struct jffs2_acl_header);
41         s = size - 4 * sizeof(struct jffs2_acl_entry_short);
42         if (s < 0) {
43                 if (size % sizeof(struct jffs2_acl_entry_short))
44                         return -1;
45                 return size / sizeof(struct jffs2_acl_entry_short);
46         } else {
47                 if (s % sizeof(struct jffs2_acl_entry))
48                         return -1;
49                 return s / sizeof(struct jffs2_acl_entry) + 4;
50         }
51 }
52
53 static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
54 {
55         void *end = value + size;
56         struct jffs2_acl_header *header = value;
57         struct jffs2_acl_entry *entry;
58         struct posix_acl *acl;
59         uint32_t ver;
60         int i, count;
61
62         if (!value)
63                 return NULL;
64         if (size < sizeof(struct jffs2_acl_header))
65                 return ERR_PTR(-EINVAL);
66         ver = je32_to_cpu(header->a_version);
67         if (ver != JFFS2_ACL_VERSION) {
68                 JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
69                 return ERR_PTR(-EINVAL);
70         }
71
72         value += sizeof(struct jffs2_acl_header);
73         count = jffs2_acl_count(size);
74         if (count < 0)
75                 return ERR_PTR(-EINVAL);
76         if (count == 0)
77                 return NULL;
78
79         acl = posix_acl_alloc(count, GFP_KERNEL);
80         if (!acl)
81                 return ERR_PTR(-ENOMEM);
82
83         for (i=0; i < count; i++) {
84                 entry = value;
85                 if (value + sizeof(struct jffs2_acl_entry_short) > end)
86                         goto fail;
87                 acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
88                 acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
89                 switch (acl->a_entries[i].e_tag) {
90                         case ACL_USER_OBJ:
91                         case ACL_GROUP_OBJ:
92                         case ACL_MASK:
93                         case ACL_OTHER:
94                                 value += sizeof(struct jffs2_acl_entry_short);
95                                 acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
96                                 break;
97
98                         case ACL_USER:
99                         case ACL_GROUP:
100                                 value += sizeof(struct jffs2_acl_entry);
101                                 if (value > end)
102                                         goto fail;
103                                 acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
104                                 break;
105
106                         default:
107                                 goto fail;
108                 }
109         }
110         if (value != end)
111                 goto fail;
112         return acl;
113  fail:
114         posix_acl_release(acl);
115         return ERR_PTR(-EINVAL);
116 }
117
118 static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
119 {
120         struct jffs2_acl_header *header;
121         struct jffs2_acl_entry *entry;
122         void *e;
123         size_t i;
124
125         *size = jffs2_acl_size(acl->a_count);
126         header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
127         if (!header)
128                 return ERR_PTR(-ENOMEM);
129         header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
130         e = header + 1;
131         for (i=0; i < acl->a_count; i++) {
132                 entry = e;
133                 entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
134                 entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
135                 switch(acl->a_entries[i].e_tag) {
136                         case ACL_USER:
137                         case ACL_GROUP:
138                                 entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
139                                 e += sizeof(struct jffs2_acl_entry);
140                                 break;
141
142                         case ACL_USER_OBJ:
143                         case ACL_GROUP_OBJ:
144                         case ACL_MASK:
145                         case ACL_OTHER:
146                                 e += sizeof(struct jffs2_acl_entry_short);
147                                 break;
148
149                         default:
150                                 goto fail;
151                 }
152         }
153         return header;
154  fail:
155         kfree(header);
156         return ERR_PTR(-EINVAL);
157 }
158
159 static struct posix_acl *jffs2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
160 {
161         struct posix_acl *acl = JFFS2_ACL_NOT_CACHED;
162
163         spin_lock(&inode->i_lock);
164         if (*i_acl != JFFS2_ACL_NOT_CACHED)
165                 acl = posix_acl_dup(*i_acl);
166         spin_unlock(&inode->i_lock);
167         return acl;
168 }
169
170 static void jffs2_iset_acl(struct inode *inode, struct posix_acl **i_acl, struct posix_acl *acl)
171 {
172         spin_lock(&inode->i_lock);
173         if (*i_acl != JFFS2_ACL_NOT_CACHED)
174                 posix_acl_release(*i_acl);
175         *i_acl = posix_acl_dup(acl);
176         spin_unlock(&inode->i_lock);
177 }
178
179 struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
180 {
181         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
182         struct posix_acl *acl;
183         char *value = NULL;
184         int rc, xprefix;
185
186         switch (type) {
187         case ACL_TYPE_ACCESS:
188                 acl = jffs2_iget_acl(inode, &f->i_acl_access);
189                 if (acl != JFFS2_ACL_NOT_CACHED)
190                         return acl;
191                 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
192                 break;
193         case ACL_TYPE_DEFAULT:
194                 acl = jffs2_iget_acl(inode, &f->i_acl_default);
195                 if (acl != JFFS2_ACL_NOT_CACHED)
196                         return acl;
197                 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
198                 break;
199         default:
200                 return ERR_PTR(-EINVAL);
201         }
202         rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
203         if (rc > 0) {
204                 value = kmalloc(rc, GFP_KERNEL);
205                 if (!value)
206                         return ERR_PTR(-ENOMEM);
207                 rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
208         }
209         if (rc > 0) {
210                 acl = jffs2_acl_from_medium(value, rc);
211         } else if (rc == -ENODATA || rc == -ENOSYS) {
212                 acl = NULL;
213         } else {
214                 acl = ERR_PTR(rc);
215         }
216         if (value)
217                 kfree(value);
218         if (!IS_ERR(acl)) {
219                 switch (type) {
220                 case ACL_TYPE_ACCESS:
221                         jffs2_iset_acl(inode, &f->i_acl_access, acl);
222                         break;
223                 case ACL_TYPE_DEFAULT:
224                         jffs2_iset_acl(inode, &f->i_acl_default, acl);
225                         break;
226                 }
227         }
228         return acl;
229 }
230
231 static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
232 {
233         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
234         size_t size = 0;
235         char *value = NULL;
236         int rc, xprefix;
237
238         if (S_ISLNK(inode->i_mode))
239                 return -EOPNOTSUPP;
240
241         switch (type) {
242         case ACL_TYPE_ACCESS:
243                 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
244                 if (acl) {
245                         mode_t mode = inode->i_mode;
246                         rc = posix_acl_equiv_mode(acl, &mode);
247                         if (rc < 0)
248                                 return rc;
249                         if (inode->i_mode != mode) {
250                                 struct iattr attr;
251
252                                 attr.ia_valid = ATTR_MODE;
253                                 attr.ia_mode = mode;
254                                 rc = jffs2_do_setattr(inode, &attr);
255                                 if (rc < 0)
256                                         return rc;
257                         }
258                         if (rc == 0)
259                                 acl = NULL;
260                 }
261                 break;
262         case ACL_TYPE_DEFAULT:
263                 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
264                 if (!S_ISDIR(inode->i_mode))
265                         return acl ? -EACCES : 0;
266                 break;
267         default:
268                 return -EINVAL;
269         }
270         if (acl) {
271                 value = jffs2_acl_to_medium(acl, &size);
272                 if (IS_ERR(value))
273                         return PTR_ERR(value);
274         }
275
276         rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
277         if (!value && rc == -ENODATA)
278                 rc = 0;
279         if (value)
280                 kfree(value);
281         if (!rc) {
282                 switch(type) {
283                 case ACL_TYPE_ACCESS:
284                         jffs2_iset_acl(inode, &f->i_acl_access, acl);
285                         break;
286                 case ACL_TYPE_DEFAULT:
287                         jffs2_iset_acl(inode, &f->i_acl_default, acl);
288                         break;
289                 }
290         }
291         return rc;
292 }
293
294 static int jffs2_check_acl(struct inode *inode, int mask)
295 {
296         struct posix_acl *acl;
297         int rc;
298
299         acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
300         if (IS_ERR(acl))
301                 return PTR_ERR(acl);
302         if (acl) {
303                 rc = posix_acl_permission(inode, acl, mask);
304                 posix_acl_release(acl);
305                 return rc;
306         }
307         return -EAGAIN;
308 }
309
310 int jffs2_permission(struct inode *inode, int mask, struct nameidata *nd)
311 {
312         return generic_permission(inode, mask, jffs2_check_acl);
313 }
314
315 int jffs2_init_acl(struct inode *inode, struct posix_acl *acl)
316 {
317         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
318         struct posix_acl *clone;
319         mode_t mode;
320         int rc = 0;
321
322         f->i_acl_access = JFFS2_ACL_NOT_CACHED;
323         f->i_acl_default = JFFS2_ACL_NOT_CACHED;
324
325         if (acl) {
326                 if (S_ISDIR(inode->i_mode)) {
327                         rc = jffs2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
328                         if (rc)
329                                 goto cleanup;
330                 }
331                 clone = posix_acl_clone(acl, GFP_KERNEL);
332                 rc = -ENOMEM;
333                 if (!clone)
334                         goto cleanup;
335                 mode = inode->i_mode;
336                 rc = posix_acl_create_masq(clone, &mode);
337                 if (rc >= 0) {
338                         inode->i_mode = mode;
339                         if (rc > 0)
340                                 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
341                 }
342                 posix_acl_release(clone);
343         }
344  cleanup:
345         posix_acl_release(acl);
346         return rc;
347 }
348
349 void jffs2_clear_acl(struct jffs2_inode_info *f)
350 {
351         if (f->i_acl_access && f->i_acl_access != JFFS2_ACL_NOT_CACHED) {
352                 posix_acl_release(f->i_acl_access);
353                 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
354         }
355         if (f->i_acl_default && f->i_acl_default != JFFS2_ACL_NOT_CACHED) {
356                 posix_acl_release(f->i_acl_default);
357                 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
358         }
359 }
360
361 int jffs2_acl_chmod(struct inode *inode)
362 {
363         struct posix_acl *acl, *clone;
364         int rc;
365
366         if (S_ISLNK(inode->i_mode))
367                 return -EOPNOTSUPP;
368         acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
369         if (IS_ERR(acl) || !acl)
370                 return PTR_ERR(acl);
371         clone = posix_acl_clone(acl, GFP_KERNEL);
372         posix_acl_release(acl);
373         if (!clone)
374                 return -ENOMEM;
375         rc = posix_acl_chmod_masq(clone, inode->i_mode);
376         if (!rc)
377                 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
378         posix_acl_release(clone);
379         return rc;
380 }
381
382 static size_t jffs2_acl_access_listxattr(struct inode *inode, char *list, size_t list_size,
383                                          const char *name, size_t name_len)
384 {
385         const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
386
387         if (list && retlen <= list_size)
388                 strcpy(list, POSIX_ACL_XATTR_ACCESS);
389         return retlen;
390 }
391
392 static size_t jffs2_acl_default_listxattr(struct inode *inode, char *list, size_t list_size,
393                                           const char *name, size_t name_len)
394 {
395         const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
396
397         if (list && retlen <= list_size)
398                 strcpy(list, POSIX_ACL_XATTR_DEFAULT);
399         return retlen;
400 }
401
402 static int jffs2_acl_getxattr(struct inode *inode, int type, void *buffer, size_t size)
403 {
404         struct posix_acl *acl;
405         int rc;
406
407         acl = jffs2_get_acl(inode, type);
408         if (IS_ERR(acl))
409                 return PTR_ERR(acl);
410         if (!acl)
411                 return -ENODATA;
412         rc = posix_acl_to_xattr(acl, buffer, size);
413         posix_acl_release(acl);
414
415         return rc;
416 }
417
418 static int jffs2_acl_access_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
419 {
420         if (name[0] != '\0')
421                 return -EINVAL;
422         return jffs2_acl_getxattr(inode, ACL_TYPE_ACCESS, buffer, size);
423 }
424
425 static int jffs2_acl_default_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
426 {
427         if (name[0] != '\0')
428                 return -EINVAL;
429         return jffs2_acl_getxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
430 }
431
432 static int jffs2_acl_setxattr(struct inode *inode, int type, const void *value, size_t size)
433 {
434         struct posix_acl *acl;
435         int rc;
436
437         if (!is_owner_or_cap(inode))
438                 return -EPERM;
439
440         if (value) {
441                 acl = posix_acl_from_xattr(value, size);
442                 if (IS_ERR(acl))
443                         return PTR_ERR(acl);
444                 if (acl) {
445                         rc = posix_acl_valid(acl);
446                         if (rc)
447                                 goto out;
448                 }
449         } else {
450                 acl = NULL;
451         }
452         rc = jffs2_set_acl(inode, type, acl);
453  out:
454         posix_acl_release(acl);
455         return rc;
456 }
457
458 static int jffs2_acl_access_setxattr(struct inode *inode, const char *name,
459                                      const void *buffer, size_t size, int flags)
460 {
461         if (name[0] != '\0')
462                 return -EINVAL;
463         return jffs2_acl_setxattr(inode, ACL_TYPE_ACCESS, buffer, size);
464 }
465
466 static int jffs2_acl_default_setxattr(struct inode *inode, const char *name,
467                                       const void *buffer, size_t size, int flags)
468 {
469         if (name[0] != '\0')
470                 return -EINVAL;
471         return jffs2_acl_setxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
472 }
473
474 struct xattr_handler jffs2_acl_access_xattr_handler = {
475         .prefix = POSIX_ACL_XATTR_ACCESS,
476         .list   = jffs2_acl_access_listxattr,
477         .get    = jffs2_acl_access_getxattr,
478         .set    = jffs2_acl_access_setxattr,
479 };
480
481 struct xattr_handler jffs2_acl_default_xattr_handler = {
482         .prefix = POSIX_ACL_XATTR_DEFAULT,
483         .list   = jffs2_acl_default_listxattr,
484         .get    = jffs2_acl_default_getxattr,
485         .set    = jffs2_acl_default_setxattr,
486 };