xattr.c 8.48 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
// SPDX-License-Identifier: GPL-2.0
/*
3
 * Copyright (C) 2014-2020 Junjiro R. Okajima
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
J. R. Okajima's avatar
J. R. Okajima committed
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
56
57
58
59
60
61
62
63
64
65
66
 */

/*
 * handling xattr functions
 */

#include <linux/fs.h>
#include <linux/posix_acl_xattr.h>
#include <linux/xattr.h>
#include "aufs.h"

static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
{
	if (!ignore_flags)
		goto out;
	switch (err) {
	case -ENOMEM:
	case -EDQUOT:
		goto out;
	}

	if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
		err = 0;
		goto out;
	}

#define cmp(brattr, prefix) do {					\
		if (!strncmp(name, XATTR_##prefix##_PREFIX,		\
			     XATTR_##prefix##_PREFIX_LEN)) {		\
			if (ignore_flags & AuBrAttr_ICEX_##brattr)	\
				err = 0;				\
			goto out;					\
		}							\
	} while (0)

	cmp(SEC, SECURITY);
	cmp(SYS, SYSTEM);
	cmp(TR, TRUSTED);
	cmp(USR, USER);
#undef cmp

	if (ignore_flags & AuBrAttr_ICEX_OTH)
		err = 0;

out:
	return err;
}

static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;

67
static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
J. R. Okajima's avatar
J. R. Okajima committed
68
69
70
71
72
73
			    char *name, char **buf, unsigned int ignore_flags,
			    unsigned int verbose)
{
	int err;
	ssize_t ssz;
	struct inode *h_idst;
74
75
	struct dentry *h_dst_dentry, *h_src_dentry;
	struct user_namespace *h_dst_userns, *h_src_userns;
J. R. Okajima's avatar
J. R. Okajima committed
76

77
78
79
80
	h_src_userns = mnt_user_ns(h_src->mnt);
	h_src_dentry = h_src->dentry;
	ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
				 GFP_NOFS);
J. R. Okajima's avatar
J. R. Okajima committed
81
82
83
84
85
	err = ssz;
	if (unlikely(err <= 0)) {
		if (err == -ENODATA
		    || (err == -EOPNOTSUPP
			&& ((ignore_flags & au_xattr_out_of_list)
86
			    || (au_test_nfs_noacl(d_inode(h_src_dentry))
J. R. Okajima's avatar
J. R. Okajima committed
87
88
89
90
91
92
93
94
95
96
97
				&& (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
				    || !strcmp(name,
					       XATTR_NAME_POSIX_ACL_DEFAULT))))
			    ))
			err = 0;
		if (err && (verbose || au_debug_test()))
			pr_err("%s, err %d\n", name, err);
		goto out;
	}

	/* unlock it temporary */
98
99
100
	h_dst_userns = mnt_user_ns(h_dst->mnt);
	h_dst_dentry = h_dst->dentry;
	h_idst = d_inode(h_dst_dentry);
J. R. Okajima's avatar
J. R. Okajima committed
101
	inode_unlock(h_idst);
102
103
	err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
			     /*flags*/0);
J. R. Okajima's avatar
J. R. Okajima committed
104
105
106
107
108
109
110
111
112
113
114
	inode_lock_nested(h_idst, AuLsc_I_CHILD2);
	if (unlikely(err)) {
		if (verbose || au_debug_test())
			pr_err("%s, err %d\n", name, err);
		err = au_xattr_ignore(err, name, ignore_flags);
	}

out:
	return err;
}

115
int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
J. R. Okajima's avatar
J. R. Okajima committed
116
117
118
119
		  unsigned int verbose)
{
	int err, unlocked, acl_access, acl_default;
	ssize_t ssz;
120
	struct dentry *h_dst_dentry, *h_src_dentry;
J. R. Okajima's avatar
J. R. Okajima committed
121
122
123
124
125
	struct inode *h_isrc, *h_idst;
	char *value, *p, *o, *e;

	/* try stopping to update the source inode while we are referencing */
	/* there should not be the parent-child relationship between them */
126
127
128
129
	h_dst_dentry = h_dst->dentry;
	h_idst = d_inode(h_dst_dentry);
	h_src_dentry = h_src->dentry;
	h_isrc = d_inode(h_src_dentry);
J. R. Okajima's avatar
J. R. Okajima committed
130
131
132
133
134
135
	inode_unlock(h_idst);
	inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
	inode_lock_nested(h_idst, AuLsc_I_CHILD2);
	unlocked = 0;

	/* some filesystems don't list POSIX ACL, for example tmpfs */
136
	ssz = vfs_listxattr(h_src_dentry, NULL, 0);
J. R. Okajima's avatar
J. R. Okajima committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	err = ssz;
	if (unlikely(err < 0)) {
		AuTraceErr(err);
		if (err == -ENODATA
		    || err == -EOPNOTSUPP)
			err = 0;	/* ignore */
		goto out;
	}

	err = 0;
	p = NULL;
	o = NULL;
	if (ssz) {
		err = -ENOMEM;
		p = kmalloc(ssz, GFP_NOFS);
		o = p;
		if (unlikely(!p))
			goto out;
155
		err = vfs_listxattr(h_src_dentry, p, ssz);
J. R. Okajima's avatar
J. R. Okajima committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	}
	inode_unlock_shared(h_isrc);
	unlocked = 1;
	AuDbg("err %d, ssz %zd\n", err, ssz);
	if (unlikely(err < 0))
		goto out_free;

	err = 0;
	e = p + ssz;
	value = NULL;
	acl_access = 0;
	acl_default = 0;
	while (!err && p < e) {
		acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
				       sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
		acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
					sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
					- 1);
		err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
				       verbose);
		p += strlen(p) + 1;
	}
	AuTraceErr(err);
	ignore_flags |= au_xattr_out_of_list;
	if (!err && !acl_access) {
		err = au_do_cpup_xattr(h_dst, h_src,
				       XATTR_NAME_POSIX_ACL_ACCESS, &value,
				       ignore_flags, verbose);
		AuTraceErr(err);
	}
	if (!err && !acl_default) {
		err = au_do_cpup_xattr(h_dst, h_src,
				       XATTR_NAME_POSIX_ACL_DEFAULT, &value,
				       ignore_flags, verbose);
		AuTraceErr(err);
	}

	au_kfree_try_rcu(value);

