xref: /openbmc/qemu/hw/9pfs/9p-xattr-user.c (revision d2dfe0b5)
1 /*
2  * 9p user. xattr callback
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 /*
15  * Not so fast! You might want to read the 9p developer docs first:
16  * https://wiki.qemu.org/Documentation/9p
17  */
18 
19 #include "qemu/osdep.h"
20 #include "9p.h"
21 #include "fsdev/file-op-9p.h"
22 #include "9p-xattr.h"
23 
24 
25 static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
26                                 const char *name, void *value, size_t size)
27 {
28     if (strncmp(name, "user.virtfs.", 12) == 0) {
29         /*
30          * Don't allow fetch of user.virtfs namespace
31          * in case of mapped security
32          */
33         errno = ENOATTR;
34         return -1;
35     }
36     return local_getxattr_nofollow(ctx, path, name, value, size);
37 }
38 
39 static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
40                                  char *name, void *value, size_t size)
41 {
42     int name_size = strlen(name) + 1;
43     if (strncmp(name, "user.virtfs.", 12) == 0) {
44 
45         /*  check if it is a mapped posix acl */
46         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
47             /* adjust the name and size */
48             name += 12;
49             name_size -= 12;
50         } else {
51             /*
52              * Don't allow fetch of user.virtfs namespace
53              * in case of mapped security
54              */
55             return 0;
56         }
57     }
58     if (!value) {
59         return name_size;
60     }
61 
62     if (size < name_size) {
63         errno = ERANGE;
64         return -1;
65     }
66 
67     /* name_size includes the trailing NUL. */
68     memcpy(value, name, name_size);
69     return name_size;
70 }
71 
72 static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
73                             void *value, size_t size, int flags)
74 {
75     if (strncmp(name, "user.virtfs.", 12) == 0) {
76         /*
77          * Don't allow fetch of user.virtfs namespace
78          * in case of mapped security
79          */
80         errno = EACCES;
81         return -1;
82     }
83     return local_setxattr_nofollow(ctx, path, name, value, size, flags);
84 }
85 
86 static int mp_user_removexattr(FsContext *ctx,
87                                const char *path, const char *name)
88 {
89     if (strncmp(name, "user.virtfs.", 12) == 0) {
90         /*
91          * Don't allow fetch of user.virtfs namespace
92          * in case of mapped security
93          */
94         errno = EACCES;
95         return -1;
96     }
97     return local_removexattr_nofollow(ctx, path, name);
98 }
99 
100 XattrOperations mapped_user_xattr = {
101     .name = "user.",
102     .getxattr = mp_user_getxattr,
103     .setxattr = mp_user_setxattr,
104     .listxattr = mp_user_listxattr,
105     .removexattr = mp_user_removexattr,
106 };
107 
108 XattrOperations passthrough_user_xattr = {
109     .name = "user.",
110     .getxattr = pt_getxattr,
111     .setxattr = pt_setxattr,
112     .listxattr = pt_listxattr,
113     .removexattr = pt_removexattr,
114 };
115