blob: bca48b308c0230a1c74f4ab67dced6f5e4888dbc [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,
22 struct inode *inode, const char *name, void *value, size_t size)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100023{
Christoph Hellwige5171d72020-02-26 17:30:34 -080024 struct xfs_da_args args = {
25 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080026 .attr_filter = handler->flags,
Christoph Hellwige5171d72020-02-26 17:30:34 -080027 .name = name,
28 .namelen = strlen(name),
29 .value = value,
30 .valuelen = size,
31 };
32 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100033
Christoph Hellwige5171d72020-02-26 17:30:34 -080034 error = xfs_attr_get(&args);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100035 if (error)
36 return error;
Christoph Hellwige5171d72020-02-26 17:30:34 -080037 return args.valuelen;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100038}
39
40static int
Al Viro59301222016-05-27 10:19:30 -040041xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused,
42 struct inode *inode, const char *name, const void *value,
43 size_t size, int flags)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100044{
Christoph Hellwiga2544622020-02-26 17:30:33 -080045 struct xfs_da_args args = {
46 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080047 .attr_filter = handler->flags,
48 .attr_flags = flags,
Christoph Hellwiga2544622020-02-26 17:30:33 -080049 .name = name,
50 .namelen = strlen(name),
51 .value = (void *)value,
52 .valuelen = size,
53 };
Brian Foster67d8e042015-11-03 12:40:59 +110054 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100055
Christoph Hellwiga2544622020-02-26 17:30:33 -080056 error = xfs_attr_set(&args);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080057 if (!error && (handler->flags & XFS_ATTR_ROOT))
Christoph Hellwig5a3930e2020-02-26 17:30:41 -080058 xfs_forget_acl(inode, name);
Brian Foster67d8e042015-11-03 12:40:59 +110059 return error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100060}
61
Stephen Hemminger46e58762010-05-13 17:53:20 -070062static const struct xattr_handler xfs_xattr_user_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100063 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +000064 .flags = 0, /* no flags implies user namespace */
65 .get = xfs_xattr_get,
66 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100067};
68
Stephen Hemminger46e58762010-05-13 17:53:20 -070069static const struct xattr_handler xfs_xattr_trusted_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100070 .prefix = XATTR_TRUSTED_PREFIX,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080071 .flags = XFS_ATTR_ROOT,
Christoph Hellwig431547b2009-11-13 09:52:56 +000072 .get = xfs_xattr_get,
73 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100074};
75
Stephen Hemminger46e58762010-05-13 17:53:20 -070076static const struct xattr_handler xfs_xattr_security_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100077 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080078 .flags = XFS_ATTR_SECURE,
Christoph Hellwig431547b2009-11-13 09:52:56 +000079 .get = xfs_xattr_get,
80 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100081};
82
Stephen Hemminger46e58762010-05-13 17:53:20 -070083const struct xattr_handler *xfs_xattr_handlers[] = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100084 &xfs_xattr_user_handler,
85 &xfs_xattr_trusted_handler,
86 &xfs_xattr_security_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020087#ifdef CONFIG_XFS_POSIX_ACL
Christoph Hellwig2401dc22013-12-20 05:16:50 -080088 &posix_acl_access_xattr_handler,
89 &posix_acl_default_xattr_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020090#endif
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100091 NULL
92};
93
Eric Sandeenf7a136a2016-12-05 12:32:14 +110094static void
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +010095__xfs_xattr_put_listent(
96 struct xfs_attr_list_context *context,
97 char *prefix,
98 int prefix_len,
99 unsigned char *name,
100 int namelen)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000101{
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100102 char *offset;
103 int arraytop;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000104
Darrick J. Wong3b500862019-02-13 11:15:17 -0800105 if (context->count < 0 || context->seen_enough)
106 return;
107
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800108 if (!context->buffer)
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100109 goto compute_size;
110
111 arraytop = context->count + prefix_len + namelen + 1;
112 if (arraytop > context->firstu) {
113 context->count = -1; /* insufficient space */
Artem Savkov791cc432016-09-14 07:40:35 +1000114 context->seen_enough = 1;
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100115 return;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100116 }
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800117 offset = context->buffer + context->count;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100118 strncpy(offset, prefix, prefix_len);
119 offset += prefix_len;
120 strncpy(offset, (char *)name, namelen); /* real name */
121 offset += namelen;
122 *offset = '\0';
123
124compute_size:
125 context->count += prefix_len + namelen + 1;
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100126 return;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000127}
128
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100129static void
Dave Chinnera9273ca2010-01-20 10:47:48 +1100130xfs_xattr_put_listent(
131 struct xfs_attr_list_context *context,
132 int flags,
133 unsigned char *name,
134 int namelen,
Eric Sandeene5bd12b2016-04-06 07:57:32 +1000135 int valuelen)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000136{
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100137 char *prefix;
138 int prefix_len;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000139
140 ASSERT(context->count >= 0);
141
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100142 if (flags & XFS_ATTR_ROOT) {
143#ifdef CONFIG_XFS_POSIX_ACL
144 if (namelen == SGI_ACL_FILE_SIZE &&
145 strncmp(name, SGI_ACL_FILE,
146 SGI_ACL_FILE_SIZE) == 0) {
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100147 __xfs_xattr_put_listent(
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100148 context, XATTR_SYSTEM_PREFIX,
149 XATTR_SYSTEM_PREFIX_LEN,
150 XATTR_POSIX_ACL_ACCESS,
151 strlen(XATTR_POSIX_ACL_ACCESS));
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100152 } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
153 strncmp(name, SGI_ACL_DEFAULT,
154 SGI_ACL_DEFAULT_SIZE) == 0) {
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100155 __xfs_xattr_put_listent(
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100156 context, XATTR_SYSTEM_PREFIX,
157 XATTR_SYSTEM_PREFIX_LEN,
158 XATTR_POSIX_ACL_DEFAULT,
159 strlen(XATTR_POSIX_ACL_DEFAULT));
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100160 }
161#endif
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000162
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100163 /*
164 * Only show root namespace entries if we are actually allowed to
165 * see them.
166 */
167 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100168 return;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100169
170 prefix = XATTR_TRUSTED_PREFIX;
171 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
172 } else if (flags & XFS_ATTR_SECURE) {
173 prefix = XATTR_SECURITY_PREFIX;
174 prefix_len = XATTR_SECURITY_PREFIX_LEN;
175 } else {
176 prefix = XATTR_USER_PREFIX;
177 prefix_len = XATTR_USER_PREFIX_LEN;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000178 }
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000179
Eric Sandeenf7a136a2016-12-05 12:32:14 +1100180 __xfs_xattr_put_listent(context, prefix, prefix_len, name,
181 namelen);
182 return;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000183}
184
185ssize_t
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000186xfs_vn_listxattr(
187 struct dentry *dentry,
188 char *data,
189 size_t size)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000190{
191 struct xfs_attr_list_context context;
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000192 struct inode *inode = d_inode(dentry);
193 int error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000194
195 /*
196 * First read the regular on-disk attributes.
197 */
198 memset(&context, 0, sizeof(context));
199 context.dp = XFS_I(inode);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000200 context.resynch = 1;
Christoph Hellwiga9c8c692020-02-26 17:30:37 -0800201 context.buffer = size ? data : NULL;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000202 context.bufsize = size;
203 context.firstu = context.bufsize;
Andreas Gruenbacher5d92b752015-12-02 14:44:40 +0100204 context.put_listent = xfs_xattr_put_listent;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000205
Christoph Hellwig17e1dd82020-02-26 17:30:39 -0800206 error = xfs_attr_list(&context);
Eric Sandeen2a6fba62016-04-06 07:57:18 +1000207 if (error)
208 return error;
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000209 if (context.count < 0)
210 return -ERANGE;
211
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000212 return context.count;
213}