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 14*6f569084SChristian Schoenebeck /* 15*6f569084SChristian Schoenebeck * Not so fast! You might want to read the 9p developer docs first: 16*6f569084SChristian Schoenebeck * https://wiki.qemu.org/Documentation/9p 17*6f569084SChristian Schoenebeck */ 18*6f569084SChristian Schoenebeck 19fbc04127SPeter Maydell #include "qemu/osdep.h" 201de7afc9SPaolo Bonzini #include "qemu/xattr.h" 21ebe74f8bSWei Liu #include "9p.h" 22353ac78dSAneesh Kumar K.V #include "fsdev/file-op-9p.h" 23267ae092SWei Liu #include "9p-xattr.h" 2470fc55ebSAneesh Kumar K.V 2570fc55ebSAneesh Kumar K.V #define MAP_ACL_ACCESS "user.virtfs.system.posix_acl_access" 2670fc55ebSAneesh Kumar K.V #define MAP_ACL_DEFAULT "user.virtfs.system.posix_acl_default" 2770fc55ebSAneesh Kumar K.V #define ACL_ACCESS "system.posix_acl_access" 2870fc55ebSAneesh Kumar K.V #define ACL_DEFAULT "system.posix_acl_default" 2970fc55ebSAneesh Kumar K.V 3070fc55ebSAneesh Kumar K.V static ssize_t mp_pacl_getxattr(FsContext *ctx, const char *path, 3170fc55ebSAneesh Kumar K.V const char *name, void *value, size_t size) 3270fc55ebSAneesh Kumar K.V { 3356ad3e54SGreg Kurz return local_getxattr_nofollow(ctx, path, MAP_ACL_ACCESS, value, size); 3470fc55ebSAneesh Kumar K.V } 3570fc55ebSAneesh Kumar K.V 3670fc55ebSAneesh Kumar K.V static ssize_t mp_pacl_listxattr(FsContext *ctx, const char *path, 3770fc55ebSAneesh Kumar K.V char *name, void *value, size_t osize) 3870fc55ebSAneesh Kumar K.V { 3970fc55ebSAneesh Kumar K.V ssize_t len = sizeof(ACL_ACCESS); 4070fc55ebSAneesh Kumar K.V 4170fc55ebSAneesh Kumar K.V if (!value) { 4270fc55ebSAneesh Kumar K.V return len; 4370fc55ebSAneesh Kumar K.V } 4470fc55ebSAneesh Kumar K.V 4570fc55ebSAneesh Kumar K.V if (osize < len) { 4670fc55ebSAneesh Kumar K.V errno = ERANGE; 4770fc55ebSAneesh Kumar K.V return -1; 4870fc55ebSAneesh Kumar K.V } 4970fc55ebSAneesh Kumar K.V 509238c209SJim Meyering /* len includes the trailing NUL */ 519238c209SJim Meyering memcpy(value, ACL_ACCESS, len); 5270fc55ebSAneesh Kumar K.V return 0; 5370fc55ebSAneesh Kumar K.V } 5470fc55ebSAneesh Kumar K.V 5570fc55ebSAneesh Kumar K.V static int mp_pacl_setxattr(FsContext *ctx, const char *path, const char *name, 5670fc55ebSAneesh Kumar K.V void *value, size_t size, int flags) 5770fc55ebSAneesh Kumar K.V { 583e36aba7SGreg Kurz return local_setxattr_nofollow(ctx, path, MAP_ACL_ACCESS, value, size, 593e36aba7SGreg Kurz flags); 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 6772f0d0bfSGreg Kurz ret = local_removexattr_nofollow(ctx, path, MAP_ACL_ACCESS); 6870fc55ebSAneesh Kumar K.V if (ret == -1 && errno == ENODATA) { 6970fc55ebSAneesh Kumar K.V /* 70a0994761SAneesh Kumar K.V * We don't get ENODATA error when trying to remove a 7170fc55ebSAneesh Kumar K.V * posix acl that is not present. So don't throw the error 7270fc55ebSAneesh Kumar K.V * even in case of mapped security model 7370fc55ebSAneesh Kumar K.V */ 7470fc55ebSAneesh Kumar K.V errno = 0; 7570fc55ebSAneesh Kumar K.V ret = 0; 7670fc55ebSAneesh Kumar K.V } 7770fc55ebSAneesh Kumar K.V return ret; 7870fc55ebSAneesh Kumar K.V } 7970fc55ebSAneesh Kumar K.V 8070fc55ebSAneesh Kumar K.V static ssize_t mp_dacl_getxattr(FsContext *ctx, const char *path, 8170fc55ebSAneesh Kumar K.V const char *name, void *value, size_t size) 8270fc55ebSAneesh Kumar K.V { 8356ad3e54SGreg Kurz return local_getxattr_nofollow(ctx, path, MAP_ACL_DEFAULT, value, size); 8470fc55ebSAneesh Kumar K.V } 8570fc55ebSAneesh Kumar K.V 8670fc55ebSAneesh Kumar K.V static ssize_t mp_dacl_listxattr(FsContext *ctx, const char *path, 8770fc55ebSAneesh Kumar K.V char *name, void *value, size_t osize) 8870fc55ebSAneesh Kumar K.V { 8970fc55ebSAneesh Kumar K.V ssize_t len = sizeof(ACL_DEFAULT); 9070fc55ebSAneesh Kumar K.V 9170fc55ebSAneesh Kumar K.V if (!value) { 9270fc55ebSAneesh Kumar K.V return len; 9370fc55ebSAneesh Kumar K.V } 9470fc55ebSAneesh Kumar K.V 9570fc55ebSAneesh Kumar K.V if (osize < len) { 9670fc55ebSAneesh Kumar K.V errno = ERANGE; 9770fc55ebSAneesh Kumar K.V return -1; 9870fc55ebSAneesh Kumar K.V } 9970fc55ebSAneesh Kumar K.V 1009238c209SJim Meyering /* len includes the trailing NUL */ 1019005c3b3SShannon Zhao memcpy(value, ACL_DEFAULT, len); 10270fc55ebSAneesh Kumar K.V return 0; 10370fc55ebSAneesh Kumar K.V } 10470fc55ebSAneesh Kumar K.V 10570fc55ebSAneesh Kumar K.V static int mp_dacl_setxattr(FsContext *ctx, const char *path, const char *name, 10670fc55ebSAneesh Kumar K.V void *value, size_t size, int flags) 10770fc55ebSAneesh Kumar K.V { 1083e36aba7SGreg Kurz return local_setxattr_nofollow(ctx, path, MAP_ACL_DEFAULT, value, size, 1093e36aba7SGreg Kurz flags); 11070fc55ebSAneesh Kumar K.V } 11170fc55ebSAneesh Kumar K.V 11270fc55ebSAneesh Kumar K.V static int mp_dacl_removexattr(FsContext *ctx, 11370fc55ebSAneesh Kumar K.V const char *path, const char *name) 11470fc55ebSAneesh Kumar K.V { 115a0994761SAneesh Kumar K.V int ret; 1164fa4ce71SChen Gang 11772f0d0bfSGreg Kurz ret = local_removexattr_nofollow(ctx, path, MAP_ACL_DEFAULT); 118a0994761SAneesh Kumar K.V if (ret == -1 && errno == ENODATA) { 119a0994761SAneesh Kumar K.V /* 120a0994761SAneesh Kumar K.V * We don't get ENODATA error when trying to remove a 121a0994761SAneesh Kumar K.V * posix acl that is not present. So don't throw the error 122a0994761SAneesh Kumar K.V * even in case of mapped security model 123a0994761SAneesh Kumar K.V */ 124a0994761SAneesh Kumar K.V errno = 0; 125a0994761SAneesh Kumar K.V ret = 0; 126a0994761SAneesh Kumar K.V } 127a0994761SAneesh Kumar K.V return ret; 12870fc55ebSAneesh Kumar K.V } 12970fc55ebSAneesh Kumar K.V 13070fc55ebSAneesh Kumar K.V 13170fc55ebSAneesh Kumar K.V XattrOperations mapped_pacl_xattr = { 13270fc55ebSAneesh Kumar K.V .name = "system.posix_acl_access", 13370fc55ebSAneesh Kumar K.V .getxattr = mp_pacl_getxattr, 13470fc55ebSAneesh Kumar K.V .setxattr = mp_pacl_setxattr, 13570fc55ebSAneesh Kumar K.V .listxattr = mp_pacl_listxattr, 13670fc55ebSAneesh Kumar K.V .removexattr = mp_pacl_removexattr, 13770fc55ebSAneesh Kumar K.V }; 13870fc55ebSAneesh Kumar K.V 13970fc55ebSAneesh Kumar K.V XattrOperations mapped_dacl_xattr = { 14070fc55ebSAneesh Kumar K.V .name = "system.posix_acl_default", 14170fc55ebSAneesh Kumar K.V .getxattr = mp_dacl_getxattr, 14270fc55ebSAneesh Kumar K.V .setxattr = mp_dacl_setxattr, 14370fc55ebSAneesh Kumar K.V .listxattr = mp_dacl_listxattr, 14470fc55ebSAneesh Kumar K.V .removexattr = mp_dacl_removexattr, 14570fc55ebSAneesh Kumar K.V }; 14670fc55ebSAneesh Kumar K.V 14770fc55ebSAneesh Kumar K.V XattrOperations passthrough_acl_xattr = { 14870fc55ebSAneesh Kumar K.V .name = "system.posix_acl_", 14970fc55ebSAneesh Kumar K.V .getxattr = pt_getxattr, 15070fc55ebSAneesh Kumar K.V .setxattr = pt_setxattr, 15170fc55ebSAneesh Kumar K.V .listxattr = pt_listxattr, 15270fc55ebSAneesh Kumar K.V .removexattr = pt_removexattr, 15370fc55ebSAneesh Kumar K.V }; 15470fc55ebSAneesh Kumar K.V 15570fc55ebSAneesh Kumar K.V XattrOperations none_acl_xattr = { 15670fc55ebSAneesh Kumar K.V .name = "system.posix_acl_", 15770fc55ebSAneesh Kumar K.V .getxattr = notsup_getxattr, 15870fc55ebSAneesh Kumar K.V .setxattr = notsup_setxattr, 15970fc55ebSAneesh Kumar K.V .listxattr = notsup_listxattr, 16070fc55ebSAneesh Kumar K.V .removexattr = notsup_removexattr, 16170fc55ebSAneesh Kumar K.V }; 162