Blame view

fs/ext2/acl.c 5.78 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /*
   * linux/fs/ext2/acl.c
   *
   * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
   */
  
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include "ext2.h"
  #include "xattr.h"
  #include "acl.h"
  
  /*
   * Convert from filesystem to in-memory representation.
   */
  static struct posix_acl *
  ext2_acl_from_disk(const void *value, size_t size)
  {
  	const char *end = (char *)value + size;
  	int n, count;
  	struct posix_acl *acl;
  
  	if (!value)
  		return NULL;
  	if (size < sizeof(ext2_acl_header))
  		 return ERR_PTR(-EINVAL);
  	if (((ext2_acl_header *)value)->a_version !=
  	    cpu_to_le32(EXT2_ACL_VERSION))
  		return ERR_PTR(-EINVAL);
  	value = (char *)value + sizeof(ext2_acl_header);
  	count = ext2_acl_count(size);
  	if (count < 0)
  		return ERR_PTR(-EINVAL);
  	if (count == 0)
  		return NULL;
  	acl = posix_acl_alloc(count, GFP_KERNEL);
  	if (!acl)
  		return ERR_PTR(-ENOMEM);
  	for (n=0; n < count; n++) {
  		ext2_acl_entry *entry =
  			(ext2_acl_entry *)value;
  		if ((char *)value + sizeof(ext2_acl_entry_short) > end)
  			goto fail;
  		acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
  		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  		switch(acl->a_entries[n].e_tag) {
  			case ACL_USER_OBJ:
  			case ACL_GROUP_OBJ:
  			case ACL_MASK:
  			case ACL_OTHER:
  				value = (char *)value +
  					sizeof(ext2_acl_entry_short);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
  				break;
  
  			case ACL_USER:
af84df93f   Eric W. Biederman   userns: Convert e...
59
60
61
62
63
64
65
  				value = (char *)value + sizeof(ext2_acl_entry);
  				if ((char *)value > end)
  					goto fail;
  				acl->a_entries[n].e_uid =
  					make_kuid(&init_user_ns,
  						  le32_to_cpu(entry->e_id));
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
68
69
  			case ACL_GROUP:
  				value = (char *)value + sizeof(ext2_acl_entry);
  				if ((char *)value > end)
  					goto fail;
af84df93f   Eric W. Biederman   userns: Convert e...
70
71
72
  				acl->a_entries[n].e_gid =
  					make_kgid(&init_user_ns,
  						  le32_to_cpu(entry->e_id));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  				break;
  
  			default:
  				goto fail;
  		}
  	}
  	if (value != end)
  		goto fail;
  	return acl;
  
  fail:
  	posix_acl_release(acl);
  	return ERR_PTR(-EINVAL);
  }
  
  /*
   * Convert from in-memory to filesystem representation.
   */
  static void *
  ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
  {
  	ext2_acl_header *ext_acl;
  	char *e;
  	size_t n;
  
  	*size = ext2_acl_size(acl->a_count);
f52720ca5   Panagiotis Issaris   [PATCH] fs: Remov...
99
100
  	ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
  			sizeof(ext2_acl_entry), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
  	if (!ext_acl)
  		return ERR_PTR(-ENOMEM);
  	ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
  	e = (char *)ext_acl + sizeof(ext2_acl_header);
  	for (n=0; n < acl->a_count; n++) {
af84df93f   Eric W. Biederman   userns: Convert e...
106
  		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  		ext2_acl_entry *entry = (ext2_acl_entry *)e;
af84df93f   Eric W. Biederman   userns: Convert e...
108
109
110
  		entry->e_tag  = cpu_to_le16(acl_e->e_tag);
  		entry->e_perm = cpu_to_le16(acl_e->e_perm);
  		switch(acl_e->e_tag) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  			case ACL_USER:
af84df93f   Eric W. Biederman   userns: Convert e...
112
113
114
115
  				entry->e_id = cpu_to_le32(
  					from_kuid(&init_user_ns, acl_e->e_uid));
  				e += sizeof(ext2_acl_entry);
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  			case ACL_GROUP:
af84df93f   Eric W. Biederman   userns: Convert e...
117
118
  				entry->e_id = cpu_to_le32(
  					from_kgid(&init_user_ns, acl_e->e_gid));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  				e += sizeof(ext2_acl_entry);
  				break;
  
  			case ACL_USER_OBJ:
  			case ACL_GROUP_OBJ:
  			case ACL_MASK:
  			case ACL_OTHER:
  				e += sizeof(ext2_acl_entry_short);
  				break;
  
  			default:
  				goto fail;
  		}
  	}
  	return (char *)ext_acl;
  
  fail:
  	kfree(ext_acl);
  	return ERR_PTR(-EINVAL);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
140
   * inode->i_mutex: don't care
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
   */
4e34e719e   Christoph Hellwig   fs: take the ACL ...
142
  struct posix_acl *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
  ext2_get_acl(struct inode *inode, int type)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
