Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * linux/fs/ext2/acl.c
  4 *
  5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  6 */
  7
 
  8#include <linux/init.h>
  9#include <linux/sched.h>
 10#include <linux/slab.h>
 11#include <linux/fs.h>
 12#include "ext2.h"
 13#include "xattr.h"
 14#include "acl.h"
 15
 16/*
 17 * Convert from filesystem to in-memory representation.
 18 */
 19static struct posix_acl *
 20ext2_acl_from_disk(const void *value, size_t size)
 21{
 22	const char *end = (char *)value + size;
 23	int n, count;
 24	struct posix_acl *acl;
 25
 26	if (!value)
 27		return NULL;
 28	if (size < sizeof(ext2_acl_header))
 29		 return ERR_PTR(-EINVAL);
 30	if (((ext2_acl_header *)value)->a_version !=
 31	    cpu_to_le32(EXT2_ACL_VERSION))
 32		return ERR_PTR(-EINVAL);
 33	value = (char *)value + sizeof(ext2_acl_header);
 34	count = ext2_acl_count(size);
 35	if (count < 0)
 36		return ERR_PTR(-EINVAL);
 37	if (count == 0)
 38		return NULL;
 39	acl = posix_acl_alloc(count, GFP_KERNEL);
 40	if (!acl)
 41		return ERR_PTR(-ENOMEM);
 42	for (n=0; n < count; n++) {
 43		ext2_acl_entry *entry =
 44			(ext2_acl_entry *)value;
 45		if ((char *)value + sizeof(ext2_acl_entry_short) > end)
 46			goto fail;
 47		acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
 48		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
 49		switch(acl->a_entries[n].e_tag) {
 50			case ACL_USER_OBJ:
 51			case ACL_GROUP_OBJ:
 52			case ACL_MASK:
 53			case ACL_OTHER:
 54				value = (char *)value +
 55					sizeof(ext2_acl_entry_short);
 
 56				break;
 57
 58			case ACL_USER:
 59				value = (char *)value + sizeof(ext2_acl_entry);
 60				if ((char *)value > end)
 61					goto fail;
 62				acl->a_entries[n].e_uid =
 63					make_kuid(&init_user_ns,
 64						  le32_to_cpu(entry->e_id));
 65				break;
 66			case ACL_GROUP:
 67				value = (char *)value + sizeof(ext2_acl_entry);
 68				if ((char *)value > end)
 69					goto fail;
 70				acl->a_entries[n].e_gid =
 71					make_kgid(&init_user_ns,
 72						  le32_to_cpu(entry->e_id));
 73				break;
 74
 75			default:
 76				goto fail;
 77		}
 78	}
 79	if (value != end)
 80		goto fail;
 81	return acl;
 82
 83fail:
 84	posix_acl_release(acl);
 85	return ERR_PTR(-EINVAL);
 86}
 87
 88/*
 89 * Convert from in-memory to filesystem representation.
 90 */
 91static void *
 92ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
 93{
 94	ext2_acl_header *ext_acl;
 95	char *e;
 96	size_t n;
 97
 98	*size = ext2_acl_size(acl->a_count);
 99	ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
100			sizeof(ext2_acl_entry), GFP_KERNEL);
101	if (!ext_acl)
102		return ERR_PTR(-ENOMEM);
103	ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
104	e = (char *)ext_acl + sizeof(ext2_acl_header);
105	for (n=0; n < acl->a_count; n++) {
106		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
107		ext2_acl_entry *entry = (ext2_acl_entry *)e;
108		entry->e_tag  = cpu_to_le16(acl_e->e_tag);
109		entry->e_perm = cpu_to_le16(acl_e->e_perm);
110		switch(acl_e->e_tag) {
111			case ACL_USER:
112				entry->e_id = cpu_to_le32(
113					from_kuid(&init_user_ns, acl_e->e_uid));
114				e += sizeof(ext2_acl_entry);
115				break;
116			case ACL_GROUP:
117				entry->e_id = cpu_to_le32(
118					from_kgid(&init_user_ns, acl_e->e_gid));
119				e += sizeof(ext2_acl_entry);
120				break;
121
122			case ACL_USER_OBJ:
123			case ACL_GROUP_OBJ:
124			case ACL_MASK:
125			case ACL_OTHER:
126				e += sizeof(ext2_acl_entry_short);
127				break;
128
129			default:
130				goto fail;
131		}
132	}
133	return (char *)ext_acl;
134
135fail:
136	kfree(ext_acl);
137	return ERR_PTR(-EINVAL);
138}
139
140/*
141 * inode->i_mutex: don't care
142 */
143struct posix_acl *
144ext2_get_acl(struct inode *inode, int type, bool rcu)
145{
146	int name_index;
147	char *value = NULL;
148	struct posix_acl *acl;
149	int retval;
150
151	if (rcu)
152		return ERR_PTR(-ECHILD);
 
 
 
 
153
154	switch (type) {
155	case ACL_TYPE_ACCESS:
156		name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
157		break;
158	case ACL_TYPE_DEFAULT:
159		name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
160		break;
161	default:
162		BUG();
163	}
164	retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
165	if (retval > 0) {
166		value = kmalloc(retval, GFP_KERNEL);
167		if (!value)
168			return ERR_PTR(-ENOMEM);
169		retval = ext2_xattr_get(inode, name_index, "", value, retval);
170	}
171	if (retval > 0)
172		acl = ext2_acl_from_disk(value, retval);
173	else if (retval == -ENODATA || retval == -ENOSYS)
174		acl = NULL;
175	else
176		acl = ERR_PTR(retval);
177	kfree(value);
178
 
 
 
179	return acl;
180}
181
 
 
 
