~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext2/acl.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * linux/fs/ext2/acl.c
  3  *
  4  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5  */
  6 
  7 #include <linux/capability.h>
  8 #include <linux/init.h>
  9 #include <linux/sched.h>
 10 #include <linux/slab.h>
 11 #include <linux/fs.h>
 12 #include "ext2.h"
 13 #include "xattr.h"
 14 #include "acl.h"
 15 
 16 /*
 17  * Convert from filesystem to in-memory representation.
 18  */
 19 static struct posix_acl *
 20 ext2_acl_from_disk(const void *value, size_t size)
 21 {
 22         const char *end = (char *)value + size;
 23         int n, count;
 24         struct posix_acl *acl;
 25 
 26         if (!value)
 27                 return NULL;
 28         if (size < sizeof(ext2_acl_header))
 29                  return ERR_PTR(-EINVAL);
 30         if (((ext2_acl_header *)value)->a_version !=
 31             cpu_to_le32(EXT2_ACL_VERSION))
 32                 return ERR_PTR(-EINVAL);
 33         value = (char *)value + sizeof(ext2_acl_header);
 34         count = ext2_acl_count(size);
 35         if (count < 0)
 36                 return ERR_PTR(-EINVAL);
 37         if (count == 0)
 38                 return NULL;
 39         acl = posix_acl_alloc(count, GFP_KERNEL);
 40         if (!acl)
 41                 return ERR_PTR(-ENOMEM);
 42         for (n=0; n < count; n++) {
 43                 ext2_acl_entry *entry =
 44                         (ext2_acl_entry *)value;
 45                 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
 46                         goto fail;
 47                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
 48                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
 49                 switch(acl->a_entries[n].e_tag) {
 50                         case ACL_USER_OBJ:
 51                         case ACL_GROUP_OBJ:
 52                         case ACL_MASK:
 53                         case ACL_OTHER:
 54                                 value = (char *)value +
 55                                         sizeof(ext2_acl_entry_short);
 56                                 break;
 57 
 58                         case ACL_USER:
 59                                 value = (char *)value + sizeof(ext2_acl_entry);
 60                                 if ((char *)value > end)
 61                                         goto fail;
 62                                 acl->a_entries[n].e_uid =
 63                                         make_kuid(&init_user_ns,
 64                                                   le32_to_cpu(entry->e_id));
 65                                 break;
 66                         case ACL_GROUP:
 67                                 value = (char *)value + sizeof(ext2_acl_entry);
 68                                 if ((char *)value > end)
 69                                         goto fail;
 70                                 acl->a_entries[n].e_gid =
 71                                         make_kgid(&init_user_ns,
 72                                                   le32_to_cpu(entry->e_id));
 73                                 break;
 74 
 75                         default:
 76                                 goto fail;
 77                 }
 78         }
 79         if (value != end)
 80                 goto fail;
 81         return acl;
 82 
 83 fail:
 84         posix_acl_release(acl);
 85         return ERR_PTR(-EINVAL);
 86 }
 87 
 88 /*
 89  * Convert from in-memory to filesystem representation.
 90  */
 91 static void *
 92 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
 93 {
 94         ext2_acl_header *ext_acl;
 95         char *e;
 96         size_t n;
 97 
 98         *size = ext2_acl_size(acl->a_count);
 99         ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
100                         sizeof(ext2_acl_entry), GFP_KERNEL);
101         if (!ext_acl)
102                 return ERR_PTR(-ENOMEM);
103         ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
104         e = (char *)ext_acl + sizeof(ext2_acl_header);
105         for (n=0; n < acl->a_count; n++) {
106                 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
107                 ext2_acl_entry *entry = (ext2_acl_entry *)e;
108                 entry->e_tag  = cpu_to_le16(acl_e->e_tag);
109                 entry->e_perm = cpu_to_le16(acl_e->e_perm);
110                 switch(acl_e->e_tag) {
111                         case ACL_USER:
112                                 entry->e_id = cpu_to_le32(
113                                         from_kuid(&init_user_ns, acl_e->e_uid));
114                                 e += sizeof(ext2_acl_entry);
115                                 break;
116                         case ACL_GROUP:
117                                 entry->e_id = cpu_to_le32(
118                                         from_kgid(&init_user_ns, acl_e->e_gid));
119                                 e += sizeof(ext2_acl_entry);
120                                 break;
121 
122                         case ACL_USER_OBJ:
123                         case ACL_GROUP_OBJ:
124                         case ACL_MASK:
125                         case ACL_OTHER:
126                                 e += sizeof(ext2_acl_entry_short);
127                                 break;
128 
129                         default:
130                                 goto fail;
131                 }
132         }
133         return (char *)ext_acl;
134 
135 fail:
136         kfree(ext_acl);
137         return ERR_PTR(-EINVAL);
138 }
139 
140 /*
141  * inode->i_mutex: don't care
142  */
143 struct posix_acl *
144 ext2_get_acl(struct inode *inode, int type)
145 {
146         int name_index;
147         char *value = NULL;
148         struct posix_acl *acl;
149         int retval;
150 
151         if (!test_opt(inode->i_sb, POSIX_ACL))
152                 return NULL;
153 
154         acl = get_cached_acl(inode, type);
155         if (acl != ACL_NOT_CACHED)
156                 return acl;
157 
158         switch (type) {
159         case ACL_TYPE_ACCESS:
160                 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
161                 break;
162         case ACL_TYPE_DEFAULT:
163                 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
164                 break;
165         default:
166                 BUG();
167         }
168         retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
169         if (retval > 0) {
170                 value = kmalloc(retval, GFP_KERNEL);
171                 if (!value)
172                         return ERR_PTR(-ENOMEM);
173                 retval = ext2_xattr_get(inode, name_index, "", value, retval);
174         }
175         if (retval > 0)
176                 acl = ext2_acl_from_disk(value, retval);
177         else if (retval == -ENODATA || retval == -ENOSYS)
178                 acl = NULL;
179         else
180                 acl = ERR_PTR(retval);
181         kfree(value);
182 
183         if (!IS_ERR(acl))
184                 set_cached_acl(inode, type, acl);
185 
186         return acl;
187 }
188 
189 /*
190  * inode->i_mutex: down
191  */
192 static int
193 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
194 {
195         int name_index;
196         void *value = NULL;
197         size_t size = 0;
198         int error;
199 
200         if (S_ISLNK(inode->i_mode))
201                 return -EOPNOTSUPP;
202         if (!test_opt(inode->i_sb, POSIX_ACL))
203                 return 0;
204 
205         switch(type) {
206                 case ACL_TYPE_ACCESS:
207                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
208                         if (acl) {
209                                 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
210                                 if (error)
211                                         return error;
212                                 inode->i_ctime = CURRENT_TIME_SEC;
213                                 mark_inode_dirty(inode);
214                         }
215                         break;
216 
217                 case ACL_TYPE_DEFAULT:
218                         name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
219                         if (!S_ISDIR(inode->i_mode))
220                                 return acl ? -EACCES : 0;
221                         break;
222 
223                 default:
224                         return -EINVAL;
225         }
226         if (acl) {
227                 value = ext2_acl_to_disk(acl, &size);
228                 if (IS_ERR(value))
229                         return (int)PTR_ERR(value);
230         }
231 
232         error = ext2_xattr_set(inode, name_index, "", value, size, 0);
233 
234         kfree(value);
235         if (!error)
236                 set_cached_acl(inode, type, acl);
237         return error;
238 }
239 
240 /*
241  * Initialize the ACLs of a new inode. Called from ext2_new_inode.
242  *
243  * dir->i_mutex: down
244  * inode->i_mutex: up (access to inode is still exclusive)
245  */
246 int
247 ext2_init_acl(struct inode *inode, struct inode *dir)
248 {
249         struct posix_acl *acl = NULL;
250         int error = 0;
251 
252         if (!S_ISLNK(inode->i_mode)) {
253                 if (test_opt(dir->i_sb, POSIX_ACL)) {
254                         acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
255                         if (IS_ERR(acl))
256                                 return PTR_ERR(acl);
257                 }
258                 if (!acl)
259                         inode->i_mode &= ~current_umask();
260         }
261         if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
262                 if (S_ISDIR(inode->i_mode)) {
263                         error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
264                         if (error)
265                                 goto cleanup;
266                 }
267                 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
268                 if (error < 0)
269                         return error;
270                 if (error > 0) {
271                         /* This is an extended ACL */
272                         error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
273                 }
274         }
275 cleanup:
276        posix_acl_release(acl);
277        return error;
278 }
279 
280 /*
281  * Does chmod for an inode that may have an Access Control List. The
282  * inode->i_mode field must be updated to the desired value by the caller
283  * before calling this function.
284  * Returns 0 on success, or a negative error number.
285  *
286  * We change the ACL rather than storing some ACL entries in the file
287  * mode permission bits (which would be more efficient), because that
288  * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
289  * for directories) are added. There are no more bits available in the
290  * file mode.
291  *
292  * inode->i_mutex: down
293  */
294 int
295 ext2_acl_chmod(struct inode *inode)
296 {
297         struct posix_acl *acl;
298         int error;
299 
300         if (!test_opt(inode->i_sb, POSIX_ACL))
301                 return 0;
302         if (S_ISLNK(inode->i_mode))
303                 return -EOPNOTSUPP;
304         acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
305         if (IS_ERR(acl) || !acl)
306                 return PTR_ERR(acl);
307         error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
308         if (error)
309                 return error;
310         error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
311         posix_acl_release(acl);
312         return error;
313 }
314 
315 /*
316  * Extended attribut handlers
317  */
318 static size_t
319 ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
320                            const char *name, size_t name_len, int type)
321 {
322         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
323 
324         if (!test_opt(dentry->d_sb, POSIX_ACL))
325                 return 0;
326         if (list && size <= list_size)
327                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
328         return size;
329 }
330 
331 static size_t
332 ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
333                             const char *name, size_t name_len, int type)
334 {
335         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
336 
337         if (!test_opt(dentry->d_sb, POSIX_ACL))
338                 return 0;
339         if (list && size <= list_size)
340                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
341         return size;
342 }
343 
344 static int
345 ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
346                    size_t size, int type)
347 {
348         struct posix_acl *acl;
349         int error;
350 
351         if (strcmp(name, "") != 0)
352                 return -EINVAL;
353         if (!test_opt(dentry->d_sb, POSIX_ACL))
354                 return -EOPNOTSUPP;
355 
356         acl = ext2_get_acl(dentry->d_inode, type);
357         if (IS_ERR(acl))
358                 return PTR_ERR(acl);
359         if (acl == NULL)
360                 return -ENODATA;
361         error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
362         posix_acl_release(acl);
363 
364         return error;
365 }
366 
367 static int
368 ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
369                    size_t size, int flags, int type)
370 {
371         struct posix_acl *acl;
372         int error;
373 
374         if (strcmp(name, "") != 0)
375                 return -EINVAL;
376         if (!test_opt(dentry->d_sb, POSIX_ACL))
377                 return -EOPNOTSUPP;
378         if (!inode_owner_or_capable(dentry->d_inode))
379                 return -EPERM;
380 
381         if (value) {
382                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
383                 if (IS_ERR(acl))
384                         return PTR_ERR(acl);
385                 else if (acl) {
386                         error = posix_acl_valid(acl);
387                         if (error)
388                                 goto release_and_out;
389                 }
390         } else
391                 acl = NULL;
392 
393         error = ext2_set_acl(dentry->d_inode, type, acl);
394 
395 release_and_out:
396         posix_acl_release(acl);
397         return error;
398 }
399 
400 const struct xattr_handler ext2_xattr_acl_access_handler = {
401         .prefix = POSIX_ACL_XATTR_ACCESS,
402         .flags  = ACL_TYPE_ACCESS,
403         .list   = ext2_xattr_list_acl_access,
404         .get    = ext2_xattr_get_acl,
405         .set    = ext2_xattr_set_acl,
406 };
407 
408 const struct xattr_handler ext2_xattr_acl_default_handler = {
409         .prefix = POSIX_ACL_XATTR_DEFAULT,
410         .flags  = ACL_TYPE_DEFAULT,
411         .list   = ext2_xattr_list_acl_default,
412         .get    = ext2_xattr_get_acl,
413         .set    = ext2_xattr_set_acl,
414 };
415 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp