1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Christoph Hellwig.
4 * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_da_format.h"
12 #include "xfs_inode.h"
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/xattr.h>
21 xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
22 struct inode *inode, const char *name, void *value, size_t size)
24 int xflags = handler->flags;
25 struct xfs_inode *ip = XFS_I(inode);
26 int error, asize = size;
28 /* Convert Linux syscall to XFS internal ATTR flags */
30 xflags |= ATTR_KERNOVAL;
34 error = xfs_attr_get(ip, name, (unsigned char **)&value, &asize, xflags);
47 * Invalidate any cached ACLs if the user has bypassed the ACL
48 * interface. We don't validate the content whatsoever so it is caller
49 * responsibility to provide data in valid format and ensure i_mode is
52 if (xflags & ATTR_ROOT) {
53 #ifdef CONFIG_XFS_POSIX_ACL
54 if (!strcmp(name, SGI_ACL_FILE))
55 forget_cached_acl(inode, ACL_TYPE_ACCESS);
56 else if (!strcmp(name, SGI_ACL_DEFAULT))
57 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
63 xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused,
64 struct inode *inode, const char *name, const void *value,
65 size_t size, int flags)
67 int xflags = handler->flags;
68 struct xfs_inode *ip = XFS_I(inode);
71 /* Convert Linux syscall to XFS internal ATTR flags */
72 if (flags & XATTR_CREATE)
73 xflags |= ATTR_CREATE;
74 if (flags & XATTR_REPLACE)
75 xflags |= ATTR_REPLACE;
78 return xfs_attr_remove(ip, (unsigned char *)name, xflags);
79 error = xfs_attr_set(ip, (unsigned char *)name,
80 (void *)value, size, xflags);
82 xfs_forget_acl(inode, name, xflags);
87 static const struct xattr_handler xfs_xattr_user_handler = {
88 .prefix = XATTR_USER_PREFIX,
89 .flags = 0, /* no flags implies user namespace */
94 static const struct xattr_handler xfs_xattr_trusted_handler = {
95 .prefix = XATTR_TRUSTED_PREFIX,
101 static const struct xattr_handler xfs_xattr_security_handler = {
102 .prefix = XATTR_SECURITY_PREFIX,
103 .flags = ATTR_SECURE,
104 .get = xfs_xattr_get,
105 .set = xfs_xattr_set,
108 const struct xattr_handler *xfs_xattr_handlers[] = {
109 &xfs_xattr_user_handler,
110 &xfs_xattr_trusted_handler,
111 &xfs_xattr_security_handler,
112 #ifdef CONFIG_XFS_POSIX_ACL
113 &posix_acl_access_xattr_handler,
114 &posix_acl_default_xattr_handler,
120 __xfs_xattr_put_listent(
121 struct xfs_attr_list_context *context,
130 if (context->count < 0 || context->seen_enough)
136 arraytop = context->count + prefix_len + namelen + 1;
137 if (arraytop > context->firstu) {
138 context->count = -1; /* insufficient space */
139 context->seen_enough = 1;
142 offset = (char *)context->alist + context->count;
143 strncpy(offset, prefix, prefix_len);
144 offset += prefix_len;
145 strncpy(offset, (char *)name, namelen); /* real name */
150 context->count += prefix_len + namelen + 1;
155 xfs_xattr_put_listent(
156 struct xfs_attr_list_context *context,
165 ASSERT(context->count >= 0);
167 if (flags & XFS_ATTR_ROOT) {
168 #ifdef CONFIG_XFS_POSIX_ACL
169 if (namelen == SGI_ACL_FILE_SIZE &&
170 strncmp(name, SGI_ACL_FILE,
171 SGI_ACL_FILE_SIZE) == 0) {
172 __xfs_xattr_put_listent(
173 context, XATTR_SYSTEM_PREFIX,
174 XATTR_SYSTEM_PREFIX_LEN,
175 XATTR_POSIX_ACL_ACCESS,
176 strlen(XATTR_POSIX_ACL_ACCESS));
177 } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
178 strncmp(name, SGI_ACL_DEFAULT,
179 SGI_ACL_DEFAULT_SIZE) == 0) {
180 __xfs_xattr_put_listent(
181 context, XATTR_SYSTEM_PREFIX,
182 XATTR_SYSTEM_PREFIX_LEN,
183 XATTR_POSIX_ACL_DEFAULT,
184 strlen(XATTR_POSIX_ACL_DEFAULT));
189 * Only show root namespace entries if we are actually allowed to
192 if (!capable(CAP_SYS_ADMIN))
195 prefix = XATTR_TRUSTED_PREFIX;
196 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
197 } else if (flags & XFS_ATTR_SECURE) {
198 prefix = XATTR_SECURITY_PREFIX;
199 prefix_len = XATTR_SECURITY_PREFIX_LEN;
201 prefix = XATTR_USER_PREFIX;
202 prefix_len = XATTR_USER_PREFIX_LEN;
205 __xfs_xattr_put_listent(context, prefix, prefix_len, name,
212 struct dentry *dentry,
216 struct xfs_attr_list_context context;
217 struct attrlist_cursor_kern cursor = { 0 };
218 struct inode *inode = d_inode(dentry);
222 * First read the regular on-disk attributes.
224 memset(&context, 0, sizeof(context));
225 context.dp = XFS_I(inode);
226 context.cursor = &cursor;
228 context.alist = size ? data : NULL;
229 context.bufsize = size;
230 context.firstu = context.bufsize;
231 context.put_listent = xfs_xattr_put_listent;
233 error = xfs_attr_list_int(&context);
236 if (context.count < 0)
239 return context.count;