148
  	int name_index;
  	char *value = NULL;
  	struct posix_acl *acl;
  	int retval;
073aaa1b1   Al Viro   helpers for acl c...
149
150
151
152
153
154
155
156
157
  	switch (type) {
  	case ACL_TYPE_ACCESS:
  		name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  		break;
  	case ACL_TYPE_DEFAULT:
  		name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  		break;
  	default:
  		BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  	}
  	retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
  	if (retval > 0) {
  		value = kmalloc(retval, GFP_KERNEL);
  		if (!value)
  			return ERR_PTR(-ENOMEM);
  		retval = ext2_xattr_get(inode, name_index, "", value, retval);
  	}
  	if (retval > 0)
  		acl = ext2_acl_from_disk(value, retval);
  	else if (retval == -ENODATA || retval == -ENOSYS)
  		acl = NULL;
  	else
  		acl = ERR_PTR(retval);
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
172
  	kfree(value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
  	return acl;
  }
a992f2d38   Jan Kara   ext2: Don't clear...
176
177
  static int
  __ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
  	int name_index;
  	void *value = NULL;
dfa08592c   Andreas Gruenbacher   [PATCH] Fix two e...
181
  	size_t size = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
  	switch(type) {
  		case ACL_TYPE_ACCESS:
  			name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  			break;
  
  		case ACL_TYPE_DEFAULT:
  			name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  			if (!S_ISDIR(inode->i_mode))
  				return acl ? -EACCES : 0;
  			break;
  
  		default:
  			return -EINVAL;
  	}
   	if (acl) {
  		value = ext2_acl_to_disk(acl, &size);
  		if (IS_ERR(value))
  			return (int)PTR_ERR(value);
  	}
  
  	error = ext2_xattr_set(inode, name_index, "", value, size, 0);
f99d49adf   Jesper Juhl   [PATCH] kfree cle...
204
  	kfree(value);
073aaa1b1   Al Viro   helpers for acl c...
205
206
  	if (!error)
  		set_cached_acl(inode, type, acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  /*
a992f2d38   Jan Kara   ext2: Don't clear...
210
211
212
213
214
215
   * inode->i_mutex: down
   */
  int
  ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  {
  	int error;
fe26569eb   Ernesto A. Fernández   ext2: preserve i_...
216
217
  	int update_mode = 0;
  	umode_t mode = inode->i_mode;
a992f2d38   Jan Kara   ext2: Don't clear...
218
219
  
  	if (type == ACL_TYPE_ACCESS && acl) {
fe26569eb   Ernesto A. Fernández   ext2: preserve i_...
220
  		error = posix_acl_update_mode(inode, &mode, &acl);
a992f2d38   Jan Kara   ext2: Don't clear...
221
222
  		if (error)
  			return error;
fe26569eb   Ernesto A. Fernández   ext2: preserve i_...
223
224
225
226
227
  		update_mode = 1;
  	}
  	error = __ext2_set_acl(inode, acl, type);
  	if (!error && update_mode) {
  		inode->i_mode = mode;
a992f2d38   Jan Kara   ext2: Don't clear...
228
229
230
  		inode->i_ctime = current_time(inode);
  		mark_inode_dirty(inode);
  	}
fe26569eb   Ernesto A. Fernández   ext2: preserve i_...
231
  	return error;
a992f2d38   Jan Kara   ext2: Don't clear...
232
233
234
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
236
   * Initialize the ACLs of a new inode. Called from ext2_new_inode.
   *
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
237
238
   * dir->i_mutex: down
   * inode->i_mutex: up (access to inode is still exclusive)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
240
241
242
   */
  int
  ext2_init_acl(struct inode *inode, struct inode *dir)
  {
64e178a71   Christoph Hellwig   ext2/3/4: use gen...
243
244
  	struct posix_acl *default_acl, *acl;
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245

64e178a71   Christoph Hellwig   ext2/3/4: use gen...
246
  	error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
bc26ab5f6   Al Viro   kill boilerplate ...
247
248
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249

64e178a71   Christoph Hellwig   ext2/3/4: use gen...
250
  	if (default_acl) {
a992f2d38   Jan Kara   ext2: Don't clear...
251
  		error = __ext2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
64e178a71   Christoph Hellwig   ext2/3/4: use gen...
252
  		posix_acl_release(default_acl);
59fed3bf8   Chengguang Xu   ext2: cache NULL ...
253
254
  	} else {
  		inode->i_default_acl = NULL;
64e178a71   Christoph Hellwig   ext2/3/4: use gen...
255
256
257
  	}
  	if (acl) {
  		if (!error)
a992f2d38   Jan Kara   ext2: Don't clear...
258
  			error = __ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
64e178a71   Christoph Hellwig   ext2/3/4: use gen...
259
  		posix_acl_release(acl);
59fed3bf8   Chengguang Xu   ext2: cache NULL ...
260
261
  	} else {
  		inode->i_acl = NULL;
64e178a71   Christoph Hellwig   ext2/3/4: use gen...
262
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
264
  	return error;
  }