1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014-2020 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * handling xattr functions
24 #include <linux/posix_acl_xattr.h>
25 #include <linux/xattr.h>
28 static int au_xattr_ignore(int err
, char *name
, unsigned int ignore_flags
)
38 if ((ignore_flags
& AuBrAttr_ICEX
) == AuBrAttr_ICEX
) {
43 #define cmp(brattr, prefix) do { \
44 if (!strncmp(name, XATTR_##prefix##_PREFIX, \
45 XATTR_##prefix##_PREFIX_LEN)) { \
46 if (ignore_flags & AuBrAttr_ICEX_##brattr) \
58 if (ignore_flags
& AuBrAttr_ICEX_OTH
)
65 static const int au_xattr_out_of_list
= AuBrAttr_ICEX_OTH
<< 1;
67 static int au_do_cpup_xattr(struct dentry
*h_dst
, struct dentry
*h_src
,
68 char *name
, char **buf
, unsigned int ignore_flags
,
75 ssz
= vfs_getxattr_alloc(h_src
, name
, buf
, 0, GFP_NOFS
);
77 if (unlikely(err
<= 0)) {
79 || (err
== -EOPNOTSUPP
80 && ((ignore_flags
& au_xattr_out_of_list
)
81 || (au_test_nfs_noacl(d_inode(h_src
))
82 && (!strcmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
)
84 XATTR_NAME_POSIX_ACL_DEFAULT
))))
87 if (err
&& (verbose
|| au_debug_test()))
88 pr_err("%s, err %d\n", name
, err
);
92 /* unlock it temporary */
93 h_idst
= d_inode(h_dst
);
95 err
= vfsub_setxattr(h_dst
, name
, *buf
, ssz
, /*flags*/0);
96 inode_lock_nested(h_idst
, AuLsc_I_CHILD2
);
98 if (verbose
|| au_debug_test())
99 pr_err("%s, err %d\n", name
, err
);
100 err
= au_xattr_ignore(err
, name
, ignore_flags
);
107 int au_cpup_xattr(struct dentry
*h_dst
, struct dentry
*h_src
, int ignore_flags
,
108 unsigned int verbose
)
110 int err
, unlocked
, acl_access
, acl_default
;
112 struct inode
*h_isrc
, *h_idst
;
113 char *value
, *p
, *o
, *e
;
115 /* try stopping to update the source inode while we are referencing */
116 /* there should not be the parent-child relationship between them */
117 h_isrc
= d_inode(h_src
);
118 h_idst
= d_inode(h_dst
);
119 inode_unlock(h_idst
);
120 inode_lock_shared_nested(h_isrc
, AuLsc_I_CHILD
);
121 inode_lock_nested(h_idst
, AuLsc_I_CHILD2
);
124 /* some filesystems don't list POSIX ACL, for example tmpfs */
125 ssz
= vfs_listxattr(h_src
, NULL
, 0);
127 if (unlikely(err
< 0)) {
130 || err
== -EOPNOTSUPP
)
131 err
= 0; /* ignore */
140 p
= kmalloc(ssz
, GFP_NOFS
);
144 err
= vfs_listxattr(h_src
, p
, ssz
);
146 inode_unlock_shared(h_isrc
);
148 AuDbg("err %d, ssz %zd\n", err
, ssz
);
149 if (unlikely(err
< 0))
157 while (!err
&& p
< e
) {
158 acl_access
|= !strncmp(p
, XATTR_NAME_POSIX_ACL_ACCESS
,
159 sizeof(XATTR_NAME_POSIX_ACL_ACCESS
) - 1);
160 acl_default
|= !strncmp(p
, XATTR_NAME_POSIX_ACL_DEFAULT
,
161 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT
)
163 err
= au_do_cpup_xattr(h_dst
, h_src
, p
, &value
, ignore_flags
,
168 ignore_flags
|= au_xattr_out_of_list
;
169 if (!err
&& !acl_access
) {
170 err
= au_do_cpup_xattr(h_dst
, h_src
,
171 XATTR_NAME_POSIX_ACL_ACCESS
, &value
,
172 ignore_flags
, verbose
);
175 if (!err
&& !acl_default
) {
176 err
= au_do_cpup_xattr(h_dst
, h_src
,
177 XATTR_NAME_POSIX_ACL_DEFAULT
, &value
,
178 ignore_flags
, verbose
);
182 au_kfree_try_rcu(value
);
188 inode_unlock_shared(h_isrc
);
193 /* ---------------------------------------------------------------------- */
195 static int au_smack_reentering(struct super_block
*sb
)
197 #if IS_ENABLED(CONFIG_SECURITY_SMACK)
199 * as a part of lookup, smack_d_instantiate() is called, and it calls
200 * i_op->getxattr(). ouch.
202 return si_pid_test(sb
);
228 static ssize_t
au_lgxattr(struct dentry
*dentry
, struct au_lgxattr
*arg
)
233 struct super_block
*sb
;
236 reenter
= au_smack_reentering(sb
);
238 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
242 err
= au_h_path_getattr(dentry
, /*force*/1, &h_path
, reenter
);
245 if (unlikely(!h_path
.dentry
))
246 /* illegally overlapped or something */
247 goto out_di
; /* pretending success */
249 /* always topmost entry only */
252 err
= vfs_listxattr(h_path
.dentry
,
253 arg
->u
.list
.list
, arg
->u
.list
.size
);
256 AuDebugOn(d_is_negative(h_path
.dentry
));
257 err
= vfs_getxattr(h_path
.dentry
,
258 arg
->u
.get
.name
, arg
->u
.get
.value
,
265 di_read_unlock(dentry
, AuLock_IR
);
274 ssize_t
aufs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
276 struct au_lgxattr arg
= {
277 .type
= AU_XATTR_LIST
,
284 return au_lgxattr(dentry
, &arg
);
287 static ssize_t
au_getxattr(struct dentry
*dentry
,
288 struct inode
*inode __maybe_unused
,
289 const char *name
, void *value
, size_t size
)
291 struct au_lgxattr arg
= {
292 .type
= AU_XATTR_GET
,
300 return au_lgxattr(dentry
, &arg
);
303 static int au_setxattr(struct dentry
*dentry
, struct inode
*inode
,
304 const char *name
, const void *value
, size_t size
,
307 struct au_sxattr arg
= {
308 .type
= AU_XATTR_SET
,
317 return au_sxattr(dentry
, inode
, &arg
);
320 /* ---------------------------------------------------------------------- */
322 static int au_xattr_get(const struct xattr_handler
*handler
,
323 struct dentry
*dentry
, struct inode
*inode
,
324 const char *name
, void *buffer
, size_t size
)
326 return au_getxattr(dentry
, inode
, name
, buffer
, size
);
329 static int au_xattr_set(const struct xattr_handler
*handler
,
330 struct dentry
*dentry
, struct inode
*inode
,
331 const char *name
, const void *value
, size_t size
,
334 return au_setxattr(dentry
, inode
, name
, value
, size
, flags
);
337 static const struct xattr_handler au_xattr_handler
= {
344 static const struct xattr_handler
*au_xattr_handlers
[] = {
345 #ifdef CONFIG_FS_POSIX_ACL
346 &posix_acl_access_xattr_handler
,
347 &posix_acl_default_xattr_handler
,
349 &au_xattr_handler
, /* must be last */
353 void au_xattr_init(struct super_block
*sb
)
355 sb
->s_xattr
= au_xattr_handlers
;