blob: c5eaffccecc33935ce6a14516bdf11bb0c52bf10 [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 *
Mingming Cao617ba132006-10-11 01:20:53 -0700145ext4_get_acl(struct inode *inode, int type)
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
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400152 switch (type) {
153 case ACL_TYPE_ACCESS:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400154 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
155 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400156 case ACL_TYPE_DEFAULT:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400157 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
158 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400159 default:
Al Viro073aaa12009-06-09 12:11:54 -0400160 BUG();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 }
Mingming Cao617ba132006-10-11 01:20:53 -0700162 retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 if (retval > 0) {
Josef Bacik216553c2008-04-29 22:02:02 -0400164 value = kmalloc(retval, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 if (!value)
166 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700167 retval = ext4_xattr_get(inode, name_index, "", value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 }
169 if (retval > 0)
Mingming Cao617ba132006-10-11 01:20:53 -0700170 acl = ext4_acl_from_disk(value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171 else if (retval == -ENODATA || retval == -ENOSYS)
172 acl = NULL;
173 else
174 acl = ERR_PTR(retval);
175 kfree(value);
176
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 return acl;
178}
179
180/*
181 * Set the access or default ACL of an inode.
182 *
Mingming Cao617ba132006-10-11 01:20:53 -0700183 * inode->i_mutex: down unless called from ext4_new_inode
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 */
185static int
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800186__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400187 struct posix_acl *acl, int xattr_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189 int name_index;
190 void *value = NULL;
191 size_t size = 0;
192 int error;
193
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400194 switch (type) {
195 case ACL_TYPE_ACCESS:
196 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400197 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400199 case ACL_TYPE_DEFAULT:
200 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
201 if (!S_ISDIR(inode->i_mode))
202 return acl ? -EACCES : 0;
203 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400205 default:
206 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 }
208 if (acl) {
Mingming Cao617ba132006-10-11 01:20:53 -0700209 value = ext4_acl_to_disk(acl, &size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 if (IS_ERR(value))
211 return (int)PTR_ERR(value);
212 }
213
Mingming Cao617ba132006-10-11 01:20:53 -0700214 error = ext4_xattr_set_handle(handle, inode, name_index, "",
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400215 value, size, xattr_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216
217 kfree(value);
Carlos Guerrero Álvarez6b6aeff2020-04-16 16:14:56 +0200218 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400219 set_cached_acl(inode, type, acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221 return error;
222}
223
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800224int
Christian Brauner549c7292021-01-21 14:19:43 +0100225ext4_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
226 struct posix_acl *acl, int type)
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800227{
228 handle_t *handle;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400229 int error, credits, retries = 0;
230 size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400231 umode_t mode = inode->i_mode;
232 int update_mode = 0;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800233
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -0400234 error = dquot_initialize(inode);
235 if (error)
236 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800237retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400238 error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
239 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400240 if (error)
241 return error;
242
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400243 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800244 if (IS_ERR(handle))
245 return PTR_ERR(handle);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -0700246 ext4_fc_start_update(inode);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800247
Jan Karaa3bb2d52017-07-30 23:33:01 -0400248 if ((type == ACL_TYPE_ACCESS) && acl) {
Christian Brauner14f3db52021-01-21 14:19:57 +0100249 error = posix_acl_update_mode(mnt_userns, inode, &mode, &acl);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400250 if (error)
251 goto out_stop;
Chengguang Xu0a1e8252018-12-10 00:22:38 -0500252 if (mode != inode->i_mode)
253 update_mode = 1;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400254 }
255
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400256 error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400257 if (!error && update_mode) {
258 inode->i_mode = mode;
259 inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -0700260 error = ext4_mark_inode_dirty(handle, inode);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400261 }
262out_stop:
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800263 ext4_journal_stop(handle);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -0700264 ext4_fc_stop_update(inode);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800265 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
266 goto retry;
267 return error;
268}
269
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270/*
Mingming Cao617ba132006-10-11 01:20:53 -0700271 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 *
273 * dir->i_mutex: down
274 * inode->i_mutex: up (access to inode is still exclusive)
275 */
276int
Mingming Cao617ba132006-10-11 01:20:53 -0700277ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278{
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800279 struct posix_acl *default_acl, *acl;
Andrew Morton63f57932006-10-11 01:21:24 -0700280 int error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800282 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400283 if (error)
284 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800285
286 if (default_acl) {
287 error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400288 default_acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800289 posix_acl_release(default_acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400290 } else {
291 inode->i_default_acl = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 }
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800293 if (acl) {
294 if (!error)
295 error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400296 acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800297 posix_acl_release(acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400298 } else {
299 inode->i_acl = NULL;
Eugene Shatokhin24ec19b2012-11-08 15:11:11 -0500300 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 return error;
302}