posix_acl: Clear SGID bit when setting file permissions
[firefly-linux-kernel-4.4.55.git] / fs / f2fs / acl.c
1 /*
2  * fs/f2fs/acl.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * Portions of this code from linux/fs/ext2/acl.c
8  *
9  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 #include <linux/f2fs_fs.h>
16 #include "f2fs.h"
17 #include "xattr.h"
18 #include "acl.h"
19
20 static inline size_t f2fs_acl_size(int count)
21 {
22         if (count <= 4) {
23                 return sizeof(struct f2fs_acl_header) +
24                         count * sizeof(struct f2fs_acl_entry_short);
25         } else {
26                 return sizeof(struct f2fs_acl_header) +
27                         4 * sizeof(struct f2fs_acl_entry_short) +
28                         (count - 4) * sizeof(struct f2fs_acl_entry);
29         }
30 }
31
32 static inline int f2fs_acl_count(size_t size)
33 {
34         ssize_t s;
35         size -= sizeof(struct f2fs_acl_header);
36         s = size - 4 * sizeof(struct f2fs_acl_entry_short);
37         if (s < 0) {
38                 if (size % sizeof(struct f2fs_acl_entry_short))
39                         return -1;
40                 return size / sizeof(struct f2fs_acl_entry_short);
41         } else {
42                 if (s % sizeof(struct f2fs_acl_entry))
43                         return -1;
44                 return s / sizeof(struct f2fs_acl_entry) + 4;
45         }
46 }
47
48 static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
49 {
50         int i, count;
51         struct posix_acl *acl;
52         struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
53         struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
54         const char *end = value + size;
55
56         if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
57                 return ERR_PTR(-EINVAL);
58
59         count = f2fs_acl_count(size);
60         if (count < 0)
61                 return ERR_PTR(-EINVAL);
62         if (count == 0)
63                 return NULL;
64
65         acl = posix_acl_alloc(count, GFP_NOFS);
66         if (!acl)
67                 return ERR_PTR(-ENOMEM);
68
69         for (i = 0; i < count; i++) {
70
71                 if ((char *)entry > end)
72                         goto fail;
73
74                 acl->a_entries[i].e_tag  = le16_to_cpu(entry->e_tag);
75                 acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
76
77                 switch (acl->a_entries[i].e_tag) {
78                 case ACL_USER_OBJ:
79                 case ACL_GROUP_OBJ:
80                 case ACL_MASK:
81                 case ACL_OTHER:
82                         entry = (struct f2fs_acl_entry *)((char *)entry +
83                                         sizeof(struct f2fs_acl_entry_short));
84                         break;
85
86                 case ACL_USER:
87                         acl->a_entries[i].e_uid =
88                                 make_kuid(&init_user_ns,
89                                                 le32_to_cpu(entry->e_id));
90                         entry = (struct f2fs_acl_entry *)((char *)entry +
91                                         sizeof(struct f2fs_acl_entry));
92                         break;
93                 case ACL_GROUP:
94                         acl->a_entries[i].e_gid =
95                                 make_kgid(&init_user_ns,
96                                                 le32_to_cpu(entry->e_id));
97                         entry = (struct f2fs_acl_entry *)((char *)entry +
98                                         sizeof(struct f2fs_acl_entry));
99                         break;
100                 default:
101                         goto fail;
102                 }
103         }
104         if ((char *)entry != end)
105                 goto fail;
106         return acl;
107 fail:
108         posix_acl_release(acl);
109         return ERR_PTR(-EINVAL);
110 }
111
112 static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
113 {
114         struct f2fs_acl_header *f2fs_acl;
115         struct f2fs_acl_entry *entry;
116         int i;
117
118         f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
119                         sizeof(struct f2fs_acl_entry), GFP_NOFS);
120         if (!f2fs_acl)
121                 return ERR_PTR(-ENOMEM);
122
123         f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
124         entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
125
126         for (i = 0; i < acl->a_count; i++) {
127
128                 entry->e_tag  = cpu_to_le16(acl->a_entries[i].e_tag);
129                 entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
130
131                 switch (acl->a_entries[i].e_tag) {
132                 case ACL_USER:
133                         entry->e_id = cpu_to_le32(
134                                         from_kuid(&init_user_ns,
135                                                 acl->a_entries[i].e_uid));
136                         entry = (struct f2fs_acl_entry *)((char *)entry +
137                                         sizeof(struct f2fs_acl_entry));
138                         break;
139                 case ACL_GROUP:
140                         entry->e_id = cpu_to_le32(
141                                         from_kgid(&init_user_ns,
142                                                 acl->a_entries[i].e_gid));
143                         entry = (struct f2fs_acl_entry *)((char *)entry +
144                                         sizeof(struct f2fs_acl_entry));
145                         break;
146                 case ACL_USER_OBJ:
147                 case ACL_GROUP_OBJ:
148                 case ACL_MASK:
149                 case ACL_OTHER:
150                         entry = (struct f2fs_acl_entry *)((char *)entry +
151                                         sizeof(struct f2fs_acl_entry_short));
152                         break;
153                 default:
154                         goto fail;
155                 }
156         }
157         *size = f2fs_acl_size(acl->a_count);
158         return (void *)f2fs_acl;
159
160 fail:
161         kfree(f2fs_acl);
162         return ERR_PTR(-EINVAL);
163 }
164
165 static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
166                                                 struct page *dpage)
167 {
168         int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
169         void *value = NULL;
170         struct posix_acl *acl;
171         int retval;
172
173         if (type == ACL_TYPE_ACCESS)
174                 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
175
176         retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
177         if (retval > 0) {
178                 value = kmalloc(retval, GFP_F2FS_ZERO);
179                 if (!value)
180                         return ERR_PTR(-ENOMEM);
181                 retval = f2fs_getxattr(inode, name_index, "", value,
182                                                         retval, dpage);
183         }
184
185         if (retval > 0)
186                 acl = f2fs_acl_from_disk(value, retval);
187         else if (retval == -ENODATA)
188                 acl = NULL;
189         else
190                 acl = ERR_PTR(retval);
191         kfree(value);
192
193         if (!IS_ERR(acl))
194                 set_cached_acl(inode, type, acl);
195
196         return acl;
197 }
198
199 struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
200 {
201         return __f2fs_get_acl(inode, type, NULL);
202 }
203
204 static int __f2fs_set_acl(struct inode *inode, int type,
205                         struct posix_acl *acl, struct page *ipage)
206 {
207         struct f2fs_inode_info *fi = F2FS_I(inode);
208         int name_index;
209         void *value = NULL;
210         size_t size = 0;
211         int error;
212
213         switch (type) {
214         case ACL_TYPE_ACCESS:
215                 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
216                 if (acl) {
217                         error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
218                         if (error)
219                                 return error;
220                         set_acl_inode(fi, inode->i_mode);
221                 }
222                 break;
223
224         case ACL_TYPE_DEFAULT:
225                 name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
226                 if (!S_ISDIR(inode->i_mode))
227                         return acl ? -EACCES : 0;
228                 break;
229
230         default:
231                 return -EINVAL;
232         }
233
234         if (acl) {
235                 value = f2fs_acl_to_disk(acl, &size);
236                 if (IS_ERR(value)) {
237                         clear_inode_flag(fi, FI_ACL_MODE);
238                         return (int)PTR_ERR(value);
239                 }
240         }
241
242         error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
243
244         kfree(value);
245         if (!error)
246                 set_cached_acl(inode, type, acl);
247
248         clear_inode_flag(fi, FI_ACL_MODE);
249         return error;
250 }
251
252 int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
253 {
254         return __f2fs_set_acl(inode, type, acl, NULL);
255 }
256
257 /*
258  * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
259  * are copied from posix_acl.c
260  */
261 static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
262                                                         gfp_t flags)
263 {
264         struct posix_acl *clone = NULL;
265
266         if (acl) {
267                 int size = sizeof(struct posix_acl) + acl->a_count *
268                                 sizeof(struct posix_acl_entry);
269                 clone = kmemdup(acl, size, flags);
270                 if (clone)
271                         atomic_set(&clone->a_refcount, 1);
272         }
273         return clone;
274 }
275
276 static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
277 {
278         struct posix_acl_entry *pa, *pe;
279         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
280         umode_t mode = *mode_p;
281         int not_equiv = 0;
282
283         /* assert(atomic_read(acl->a_refcount) == 1); */
284
285         FOREACH_ACL_ENTRY(pa, acl, pe) {
286                 switch(pa->e_tag) {
287                 case ACL_USER_OBJ:
288                         pa->e_perm &= (mode >> 6) | ~S_IRWXO;
289                         mode &= (pa->e_perm << 6) | ~S_IRWXU;
290                         break;
291
292                 case ACL_USER:
293                 case ACL_GROUP:
294                         not_equiv = 1;
295                         break;
296
297                 case ACL_GROUP_OBJ:
298                         group_obj = pa;
299                         break;
300
301                 case ACL_OTHER:
302                         pa->e_perm &= mode | ~S_IRWXO;
303                         mode &= pa->e_perm | ~S_IRWXO;
304                         break;
305
306                 case ACL_MASK:
307                         mask_obj = pa;
308                         not_equiv = 1;
309                         break;
310
311                 default:
312                         return -EIO;
313                 }
314         }
315
316         if (mask_obj) {
317                 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
318                 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
319         } else {
320                 if (!group_obj)
321                         return -EIO;
322                 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
323                 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
324         }
325
326         *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
327         return not_equiv;
328 }
329
330 static int f2fs_acl_create(struct inode *dir, umode_t *mode,
331                 struct posix_acl **default_acl, struct posix_acl **acl,
332                 struct page *dpage)
333 {
334         struct posix_acl *p;
335         struct posix_acl *clone;
336         int ret;
337
338         *acl = NULL;
339         *default_acl = NULL;
340
341         if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
342                 return 0;
343
344         p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
345         if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
346                 *mode &= ~current_umask();
347                 return 0;
348         }
349         if (IS_ERR(p))
350                 return PTR_ERR(p);
351
352         clone = f2fs_acl_clone(p, GFP_NOFS);
353         if (!clone)
354                 goto no_mem;
355
356         ret = f2fs_acl_create_masq(clone, mode);
357         if (ret < 0)
358                 goto no_mem_clone;
359
360         if (ret == 0)
361                 posix_acl_release(clone);
362         else
363                 *acl = clone;
364
365         if (!S_ISDIR(*mode))
366                 posix_acl_release(p);
367         else
368                 *default_acl = p;
369
370         return 0;
371
372 no_mem_clone:
373         posix_acl_release(clone);
374 no_mem:
375         posix_acl_release(p);
376         return -ENOMEM;
377 }
378
379 int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
380                                                         struct page *dpage)
381 {
382         struct posix_acl *default_acl = NULL, *acl = NULL;
383         int error = 0;
384
385         error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
386         if (error)
387                 return error;
388
389         if (default_acl) {
390                 error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
391                                        ipage);
392                 posix_acl_release(default_acl);
393         }
394         if (acl) {
395                 if (!error)
396                         error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
397                                                ipage);
398                 posix_acl_release(acl);
399         }
400
401         return error;
402 }