virtiofsd xattr name mappings
cover virtiofsd xattr name mappings
From: "Dr. David Alan Gilbert" <[email protected]>
Hi,
This is a first cut of a xattr name mapping option for virtiofsd.
It allows the user of virtiofsd to define a fairly flexible mapping from the view of the xattr names the host fs has and the ones that the guest sees.
The hope is this allows things like:
a) Different selinux attributes on host/guest
b) separation of trusted. attributes that clash on overlayfs
c) support for privileged xattr's in guests running with an unprivileged virtiofsd.
There's no apparent standard for this kind of mapping, so I made it flexible by specifying a mapping rule in the option.
Prefix's can be added (selectively or globally), xattr's can be dropped in either direction or passed through.
One example is:
-o xattrmap=" :ch:p::user.virtiofs.::ch:b:::"
which prepends user.virtiofs. to any xattr name generated by the guest and blocks any non-prefix'd name.
This should be able to match existing filesystems with xattr's from other implementations as well given the write map rules.
(TODO: My rst is a bit rusty, so I know I need to rework the docs in the first patch).
Dave
Dr. David Alan Gilbert (3):
tools/virtiofsd: xattr name mappings: Add option tools/virtiofsd: xattr name mappings: Map client xattr names tools/virtiofsd: xattr name mappings: Map host xattr names
docs/tools/virtiofsd.rst | 46 +++++ tools/virtiofsd/passthrough_ll.c | 345 ++++++++++++++++++++++++++++++- 2 files changed, 388 insertions(+), 3 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/3 tools/virtiofsd: xattr name mappings: Add option
From: "Dr. David Alan Gilbert" <[email protected]>
Add an option to define mappings of xattr names so that the client and host filesystems see different views. This can be used to have different SELinux mappings as seen by the guest, to run the virtiofsd with less privileges (e.g. in a case where it can't set trusted/system/security xattrs but you want the guest to be able to), or to isolate multiple users of the same name; e.g. trusted attributes used by stacking overlayfs.
A mapping engine is used wit 3 simple rules; the rules can be combined to allow most useful mapping scenarios. The ruleset is defined by -o xattrmap='rules...'.
This patch doesn't use the rule maps yet.
--- docs/tools/virtiofsd.rst | 46 +++++++++ tools/virtiofsd/passthrough_ll.c | 156 ++++++++++++++++++++++++++++++- 2 files changed, 201 insertions(+), 1 deletion(-)
docs/tools/virtiofsd.rst +46 -0
@@ -107,6 +107,51 @@ Options performance. ``auto`` acts similar to NFS with a 1 second metadata cache timeout. ``always`` sets a long cache lifetime at the expense of coherency. +xattr-mapping +------------- + +By default the name of xattr's used by the client are passe through to the host +file system. This can be a problem where either those xattr names are used +by something on the host (e.g. selinux guest/host confusion) or if the +virtiofsd is running in a container with restricted priviliges where it cannot +access some attributes. + +A mapping of xattr names can be made using -o xattrmap=mapping where the ``mapping`` +string consists of a series of rules. + +Each rule starts and ends with a ':'. The mapping stops on a matching +rule. White space may be added before and after each rule. + +:scope:type:key:prepend: + +scope= 'c' - match 'key' against a xattr name from the client + for setxattr/getxattr/removexattr + 'h' - match 'prepend' against a xattr name from the host + for listxattr + both letters can be included to match both cases. + +type is one of: + 'p' Prefixing: If 'key' matches the client then the 'prepend' + is added before the name is passed to the host. + For a host case, the prepend is tested and stripped + if matching. + + 'o' OK: The attribute name is OK and passed through to + the host unchanged. + + 'b' Bad: If a client tries to use this name it's + denied using EPERM; when the host passes an attribute + name matching it's hidden. + +key is a string tested as a prefix on an attribute name originating + on the client. It maybe empty in which case a 'c' rule + will always match on client names. + +prepend is a string tested as a prefix on an attribute name originiating + on the host, and used as a new prefix by 'p'. It maybe empty + in which case a 'h' rule will always match on host names. + + Examples --------
@@ -123,3 +168,4 @@ Export ``/var/lib/fs/vm001/`` on vhost-user UNIX domain socket -numa node,memdev=mem \ ... guest# mount -t virtiofs myfs /mnt +
tools/virtiofsd/passthrough_ll.c +155 -1
180 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH 1/3] tools/virtiofsd: xattr name mappings: Add option
Quoted 41 lines · Dr. David Alan Gilbert (git)
docs/tools/virtiofsd.rst +33
-0@@ -107,6 +107,51 @@ Options performance. ``auto`` acts similar to NFS with a 1 second metadata cache timeout. ``always`` sets a long cache lifetime at the expense of coherency. +xattr-mapping +------------- + +By default the name of xattr's used by the client are passe through to the host +file system. This can be a problem where either those xattr names are used +by something on the host (e.g. selinux guest/host confusion) or if the +virtiofsd is running in a container with restricted priviliges where it cannot +access some attributes. + +A mapping of xattr names can be made using -o xattrmap=mapping where the ``mapping`` +string consists of a series of rules. + +Each rule starts and ends with a ':'. The mapping stops on a matching +rule. White space may be added before and after each rule. + +:scope:type:key:prepend: + +scope= 'c' - match 'key' against a xattr name from the client + for setxattr/getxattr/removexattr + 'h' - match 'prepend' against a xattr name from the host + for listxattr + both letters can be included to match both cases. + +type is one of: + 'p' Prefixing: If 'key' matches the client then the 'prepend' + is added before the name is passed to the host. + For a host case, the prepend is tested and stripped + if matching. + + 'o' OK: The attribute name is OK and passed through to + the host unchanged. + + 'b' Bad: If a client tries to use this name it's
docs/tools/virtiofsd.rst Options · line 152
Quoted 10 lines
docs/tools/virtiofsd.rst +10
-0+ denied using EPERM; when the host passes an attribute + name matching it's hidden. + +key is a string tested as a prefix on an attribute name originating + on the client. It maybe empty in which case a 'c' rule + will always match on client names. + +prepend is a string tested as a prefix on an attribute name originiating + on the host, and used as a new prefix by 'p'. It maybe empty + in which case a 'h' rule will always match on host names.
This syntax and the documentation is hard to understand. Defining concrete commands instead of a single super-command would make it more straightforward.
Here is the functionality I've been able to extract from the documentation:
1. Rewrite client xattrs
rewrite OLD NEW -> s/^OLD/NEW/
2. Allow client xattrs
allow PREFIX -> allow if matching
3. Deny client xattrs
deny PREFIX -> EPERM if matching
4. Unprefix server xattrs
unprefix PREFIX -> s/^PREFIX//
5. Hide server xattrs
hide PREFIX -> skip if matching
Did I miss any functionality?
I suggest using "client" and "server" terminology instead of "client" and "host".
Prefix syntax: xattr names are matched by prefix. The empty string matches all xattr names. How is ':' escaped?
It is unclear whether 'o' terminates processing immediately. Will later 'p' rules still execute?
It is unclear whether 'o'/'b' match the original client name or the rewritten name after a 'p' command.
Re: [PATCH 1/3] tools/virtiofsd: xattr name mappings: Add option
Quoted 48 lines · * Stefan Hajnoczi ([email protected]) wrote:
Quoted 47 lines · Dr. David Alan Gilbert (git)
docs/tools/virtiofsd.rst +39
-0@@ -107,6 +107,51 @@ Options performance. ``auto`` acts similar to NFS with a 1 second metadata cache timeout. ``always`` sets a long cache lifetime at the expense of coherency. +xattr-mapping +------------- + +By default the name of xattr's used by the client are passe through to the host +file system. This can be a problem where either those xattr names are used +by something on the host (e.g. selinux guest/host confusion) or if the +virtiofsd is running in a container with restricted priviliges where it cannot +access some attributes. + +A mapping of xattr names can be made using -o xattrmap=mapping where the ``mapping`` +string consists of a series of rules. + +Each rule starts and ends with a ':'. The mapping stops on a matching +rule. White space may be added before and after each rule. + +:scope:type:key:prepend: + +scope= 'c' - match 'key' against a xattr name from the client + for setxattr/getxattr/removexattr + 'h' - match 'prepend' against a xattr name from the host + for listxattr + both letters can be included to match both cases. + +type is one of: + 'p' Prefixing: If 'key' matches the client then the 'prepend' + is added before the name is passed to the host. + For a host case, the prepend is tested and stripped + if matching. + + 'o' OK: The attribute name is OK and passed through to + the host unchanged. + + 'b' Bad: If a client tries to use this name it's + denied using EPERM; when the host passes an attribute + name matching it's hidden. + +key is a string tested as a prefix on an attribute name originating + on the client. It maybe empty in which case a 'c' rule + will always match on client names.
Quoted 8 lines
docs/tools/virtiofsd.rst Options · line 152
Quoted 4 lines
docs/tools/virtiofsd.rst +4
-0+ +prepend is a string tested as a prefix on an attribute name originiating + on the host, and used as a new prefix by 'p'. It maybe empty + in which case a 'h' rule will always match on host names.This syntax and the documentation is hard to understand. Defining concrete commands instead of a single super-command would make it more straightforward.
Yeh I realised it was getting a bit arcane.
Quoted 6 lines
Here is the functionality I've been able to extract from the documentation:
1. Rewrite client xattrs
rewrite OLD NEW -> s/^OLD/NEW/
It's not actually that flexible; it can only prepend a prefix; conditionally on a given prefix, i.e.
s/^OLD/NEWOLD/
it can't do a generic rewrite. It's precisely the inverse of (4).
Quoted 10 lines
2. Allow client xattrs
allow PREFIX -> allow if matching
3. Deny client xattrs
deny PREFIX -> EPERM if matching
4. Unprefix server xattrs
Quoted 7 lines
unprefix PREFIX -> s/^PREFIX//5. Hide server xattrs
hide PREFIX -> skip if matching
Did I miss any functionality?
It can explicitly allow server xattrs.
What we have is
(client, server, all) x ([un-]prefix , good, bad)
Quoted 2 lines
I suggest using "client" and "server" terminology instead of "client" and "host".
OK; so the 'server' being the one with the underlying fs from which a capability is read.
Quoted 2 lines
Prefix syntax: xattr names are matched by prefix. The empty string matches all xattr names. How is ':' escaped?
I didn't bother adding any escaping code; do you think we need to bother? I've not seen any use of an xattr pattern that uses a : - if I was going to suggest an easiest thing I think I'd do it like 'sed' in making the first character be the matching character rather than explicitly :.
Quoted 2 lines
It is unclear whether 'o' terminates processing immediately. Will later 'p' rules still execute?
The point of 'o' and 'b' is to terminate immediately; the idea is to be able to do something like:
'trusted.chocolate' -> OK
'trusted.cheese' -> BAD
'trusted' -> prefix by user.virtiofsso that the trusted.something is accepted and stops processing, and then any other trusted's get prefixed.
Quoted 2 lines
It is unclear whether 'o'/'b' match the original client name or the rewritten name after a 'p' command.
Any match terminates; so a 'p' prefixes and that's it - no other command is processed.
I'll rework so that:
a) I replace any single letters by an explicit name
b) I use 'server' instead of host'
Dave
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH 2/3 tools/virtiofsd: xattr name mappings: Map client xattr names
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Map xattr names originating at the client; from get/set/remove xattr.
--- tools/virtiofsd/passthrough_ll.c | 101 ++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-)
tools/virtiofsd/passthrough_ll.c +99 -2
@@ -2152,20 +2152,82 @@ static XattrMapEntry *parse_xattrmap(const char *map)return res; } + +/* +* For use with getxattr/setxattr/removexattr, where the client +* gives us a name and we may need to choose a different one. +* Allocates a buffer for the result placing it in *out_name. +* If there's no change then *out_name is not set. +* Returns 0 on success +* Can return -EPERM to indicate we block a given attribute +* (in which case out_name is not allocated) +* Can return -ENOMEM to indicate out_name couldn't be allocated. +*/ +static int xattr_map_client (const char *client_name,char **out_name) +{ +const XattrMapEntry *cur_entry; + +for (cur_entry = xattr_map_list; ; cur_entry++) { +if ((cur_entry->flags &XATTR_MAP_FLAG_CLIENT ) && + (!strncmp (cur_entry->key, + client_name, +strlen (cur_entry->key)))) { +if (cur_entry->flags &XATTR_MAP_FLAG_END_BAD ) { +return -EPERM ; + } +if (cur_entry->flags &XATTR_MAP_FLAG_END_OK ) { +/* Unmodified name */ +return 0 ; + } +if (cur_entry->flags &XATTR_MAP_FLAG_PREFIX ) { + *out_name =g_try_malloc (strlen (client_name) + +strlen (cur_entry->prepend) +1 ); +if (!*out_name) { +return -ENOMEM ; + } +sprintf (*out_name,"%s%s" , cur_entry->prepend, client_name); +return 0 ; + } + } + } + +/* Shouldn't get here - rules should have an END_* */ +abort (); +} +static void lo_getxattr (fuse_req_t req,fuse_ino_t ino,const char *in_name,size_t size) {struct lo_data *lo =lo_data (req);char *value =NULL ;char procname[64 ]; +const char *name; +char *mapped_name;struct lo_inode *inode;ssize_t ret;int saverr;int fd =-1 ; + mapped_name =NULL ; + name = in_name; +if (lo->xattrmap) { + ret =xattr_map_client (in_name, &mapped_name); +if (ret <0 ) { +if (ret == -EPERM ) { + ret = -ENODATA ; + } +fuse_reply_err (req, -ret); +return ; + } +if (mapped_name) { + name = mapped_name; + } + } + inode =lo_inode (req, ino);if (!inode) {fuse_reply_err (req,EBADF ); +g_free (mapped_name);return ; }
@@ -2230,6 +2292,7 @@ out_err: saverr = errno; out:fuse_reply_err (req, saverr); +g_free (mapped_name); goto out_free; }
@@ -2307,19 +2370,35 @@ out: goto out_free; } -static void lo_setxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +static void lo_setxattr (fuse_req_t req,fuse_ino_t ino,const char *in_name,const char *value,size_t size,int flags) {char procname[64 ]; +const char *name; +char *mapped_name;struct lo_data *lo =lo_data (req);struct lo_inode *inode;ssize_t ret;int saverr;int fd =-1 ; + mapped_name =NULL ; + name = in_name; +if (lo->xattrmap) { + ret =xattr_map_client (in_name, &mapped_name); +if (ret <0 ) { +fuse_reply_err (req, -ret); +return ; + } +if (mapped_name) { + name = mapped_name; + } + } + inode =lo_inode (req, ino);if (!inode) {fuse_reply_err (req,EBADF ); +g_free (mapped_name);return ; }
@@ -2354,21 +2433,38 @@ out: }lo_inode_put (lo, &inode); +g_free (mapped_name);fuse_reply_err (req, saverr); } -static void lo_removexattr (fuse_req_t req,fuse_ino_t ino,const char *name) +static void lo_removexattr (fuse_req_t req,fuse_ino_t ino,const char *in_name) {char procname[64 ]; +const char *name; +char *mapped_name;struct lo_data *lo =lo_data (req);struct lo_inode *inode;ssize_t ret;int saverr;int fd =-1 ; + mapped_name =NULL ; + name = in_name; +if (lo->xattrmap) { + ret =xattr_map_client (in_name, &mapped_name); +if (ret <0 ) { +fuse_reply_err (req, -ret); +return ; + } +if (mapped_name) { + name = mapped_name; + } + } + inode =lo_inode (req, ino);if (!inode) {fuse_reply_err (req,EBADF ); +g_free (mapped_name);return ; }
@@ -2403,6 +2499,7 @@ out: }lo_inode_put (lo, &inode); +g_free (mapped_name);fuse_reply_err (req, saverr); }
Signature · 1 line
2.26.2
PATCH 3/3 tools/virtiofsd: xattr name mappings: Map host xattr names
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Map xattr names coming from the host, i.e. listxattr.
--- tools/virtiofsd/passthrough_ll.c | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+)
tools/virtiofsd/passthrough_ll.c +88 -0
@@ -2195,6 +2195,42 @@ static int xattr_map_client(const char *client_name, char **out_name)abort (); } +/* +* For use with listxattr where the host fs gives us a name and we may need +* to sanitize this for the client. +* Returns a pointer to the result in *out_name +* This is always the original string or the current string with some prefix +* removed; no reallocation is done. +* Returns 0 on success +* Can return -ENODATA to indicate the name should be dropped from the list. +*/ +static int xattr_map_host (const char *host_name,const char **out_name) +{ +const XattrMapEntry *cur_entry; +for (cur_entry = xattr_map_list; ; cur_entry++) { +if ((cur_entry->flags &XATTR_MAP_FLAG_HOST ) && + (!strncmp (cur_entry->prepend, + host_name, +strlen (cur_entry->prepend)))) { +if (cur_entry->flags &XATTR_MAP_FLAG_END_BAD ) { +return -ENODATA ; + } +if (cur_entry->flags &XATTR_MAP_FLAG_END_OK ) { + *out_name = host_name; +return 0 ; + } +if (cur_entry->flags &XATTR_MAP_FLAG_PREFIX ) { +/* Remove prefix */ + *out_name = host_name +strlen (cur_entry->prepend); +return 0 ; + } + } + } + +/* Shouldn't get here - rules should have an END_* */ +abort (); +} +static void lo_getxattr (fuse_req_t req,fuse_ino_t ino,const char *in_name,size_t size) {
@@ -2349,8 +2385,60 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)if (ret ==0 ) { goto out; } + +if (lo->xattrmap) { +/* +* Map the names back, some attributes might be dropped, +* some shortened, but not increased, so we shouldn't +* run out of room. +*/ +size_t out_index, in_index; + out_index =0 ; + in_index =0 ; +while (in_index < ret) { +const char *map_out; +char *in_ptr = value + in_index; +/* Length of current attribute name */ +size_t in_len =strlen (value + in_index) +1 ; + +int mapret =xattr_map_host (in_ptr, &map_out); +if (mapret != -ENODATA && mapret !=0 ) { +/* Shouldn't happen */ + saverr = -mapret; + goto out; + } +if (mapret ==0 ) { +/* Either unchanged, or truncated */ +size_t out_len; +if (map_out != in_ptr) { +/* +1 copies the NIL */ + out_len =strlen (map_out) +1 ; + }else { +/* No change */ + out_len = in_len; + } +/* +* Move result along, may still be needed for an unchanged +* entry if a previous entry was changed. +*/ +memmove (value + out_index, map_out, out_len); + + out_index += out_len; + } + in_index += in_len; + } + ret = out_index; +if (ret ==0 ) { + goto out; + } + }fuse_reply_buf (req, value, ret); }else { +/* +* xattrmap only ever shortens the result, +* so we don't need to do anything clever with the +* allocation length here. +*/ fuse_reply_xattr (req, ret); } out_free:
Signature · 1 line
2.26.2