out_free:
	au_kfree_try_rcu(o);
out:
	if (!unlocked)
		inode_unlock_shared(h_isrc);
	AuTraceErr(err);
	return err;
}

/* ---------------------------------------------------------------------- */

static int au_smack_reentering(struct super_block *sb)
{
208
#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
J. R. Okajima's avatar
J. R. Okajima committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
	/*
	 * as a part of lookup, smack_d_instantiate() is called, and it calls
	 * i_op->getxattr(). ouch.
	 */
	return si_pid_test(sb);
#else
	return 0;
#endif
}

enum {
	AU_XATTR_LIST,
	AU_XATTR_GET
};

struct au_lgxattr {
	int type;
	union {
		struct {
			char	*list;
			size_t	size;
		} list;
		struct {
			const char	*name;
			void		*value;
			size_t		size;
		} get;
	} u;
};

239
240
static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
			  struct au_lgxattr *arg)
J. R. Okajima's avatar
J. R. Okajima committed
241
242
243
244
245
246
247
248
249
250
251
252
253
{
	ssize_t err;
	int reenter;
	struct path h_path;
	struct super_block *sb;

	sb = dentry->d_sb;
	reenter = au_smack_reentering(sb);
	if (!reenter) {
		err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
		if (unlikely(err))
			goto out;
	}
254
	err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
J. R. Okajima's avatar
J. R. Okajima committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	if (unlikely(err))
		goto out_si;
	if (unlikely(!h_path.dentry))
		/* illegally overlapped or something */
		goto out_di; /* pretending success */

	/* always topmost entry only */
	switch (arg->type) {
	case AU_XATTR_LIST:
		err = vfs_listxattr(h_path.dentry,
				    arg->u.list.list, arg->u.list.size);
		break;
	case AU_XATTR_GET:
		AuDebugOn(d_is_negative(h_path.dentry));
269
		err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
J. R. Okajima's avatar
J. R. Okajima committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
				   arg->u.get.name, arg->u.get.value,
				   arg->u.get.size);
		break;
	}

out_di:
	if (!reenter)
		di_read_unlock(dentry, AuLock_IR);
out_si:
	if (!reenter)
		si_read_unlock(sb);
out:
	AuTraceErr(err);
	return err;
}

ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
{
	struct au_lgxattr arg = {
		.type = AU_XATTR_LIST,
		.u.list = {
			.list	= list,
			.size	= size
		},
	};

296
	return au_lgxattr(dentry, /*inode*/NULL, &arg);
J. R. Okajima's avatar
J. R. Okajima committed
297
298
}

299
static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
J. R. Okajima's avatar
J. R. Okajima committed
300
301
302
303
304
305
306
307
308
309
310
			   const char *name, void *value, size_t size)
{
	struct au_lgxattr arg = {
		.type = AU_XATTR_GET,
		.u.get = {
			.name	= name,
			.value	= value,
			.size	= size
		},
	};

311
	return au_lgxattr(dentry, inode, &arg);
J. R. Okajima's avatar
J. R. Okajima committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
}

static int au_setxattr(struct dentry *dentry, struct inode *inode,
		       const char *name, const void *value, size_t size,
		       int flags)
{
	struct au_sxattr arg = {
		.type = AU_XATTR_SET,
		.u.set = {
			.name	= name,
			.value	= value,
			.size	= size,
			.flags	= flags
		},
	};

	return au_sxattr(dentry, inode, &arg);
}

/* ---------------------------------------------------------------------- */

static int au_xattr_get(const struct xattr_handler *handler,
			struct dentry *dentry, struct inode *inode,
			const char *name, void *buffer, size_t size)
{
	return au_getxattr(dentry, inode, name, buffer, size);
}

static int au_xattr_set(const struct xattr_handler *handler,
341
			struct user_namespace *userns,
J. R. Okajima's avatar
J. R. Okajima committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
			struct dentry *dentry, struct inode *inode,
			const char *name, const void *value, size_t size,
			int flags)
{
	return au_setxattr(dentry, inode, name, value, size, flags);
}

static const struct xattr_handler au_xattr_handler = {
	.name	= "",
	.prefix	= "",
	.get	= au_xattr_get,
	.set	= au_xattr_set
};

static const struct xattr_handler *au_xattr_handlers[] = {
#ifdef CONFIG_FS_POSIX_ACL
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
#endif
	&au_xattr_handler, /* must be last */
	NULL
};

void au_xattr_init(struct super_block *sb)
{
	sb->s_xattr = au_xattr_handlers;
}