xref: /openbmc/qemu/hw/9pfs/9p-xattr-user.c (revision a0984714)
1267ae092SWei Liu /*
2267ae092SWei Liu  * 9p user. xattr callback
3267ae092SWei Liu  *
4267ae092SWei Liu  * Copyright IBM, Corp. 2010
5267ae092SWei Liu  *
6267ae092SWei Liu  * Authors:
7267ae092SWei Liu  * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8267ae092SWei Liu  *
9267ae092SWei Liu  * This work is licensed under the terms of the GNU GPL, version 2.  See
10267ae092SWei Liu  * the COPYING file in the top-level directory.
11267ae092SWei Liu  *
12267ae092SWei Liu  */
13267ae092SWei Liu 
146f569084SChristian Schoenebeck /*
156f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
166f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
176f569084SChristian Schoenebeck  */
186f569084SChristian Schoenebeck 
19fbc04127SPeter Maydell #include "qemu/osdep.h"
20ebe74f8bSWei Liu #include "9p.h"
21267ae092SWei Liu #include "fsdev/file-op-9p.h"
22267ae092SWei Liu #include "9p-xattr.h"
23267ae092SWei Liu 
24267ae092SWei Liu 
mp_user_getxattr(FsContext * ctx,const char * path,const char * name,void * value,size_t size)25267ae092SWei Liu static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
26267ae092SWei Liu                                 const char *name, void *value, size_t size)
27267ae092SWei Liu {
28267ae092SWei Liu     if (strncmp(name, "user.virtfs.", 12) == 0) {
29267ae092SWei Liu         /*
30*a0984714SDr. David Alan Gilbert          * Don't allow fetch of user.virtfs namespace
31267ae092SWei Liu          * in case of mapped security
32267ae092SWei Liu          */
33267ae092SWei Liu         errno = ENOATTR;
34267ae092SWei Liu         return -1;
35267ae092SWei Liu     }
3656ad3e54SGreg Kurz     return local_getxattr_nofollow(ctx, path, name, value, size);
37267ae092SWei Liu }
38267ae092SWei Liu 
mp_user_listxattr(FsContext * ctx,const char * path,char * name,void * value,size_t size)39267ae092SWei Liu static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
40267ae092SWei Liu                                  char *name, void *value, size_t size)
41267ae092SWei Liu {
42267ae092SWei Liu     int name_size = strlen(name) + 1;
43267ae092SWei Liu     if (strncmp(name, "user.virtfs.", 12) == 0) {
44267ae092SWei Liu 
45267ae092SWei Liu         /*  check if it is a mapped posix acl */
46267ae092SWei Liu         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
47267ae092SWei Liu             /* adjust the name and size */
48267ae092SWei Liu             name += 12;
49267ae092SWei Liu             name_size -= 12;
50267ae092SWei Liu         } else {
51267ae092SWei Liu             /*
52*a0984714SDr. David Alan Gilbert              * Don't allow fetch of user.virtfs namespace
53267ae092SWei Liu              * in case of mapped security
54267ae092SWei Liu              */
55267ae092SWei Liu             return 0;
56267ae092SWei Liu         }
57267ae092SWei Liu     }
58267ae092SWei Liu     if (!value) {
59267ae092SWei Liu         return name_size;
60267ae092SWei Liu     }
61267ae092SWei Liu 
62267ae092SWei Liu     if (size < name_size) {
63267ae092SWei Liu         errno = ERANGE;
64267ae092SWei Liu         return -1;
65267ae092SWei Liu     }
66267ae092SWei Liu 
67267ae092SWei Liu     /* name_size includes the trailing NUL. */
68267ae092SWei Liu     memcpy(value, name, name_size);
69267ae092SWei Liu     return name_size;
70267ae092SWei Liu }
71267ae092SWei Liu 
mp_user_setxattr(FsContext * ctx,const char * path,const char * name,void * value,size_t size,int flags)72267ae092SWei Liu static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
73267ae092SWei Liu                             void *value, size_t size, int flags)
74267ae092SWei Liu {
75267ae092SWei Liu     if (strncmp(name, "user.virtfs.", 12) == 0) {
76267ae092SWei Liu         /*
77*a0984714SDr. David Alan Gilbert          * Don't allow fetch of user.virtfs namespace
78267ae092SWei Liu          * in case of mapped security
79267ae092SWei Liu          */
80267ae092SWei Liu         errno = EACCES;
81267ae092SWei Liu         return -1;
82267ae092SWei Liu     }
833e36aba7SGreg Kurz     return local_setxattr_nofollow(ctx, path, name, value, size, flags);
84267ae092SWei Liu }
85267ae092SWei Liu 
mp_user_removexattr(FsContext * ctx,const char * path,const char * name)86267ae092SWei Liu static int mp_user_removexattr(FsContext *ctx,
87267ae092SWei Liu                                const char *path, const char *name)
88267ae092SWei Liu {
89267ae092SWei Liu     if (strncmp(name, "user.virtfs.", 12) == 0) {
90267ae092SWei Liu         /*
91*a0984714SDr. David Alan Gilbert          * Don't allow fetch of user.virtfs namespace
92267ae092SWei Liu          * in case of mapped security
93267ae092SWei Liu          */
94267ae092SWei Liu         errno = EACCES;
95267ae092SWei Liu         return -1;
96267ae092SWei Liu     }
9772f0d0bfSGreg Kurz     return local_removexattr_nofollow(ctx, path, name);
98267ae092SWei Liu }
99267ae092SWei Liu 
100267ae092SWei Liu XattrOperations mapped_user_xattr = {
101267ae092SWei Liu     .name = "user.",
102267ae092SWei Liu     .getxattr = mp_user_getxattr,
103267ae092SWei Liu     .setxattr = mp_user_setxattr,
104267ae092SWei Liu     .listxattr = mp_user_listxattr,
105267ae092SWei Liu     .removexattr = mp_user_removexattr,
106267ae092SWei Liu };
107267ae092SWei Liu 
108267ae092SWei Liu XattrOperations passthrough_user_xattr = {
109267ae092SWei Liu     .name = "user.",
110267ae092SWei Liu     .getxattr = pt_getxattr,
111267ae092SWei Liu     .setxattr = pt_setxattr,
112267ae092SWei Liu     .listxattr = pt_listxattr,
113267ae092SWei Liu     .removexattr = pt_removexattr,
114267ae092SWei Liu };
115