blob: b3eba92f38f579fc576de2c3af2b6f7464f4bc79 [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
225ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
226{
227 handle_t *handle;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400228 int error, credits, retries = 0;
229 size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400230 umode_t mode = inode->i_mode;
231 int update_mode = 0;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800232
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -0400233 error = dquot_initialize(inode);
234 if (error)
235 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800236retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400237 error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
238 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400239 if (error)
240 return error;
241
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400242 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800243 if (IS_ERR(handle))
244 return PTR_ERR(handle);
245
Jan Karaa3bb2d52017-07-30 23:33:01 -0400246 if ((type == ACL_TYPE_ACCESS) && acl) {
247 error = posix_acl_update_mode(inode, &mode, &acl);
248 if (error)
249 goto out_stop;
Chengguang Xu0a1e8252018-12-10 00:22:38 -0500250 if (mode != inode->i_mode)
251 update_mode = 1;
Jan Karaa3bb2d52017-07-30 23:33:01 -0400252 }
253
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400254 error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
Jan Karaa3bb2d52017-07-30 23:33:01 -0400255 if (!error && update_mode) {
256 inode->i_mode = mode;
257 inode->i_ctime = current_time(inode);
258 ext4_mark_inode_dirty(handle, inode);
259 }
260out_stop:
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800261 ext4_journal_stop(handle);
262 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
263 goto retry;
264 return error;
265}
266
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267/*
Mingming Cao617ba132006-10-11 01:20:53 -0700268 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 *
270 * dir->i_mutex: down
271 * inode->i_mutex: up (access to inode is still exclusive)
272 */
273int
Mingming Cao617ba132006-10-11 01:20:53 -0700274ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275{
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800276 struct posix_acl *default_acl, *acl;
Andrew Morton63f57932006-10-11 01:21:24 -0700277 int error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800279 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400280 if (error)
281 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800282
283 if (default_acl) {
284 error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400285 default_acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800286 posix_acl_release(default_acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400287 } else {
288 inode->i_default_acl = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 }
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800290 if (acl) {
291 if (!error)
292 error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400293 acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800294 posix_acl_release(acl);
Chengguang Xu6fd94172018-10-06 22:40:34 -0400295 } else {
296 inode->i_acl = NULL;
Eugene Shatokhin24ec19b2012-11-08 15:11:11 -0500297 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 return error;
299}