1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
17 * Convert from filesystem to in-memory representation.
19 static struct posix_acl *
20 ext2_acl_from_disk(const void *value, size_t size)
22 const char *end = (char *)value + size;
24 struct posix_acl *acl;
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);
36 return ERR_PTR(-EINVAL);
39 acl = posix_acl_alloc(count, GFP_KERNEL);
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)
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) {
54 value = (char *)value +
55 sizeof(ext2_acl_entry_short);
59 value = (char *)value + sizeof(ext2_acl_entry);
60 if ((char *)value > end)
62 acl->a_entries[n].e_uid =
63 make_kuid(&init_user_ns,
64 le32_to_cpu(entry->e_id));
67 value = (char *)value + sizeof(ext2_acl_entry);
68 if ((char *)value > end)
70 acl->a_entries[n].e_gid =
71 make_kgid(&init_user_ns,
72 le32_to_cpu(entry->e_id));
84 posix_acl_release(acl);
85 return ERR_PTR(-EINVAL);
89 * Convert from in-memory to filesystem representation.
92 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
94 ext2_acl_header *ext_acl;
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);
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) {
112 entry->e_id = cpu_to_le32(
113 from_kuid(&init_user_ns, acl_e->e_uid));
114 e += sizeof(ext2_acl_entry);
117 entry->e_id = cpu_to_le32(
118 from_kgid(&init_user_ns, acl_e->e_gid));
119 e += sizeof(ext2_acl_entry);
126 e += sizeof(ext2_acl_entry_short);
133 return (char *)ext_acl;
137 return ERR_PTR(-EINVAL);
141 * inode->i_mutex: don't care
144 ext2_get_acl(struct inode *inode, int type, bool rcu)
148 struct posix_acl *acl;
152 return ERR_PTR(-ECHILD);
155 case ACL_TYPE_ACCESS:
156 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
158 case ACL_TYPE_DEFAULT:
159 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
164 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
166 value = kmalloc(retval, GFP_KERNEL);
168 return ERR_PTR(-ENOMEM);
169 retval = ext2_xattr_get(inode, name_index, "", value, retval);
172 acl = ext2_acl_from_disk(value, retval);
173 else if (retval == -ENODATA || retval == -ENOSYS)
176 acl = ERR_PTR(retval);
183 __ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
191 case ACL_TYPE_ACCESS:
192 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
195 case ACL_TYPE_DEFAULT:
196 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
197 if (!S_ISDIR(inode->i_mode))
198 return acl ? -EACCES : 0;
205 value = ext2_acl_to_disk(acl, &size);
207 return (int)PTR_ERR(value);
210 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
214 set_cached_acl(inode, type, acl);
219 * inode->i_mutex: down
222 ext2_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
223 struct posix_acl *acl, int type)
227 struct inode *inode = d_inode(dentry);
228 umode_t mode = inode->i_mode;
230 if (type == ACL_TYPE_ACCESS && acl) {
231 error = posix_acl_update_mode(&init_user_ns, inode, &mode,
237 error = __ext2_set_acl(inode, acl, type);
238 if (!error && update_mode) {
239 inode->i_mode = mode;
240 inode->i_ctime = current_time(inode);
241 mark_inode_dirty(inode);
247 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
250 * inode->i_mutex: up (access to inode is still exclusive)
253 ext2_init_acl(struct inode *inode, struct inode *dir)
255 struct posix_acl *default_acl, *acl;
258 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
263 error = __ext2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
264 posix_acl_release(default_acl);
266 inode->i_default_acl = NULL;
270 error = __ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
271 posix_acl_release(acl);