]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ef14f0c1 CH |
2 | /* |
3 | * Copyright (c) 2008, Christoph Hellwig | |
4 | * All Rights Reserved. | |
ef14f0c1 CH |
5 | */ |
6 | #include "xfs.h" | |
5467b34b | 7 | #include "xfs_shared.h" |
a4fbe6ab | 8 | #include "xfs_format.h" |
69432832 | 9 | #include "xfs_log_format.h" |
7fd36c44 | 10 | #include "xfs_trans_resv.h" |
0a8aa193 | 11 | #include "xfs_mount.h" |
a4fbe6ab | 12 | #include "xfs_inode.h" |
a4fbe6ab | 13 | #include "xfs_attr.h" |
0b1b213f | 14 | #include "xfs_trace.h" |
a5155b87 | 15 | #include "xfs_error.h" |
5f213ddb | 16 | #include "xfs_acl.h" |
a2544622 CH |
17 | #include "xfs_da_format.h" |
18 | #include "xfs_da_btree.h" | |
ef14f0c1 | 19 | |
5f213ddb | 20 | #include <linux/posix_acl_xattr.h> |
ef14f0c1 | 21 | |
ef14f0c1 CH |
22 | /* |
23 | * Locking scheme: | |
24 | * - all ACL updates are protected by inode->i_mutex, which is taken before | |
25 | * calling into this file. | |
ef14f0c1 CH |
26 | */ |
27 | ||
28 | STATIC struct posix_acl * | |
0a8aa193 | 29 | xfs_acl_from_disk( |
a5155b87 | 30 | struct xfs_mount *mp, |
86a21c79 AG |
31 | const struct xfs_acl *aclp, |
32 | int len, | |
33 | int max_entries) | |
ef14f0c1 CH |
34 | { |
35 | struct posix_acl_entry *acl_e; | |
36 | struct posix_acl *acl; | |
86a21c79 | 37 | const struct xfs_acl_entry *ace; |
093019cf | 38 | unsigned int count, i; |
ef14f0c1 | 39 | |
a5155b87 DW |
40 | if (len < sizeof(*aclp)) { |
41 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp, | |
42 | len); | |
86a21c79 | 43 | return ERR_PTR(-EFSCORRUPTED); |
a5155b87 DW |
44 | } |
45 | ||
ef14f0c1 | 46 | count = be32_to_cpu(aclp->acl_cnt); |
a5155b87 DW |
47 | if (count > max_entries || XFS_ACL_SIZE(count) != len) { |
48 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp, | |
49 | len); | |
fa8b18ed | 50 | return ERR_PTR(-EFSCORRUPTED); |
a5155b87 | 51 | } |
ef14f0c1 CH |
52 | |
53 | acl = posix_acl_alloc(count, GFP_KERNEL); | |
54 | if (!acl) | |
55 | return ERR_PTR(-ENOMEM); | |
56 | ||
57 | for (i = 0; i < count; i++) { | |
58 | acl_e = &acl->a_entries[i]; | |
59 | ace = &aclp->acl_entry[i]; | |
60 | ||
61 | /* | |
62 | * The tag is 32 bits on disk and 16 bits in core. | |
63 | * | |
64 | * Because every access to it goes through the core | |
65 | * format first this is not a problem. | |
66 | */ | |
67 | acl_e->e_tag = be32_to_cpu(ace->ae_tag); | |
68 | acl_e->e_perm = be16_to_cpu(ace->ae_perm); | |
69 | ||
70 | switch (acl_e->e_tag) { | |
71 | case ACL_USER: | |
ba8adad5 CH |
72 | acl_e->e_uid = make_kuid(&init_user_ns, |
73 | be32_to_cpu(ace->ae_id)); | |
288bbe0e | 74 | break; |
ef14f0c1 | 75 | case ACL_GROUP: |
ba8adad5 CH |
76 | acl_e->e_gid = make_kgid(&init_user_ns, |
77 | be32_to_cpu(ace->ae_id)); | |
ef14f0c1 CH |
78 | break; |
79 | case ACL_USER_OBJ: | |
80 | case ACL_GROUP_OBJ: | |
81 | case ACL_MASK: | |
82 | case ACL_OTHER: | |
ef14f0c1 CH |
83 | break; |
84 | default: | |
85 | goto fail; | |
86 | } | |
87 | } | |
88 | return acl; | |
89 | ||
90 | fail: | |
91 | posix_acl_release(acl); | |
92 | return ERR_PTR(-EINVAL); | |
93 | } | |
94 | ||
95 | STATIC void | |
96 | xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl) | |
97 | { | |
98 | const struct posix_acl_entry *acl_e; | |
99 | struct xfs_acl_entry *ace; | |
100 | int i; | |
101 | ||
102 | aclp->acl_cnt = cpu_to_be32(acl->a_count); | |
103 | for (i = 0; i < acl->a_count; i++) { | |
104 | ace = &aclp->acl_entry[i]; | |
105 | acl_e = &acl->a_entries[i]; | |
106 | ||
107 | ace->ae_tag = cpu_to_be32(acl_e->e_tag); | |
288bbe0e DE |
108 | switch (acl_e->e_tag) { |
109 | case ACL_USER: | |
ba8adad5 CH |
110 | ace->ae_id = cpu_to_be32( |
111 | from_kuid(&init_user_ns, acl_e->e_uid)); | |
288bbe0e DE |
112 | break; |
113 | case ACL_GROUP: | |
ba8adad5 CH |
114 | ace->ae_id = cpu_to_be32( |
115 | from_kgid(&init_user_ns, acl_e->e_gid)); | |
288bbe0e DE |
116 | break; |
117 | default: | |
118 | ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID); | |
119 | break; | |
120 | } | |
121 | ||
ef14f0c1 CH |
122 | ace->ae_perm = cpu_to_be16(acl_e->e_perm); |
123 | } | |
124 | } | |
125 | ||
ef14f0c1 CH |
126 | struct posix_acl * |
127 | xfs_get_acl(struct inode *inode, int type) | |
128 | { | |
e5171d7e CH |
129 | struct xfs_inode *ip = XFS_I(inode); |
130 | struct xfs_mount *mp = ip->i_mount; | |
131 | struct posix_acl *acl = NULL; | |
132 | struct xfs_da_args args = { | |
133 | .dp = ip, | |
d5f0f49a | 134 | .attr_filter = XFS_ATTR_ROOT, |
e5171d7e CH |
135 | .valuelen = XFS_ACL_MAX_SIZE(mp), |
136 | }; | |
137 | int error; | |
ef14f0c1 | 138 | |
4e34e719 CH |
139 | trace_xfs_get_acl(ip); |
140 | ||
ef14f0c1 CH |
141 | switch (type) { |
142 | case ACL_TYPE_ACCESS: | |
e5171d7e | 143 | args.name = SGI_ACL_FILE; |
ef14f0c1 CH |
144 | break; |
145 | case ACL_TYPE_DEFAULT: | |
e5171d7e | 146 | args.name = SGI_ACL_DEFAULT; |
ef14f0c1 CH |
147 | break; |
148 | default: | |
1cbd20d8 | 149 | BUG(); |
ef14f0c1 | 150 | } |
e5171d7e | 151 | args.namelen = strlen(args.name); |
ef14f0c1 | 152 | |
d49db18b CH |
153 | /* |
154 | * If the attribute doesn't exist make sure we have a negative cache | |
155 | * entry, for any other error assume it is transient. | |
156 | */ | |
e5171d7e | 157 | error = xfs_attr_get(&args); |
d49db18b | 158 | if (!error) { |
e5171d7e CH |
159 | acl = xfs_acl_from_disk(mp, args.value, args.valuelen, |
160 | XFS_ACL_MAX_ENTRIES(mp)); | |
d49db18b CH |
161 | } else if (error != -ENOATTR) { |
162 | acl = ERR_PTR(error); | |
ef14f0c1 | 163 | } |
d49db18b CH |
164 | |
165 | kmem_free(args.value); | |
ef14f0c1 CH |
166 | return acl; |
167 | } | |
168 | ||
8ba35875 JK |
169 | int |
170 | __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) | |
ef14f0c1 | 171 | { |
a2544622 CH |
172 | struct xfs_inode *ip = XFS_I(inode); |
173 | struct xfs_da_args args = { | |
174 | .dp = ip, | |
d5f0f49a | 175 | .attr_filter = XFS_ATTR_ROOT, |
a2544622 CH |
176 | }; |
177 | int error; | |
ef14f0c1 | 178 | |
ef14f0c1 CH |
179 | switch (type) { |
180 | case ACL_TYPE_ACCESS: | |
a2544622 | 181 | args.name = SGI_ACL_FILE; |
ef14f0c1 CH |
182 | break; |
183 | case ACL_TYPE_DEFAULT: | |
184 | if (!S_ISDIR(inode->i_mode)) | |
185 | return acl ? -EACCES : 0; | |
a2544622 | 186 | args.name = SGI_ACL_DEFAULT; |
ef14f0c1 CH |
187 | break; |
188 | default: | |
189 | return -EINVAL; | |
190 | } | |
a2544622 | 191 | args.namelen = strlen(args.name); |
ef14f0c1 CH |
192 | |
193 | if (acl) { | |
ed02d13f | 194 | args.valuelen = XFS_ACL_SIZE(acl->a_count); |
8ca79df8 | 195 | args.value = kvzalloc(args.valuelen, GFP_KERNEL); |
a2544622 | 196 | if (!args.value) |
fdd3ccee | 197 | return -ENOMEM; |
a2544622 | 198 | xfs_acl_to_disk(args.value, acl); |
ef14f0c1 CH |
199 | } |
200 | ||
a2544622 CH |
201 | error = xfs_attr_set(&args); |
202 | kmem_free(args.value); | |
0eb81a5f CH |
203 | |
204 | /* | |
205 | * If the attribute didn't exist to start with that's fine. | |
206 | */ | |
207 | if (!acl && error == -ENOATTR) | |
208 | error = 0; | |
ef14f0c1 | 209 | if (!error) |
1cbd20d8 | 210 | set_cached_acl(inode, type, acl); |
ef14f0c1 CH |
211 | return error; |
212 | } | |
213 | ||
ef14f0c1 | 214 | static int |
d3fb6120 | 215 | xfs_set_mode(struct inode *inode, umode_t mode) |
ef14f0c1 CH |
216 | { |
217 | int error = 0; | |
218 | ||
219 | if (mode != inode->i_mode) { | |
220 | struct iattr iattr; | |
221 | ||
d6d59bad | 222 | iattr.ia_valid = ATTR_MODE | ATTR_CTIME; |
ef14f0c1 | 223 | iattr.ia_mode = mode; |
c2050a45 | 224 | iattr.ia_ctime = current_time(inode); |
ef14f0c1 | 225 | |
2451337d | 226 | error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); |
ef14f0c1 CH |
227 | } |
228 | ||
229 | return error; | |
230 | } | |
231 | ||
ef14f0c1 | 232 | int |
2401dc29 | 233 | xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
ef14f0c1 | 234 | { |
67f2ffe3 DC |
235 | umode_t mode; |
236 | bool set_mode = false; | |
431547b3 | 237 | int error = 0; |
ef14f0c1 | 238 | |
2401dc29 | 239 | if (!acl) |
ef14f0c1 CH |
240 | goto set_acl; |
241 | ||
4ae69fea | 242 | error = -E2BIG; |
0a8aa193 | 243 | if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) |
2401dc29 | 244 | return error; |
ef14f0c1 CH |
245 | |
246 | if (type == ACL_TYPE_ACCESS) { | |
07393101 JK |
247 | error = posix_acl_update_mode(inode, &mode, &acl); |
248 | if (error) | |
249 | return error; | |
67f2ffe3 | 250 | set_mode = true; |
ef14f0c1 CH |
251 | } |
252 | ||
253 | set_acl: | |
67f2ffe3 DC |
254 | error = __xfs_set_acl(inode, acl, type); |
255 | if (error) | |
256 | return error; | |
257 | ||
258 | /* | |
259 | * We set the mode after successfully updating the ACL xattr because the | |
260 | * xattr update can fail at ENOSPC and we don't want to change the mode | |
261 | * if the ACL update hasn't been applied. | |
262 | */ | |
263 | if (set_mode) | |
264 | error = xfs_set_mode(inode, mode); | |
265 | ||
266 | return error; | |
ef14f0c1 | 267 | } |
5a3930e2 CH |
268 | |
269 | /* | |
270 | * Invalidate any cached ACLs if the user has bypassed the ACL interface. | |
271 | * We don't validate the content whatsoever so it is caller responsibility to | |
272 | * provide data in valid format and ensure i_mode is consistent. | |
273 | */ | |
274 | void | |
275 | xfs_forget_acl( | |
276 | struct inode *inode, | |
277 | const char *name) | |
278 | { | |
279 | if (!strcmp(name, SGI_ACL_FILE)) | |
280 | forget_cached_acl(inode, ACL_TYPE_ACCESS); | |
281 | else if (!strcmp(name, SGI_ACL_DEFAULT)) | |
282 | forget_cached_acl(inode, ACL_TYPE_DEFAULT); | |
283 | } |