182static int
183__ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
184{
185	int name_index;
186	void *value = NULL;
187	size_t size = 0;
188	int error;
189
 
 
 
 
 
190	switch(type) {
191		case ACL_TYPE_ACCESS:
192			name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
 
 
 
 
 
 
 
 
 
 
 
193			break;
194
195		case ACL_TYPE_DEFAULT:
196			name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
197			if (!S_ISDIR(inode->i_mode))
198				return acl ? -EACCES : 0;
199			break;
200
201		default:
202			return -EINVAL;
203	}
204 	if (acl) {
205		value = ext2_acl_to_disk(acl, &size);
206		if (IS_ERR(value))
207			return (int)PTR_ERR(value);
208	}
209
210	error = ext2_xattr_set(inode, name_index, "", value, size, 0);
211
212	kfree(value);
213	if (!error)
214		set_cached_acl(inode, type, acl);
215	return error;
216}
217
218/*
219 * inode->i_mutex: down
 
 
 
220 */
221int
222ext2_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
223	     struct posix_acl *acl, int type)
224{
225	int error;
226	int update_mode = 0;
227	struct inode *inode = d_inode(dentry);
228	umode_t mode = inode->i_mode;
229
230	if (type == ACL_TYPE_ACCESS && acl) {
231		error = posix_acl_update_mode(&init_user_ns, inode, &mode,
232					      &acl);
233		if (error)
234			return error;
235		update_mode = 1;
236	}
237	error = __ext2_set_acl(inode, acl, type);
238	if (!error && update_mode) {
239		inode->i_mode = mode;
240		inode->i_ctime = current_time(inode);
241		mark_inode_dirty(inode);
 
 
 
 
 
 
 
 
242	}
243	return error;
 
 
244}
245
246/*
247 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
 
 
 
248 *
249 * dir->i_mutex: down
250 * inode->i_mutex: up (access to inode is still exclusive)
 
 
 
 
 
251 */
252int
253ext2_init_acl(struct inode *inode, struct inode *dir)
254{
255	struct posix_acl *default_acl, *acl;
256	int error;
257
258	error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
 
 
 
 
 
 
 
259	if (error)
260		return error;
 
 
 
 
261
262	if (default_acl) {
263		error = __ext2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
264		posix_acl_release(default_acl);
265	} else {
266		inode->i_default_acl = NULL;
267	}
268	if (acl) {
269		if (!error)
270			error = __ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
271		posix_acl_release(acl);
272	} else {
273		inode->i_acl = NULL;
274	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275	return error;
276}
v3.1
 
  1/*
  2 * linux/fs/ext2/acl.c
  3 *
  4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5 */
  6
  7#include <linux/capability.h>
  8#include <linux/init.h>
  9#include <linux/sched.h>
 10#include <linux/slab.h>
 11#include <linux/fs.h>
 12#include "ext2.h"
 13#include "xattr.h"
 14#include "acl.h"
 15
 16/*
 17 * Convert from filesystem to in-memory representation.
 18 */
 19static struct posix_acl *
 20ext2_acl_from_disk(const void *value, size_t size)
 21{
 22	const char *end = (char *)value + size;
 23	int n, count;
 24	struct posix_acl *acl;
 25
 26	if (!value)
 27		return NULL;
 28	if (size < sizeof(ext2_acl_header))
 29		 return ERR_PTR(-EINVAL);
 30	if (((ext2_acl_header *)value)->a_version !=
 31	    cpu_to_le32(EXT2_ACL_VERSION))
 32		return ERR_PTR(-EINVAL);
 33	value = (char *)value + sizeof(ext2_acl_header);
 34	count = ext2_acl_count(size);
 35	if (count < 0)
 36		return ERR_PTR(-EINVAL);
 37	if (count == 0)
 38		return NULL;
 39	acl = posix_acl_alloc(count, GFP_KERNEL);
 40	if (!acl)
 41		return ERR_PTR(-ENOMEM);
 42	for (n=0; n < count; n++) {
 43		ext2_acl_entry *entry =
 44			(ext2_acl_entry *)value;
 45		if ((char *)value + sizeof(ext2_acl_entry_short) > end)
 46			goto fail;
 47		acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
 48		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
 49		switch(acl->a_entries[n].e_tag) {
 50			case ACL_USER_OBJ:
 51			case ACL_GROUP_OBJ:
 52			case ACL_MASK:
 53			case ACL_OTHER:
 54				value = (char *)value +
 55					sizeof(ext2_acl_entry_short);
 56				acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
 57				break;
 58
 59			case ACL_USER:
 
 
 
 
 
 
 
 60			case ACL_GROUP:
 61				value = (char *)value + sizeof(ext2_acl_entry);
 62				if ((char *)value > end)
 63					goto fail;
 64				acl->a_entries[n].e_id =
 65					le32_to_cpu(entry->e_id);
 
 66				break;
 67
 68			default:
 69				goto fail;
 70		}
 71	}
 72	if (value != end)
 73		goto fail;
 74	return acl;
 75
 76fail:
 77	posix_acl_release(acl);
 78	return ERR_PTR(-EINVAL);
 79}
 80
 81/*
 82 * Convert from in-memory to filesystem representation.
 83 */
 84static void *
 85ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
 86{
 87	ext2_acl_header *ext_acl;
 88	char *e;
 89	size_t n;
 90
 91	*size = ext2_acl_size(acl->a_count);
 92	ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
 93			sizeof(ext2_acl_entry), GFP_KERNEL);
 94	if (!ext_acl)
 95		return ERR_PTR(-ENOMEM);
 96	ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
 97	e = (char *)ext_acl + sizeof(ext2_acl_header);
 98	for (n=0; n < acl->a_count; n++) {
 
 99		ext2_acl_entry *entry = (ext2_acl_entry *)e;
100		entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
101		entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
102		switch(acl->a_entries[n].e_tag) {
103			case ACL_USER:
 
 
 
 
104			case ACL_GROUP:
105				entry->e_id =
106					cpu_to_le32(acl->a_entries[n].e_id);
107				e += sizeof(ext2_acl_entry);
108				break;
109
110			case ACL_USER_OBJ:
111			case ACL_GROUP_OBJ:
112			case ACL_MASK:
113			case ACL_OTHER:
114				e += sizeof(ext2_acl_entry_short);
115				break;
116
117			default:
118				goto fail;
119		}
120	}
121	return (char *)ext_acl;
122
123fail:
124	kfree(ext_acl);
125	return ERR_PTR(-EINVAL);
126}
127
128/*
129 * inode->i_mutex: don't care
130 */
131struct posix_acl *
132ext2_get_acl(struct inode *inode, int type)
133{
134	int name_index;
135	char *value = NULL;
136	struct posix_acl *acl;
137	int retval;
138
139	if (!test_opt(inode->i_sb, POSIX_ACL))
140		return NULL;
141
142	acl = get_cached_acl(inode, type);
143	if (acl != ACL_NOT_CACHED)
144		return acl;
145
146	switch (type) {
147	case ACL_TYPE_ACCESS:
148		name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
149		break;
150	case ACL_TYPE_DEFAULT:
151		name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
152		break;
153	default:
154		BUG();
155	}
156	retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
157	if (retval > 0) {
158		value = kmalloc(retval, GFP_KERNEL);
159		if (!value)
160			return ERR_PTR(-ENOMEM);
161		retval = ext2_xattr_get(inode, name_index, "", value, retval);
162	}
163	if (retval > 0)
164		acl = ext2_acl_from_disk(value, retval);
165	else if (retval == -ENODATA || retval == -ENOSYS)
166		acl = NULL;
167	else
168		acl = ERR_PTR(retval);
169	kfree(value);
170
171	if (!IS_ERR(acl))
172		set_cached_acl(inode, type, acl);
173
174	return acl;
175}
176
177/*
178 * inode->i_mutex: down
179 */
180static int
181ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
182{
183	int name_index;
184	void *value = NULL;
185	size_t size = 0;
186	int error;
187
188	if (S_ISLNK(inode->i_mode))
189		return -EOPNOTSUPP;
190	if (!test_opt(inode->i_sb, POSIX_ACL))
191		return 0;
192
193	switch(type) {
194		case ACL_TYPE_ACCESS:
195			name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
196			if (acl) {
197				error = posix_acl_equiv_mode(acl, &inode->i_mode);
198				if (error < 0)
199					return error;
200				else {
201					inode->i_ctime = CURRENT_TIME_SEC;
202					mark_inode_dirty(inode);
203					if (error == 0)
204						acl = NULL;
205				}
206			}
207			break;
208
209		case ACL_TYPE_DEFAULT:
210			name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
211			if (!S_ISDIR(inode->i_mode))
212				return acl ? -EACCES : 0;
213			break;
214
215		default:
216			return -EINVAL;
217	}
218 	if (acl) {
219		value = ext2_acl_to_disk(acl, &size);
220		if (IS_ERR(value))
221			return (int)PTR_ERR(value);
222	}
223
224	error = ext2_xattr_set(inode, name_index, "", value, size, 0);
225
226	kfree(value);
227	if (!error)
228		set_cached_acl(inode, type, acl);
229	return error;
230}
231
232/*
233 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
234 *
235 * dir->i_mutex: down
236 * inode->i_mutex: up (access to inode is still exclusive)
237 */
238int
239ext2_init_acl(struct inode *inode, struct inode *dir)
 
