blob: dd81d10d06794d2dc987b3b49ce713d59728e126 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +10002/*
3 * Copyright (C) 2008 Christoph Hellwig.
4 * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +10005 */
6
7#include "xfs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner69432832013-08-12 20:49:23 +100010#include "xfs_log_format.h"
Dave Chinner57062782013-10-15 09:17:51 +110011#include "xfs_da_format.h"
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100012#include "xfs_inode.h"
13#include "xfs_attr.h"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080014#include "xfs_acl.h"
Christoph Hellwiga2544622020-02-26 17:30:33 -080015#include "xfs_da_btree.h"
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100016
17#include <linux/posix_acl_xattr.h>
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100018
19
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100020static int
Al Virob2968212016-04-10 20:48:24 -040021xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
Mark Salyzyn3484eba2019-11-04 08:57:10 -080022 struct inode *inode, const char *name, void *value, size_t size,
23 int flags)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100024{
Christoph Hellwige5171d72020-02-26 17:30:34 -080025 struct xfs_da_args args = {
26 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080027 .attr_filter = handler->flags,
Christoph Hellwige5171d72020-02-26 17:30:34 -080028 .name = name,
29 .namelen = strlen(name),
30 .value = value,
31 .valuelen = size,
32 };
33 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100034
Christoph Hellwige5171d72020-02-26 17:30:34 -080035 error = xfs_attr_get(&args);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100036 if (error)
37 return error;
Christoph Hellwige5171d72020-02-26 17:30:34 -080038 return args.valuelen;
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +110039}
40
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100041static int
Al Viro59301222016-05-27 10:19:30 -040042xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused,
43 struct inode *inode, const char *name, const void *value,
44 size_t size, int flags)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100045{
Christoph Hellwiga2544622020-02-26 17:30:33 -080046 struct xfs_da_args args = {
47 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080048 .attr_filter = handler->flags,
49 .attr_flags = flags,
Christoph Hellwiga2544622020-02-26 17:30:33 -080050 .name = name,
51 .namelen = strlen(name),
52 .value = (void *)value,
53 .valuelen = size,
54 };
Brian Foster67d8e042015-11-03 12:40:59 +110055 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100056
Christoph Hellwiga2544622020-02-26 17:30:33 -080057 error = xfs_attr_set(&args);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080058 if (!error && (handler->flags & XFS_ATTR_ROOT))
Christoph Hellwig5a3930e2020-02-26 17:30:41 -080059 xfs_forget_acl(inode, name);
Brian Foster67d8e042015-11-03 12:40:59 +110060 return error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100061}
62
Stephen Hemminger46e58762010-05-13 17:53:20 -070063static const struct xattr_handler xfs_xattr_user_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100064 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +000065 .flags = 0, /* no flags implies user namespace */
66 .get = xfs_xattr_get,
67 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100068};
69
Stephen Hemminger46e58762010-05-13 17:53:20 -070070static const struct xattr_handler xfs_xattr_trusted_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100071 .prefix = XATTR_TRUSTED_PREFIX,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080072 .flags = XFS_ATTR_ROOT,
Christoph Hellwig431547b2009-11-13 09:52:56 +000073 .get = xfs_xattr_get,
74 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100075};
76
Stephen Hemminger46e58762010-05-13 17:53:20 -070077static const struct xattr_handler xfs_xattr_security_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100078 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080079 .flags = XFS_ATTR_SECURE,
Christoph Hellwig431547b2009-11-13 09:52:56 +000080 .get = xfs_xattr_get,
81 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100082};
83
Stephen Hemminger46e58762010-05-13 17:53:20 -070084const struct xattr_handler *xfs_xattr_handlers[] = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100085 &xfs_xattr_user_handler,
86 &xfs_xattr_trusted_handler,
87 &xfs_xattr_security_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020088#ifdef CONFIG_XFS_POSIX_ACL
Christoph Hellwig2401dc22013-12-20 05:16:50 -080089 &posix_acl_access_xattr_handler,
90 &posix_acl_default_xattr_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020091#endif
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100092 NULL
93};
94
Eric Sandeenf7a136a2016-12-05 12:32:14 +110095static void
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +010096__xfs_xattr_put_listent(
97 struct xfs_attr_list_context *context,
98 char *prefix,
99 int prefix_len,
100 unsigned char *name,
101 int namelen)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000102{
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100103 char *offset;
104 int arraytop;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000105
Darrick J. Wong3b500862019-02-13 11:15:17 -0800106 if (context->count < 0 || context->seen_enough)
107 return;
108
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800109 if (!context->buffer)
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100110 goto compute_size;
111
112 arraytop = context->count + prefix_len + namelen + 1;
113 if (arraytop > context->firstu) {
114 context->count = -1; /* insufficient space */
Artem Savkov791cc432016-09-14 07:40:35 +1000115 context->seen_enough = 1;
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100116 return;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100117 }
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800118 offset = context->buffer + context->count;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100119 strncpy(offset, prefix, prefix_len);
120 offset += prefix_len;
121 strncpy(offset, (char *)name, namelen); /* real name */
122 offset += namelen;
123 *offset = '\0';
124
125compute_size:
126 context->count += prefix_len + namelen + 1;
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100127 return;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000128}
129
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100130static void
Dave Chinnera9273ca2010-01-20 10:47:48 +1100131xfs_xattr_put_listent(
132 struct xfs_attr_list_context *context,
133 int flags,
134 unsigned char *name,
135 int namelen,
Eric Sandeene5bd12b2016-04-06 07:57:32 +1000136 int valuelen)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000137{
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100138 char *prefix;
139 int prefix_len;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000140
141 ASSERT(context->count >= 0);
142
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100143 if (flags & XFS_ATTR_ROOT) {
144#ifdef CONFIG_XFS_POSIX_ACL
145 if (namelen == SGI_ACL_FILE_SIZE &&
146 strncmp(name, SGI_ACL_FILE,
147 SGI_ACL_FILE_SIZE) == 0) {
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100148 __xfs_xattr_put_listent(
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100149 context, XATTR_SYSTEM_PREFIX,
150 XATTR_SYSTEM_PREFIX_LEN,
151 XATTR_POSIX_ACL_ACCESS,
152 strlen(XATTR_POSIX_ACL_ACCESS));
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100153 } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
154 strncmp(name, SGI_ACL_DEFAULT,
155 SGI_ACL_DEFAULT_SIZE) == 0) {
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100156 __xfs_xattr_put_listent(
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100157 context, XATTR_SYSTEM_PREFIX,
158 XATTR_SYSTEM_PREFIX_LEN,
159 XATTR_POSIX_ACL_DEFAULT,
160 strlen(XATTR_POSIX_ACL_DEFAULT));
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100161 }
162#endif
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000163
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100164 /*
165 * Only show root namespace entries if we are actually allowed to
166 * see them.
167 */
168 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100169 return;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100170
171 prefix = XATTR_TRUSTED_PREFIX;
172 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
173 } else if (flags & XFS_ATTR_SECURE) {
174 prefix = XATTR_SECURITY_PREFIX;
175 prefix_len = XATTR_SECURITY_PREFIX_LEN;
176 } else {
177 prefix = XATTR_USER_PREFIX;
178 prefix_len = XATTR_USER_PREFIX_LEN;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000179 }
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000180
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100181 __xfs_xattr_put_listent(context, prefix, prefix_len, name,
182 namelen);
183 return;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000184}
185
186ssize_t
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000187xfs_vn_listxattr(
188 struct dentry *dentry,
189 char *data,
190 size_t size)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000191{
192 struct xfs_attr_list_context context;
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000193 struct inode *inode = d_inode(dentry);
194 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000195
196 /*
197 * First read the regular on-disk attributes.
198 */
199 memset(&context, 0, sizeof(context));
200 context.dp = XFS_I(inode);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000201 context.resynch = 1;
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800202 context.buffer = size ? data : NULL;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000203 context.bufsize = size;
204 context.firstu = context.bufsize;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100205 context.put_listent = xfs_xattr_put_listent;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000206
Christoph Hellwig17e1dd82020-02-26 17:30:39 -0800207 error = xfs_attr_list(&context);
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000208 if (error)
209 return error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000210 if (context.count < 0)
211 return -ERANGE;
212
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000213 return context.count;
214}