4 * Almost all from linux/fs/ext2/acl.c:
5 * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org>
8 #include <linux/export.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/gfp.h>
12 #include <linux/user_namespace.h>
15 * Fix up the uids and gids in posix acl extended attributes in place.
17 static void posix_acl_fix_xattr_userns(
18 struct user_namespace *to, struct user_namespace *from,
19 void *value, size_t size)
21 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
22 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
29 if (size < sizeof(posix_acl_xattr_header))
31 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
34 count = posix_acl_xattr_count(size);
40 for (end = entry + count; entry != end; entry++) {
41 switch(le16_to_cpu(entry->e_tag)) {
43 uid = make_kuid(from, le32_to_cpu(entry->e_id));
44 entry->e_id = cpu_to_le32(from_kuid(to, uid));
47 gid = make_kgid(from, le32_to_cpu(entry->e_id));
48 entry->e_id = cpu_to_le32(from_kgid(to, gid));
56 void posix_acl_fix_xattr_from_user(void *value, size_t size)
58 struct user_namespace *user_ns = current_user_ns();
59 if (user_ns == &init_user_ns)
61 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
64 void posix_acl_fix_xattr_to_user(void *value, size_t size)
66 struct user_namespace *user_ns = current_user_ns();
67 if (user_ns == &init_user_ns)
69 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
73 * Convert from extended attribute to in-memory representation.
76 posix_acl_from_xattr(struct user_namespace *user_ns,
77 const void *value, size_t size)
79 posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
80 posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
82 struct posix_acl *acl;
83 struct posix_acl_entry *acl_e;
87 if (size < sizeof(posix_acl_xattr_header))
88 return ERR_PTR(-EINVAL);
89 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
90 return ERR_PTR(-EOPNOTSUPP);
92 count = posix_acl_xattr_count(size);
94 return ERR_PTR(-EINVAL);
98 acl = posix_acl_alloc(count, GFP_NOFS);
100 return ERR_PTR(-ENOMEM);
101 acl_e = acl->a_entries;
103 for (end = entry + count; entry != end; acl_e++, entry++) {
104 acl_e->e_tag = le16_to_cpu(entry->e_tag);
105 acl_e->e_perm = le16_to_cpu(entry->e_perm);
107 switch(acl_e->e_tag) {
117 le32_to_cpu(entry->e_id));
118 if (!uid_valid(acl_e->e_uid))
124 le32_to_cpu(entry->e_id));
125 if (!gid_valid(acl_e->e_gid))
136 posix_acl_release(acl);
137 return ERR_PTR(-EINVAL);
139 EXPORT_SYMBOL (posix_acl_from_xattr);
142 * Convert from in-memory to extended attribute representation.
145 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
146 void *buffer, size_t size)
148 posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
149 posix_acl_xattr_entry *ext_entry = ext_acl->a_entries;
152 real_size = posix_acl_xattr_size(acl->a_count);
155 if (real_size > size)
158 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
160 for (n=0; n < acl->a_count; n++, ext_entry++) {
161 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
162 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
163 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
164 switch(acl_e->e_tag) {
167 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
171 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
174 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
180 EXPORT_SYMBOL (posix_acl_to_xattr);