| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * linux/fs/xattr_acl.c | 
|  | 3 | * | 
|  | 4 | * Almost all from linux/fs/ext2/acl.c: | 
|  | 5 | * Copyright (C) 2001 by Andreas Gruenbacher, <a.gruenbacher@computer.org> | 
|  | 6 | */ | 
|  | 7 |  | 
| Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 8 | #include <linux/export.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/fs.h> | 
|  | 10 | #include <linux/posix_acl_xattr.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/gfp.h> | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 12 | #include <linux/user_namespace.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 14 | /* | 
|  | 15 | * Fix up the uids and gids in posix acl extended attributes in place. | 
|  | 16 | */ | 
|  | 17 | static void posix_acl_fix_xattr_userns( | 
|  | 18 | struct user_namespace *to, struct user_namespace *from, | 
|  | 19 | void *value, size_t size) | 
|  | 20 | { | 
|  | 21 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; | 
|  | 22 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; | 
|  | 23 | int count; | 
|  | 24 | kuid_t uid; | 
|  | 25 | kgid_t gid; | 
|  | 26 |  | 
|  | 27 | if (!value) | 
|  | 28 | return; | 
|  | 29 | if (size < sizeof(posix_acl_xattr_header)) | 
|  | 30 | return; | 
|  | 31 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) | 
|  | 32 | return; | 
|  | 33 |  | 
|  | 34 | count = posix_acl_xattr_count(size); | 
|  | 35 | if (count < 0) | 
|  | 36 | return; | 
|  | 37 | if (count == 0) | 
|  | 38 | return; | 
|  | 39 |  | 
|  | 40 | for (end = entry + count; entry != end; entry++) { | 
|  | 41 | switch(le16_to_cpu(entry->e_tag)) { | 
|  | 42 | case ACL_USER: | 
|  | 43 | uid = make_kuid(from, le32_to_cpu(entry->e_id)); | 
|  | 44 | entry->e_id = cpu_to_le32(from_kuid(to, uid)); | 
|  | 45 | break; | 
|  | 46 | case ACL_GROUP: | 
|  | 47 | gid = make_kgid(from, le32_to_cpu(entry->e_id)); | 
| Eric W. Biederman | ea1fd777 | 2012-10-09 15:11:55 -0700 | [diff] [blame] | 48 | entry->e_id = cpu_to_le32(from_kgid(to, gid)); | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 49 | break; | 
|  | 50 | default: | 
|  | 51 | break; | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | void posix_acl_fix_xattr_from_user(void *value, size_t size) | 
|  | 57 | { | 
|  | 58 | struct user_namespace *user_ns = current_user_ns(); | 
|  | 59 | if (user_ns == &init_user_ns) | 
|  | 60 | return; | 
|  | 61 | posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | void posix_acl_fix_xattr_to_user(void *value, size_t size) | 
|  | 65 | { | 
|  | 66 | struct user_namespace *user_ns = current_user_ns(); | 
|  | 67 | if (user_ns == &init_user_ns) | 
|  | 68 | return; | 
|  | 69 | posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size); | 
|  | 70 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | /* | 
|  | 73 | * Convert from extended attribute to in-memory representation. | 
|  | 74 | */ | 
|  | 75 | struct posix_acl * | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 76 | posix_acl_from_xattr(struct user_namespace *user_ns, | 
|  | 77 | const void *value, size_t size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { | 
|  | 79 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; | 
|  | 80 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; | 
|  | 81 | int count; | 
|  | 82 | struct posix_acl *acl; | 
|  | 83 | struct posix_acl_entry *acl_e; | 
|  | 84 |  | 
|  | 85 | if (!value) | 
|  | 86 | return NULL; | 
|  | 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); | 
|  | 91 |  | 
|  | 92 | count = posix_acl_xattr_count(size); | 
|  | 93 | if (count < 0) | 
|  | 94 | return ERR_PTR(-EINVAL); | 
|  | 95 | if (count == 0) | 
|  | 96 | return NULL; | 
|  | 97 |  | 
| Steven Whitehouse | ab20183 | 2009-09-29 16:31:03 +0100 | [diff] [blame] | 98 | acl = posix_acl_alloc(count, GFP_NOFS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (!acl) | 
|  | 100 | return ERR_PTR(-ENOMEM); | 
|  | 101 | acl_e = acl->a_entries; | 
|  | 102 |  | 
|  | 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); | 
|  | 106 |  | 
|  | 107 | switch(acl_e->e_tag) { | 
|  | 108 | case ACL_USER_OBJ: | 
|  | 109 | case ACL_GROUP_OBJ: | 
|  | 110 | case ACL_MASK: | 
|  | 111 | case ACL_OTHER: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | break; | 
|  | 113 |  | 
|  | 114 | case ACL_USER: | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 115 | acl_e->e_uid = | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 116 | make_kuid(user_ns, | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 117 | le32_to_cpu(entry->e_id)); | 
|  | 118 | if (!uid_valid(acl_e->e_uid)) | 
|  | 119 | goto fail; | 
|  | 120 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | case ACL_GROUP: | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 122 | acl_e->e_gid = | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 123 | make_kgid(user_ns, | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 124 | le32_to_cpu(entry->e_id)); | 
|  | 125 | if (!gid_valid(acl_e->e_gid)) | 
|  | 126 | goto fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | break; | 
|  | 128 |  | 
|  | 129 | default: | 
|  | 130 | goto fail; | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 | return acl; | 
|  | 134 |  | 
|  | 135 | fail: | 
|  | 136 | posix_acl_release(acl); | 
|  | 137 | return ERR_PTR(-EINVAL); | 
|  | 138 | } | 
|  | 139 | EXPORT_SYMBOL (posix_acl_from_xattr); | 
|  | 140 |  | 
|  | 141 | /* | 
|  | 142 | * Convert from in-memory to extended attribute representation. | 
|  | 143 | */ | 
|  | 144 | int | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 145 | posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, | 
|  | 146 | void *buffer, size_t size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { | 
|  | 148 | posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer; | 
|  | 149 | posix_acl_xattr_entry *ext_entry = ext_acl->a_entries; | 
|  | 150 | int real_size, n; | 
|  | 151 |  | 
|  | 152 | real_size = posix_acl_xattr_size(acl->a_count); | 
|  | 153 | if (!buffer) | 
|  | 154 | return real_size; | 
|  | 155 | if (real_size > size) | 
|  | 156 | return -ERANGE; | 
|  | 157 |  | 
|  | 158 | ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); | 
|  | 159 |  | 
|  | 160 | for (n=0; n < acl->a_count; n++, ext_entry++) { | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 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) { | 
|  | 165 | case ACL_USER: | 
|  | 166 | ext_entry->e_id = | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 167 | cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 168 | break; | 
|  | 169 | case ACL_GROUP: | 
|  | 170 | ext_entry->e_id = | 
| Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 171 | cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); | 
| Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 172 | break; | 
|  | 173 | default: | 
|  | 174 | ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); | 
|  | 175 | break; | 
|  | 176 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } | 
|  | 178 | return real_size; | 
|  | 179 | } | 
|  | 180 | EXPORT_SYMBOL (posix_acl_to_xattr); |