Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 3 | * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 5 | * Fixes from William Schumacher incorporated on 15 March 2001. |
| 6 | * (Reported by Charles Bertsch, <CBertsch@microtest.com>). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This file contains generic functions for manipulating |
| 11 | * POSIX 1003.1e draft standard 17 ACLs. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
| 18 | #include <linux/sched.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 19 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/posix_acl.h> |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 21 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 22 | #include <linux/xattr.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 23 | #include <linux/export.h> |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 24 | #include <linux/user_namespace.h> |
Miklos Szeredi | 332f606 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 25 | #include <linux/namei.h> |
Christian Brauner | a793d79 | 2021-12-03 12:16:59 +0100 | [diff] [blame] | 26 | #include <linux/mnt_idmapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Andreas Gruenbacher | 04c57f4 | 2016-03-24 14:38:38 +0100 | [diff] [blame] | 28 | static struct posix_acl **acl_by_type(struct inode *inode, int type) |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 29 | { |
| 30 | switch (type) { |
| 31 | case ACL_TYPE_ACCESS: |
| 32 | return &inode->i_acl; |
| 33 | case ACL_TYPE_DEFAULT: |
| 34 | return &inode->i_default_acl; |
| 35 | default: |
| 36 | BUG(); |
| 37 | } |
| 38 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 39 | |
| 40 | struct posix_acl *get_cached_acl(struct inode *inode, int type) |
| 41 | { |
| 42 | struct posix_acl **p = acl_by_type(inode, type); |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 43 | struct posix_acl *acl; |
| 44 | |
| 45 | for (;;) { |
| 46 | rcu_read_lock(); |
| 47 | acl = rcu_dereference(*p); |
| 48 | if (!acl || is_uncached_acl(acl) || |
Elena Reshetova | 6671726 | 2017-11-29 13:19:31 +0200 | [diff] [blame] | 49 | refcount_inc_not_zero(&acl->a_refcount)) |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 50 | break; |
| 51 | rcu_read_unlock(); |
| 52 | cpu_relax(); |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 53 | } |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 54 | rcu_read_unlock(); |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 55 | return acl; |
| 56 | } |
| 57 | EXPORT_SYMBOL(get_cached_acl); |
| 58 | |
| 59 | struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type) |
| 60 | { |
Miklos Szeredi | 332f606 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 61 | struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type)); |
| 62 | |
| 63 | if (acl == ACL_DONT_CACHE) { |
| 64 | struct posix_acl *ret; |
| 65 | |
| 66 | ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU); |
| 67 | if (!IS_ERR(ret)) |
| 68 | acl = ret; |
| 69 | } |
| 70 | |
| 71 | return acl; |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 72 | } |
| 73 | EXPORT_SYMBOL(get_cached_acl_rcu); |
| 74 | |
| 75 | void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl) |
| 76 | { |
| 77 | struct posix_acl **p = acl_by_type(inode, type); |
| 78 | struct posix_acl *old; |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 79 | |
| 80 | old = xchg(p, posix_acl_dup(acl)); |
| 81 | if (!is_uncached_acl(old)) |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 82 | posix_acl_release(old); |
| 83 | } |
| 84 | EXPORT_SYMBOL(set_cached_acl); |
| 85 | |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 86 | static void __forget_cached_acl(struct posix_acl **p) |
| 87 | { |
| 88 | struct posix_acl *old; |
| 89 | |
| 90 | old = xchg(p, ACL_NOT_CACHED); |
| 91 | if (!is_uncached_acl(old)) |
| 92 | posix_acl_release(old); |
| 93 | } |
| 94 | |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 95 | void forget_cached_acl(struct inode *inode, int type) |
| 96 | { |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 97 | __forget_cached_acl(acl_by_type(inode, type)); |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 98 | } |
| 99 | EXPORT_SYMBOL(forget_cached_acl); |
| 100 | |
| 101 | void forget_all_cached_acls(struct inode *inode) |
| 102 | { |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 103 | __forget_cached_acl(&inode->i_acl); |
| 104 | __forget_cached_acl(&inode->i_default_acl); |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 105 | } |
| 106 | EXPORT_SYMBOL(forget_all_cached_acls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 108 | struct posix_acl *get_acl(struct inode *inode, int type) |
| 109 | { |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 110 | void *sentinel; |
| 111 | struct posix_acl **p; |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 112 | struct posix_acl *acl; |
| 113 | |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 114 | /* |
| 115 | * The sentinel is used to detect when another operation like |
| 116 | * set_cached_acl() or forget_cached_acl() races with get_acl(). |
| 117 | * It is guaranteed that is_uncached_acl(sentinel) is true. |
| 118 | */ |
| 119 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 120 | acl = get_cached_acl(inode, type); |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 121 | if (!is_uncached_acl(acl)) |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 122 | return acl; |
| 123 | |
| 124 | if (!IS_POSIXACL(inode)) |
| 125 | return NULL; |
| 126 | |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 127 | sentinel = uncached_acl_sentinel(current); |
| 128 | p = acl_by_type(inode, type); |
| 129 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 130 | /* |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 131 | * If the ACL isn't being read yet, set our sentinel. Otherwise, the |
| 132 | * current value of the ACL will not be ACL_NOT_CACHED and so our own |
| 133 | * sentinel will not be set; another task will update the cache. We |
| 134 | * could wait for that other task to complete its job, but it's easier |
| 135 | * to just call ->get_acl to fetch the ACL ourself. (This is going to |
| 136 | * be an unlikely race.) |
| 137 | */ |
Arnd Bergmann | d1cef29 | 2021-11-05 13:35:01 -0700 | [diff] [blame] | 138 | cmpxchg(p, ACL_NOT_CACHED, sentinel); |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Normally, the ACL returned by ->get_acl will be cached. |
| 142 | * A filesystem can prevent that by calling |
| 143 | * forget_cached_acl(inode, type) in ->get_acl. |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 144 | * |
| 145 | * If the filesystem doesn't have a get_acl() function at all, we'll |
| 146 | * just create the negative cache entry. |
| 147 | */ |
| 148 | if (!inode->i_op->get_acl) { |
| 149 | set_cached_acl(inode, type, NULL); |
| 150 | return NULL; |
| 151 | } |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 152 | acl = inode->i_op->get_acl(inode, type, false); |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 153 | |
| 154 | if (IS_ERR(acl)) { |
| 155 | /* |
| 156 | * Remove our sentinel so that we don't block future attempts |
| 157 | * to cache the ACL. |
| 158 | */ |
| 159 | cmpxchg(p, sentinel, ACL_NOT_CACHED); |
| 160 | return acl; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Cache the result, but only if our sentinel is still in place. |
| 165 | */ |
| 166 | posix_acl_dup(acl); |
| 167 | if (unlikely(cmpxchg(p, sentinel, acl) != sentinel)) |
| 168 | posix_acl_release(acl); |
| 169 | return acl; |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL(get_acl); |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 174 | * Init a fresh posix_acl |
| 175 | */ |
| 176 | void |
| 177 | posix_acl_init(struct posix_acl *acl, int count) |
| 178 | { |
Elena Reshetova | 6671726 | 2017-11-29 13:19:31 +0200 | [diff] [blame] | 179 | refcount_set(&acl->a_refcount, 1); |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 180 | acl->a_count = count; |
| 181 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 182 | EXPORT_SYMBOL(posix_acl_init); |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * Allocate a new ACL with the specified number of entries. |
| 186 | */ |
| 187 | struct posix_acl * |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 188 | posix_acl_alloc(int count, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | const size_t size = sizeof(struct posix_acl) + |
| 191 | count * sizeof(struct posix_acl_entry); |
| 192 | struct posix_acl *acl = kmalloc(size, flags); |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 193 | if (acl) |
| 194 | posix_acl_init(acl, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return acl; |
| 196 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 197 | EXPORT_SYMBOL(posix_acl_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Clone an ACL. |
| 201 | */ |
Al Viro | edde854 | 2011-07-23 03:27:37 -0400 | [diff] [blame] | 202 | static struct posix_acl * |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 203 | posix_acl_clone(const struct posix_acl *acl, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | struct posix_acl *clone = NULL; |
| 206 | |
| 207 | if (acl) { |
| 208 | int size = sizeof(struct posix_acl) + acl->a_count * |
| 209 | sizeof(struct posix_acl_entry); |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 210 | clone = kmemdup(acl, size, flags); |
| 211 | if (clone) |
Elena Reshetova | 6671726 | 2017-11-29 13:19:31 +0200 | [diff] [blame] | 212 | refcount_set(&clone->a_refcount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | return clone; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Check if an acl is valid. Returns 0 if it is, or -E... otherwise. |
| 219 | */ |
| 220 | int |
Eric W. Biederman | 0d4d717 | 2016-06-27 16:04:06 -0500 | [diff] [blame] | 221 | posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | const struct posix_acl_entry *pa, *pe; |
| 224 | int state = ACL_USER_OBJ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int needs_mask = 0; |
| 226 | |
| 227 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 228 | if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE)) |
| 229 | return -EINVAL; |
| 230 | switch (pa->e_tag) { |
| 231 | case ACL_USER_OBJ: |
| 232 | if (state == ACL_USER_OBJ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | state = ACL_USER; |
| 234 | break; |
| 235 | } |
| 236 | return -EINVAL; |
| 237 | |
| 238 | case ACL_USER: |
| 239 | if (state != ACL_USER) |
| 240 | return -EINVAL; |
Eric W. Biederman | 0d4d717 | 2016-06-27 16:04:06 -0500 | [diff] [blame] | 241 | if (!kuid_has_mapping(user_ns, pa->e_uid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | needs_mask = 1; |
| 244 | break; |
| 245 | |
| 246 | case ACL_GROUP_OBJ: |
| 247 | if (state == ACL_USER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | state = ACL_GROUP; |
| 249 | break; |
| 250 | } |
| 251 | return -EINVAL; |
| 252 | |
| 253 | case ACL_GROUP: |
| 254 | if (state != ACL_GROUP) |
| 255 | return -EINVAL; |
Eric W. Biederman | 0d4d717 | 2016-06-27 16:04:06 -0500 | [diff] [blame] | 256 | if (!kgid_has_mapping(user_ns, pa->e_gid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | needs_mask = 1; |
| 259 | break; |
| 260 | |
| 261 | case ACL_MASK: |
| 262 | if (state != ACL_GROUP) |
| 263 | return -EINVAL; |
| 264 | state = ACL_OTHER; |
| 265 | break; |
| 266 | |
| 267 | case ACL_OTHER: |
| 268 | if (state == ACL_OTHER || |
| 269 | (state == ACL_GROUP && !needs_mask)) { |
| 270 | state = 0; |
| 271 | break; |
| 272 | } |
| 273 | return -EINVAL; |
| 274 | |
| 275 | default: |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | } |
| 279 | if (state == 0) |
| 280 | return 0; |
| 281 | return -EINVAL; |
| 282 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 283 | EXPORT_SYMBOL(posix_acl_valid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * Returns 0 if the acl can be exactly represented in the traditional |
| 287 | * file mode permission bits, or else 1. Returns -E... on error. |
| 288 | */ |
| 289 | int |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 290 | posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | const struct posix_acl_entry *pa, *pe; |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 293 | umode_t mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | int not_equiv = 0; |
| 295 | |
Christoph Hellwig | 50c6e28 | 2014-05-04 13:03:32 +0200 | [diff] [blame] | 296 | /* |
| 297 | * A null ACL can always be presented as mode bits. |
| 298 | */ |
| 299 | if (!acl) |
| 300 | return 0; |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 303 | switch (pa->e_tag) { |
| 304 | case ACL_USER_OBJ: |
| 305 | mode |= (pa->e_perm & S_IRWXO) << 6; |
| 306 | break; |
| 307 | case ACL_GROUP_OBJ: |
| 308 | mode |= (pa->e_perm & S_IRWXO) << 3; |
| 309 | break; |
| 310 | case ACL_OTHER: |
| 311 | mode |= pa->e_perm & S_IRWXO; |
| 312 | break; |
| 313 | case ACL_MASK: |
| 314 | mode = (mode & ~S_IRWXG) | |
| 315 | ((pa->e_perm & S_IRWXO) << 3); |
| 316 | not_equiv = 1; |
| 317 | break; |
| 318 | case ACL_USER: |
| 319 | case ACL_GROUP: |
| 320 | not_equiv = 1; |
| 321 | break; |
| 322 | default: |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | } |
| 326 | if (mode_p) |
| 327 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; |
| 328 | return not_equiv; |
| 329 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 330 | EXPORT_SYMBOL(posix_acl_equiv_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Create an ACL representing the file mode permission bits of an inode. |
| 334 | */ |
| 335 | struct posix_acl * |
Al Viro | 3a5fba1 | 2011-07-23 19:01:48 -0400 | [diff] [blame] | 336 | posix_acl_from_mode(umode_t mode, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | struct posix_acl *acl = posix_acl_alloc(3, flags); |
| 339 | if (!acl) |
| 340 | return ERR_PTR(-ENOMEM); |
| 341 | |
| 342 | acl->a_entries[0].e_tag = ACL_USER_OBJ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6; |
| 344 | |
| 345 | acl->a_entries[1].e_tag = ACL_GROUP_OBJ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3; |
| 347 | |
| 348 | acl->a_entries[2].e_tag = ACL_OTHER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | acl->a_entries[2].e_perm = (mode & S_IRWXO); |
| 350 | return acl; |
| 351 | } |
Andrew Morton | 0afaa12 | 2014-01-21 15:48:42 -0800 | [diff] [blame] | 352 | EXPORT_SYMBOL(posix_acl_from_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Return 0 if current is granted want access to the inode |
| 356 | * by the acl. Returns -E... otherwise. |
| 357 | */ |
| 358 | int |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 359 | posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode, |
| 360 | const struct posix_acl *acl, int want) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | const struct posix_acl_entry *pa, *pe, *mask_obj; |
| 363 | int found = 0; |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 364 | kuid_t uid; |
| 365 | kgid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 63d72b9 | 2020-06-07 12:19:06 -0700 | [diff] [blame] | 367 | want &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Andreas Gruenbacher | d124b60 | 2011-10-23 23:13:32 +0530 | [diff] [blame] | 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 370 | switch(pa->e_tag) { |
| 371 | case ACL_USER_OBJ: |
| 372 | /* (May have been checked already) */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 373 | uid = i_uid_into_mnt(mnt_userns, inode); |
| 374 | if (uid_eq(uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | goto check_perm; |
| 376 | break; |
| 377 | case ACL_USER: |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 378 | uid = mapped_kuid_fs(mnt_userns, |
Christian Brauner | bd30336 | 2021-12-03 12:17:07 +0100 | [diff] [blame] | 379 | i_user_ns(inode), |
| 380 | pa->e_uid); |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 381 | if (uid_eq(uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | goto mask; |
| 383 | break; |
| 384 | case ACL_GROUP_OBJ: |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 385 | gid = i_gid_into_mnt(mnt_userns, inode); |
| 386 | if (in_group_p(gid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | found = 1; |
| 388 | if ((pa->e_perm & want) == want) |
| 389 | goto mask; |
| 390 | } |
| 391 | break; |
| 392 | case ACL_GROUP: |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 393 | gid = mapped_kgid_fs(mnt_userns, |
Christian Brauner | bd30336 | 2021-12-03 12:17:07 +0100 | [diff] [blame] | 394 | i_user_ns(inode), |
| 395 | pa->e_gid); |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 396 | if (in_group_p(gid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | found = 1; |
| 398 | if ((pa->e_perm & want) == want) |
| 399 | goto mask; |
| 400 | } |
| 401 | break; |
| 402 | case ACL_MASK: |
| 403 | break; |
| 404 | case ACL_OTHER: |
| 405 | if (found) |
| 406 | return -EACCES; |
| 407 | else |
| 408 | goto check_perm; |
| 409 | default: |
| 410 | return -EIO; |
| 411 | } |
| 412 | } |
| 413 | return -EIO; |
| 414 | |
| 415 | mask: |
| 416 | for (mask_obj = pa+1; mask_obj != pe; mask_obj++) { |
| 417 | if (mask_obj->e_tag == ACL_MASK) { |
| 418 | if ((pa->e_perm & mask_obj->e_perm & want) == want) |
| 419 | return 0; |
| 420 | return -EACCES; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | check_perm: |
| 425 | if ((pa->e_perm & want) == want) |
| 426 | return 0; |
| 427 | return -EACCES; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Modify acl when creating a new inode. The caller must ensure the acl is |
| 432 | * only referenced once. |
| 433 | * |
| 434 | * mode_p initially must contain the mode parameter to the open() / creat() |
| 435 | * system calls. All permissions that are not granted by the acl are removed. |
| 436 | * The permissions in the acl are changed to reflect the mode_p parameter. |
| 437 | */ |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 438 | static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | struct posix_acl_entry *pa, *pe; |
| 441 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 442 | umode_t mode = *mode_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | int not_equiv = 0; |
| 444 | |
| 445 | /* assert(atomic_read(acl->a_refcount) == 1); */ |
| 446 | |
| 447 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 448 | switch(pa->e_tag) { |
| 449 | case ACL_USER_OBJ: |
| 450 | pa->e_perm &= (mode >> 6) | ~S_IRWXO; |
| 451 | mode &= (pa->e_perm << 6) | ~S_IRWXU; |
| 452 | break; |
| 453 | |
| 454 | case ACL_USER: |
| 455 | case ACL_GROUP: |
| 456 | not_equiv = 1; |
| 457 | break; |
| 458 | |
| 459 | case ACL_GROUP_OBJ: |
| 460 | group_obj = pa; |
| 461 | break; |
| 462 | |
| 463 | case ACL_OTHER: |
| 464 | pa->e_perm &= mode | ~S_IRWXO; |
| 465 | mode &= pa->e_perm | ~S_IRWXO; |
| 466 | break; |
| 467 | |
| 468 | case ACL_MASK: |
| 469 | mask_obj = pa; |
| 470 | not_equiv = 1; |
| 471 | break; |
| 472 | |
| 473 | default: |
| 474 | return -EIO; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (mask_obj) { |
| 479 | mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO; |
| 480 | mode &= (mask_obj->e_perm << 3) | ~S_IRWXG; |
| 481 | } else { |
| 482 | if (!group_obj) |
| 483 | return -EIO; |
| 484 | group_obj->e_perm &= (mode >> 3) | ~S_IRWXO; |
| 485 | mode &= (group_obj->e_perm << 3) | ~S_IRWXG; |
| 486 | } |
| 487 | |
| 488 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; |
| 489 | return not_equiv; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Modify the ACL for the chmod syscall. |
| 494 | */ |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 495 | static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | { |
| 497 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; |
| 498 | struct posix_acl_entry *pa, *pe; |
| 499 | |
| 500 | /* assert(atomic_read(acl->a_refcount) == 1); */ |
| 501 | |
| 502 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 503 | switch(pa->e_tag) { |
| 504 | case ACL_USER_OBJ: |
| 505 | pa->e_perm = (mode & S_IRWXU) >> 6; |
| 506 | break; |
| 507 | |
| 508 | case ACL_USER: |
| 509 | case ACL_GROUP: |
| 510 | break; |
| 511 | |
| 512 | case ACL_GROUP_OBJ: |
| 513 | group_obj = pa; |
| 514 | break; |
| 515 | |
| 516 | case ACL_MASK: |
| 517 | mask_obj = pa; |
| 518 | break; |
| 519 | |
| 520 | case ACL_OTHER: |
| 521 | pa->e_perm = (mode & S_IRWXO); |
| 522 | break; |
| 523 | |
| 524 | default: |
| 525 | return -EIO; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (mask_obj) { |
| 530 | mask_obj->e_perm = (mode & S_IRWXG) >> 3; |
| 531 | } else { |
| 532 | if (!group_obj) |
| 533 | return -EIO; |
| 534 | group_obj->e_perm = (mode & S_IRWXG) >> 3; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 539 | |
| 540 | int |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 541 | __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 542 | { |
| 543 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); |
| 544 | int err = -ENOMEM; |
| 545 | if (clone) { |
| 546 | err = posix_acl_create_masq(clone, mode_p); |
| 547 | if (err < 0) { |
| 548 | posix_acl_release(clone); |
| 549 | clone = NULL; |
| 550 | } |
| 551 | } |
| 552 | posix_acl_release(*acl); |
| 553 | *acl = clone; |
| 554 | return err; |
| 555 | } |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 556 | EXPORT_SYMBOL(__posix_acl_create); |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 557 | |
| 558 | int |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 559 | __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 560 | { |
| 561 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); |
| 562 | int err = -ENOMEM; |
| 563 | if (clone) { |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 564 | err = __posix_acl_chmod_masq(clone, mode); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 565 | if (err) { |
| 566 | posix_acl_release(clone); |
| 567 | clone = NULL; |
| 568 | } |
| 569 | } |
| 570 | posix_acl_release(*acl); |
| 571 | *acl = clone; |
| 572 | return err; |
| 573 | } |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 574 | EXPORT_SYMBOL(__posix_acl_chmod); |
| 575 | |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 576 | /** |
| 577 | * posix_acl_chmod - chmod a posix acl |
| 578 | * |
| 579 | * @mnt_userns: user namespace of the mount @inode was found from |
| 580 | * @inode: inode to check permissions on |
| 581 | * @mode: the new mode of @inode |
| 582 | * |
| 583 | * If the inode has been found through an idmapped mount the user namespace of |
| 584 | * the vfsmount must be passed through @mnt_userns. This function will then |
| 585 | * take care to map the inode according to @mnt_userns before checking |
| 586 | * permissions. On non-idmapped mounts or if permission checking is to be |
| 587 | * performed on the raw inode simply passs init_user_ns. |
| 588 | */ |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 589 | int |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 590 | posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode, |
| 591 | umode_t mode) |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 592 | { |
| 593 | struct posix_acl *acl; |
| 594 | int ret = 0; |
| 595 | |
| 596 | if (!IS_POSIXACL(inode)) |
| 597 | return 0; |
| 598 | if (!inode->i_op->set_acl) |
| 599 | return -EOPNOTSUPP; |
| 600 | |
| 601 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
Trond Myklebust | 789b663 | 2014-01-31 14:25:19 -0500 | [diff] [blame] | 602 | if (IS_ERR_OR_NULL(acl)) { |
| 603 | if (acl == ERR_PTR(-EOPNOTSUPP)) |
| 604 | return 0; |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 605 | return PTR_ERR(acl); |
Trond Myklebust | 789b663 | 2014-01-31 14:25:19 -0500 | [diff] [blame] | 606 | } |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 607 | |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 608 | ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode); |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 609 | if (ret) |
| 610 | return ret; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 611 | ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS); |
Christoph Hellwig | 5bf3258 | 2013-12-20 05:16:41 -0800 | [diff] [blame] | 612 | posix_acl_release(acl); |
| 613 | return ret; |
| 614 | } |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 615 | EXPORT_SYMBOL(posix_acl_chmod); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 616 | |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 617 | int |
| 618 | posix_acl_create(struct inode *dir, umode_t *mode, |
| 619 | struct posix_acl **default_acl, struct posix_acl **acl) |
| 620 | { |
| 621 | struct posix_acl *p; |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 622 | struct posix_acl *clone; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 623 | int ret; |
| 624 | |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 625 | *acl = NULL; |
| 626 | *default_acl = NULL; |
| 627 | |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 628 | if (S_ISLNK(*mode) || !IS_POSIXACL(dir)) |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 629 | return 0; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 630 | |
| 631 | p = get_acl(dir, ACL_TYPE_DEFAULT); |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 632 | if (!p || p == ERR_PTR(-EOPNOTSUPP)) { |
| 633 | *mode &= ~current_umask(); |
| 634 | return 0; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 635 | } |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 636 | if (IS_ERR(p)) |
| 637 | return PTR_ERR(p); |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 638 | |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 639 | ret = -ENOMEM; |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 640 | clone = posix_acl_clone(p, GFP_NOFS); |
| 641 | if (!clone) |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 642 | goto err_release; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 643 | |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 644 | ret = posix_acl_create_masq(clone, mode); |
Omar Sandoval | fed0b58 | 2015-02-08 21:45:25 -0800 | [diff] [blame] | 645 | if (ret < 0) |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 646 | goto err_release_clone; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 647 | |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 648 | if (ret == 0) |
| 649 | posix_acl_release(clone); |
| 650 | else |
| 651 | *acl = clone; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 652 | |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 653 | if (!S_ISDIR(*mode)) |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 654 | posix_acl_release(p); |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 655 | else |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 656 | *default_acl = p; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 657 | |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 658 | return 0; |
Omar Sandoval | fed0b58 | 2015-02-08 21:45:25 -0800 | [diff] [blame] | 659 | |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 660 | err_release_clone: |
Dan Carpenter | c0c3a71 | 2015-06-19 09:00:55 +1000 | [diff] [blame] | 661 | posix_acl_release(clone); |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 662 | err_release: |
Omar Sandoval | fed0b58 | 2015-02-08 21:45:25 -0800 | [diff] [blame] | 663 | posix_acl_release(p); |
Miklos Szeredi | beaf226 | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 664 | return ret; |
Christoph Hellwig | 37bc153 | 2013-12-20 05:16:42 -0800 | [diff] [blame] | 665 | } |
| 666 | EXPORT_SYMBOL_GPL(posix_acl_create); |
| 667 | |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 668 | /** |
| 669 | * posix_acl_update_mode - update mode in set_acl |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 670 | * @mnt_userns: user namespace of the mount @inode was found from |
| 671 | * @inode: target inode |
| 672 | * @mode_p: mode (pointer) for update |
| 673 | * @acl: acl pointer |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 674 | * |
| 675 | * Update the file mode when setting an ACL: compute the new file permission |
| 676 | * bits based on the ACL. In addition, if the ACL is equivalent to the new |
Randy Dunlap | e39e773 | 2020-01-04 13:00:05 -0800 | [diff] [blame] | 677 | * file mode, set *@acl to NULL to indicate that no ACL should be set. |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 678 | * |
Randy Dunlap | e39e773 | 2020-01-04 13:00:05 -0800 | [diff] [blame] | 679 | * As with chmod, clear the setgid bit if the caller is not in the owning group |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 680 | * or capable of CAP_FSETID (see inode_change_ok). |
| 681 | * |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 682 | * If the inode has been found through an idmapped mount the user namespace of |
| 683 | * the vfsmount must be passed through @mnt_userns. This function will then |
| 684 | * take care to map the inode according to @mnt_userns before checking |
| 685 | * permissions. On non-idmapped mounts or if permission checking is to be |
| 686 | * performed on the raw inode simply passs init_user_ns. |
| 687 | * |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 688 | * Called from set_acl inode operations. |
| 689 | */ |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 690 | int posix_acl_update_mode(struct user_namespace *mnt_userns, |
| 691 | struct inode *inode, umode_t *mode_p, |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 692 | struct posix_acl **acl) |
| 693 | { |
| 694 | umode_t mode = inode->i_mode; |
| 695 | int error; |
| 696 | |
| 697 | error = posix_acl_equiv_mode(*acl, &mode); |
| 698 | if (error < 0) |
| 699 | return error; |
| 700 | if (error == 0) |
| 701 | *acl = NULL; |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 702 | if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) && |
| 703 | !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 704 | mode &= ~S_ISGID; |
| 705 | *mode_p = mode; |
| 706 | return 0; |
| 707 | } |
| 708 | EXPORT_SYMBOL(posix_acl_update_mode); |
| 709 | |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 710 | /* |
| 711 | * Fix up the uids and gids in posix acl extended attributes in place. |
| 712 | */ |
| 713 | static void posix_acl_fix_xattr_userns( |
| 714 | struct user_namespace *to, struct user_namespace *from, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 715 | struct user_namespace *mnt_userns, |
| 716 | void *value, size_t size, bool from_user) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 717 | { |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 718 | struct posix_acl_xattr_header *header = value; |
| 719 | struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end; |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 720 | int count; |
| 721 | kuid_t uid; |
| 722 | kgid_t gid; |
| 723 | |
| 724 | if (!value) |
| 725 | return; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 726 | if (size < sizeof(struct posix_acl_xattr_header)) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 727 | return; |
| 728 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) |
| 729 | return; |
| 730 | |
| 731 | count = posix_acl_xattr_count(size); |
| 732 | if (count < 0) |
| 733 | return; |
| 734 | if (count == 0) |
| 735 | return; |
| 736 | |
| 737 | for (end = entry + count; entry != end; entry++) { |
| 738 | switch(le16_to_cpu(entry->e_tag)) { |
| 739 | case ACL_USER: |
| 740 | uid = make_kuid(from, le32_to_cpu(entry->e_id)); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 741 | if (from_user) |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 742 | uid = mapped_kuid_user(mnt_userns, &init_user_ns, uid); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 743 | else |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 744 | uid = mapped_kuid_fs(mnt_userns, &init_user_ns, uid); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 745 | entry->e_id = cpu_to_le32(from_kuid(to, uid)); |
| 746 | break; |
| 747 | case ACL_GROUP: |
| 748 | gid = make_kgid(from, le32_to_cpu(entry->e_id)); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 749 | if (from_user) |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 750 | gid = mapped_kgid_user(mnt_userns, &init_user_ns, gid); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 751 | else |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 752 | gid = mapped_kgid_fs(mnt_userns, &init_user_ns, gid); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 753 | entry->e_id = cpu_to_le32(from_kgid(to, gid)); |
| 754 | break; |
| 755 | default: |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 761 | void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns, |
| 762 | void *value, size_t size) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 763 | { |
| 764 | struct user_namespace *user_ns = current_user_ns(); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 765 | if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns)) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 766 | return; |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 767 | posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value, |
| 768 | size, true); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 771 | void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns, |
| 772 | void *value, size_t size) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 773 | { |
| 774 | struct user_namespace *user_ns = current_user_ns(); |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 775 | if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns)) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 776 | return; |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 777 | posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value, |
| 778 | size, false); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Convert from extended attribute to in-memory representation. |
| 783 | */ |
| 784 | struct posix_acl * |
| 785 | posix_acl_from_xattr(struct user_namespace *user_ns, |
| 786 | const void *value, size_t size) |
| 787 | { |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 788 | const struct posix_acl_xattr_header *header = value; |
| 789 | const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end; |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 790 | int count; |
| 791 | struct posix_acl *acl; |
| 792 | struct posix_acl_entry *acl_e; |
| 793 | |
| 794 | if (!value) |
| 795 | return NULL; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 796 | if (size < sizeof(struct posix_acl_xattr_header)) |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 797 | return ERR_PTR(-EINVAL); |
| 798 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) |
| 799 | return ERR_PTR(-EOPNOTSUPP); |
| 800 | |
| 801 | count = posix_acl_xattr_count(size); |
| 802 | if (count < 0) |
| 803 | return ERR_PTR(-EINVAL); |
| 804 | if (count == 0) |
| 805 | return NULL; |
| 806 | |
| 807 | acl = posix_acl_alloc(count, GFP_NOFS); |
| 808 | if (!acl) |
| 809 | return ERR_PTR(-ENOMEM); |
| 810 | acl_e = acl->a_entries; |
| 811 | |
| 812 | for (end = entry + count; entry != end; acl_e++, entry++) { |
| 813 | acl_e->e_tag = le16_to_cpu(entry->e_tag); |
| 814 | acl_e->e_perm = le16_to_cpu(entry->e_perm); |
| 815 | |
| 816 | switch(acl_e->e_tag) { |
| 817 | case ACL_USER_OBJ: |
| 818 | case ACL_GROUP_OBJ: |
| 819 | case ACL_MASK: |
| 820 | case ACL_OTHER: |
| 821 | break; |
| 822 | |
| 823 | case ACL_USER: |
| 824 | acl_e->e_uid = |
| 825 | make_kuid(user_ns, |
| 826 | le32_to_cpu(entry->e_id)); |
| 827 | if (!uid_valid(acl_e->e_uid)) |
| 828 | goto fail; |
| 829 | break; |
| 830 | case ACL_GROUP: |
| 831 | acl_e->e_gid = |
| 832 | make_kgid(user_ns, |
| 833 | le32_to_cpu(entry->e_id)); |
| 834 | if (!gid_valid(acl_e->e_gid)) |
| 835 | goto fail; |
| 836 | break; |
| 837 | |
| 838 | default: |
| 839 | goto fail; |
| 840 | } |
| 841 | } |
| 842 | return acl; |
| 843 | |
| 844 | fail: |
| 845 | posix_acl_release(acl); |
| 846 | return ERR_PTR(-EINVAL); |
| 847 | } |
| 848 | EXPORT_SYMBOL (posix_acl_from_xattr); |
| 849 | |
| 850 | /* |
| 851 | * Convert from in-memory to extended attribute representation. |
| 852 | */ |
| 853 | int |
| 854 | posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, |
| 855 | void *buffer, size_t size) |
| 856 | { |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 857 | struct posix_acl_xattr_header *ext_acl = buffer; |
| 858 | struct posix_acl_xattr_entry *ext_entry; |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 859 | int real_size, n; |
| 860 | |
| 861 | real_size = posix_acl_xattr_size(acl->a_count); |
| 862 | if (!buffer) |
| 863 | return real_size; |
| 864 | if (real_size > size) |
| 865 | return -ERANGE; |
Dan Carpenter | 47ba973 | 2014-02-14 12:05:49 +0300 | [diff] [blame] | 866 | |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 867 | ext_entry = (void *)(ext_acl + 1); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 868 | ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 869 | |
| 870 | for (n=0; n < acl->a_count; n++, ext_entry++) { |
| 871 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
| 872 | ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); |
| 873 | ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); |
| 874 | switch(acl_e->e_tag) { |
| 875 | case ACL_USER: |
| 876 | ext_entry->e_id = |
| 877 | cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); |
| 878 | break; |
| 879 | case ACL_GROUP: |
| 880 | ext_entry->e_id = |
| 881 | cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); |
| 882 | break; |
| 883 | default: |
| 884 | ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | return real_size; |
| 889 | } |
| 890 | EXPORT_SYMBOL (posix_acl_to_xattr); |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 891 | |
| 892 | static int |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 893 | posix_acl_xattr_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 894 | struct dentry *unused, struct inode *inode, |
| 895 | const char *name, void *value, size_t size) |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 896 | { |
| 897 | struct posix_acl *acl; |
| 898 | int error; |
| 899 | |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 900 | if (!IS_POSIXACL(inode)) |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 901 | return -EOPNOTSUPP; |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 902 | if (S_ISLNK(inode->i_mode)) |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 903 | return -EOPNOTSUPP; |
| 904 | |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 905 | acl = get_acl(inode, handler->flags); |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 906 | if (IS_ERR(acl)) |
| 907 | return PTR_ERR(acl); |
| 908 | if (acl == NULL) |
| 909 | return -ENODATA; |
| 910 | |
| 911 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
| 912 | posix_acl_release(acl); |
| 913 | |
| 914 | return error; |
| 915 | } |
| 916 | |
Andreas Gruenbacher | 485e71e | 2016-06-22 23:57:25 +0200 | [diff] [blame] | 917 | int |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 918 | set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 919 | int type, struct posix_acl *acl) |
Andreas Gruenbacher | 485e71e | 2016-06-22 23:57:25 +0200 | [diff] [blame] | 920 | { |
| 921 | if (!IS_POSIXACL(inode)) |
| 922 | return -EOPNOTSUPP; |
| 923 | if (!inode->i_op->set_acl) |
| 924 | return -EOPNOTSUPP; |
| 925 | |
| 926 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) |
| 927 | return acl ? -EACCES : 0; |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 928 | if (!inode_owner_or_capable(mnt_userns, inode)) |
Andreas Gruenbacher | 485e71e | 2016-06-22 23:57:25 +0200 | [diff] [blame] | 929 | return -EPERM; |
| 930 | |
| 931 | if (acl) { |
Linus Torvalds | a867d73 | 2016-07-29 15:54:19 -0700 | [diff] [blame] | 932 | int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl); |
Andreas Gruenbacher | 485e71e | 2016-06-22 23:57:25 +0200 | [diff] [blame] | 933 | if (ret) |
| 934 | return ret; |
| 935 | } |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 936 | return inode->i_op->set_acl(mnt_userns, inode, acl, type); |
Andreas Gruenbacher | 485e71e | 2016-06-22 23:57:25 +0200 | [diff] [blame] | 937 | } |
| 938 | EXPORT_SYMBOL(set_posix_acl); |
| 939 | |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 940 | static int |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 941 | posix_acl_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 942 | struct user_namespace *mnt_userns, |
| 943 | struct dentry *unused, struct inode *inode, |
| 944 | const char *name, const void *value, size_t size, |
| 945 | int flags) |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 946 | { |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 947 | struct posix_acl *acl = NULL; |
| 948 | int ret; |
| 949 | |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 950 | if (value) { |
| 951 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 952 | if (IS_ERR(acl)) |
| 953 | return PTR_ERR(acl); |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 954 | } |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 955 | ret = set_posix_acl(mnt_userns, inode, handler->flags, acl); |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 956 | posix_acl_release(acl); |
| 957 | return ret; |
| 958 | } |
| 959 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 960 | static bool |
| 961 | posix_acl_xattr_list(struct dentry *dentry) |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 962 | { |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 963 | return IS_POSIXACL(d_backing_inode(dentry)); |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | const struct xattr_handler posix_acl_access_xattr_handler = { |
Andreas Gruenbacher | 98e9cb5 | 2015-12-02 14:44:36 +0100 | [diff] [blame] | 967 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 968 | .flags = ACL_TYPE_ACCESS, |
| 969 | .list = posix_acl_xattr_list, |
| 970 | .get = posix_acl_xattr_get, |
| 971 | .set = posix_acl_xattr_set, |
| 972 | }; |
| 973 | EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler); |
| 974 | |
| 975 | const struct xattr_handler posix_acl_default_xattr_handler = { |
Andreas Gruenbacher | 98e9cb5 | 2015-12-02 14:44:36 +0100 | [diff] [blame] | 976 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
Christoph Hellwig | 2aeccbe | 2013-12-20 05:16:40 -0800 | [diff] [blame] | 977 | .flags = ACL_TYPE_DEFAULT, |
| 978 | .list = posix_acl_xattr_list, |
| 979 | .get = posix_acl_xattr_get, |
| 980 | .set = posix_acl_xattr_set, |
| 981 | }; |
| 982 | EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 983 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 984 | int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 985 | struct posix_acl *acl, int type) |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 986 | { |
| 987 | int error; |
| 988 | |
| 989 | if (type == ACL_TYPE_ACCESS) { |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 990 | error = posix_acl_update_mode(mnt_userns, inode, |
Gu Zheng | 497de07 | 2017-01-09 09:34:48 +0800 | [diff] [blame] | 991 | &inode->i_mode, &acl); |
| 992 | if (error) |
| 993 | return error; |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 996 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 997 | set_cached_acl(inode, type, acl); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | int simple_acl_create(struct inode *dir, struct inode *inode) |
| 1002 | { |
| 1003 | struct posix_acl *default_acl, *acl; |
| 1004 | int error; |
| 1005 | |
| 1006 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
| 1007 | if (error) |
| 1008 | return error; |
| 1009 | |
| 1010 | set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl); |
| 1011 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
| 1012 | |
| 1013 | if (default_acl) |
| 1014 | posix_acl_release(default_acl); |
| 1015 | if (acl) |
| 1016 | posix_acl_release(acl); |
| 1017 | return 0; |
| 1018 | } |