xref: /openbmc/qemu/hw/9pfs/9p-xattr-user.c (revision 56ad3e54dad6cdcee8668d170df161d89581846f)
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 #include "qemu/osdep.h"
15 #include "9p.h"
16 #include "fsdev/file-op-9p.h"
17 #include "9p-xattr.h"
18 
19 
20 static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
21                                 const char *name, void *value, size_t size)
22 {
23     if (strncmp(name, "user.virtfs.", 12) == 0) {
24         /*
25          * Don't allow fetch of user.virtfs namesapce
26          * in case of mapped security
27          */
28         errno = ENOATTR;
29         return -1;
30     }
31     return local_getxattr_nofollow(ctx, path, name, value, size);
32 }
33 
34 static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
35                                  char *name, void *value, size_t size)
36 {
37     int name_size = strlen(name) + 1;
38     if (strncmp(name, "user.virtfs.", 12) == 0) {
39 
40         /*  check if it is a mapped posix acl */
41         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
42             /* adjust the name and size */
43             name += 12;
44             name_size -= 12;
45         } else {
46             /*
47              * Don't allow fetch of user.virtfs namesapce
48              * in case of mapped security
49              */
50             return 0;
51         }
52     }
53     if (!value) {
54         return name_size;
55     }
56 
57     if (size < name_size) {
58         errno = ERANGE;
59         return -1;
60     }
61 
62     /* name_size includes the trailing NUL. */
63     memcpy(value, name, name_size);
64     return name_size;
65 }
66 
67 static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
68                             void *value, size_t size, int flags)
69 {
70     char *buffer;
71     int ret;
72 
73     if (strncmp(name, "user.virtfs.", 12) == 0) {
74         /*
75          * Don't allow fetch of user.virtfs namesapce
76          * in case of mapped security
77          */
78         errno = EACCES;
79         return -1;
80     }
81     buffer = rpath(ctx, path);
82     ret = lsetxattr(buffer, name, value, size, flags);
83     g_free(buffer);
84     return ret;
85 }
86 
87 static int mp_user_removexattr(FsContext *ctx,
88                                const char *path, const char *name)
89 {
90     char *buffer;
91     int ret;
92 
93     if (strncmp(name, "user.virtfs.", 12) == 0) {
94         /*
95          * Don't allow fetch of user.virtfs namesapce
96          * in case of mapped security
97          */
98         errno = EACCES;
99         return -1;
100     }
101     buffer = rpath(ctx, path);
102     ret = lremovexattr(buffer, name);
103     g_free(buffer);
104     return ret;
105 }
106 
107 XattrOperations mapped_user_xattr = {
108     .name = "user.",
109     .getxattr = mp_user_getxattr,
110     .setxattr = mp_user_setxattr,
111     .listxattr = mp_user_listxattr,
112     .removexattr = mp_user_removexattr,
113 };
114 
115 XattrOperations passthrough_user_xattr = {
116     .name = "user.",
117     .getxattr = pt_getxattr,
118     .setxattr = pt_setxattr,
119     .listxattr = pt_listxattr,
120     .removexattr = pt_removexattr,
121 };
122