blob: 5a35768d6149a151160f99dc4ba12b80c8d02b7e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/acl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 */
7
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04008#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -04009#include "ext4_jbd2.h"
10#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070011#include "xattr.h"
12#include "acl.h"
13
14/*
15 * Convert from filesystem to in-memory representation.
16 */
17static struct posix_acl *
Mingming Cao617ba132006-10-11 01:20:53 -070018ext4_acl_from_disk(const void *value, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070019{
20 const char *end = (char *)value + size;
21 int n, count;
22 struct posix_acl *acl;
23
24 if (!value)
25 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -070026 if (size < sizeof(ext4_acl_header))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027 return ERR_PTR(-EINVAL);
Mingming Cao617ba132006-10-11 01:20:53 -070028 if (((ext4_acl_header *)value)->a_version !=
29 cpu_to_le32(EXT4_ACL_VERSION))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030 return ERR_PTR(-EINVAL);
Mingming Cao617ba132006-10-11 01:20:53 -070031 value = (char *)value + sizeof(ext4_acl_header);
32 count = ext4_acl_count(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033 if (count < 0)
34 return ERR_PTR(-EINVAL);
35 if (count == 0)
36 return NULL;
Josef Bacik216553c2008-04-29 22:02:02 -040037 acl = posix_acl_alloc(count, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038 if (!acl)
39 return ERR_PTR(-ENOMEM);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040040 for (n = 0; n < count; n++) {
Mingming Cao617ba132006-10-11 01:20:53 -070041 ext4_acl_entry *entry =
42 (ext4_acl_entry *)value;
43 if ((char *)value + sizeof(ext4_acl_entry_short) > end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044 goto fail;
45 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
46 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040048 switch (acl->a_entries[n].e_tag) {
49 case ACL_USER_OBJ:
50 case ACL_GROUP_OBJ:
51 case ACL_MASK:
52 case ACL_OTHER:
53 value = (char *)value +
54 sizeof(ext4_acl_entry_short);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040055 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040057 case ACL_USER:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -070058 value = (char *)value + sizeof(ext4_acl_entry);
59 if ((char *)value > end)
60 goto fail;
61 acl->a_entries[n].e_uid =
62 make_kuid(&init_user_ns,
63 le32_to_cpu(entry->e_id));
64 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040065 case ACL_GROUP:
66 value = (char *)value + sizeof(ext4_acl_entry);
67 if ((char *)value > end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070068 goto fail;
Eric W. Biedermanaf84df92012-09-10 20:44:54 -070069 acl->a_entries[n].e_gid =
70 make_kgid(&init_user_ns,
71 le32_to_cpu(entry->e_id));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040072 break;
73
74 default:
75 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 }
77 }
78 if (value != end)
79 goto fail;
80 return acl;
81
82fail:
83 posix_acl_release(acl);
84 return ERR_PTR(-EINVAL);
85}
86
87/*
88 * Convert from in-memory to filesystem representation.
89 */
90static void *
Mingming Cao617ba132006-10-11 01:20:53 -070091ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092{
Mingming Cao617ba132006-10-11 01:20:53 -070093 ext4_acl_header *ext_acl;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070094 char *e;
95 size_t n;
96
Mingming Cao617ba132006-10-11 01:20:53 -070097 *size = ext4_acl_size(acl->a_count);
98 ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
Josef Bacik216553c2008-04-29 22:02:02 -040099 sizeof(ext4_acl_entry), GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 if (!ext_acl)
101 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700102 ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
103 e = (char *)ext_acl + sizeof(ext4_acl_header);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400104 for (n = 0; n < acl->a_count; n++) {
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700105 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
Mingming Cao617ba132006-10-11 01:20:53 -0700106 ext4_acl_entry *entry = (ext4_acl_entry *)e;
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700107 entry->e_tag = cpu_to_le16(acl_e->e_tag);
108 entry->e_perm = cpu_to_le16(acl_e->e_perm);
109 switch (acl_e->e_tag) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400110 case ACL_USER:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700111 entry->e_id = cpu_to_le32(
112 from_kuid(&init_user_ns, acl_e->e_uid));
113 e += sizeof(ext4_acl_entry);
114 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400115 case ACL_GROUP:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700116 entry->e_id = cpu_to_le32(
117 from_kgid(&init_user_ns, acl_e->e_gid));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400118 e += sizeof(ext4_acl_entry);
119 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400121 case ACL_USER_OBJ:
122 case ACL_GROUP_OBJ:
123 case ACL_MASK:
124 case ACL_OTHER:
125 e += sizeof(ext4_acl_entry_short);
126 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700127
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400128 default:
129 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700130 }
131 }
132 return (char *)ext_acl;
133
134fail:
135 kfree(ext_acl);
136 return ERR_PTR(-EINVAL);
137}
138
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139/*
140 * Inode operation get_posix_acl().
141 *
142 * inode->i_mutex: don't care
143 */
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200144struct posix_acl *
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200145ext4_get_acl(struct inode *inode, int type, bool rcu)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 int name_index;
148 char *value = NULL;
149 struct posix_acl *acl;
150 int retval;
151
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200152 if (rcu)
153 return ERR_PTR(-ECHILD);
154
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400155 switch (type) {
156 case ACL_TYPE_ACCESS:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400157 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
158 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400159 case ACL_TYPE_DEFAULT:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400160 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
161 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400162 default:
Al Viro073aaa12009-06-09 12:11:54 -0400163 BUG();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164 }
Mingming Cao617ba132006-10-11 01:20:53 -0700165 retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 if (retval > 0) {
Josef Bacik216553c2008-04-29 22:02:02 -0400167 value = kmalloc(retval, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 if (!value)
169 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700170 retval = ext4_xattr_get(inode, name_index, "", value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171 }
172 if (retval > 0)
Mingming Cao617ba132006-10-11 01:20:53 -0700173 acl = ext4_acl_from_disk(value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 else if (retval == -ENODATA || retval == -ENOSYS)
175 acl = NULL;
176 else
177 acl = ERR_PTR(retval);
178 kfree(value);
179
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 return acl;
181}
182
183/*
184 * Set the access or default ACL of an inode.
185 *
Mingming Cao617ba132006-10-11 01:20:53 -0700186 * inode->i_mutex: down unless called from ext4_new_inode
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 */
188static int
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800189__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400190 struct posix_acl *acl, int xattr_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 int name_index;
193 void *value = NULL;
194 size_t size = 0;
195 int error;
196
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400197 switch (type) {
198 case ACL_TYPE_ACCESS:
199 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400200 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400202 case ACL_TYPE_DEFAULT:
203 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
204 if (!S_ISDIR(inode->i_mode))
205 return acl ? -EACCES : 0;
206 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400208 default:
209 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 }
211 if (acl) {
Mingming Cao617ba132006-10-11 01:20:53 -0700212 value = ext4_acl_to_disk(acl, &size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 if (IS_ERR(value))
214 return (int)PTR_ERR(value);
215 }
216
Mingming Cao617ba132006-10-11 01:20:53 -0700217 error = ext4_xattr_set_handle(handle, inode, name_index, "",
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400218 value, size, xattr_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219
220 kfree(value);
Carlos Guerrero Álvarez6b6aeff2020-04-16 16:14:56 +0200221 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400222 set_cached_acl(inode, type, acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 return error;
225}
226
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800227int
Christian Brauner549c7292021-01-21 14:19:43 +0100228ext4_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
229 struct posix_acl *acl, int type)
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800230{
231 handle_t *handle;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400232 int error, credits, retries = 0;
233 size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400234 umode_t mode = inode->i_mode;
235 int update_mode = 0;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800236
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -0400237 error = dquot_initialize(inode);
238 if (error)
239 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800240retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400241 error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
242 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400243 if (error)
244 return error;
245
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400246 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800247 if (IS_ERR(handle))
248 return PTR_ERR(handle);
249
Jan Karaa3bb2d52017-07-30 23:33:01 -0400250 if ((type == ACL_TYPE_ACCESS) && acl) {
Christian Brauner14f3db52021-01-21 14:19:57 +0100251 error = posix_acl_update_mode(mnt_userns, inode, &mode, &acl);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400252 if (error)
253 goto out_stop;
Chengguang Xu0a1e8252018-12-10 00:22:38 -0500254 if (mode != inode->i_mode)
255 update_mode = 1;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400256 }
257
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400258 error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400259 if (!error && update_mode) {
260 inode->i_mode = mode;
261 inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700262 error = ext4_mark_inode_dirty(handle, inode);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400263 }
264out_stop:
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800265 ext4_journal_stop(handle);
266 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
267 goto retry;
268 return error;
269}
270
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271/*
Mingming Cao617ba132006-10-11 01:20:53 -0700272 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 *
274 * dir->i_mutex: down
275 * inode->i_mutex: up (access to inode is still exclusive)
276 */
277int
Mingming Cao617ba132006-10-11 01:20:53 -0700278ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279{
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800280 struct posix_acl *default_acl, *acl;
Andrew Morton63f57932006-10-11 01:21:24 -0700281 int error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800283 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400284 if (error)
285 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800286
287 if (default_acl) {
288 error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400289 default_acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800290 posix_acl_release(default_acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400291 } else {
292 inode->i_default_acl = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 }
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800294 if (acl) {
295 if (!error)
296 error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400297 acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800298 posix_acl_release(acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400299 } else {
300 inode->i_acl = NULL;
Eugene Shatokhin24ec19b2012-11-08 15:11:11 -0500301 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 return error;
303}