240{
241	struct posix_acl *acl = NULL;
242	int error = 0;
243
244	if (!S_ISLNK(inode->i_mode)) {
245		if (test_opt(dir->i_sb, POSIX_ACL)) {
246			acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
247			if (IS_ERR(acl))
248				return PTR_ERR(acl);
249		}
250		if (!acl)
251			inode->i_mode &= ~current_umask();
252	}
253	if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
254		if (S_ISDIR(inode->i_mode)) {
255			error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
256			if (error)
257				goto cleanup;
258		}
259		error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
260		if (error < 0)
261			return error;
262		if (error > 0) {
263			/* This is an extended ACL */
264			error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
265		}
266	}
267cleanup:
268       posix_acl_release(acl);
269       return error;
270}
271
272/*
273 * Does chmod for an inode that may have an Access Control List. The
274 * inode->i_mode field must be updated to the desired value by the caller
275 * before calling this function.
276 * Returns 0 on success, or a negative error number.
277 *
278 * We change the ACL rather than storing some ACL entries in the file
279 * mode permission bits (which would be more efficient), because that
280 * would break once additional permissions (like  ACL_APPEND, ACL_DELETE
281 * for directories) are added. There are no more bits available in the
282 * file mode.
283 *
284 * inode->i_mutex: down
285 */
286int
287ext2_acl_chmod(struct inode *inode)
288{
289	struct posix_acl *acl;
290        int error;
291
292	if (!test_opt(inode->i_sb, POSIX_ACL))
293		return 0;
294	if (S_ISLNK(inode->i_mode))
295		return -EOPNOTSUPP;
296	acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
297	if (IS_ERR(acl) || !acl)
298		return PTR_ERR(acl);
299	error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
300	if (error)
301		return error;
302	error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
303	posix_acl_release(acl);
304	return error;
305}
306
307/*
308 * Extended attribut handlers
309 */
310static size_t
311ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
312			   const char *name, size_t name_len, int type)
313{
314	const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
315
316	if (!test_opt(dentry->d_sb, POSIX_ACL))
317		return 0;
318	if (list && size <= list_size)
319		memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
320	return size;
321}
322
323static size_t
324ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
325			    const char *name, size_t name_len, int type)
326{
327	const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
328
329	if (!test_opt(dentry->d_sb, POSIX_ACL))
330		return 0;
331	if (list && size <= list_size)
332		memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
333	return size;
334}
335
336static int
337ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
338		   size_t size, int type)
339{
340	struct posix_acl *acl;
341	int error;
342
343	if (strcmp(name, "") != 0)
344		return -EINVAL;
345	if (!test_opt(dentry->d_sb, POSIX_ACL))
346		return -EOPNOTSUPP;
347
348	acl = ext2_get_acl(dentry->d_inode, type);
349	if (IS_ERR(acl))
350		return PTR_ERR(acl);
351	if (acl == NULL)
352		return -ENODATA;
353	error = posix_acl_to_xattr(acl, buffer, size);
354	posix_acl_release(acl);
355
356	return error;
357}
358
359static int
360ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
361		   size_t size, int flags, int type)
362{
363	struct posix_acl *acl;
364	int error;
365
366	if (strcmp(name, "") != 0)
367		return -EINVAL;
368	if (!test_opt(dentry->d_sb, POSIX_ACL))
369		return -EOPNOTSUPP;
370	if (!inode_owner_or_capable(dentry->d_inode))
371		return -EPERM;
372
373	if (value) {
374		acl = posix_acl_from_xattr(value, size);
375		if (IS_ERR(acl))
376			return PTR_ERR(acl);
377		else if (acl) {
378			error = posix_acl_valid(acl);
379			if (error)
380				goto release_and_out;
381		}
382	} else
383		acl = NULL;
384
385	error = ext2_set_acl(dentry->d_inode, type, acl);
386
387release_and_out:
388	posix_acl_release(acl);
389	return error;
390}
391
392const struct xattr_handler ext2_xattr_acl_access_handler = {
393	.prefix	= POSIX_ACL_XATTR_ACCESS,
394	.flags	= ACL_TYPE_ACCESS,
395	.list	= ext2_xattr_list_acl_access,
396	.get	= ext2_xattr_get_acl,
397	.set	= ext2_xattr_set_acl,
398};
399
400const struct xattr_handler ext2_xattr_acl_default_handler = {
401	.prefix	= POSIX_ACL_XATTR_DEFAULT,
402	.flags	= ACL_TYPE_DEFAULT,
403	.list	= ext2_xattr_list_acl_default,
404	.get	= ext2_xattr_get_acl,
405	.set	= ext2_xattr_set_acl,
406};