xref: /qemu/hw/9pfs/9p-posix-acl.c (revision 56ad3e54dad6cdcee8668d170df161d89581846f)
170fc55ebSAneesh Kumar K.V /*
2d57b7800SWei Liu  * 9p system.posix* xattr callback
370fc55ebSAneesh Kumar K.V  *
470fc55ebSAneesh Kumar K.V  * Copyright IBM, Corp. 2010
570fc55ebSAneesh Kumar K.V  *
670fc55ebSAneesh Kumar K.V  * Authors:
770fc55ebSAneesh Kumar K.V  * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
870fc55ebSAneesh Kumar K.V  *
970fc55ebSAneesh Kumar K.V  * This work is licensed under the terms of the GNU GPL, version 2.  See
1070fc55ebSAneesh Kumar K.V  * the COPYING file in the top-level directory.
1170fc55ebSAneesh Kumar K.V  *
1270fc55ebSAneesh Kumar K.V  */
1370fc55ebSAneesh Kumar K.V 
14fbc04127SPeter Maydell #include "qemu/osdep.h"
151de7afc9SPaolo Bonzini #include "qemu/xattr.h"
16ebe74f8bSWei Liu #include "9p.h"
17353ac78dSAneesh Kumar K.V #include "fsdev/file-op-9p.h"
18267ae092SWei Liu #include "9p-xattr.h"
1970fc55ebSAneesh Kumar K.V 
2070fc55ebSAneesh Kumar K.V #define MAP_ACL_ACCESS "user.virtfs.system.posix_acl_access"
2170fc55ebSAneesh Kumar K.V #define MAP_ACL_DEFAULT "user.virtfs.system.posix_acl_default"
2270fc55ebSAneesh Kumar K.V #define ACL_ACCESS "system.posix_acl_access"
2370fc55ebSAneesh Kumar K.V #define ACL_DEFAULT "system.posix_acl_default"
2470fc55ebSAneesh Kumar K.V 
2570fc55ebSAneesh Kumar K.V static ssize_t mp_pacl_getxattr(FsContext *ctx, const char *path,
2670fc55ebSAneesh Kumar K.V                                 const char *name, void *value, size_t size)
2770fc55ebSAneesh Kumar K.V {
28*56ad3e54SGreg Kurz     return local_getxattr_nofollow(ctx, path, MAP_ACL_ACCESS, value, size);
2970fc55ebSAneesh Kumar K.V }
3070fc55ebSAneesh Kumar K.V 
3170fc55ebSAneesh Kumar K.V static ssize_t mp_pacl_listxattr(FsContext *ctx, const char *path,
3270fc55ebSAneesh Kumar K.V                                  char *name, void *value, size_t osize)
3370fc55ebSAneesh Kumar K.V {
3470fc55ebSAneesh Kumar K.V     ssize_t len = sizeof(ACL_ACCESS);
3570fc55ebSAneesh Kumar K.V 
3670fc55ebSAneesh Kumar K.V     if (!value) {
3770fc55ebSAneesh Kumar K.V         return len;
3870fc55ebSAneesh Kumar K.V     }
3970fc55ebSAneesh Kumar K.V 
4070fc55ebSAneesh Kumar K.V     if (osize < len) {
4170fc55ebSAneesh Kumar K.V         errno = ERANGE;
4270fc55ebSAneesh Kumar K.V         return -1;
4370fc55ebSAneesh Kumar K.V     }
4470fc55ebSAneesh Kumar K.V 
459238c209SJim Meyering     /* len includes the trailing NUL */
469238c209SJim Meyering     memcpy(value, ACL_ACCESS, len);
4770fc55ebSAneesh Kumar K.V     return 0;
4870fc55ebSAneesh Kumar K.V }
4970fc55ebSAneesh Kumar K.V 
5070fc55ebSAneesh Kumar K.V static int mp_pacl_setxattr(FsContext *ctx, const char *path, const char *name,
5170fc55ebSAneesh Kumar K.V                             void *value, size_t size, int flags)
5270fc55ebSAneesh Kumar K.V {
534fa4ce71SChen Gang     char *buffer;
544fa4ce71SChen Gang     int ret;
554fa4ce71SChen Gang 
564fa4ce71SChen Gang     buffer = rpath(ctx, path);
574fa4ce71SChen Gang     ret = lsetxattr(buffer, MAP_ACL_ACCESS, value, size, flags);
584fa4ce71SChen Gang     g_free(buffer);
594fa4ce71SChen Gang     return ret;
6070fc55ebSAneesh Kumar K.V }
6170fc55ebSAneesh Kumar K.V 
6270fc55ebSAneesh Kumar K.V static int mp_pacl_removexattr(FsContext *ctx,
6370fc55ebSAneesh Kumar K.V                                const char *path, const char *name)
6470fc55ebSAneesh Kumar K.V {
6570fc55ebSAneesh Kumar K.V     int ret;
664fa4ce71SChen Gang     char *buffer;
674fa4ce71SChen Gang 
684fa4ce71SChen Gang     buffer = rpath(ctx, path);
694fa4ce71SChen Gang     ret  = lremovexattr(buffer, MAP_ACL_ACCESS);
7070fc55ebSAneesh Kumar K.V     if (ret == -1 && errno == ENODATA) {
7170fc55ebSAneesh Kumar K.V         /*
72a0994761SAneesh Kumar K.V          * We don't get ENODATA error when trying to remove a
7370fc55ebSAneesh Kumar K.V          * posix acl that is not present. So don't throw the error
7470fc55ebSAneesh Kumar K.V          * even in case of mapped security model
7570fc55ebSAneesh Kumar K.V          */
7670fc55ebSAneesh Kumar K.V         errno = 0;
7770fc55ebSAneesh Kumar K.V         ret = 0;
7870fc55ebSAneesh Kumar K.V     }
794fa4ce71SChen Gang     g_free(buffer);
8070fc55ebSAneesh Kumar K.V     return ret;
8170fc55ebSAneesh Kumar K.V }
8270fc55ebSAneesh Kumar K.V 
8370fc55ebSAneesh Kumar K.V static ssize_t mp_dacl_getxattr(FsContext *ctx, const char *path,
8470fc55ebSAneesh Kumar K.V                                 const char *name, void *value, size_t size)
8570fc55ebSAneesh Kumar K.V {
86*56ad3e54SGreg Kurz     return local_getxattr_nofollow(ctx, path, MAP_ACL_DEFAULT, value, size);
8770fc55ebSAneesh Kumar K.V }
8870fc55ebSAneesh Kumar K.V 
8970fc55ebSAneesh Kumar K.V static ssize_t mp_dacl_listxattr(FsContext *ctx, const char *path,
9070fc55ebSAneesh Kumar K.V                                  char *name, void *value, size_t osize)
9170fc55ebSAneesh Kumar K.V {
9270fc55ebSAneesh Kumar K.V     ssize_t len = sizeof(ACL_DEFAULT);
9370fc55ebSAneesh Kumar K.V 
9470fc55ebSAneesh Kumar K.V     if (!value) {
9570fc55ebSAneesh Kumar K.V         return len;
9670fc55ebSAneesh Kumar K.V     }
9770fc55ebSAneesh Kumar K.V 
9870fc55ebSAneesh Kumar K.V     if (osize < len) {
9970fc55ebSAneesh Kumar K.V         errno = ERANGE;
10070fc55ebSAneesh Kumar K.V         return -1;
10170fc55ebSAneesh Kumar K.V     }
10270fc55ebSAneesh Kumar K.V 
1039238c209SJim Meyering     /* len includes the trailing NUL */
1049005c3b3SShannon Zhao     memcpy(value, ACL_DEFAULT, len);
10570fc55ebSAneesh Kumar K.V     return 0;
10670fc55ebSAneesh Kumar K.V }
10770fc55ebSAneesh Kumar K.V 
10870fc55ebSAneesh Kumar K.V static int mp_dacl_setxattr(FsContext *ctx, const char *path, const char *name,
10970fc55ebSAneesh Kumar K.V                             void *value, size_t size, int flags)
11070fc55ebSAneesh Kumar K.V {
1114fa4ce71SChen Gang     char *buffer;
1124fa4ce71SChen Gang     int ret;
1134fa4ce71SChen Gang 
1144fa4ce71SChen Gang     buffer = rpath(ctx, path);
1154fa4ce71SChen Gang     ret = lsetxattr(buffer, MAP_ACL_DEFAULT, value, size, flags);
1164fa4ce71SChen Gang     g_free(buffer);
1174fa4ce71SChen Gang     return ret;
11870fc55ebSAneesh Kumar K.V }
11970fc55ebSAneesh Kumar K.V 
12070fc55ebSAneesh Kumar K.V static int mp_dacl_removexattr(FsContext *ctx,
12170fc55ebSAneesh Kumar K.V                                const char *path, const char *name)
12270fc55ebSAneesh Kumar K.V {
123a0994761SAneesh Kumar K.V     int ret;
1244fa4ce71SChen Gang     char *buffer;
1254fa4ce71SChen Gang 
1264fa4ce71SChen Gang     buffer = rpath(ctx, path);
1274fa4ce71SChen Gang     ret  = lremovexattr(buffer, MAP_ACL_DEFAULT);
128a0994761SAneesh Kumar K.V     if (ret == -1 && errno == ENODATA) {
129a0994761SAneesh Kumar K.V         /*
130a0994761SAneesh Kumar K.V          * We don't get ENODATA error when trying to remove a
131a0994761SAneesh Kumar K.V          * posix acl that is not present. So don't throw the error
132a0994761SAneesh Kumar K.V          * even in case of mapped security model
133a0994761SAneesh Kumar K.V          */
134a0994761SAneesh Kumar K.V         errno = 0;
135a0994761SAneesh Kumar K.V         ret = 0;
136a0994761SAneesh Kumar K.V     }
1374fa4ce71SChen Gang     g_free(buffer);
138a0994761SAneesh Kumar K.V     return ret;
13970fc55ebSAneesh Kumar K.V }
14070fc55ebSAneesh Kumar K.V 
14170fc55ebSAneesh Kumar K.V 
14270fc55ebSAneesh Kumar K.V XattrOperations mapped_pacl_xattr = {
14370fc55ebSAneesh Kumar K.V     .name = "system.posix_acl_access",
14470fc55ebSAneesh Kumar K.V     .getxattr = mp_pacl_getxattr,
14570fc55ebSAneesh Kumar K.V     .setxattr = mp_pacl_setxattr,
14670fc55ebSAneesh Kumar K.V     .listxattr = mp_pacl_listxattr,
14770fc55ebSAneesh Kumar K.V     .removexattr = mp_pacl_removexattr,
14870fc55ebSAneesh Kumar K.V };
14970fc55ebSAneesh Kumar K.V 
15070fc55ebSAneesh Kumar K.V XattrOperations mapped_dacl_xattr = {
15170fc55ebSAneesh Kumar K.V     .name = "system.posix_acl_default",
15270fc55ebSAneesh Kumar K.V     .getxattr = mp_dacl_getxattr,
15370fc55ebSAneesh Kumar K.V     .setxattr = mp_dacl_setxattr,
15470fc55ebSAneesh Kumar K.V     .listxattr = mp_dacl_listxattr,
15570fc55ebSAneesh Kumar K.V     .removexattr = mp_dacl_removexattr,
15670fc55ebSAneesh Kumar K.V };
15770fc55ebSAneesh Kumar K.V 
15870fc55ebSAneesh Kumar K.V XattrOperations passthrough_acl_xattr = {
15970fc55ebSAneesh Kumar K.V     .name = "system.posix_acl_",
16070fc55ebSAneesh Kumar K.V     .getxattr = pt_getxattr,
16170fc55ebSAneesh Kumar K.V     .setxattr = pt_setxattr,
16270fc55ebSAneesh Kumar K.V     .listxattr = pt_listxattr,
16370fc55ebSAneesh Kumar K.V     .removexattr = pt_removexattr,
16470fc55ebSAneesh Kumar K.V };
16570fc55ebSAneesh Kumar K.V 
16670fc55ebSAneesh Kumar K.V XattrOperations none_acl_xattr = {
16770fc55ebSAneesh Kumar K.V     .name = "system.posix_acl_",
16870fc55ebSAneesh Kumar K.V     .getxattr = notsup_getxattr,
16970fc55ebSAneesh Kumar K.V     .setxattr = notsup_setxattr,
17070fc55ebSAneesh Kumar K.V     .listxattr = notsup_listxattr,
17170fc55ebSAneesh Kumar K.V     .removexattr = notsup_removexattr,
17270fc55ebSAneesh Kumar K.V };
173