ia64/xen-unstable

changeset 16532:b47849b774f1

xenstore: deprecating but \-quoting binary data.

Presently it's not clear what the allowable character set is for
values in xenstore. The current command-line tools just pass values
to printf("%s",...) so implicitly assume that it's 7-bit printable
ASCII (since the interpretation of 8-bit characters would be unclear).
However there are rumours of programs which dump binary data into
xenstore and/or bugs involving nul bytes being added to the ends of
xenstore values (and even of some drivers insisting on a spurious
nul).

There isn't all that much useful documentation about xenstore. There
is a doc detailing which xenstore keys may be used and what their
meanings are (interface.tex) but it is very out of date, amongst other
reasons because it's in format which is not very easy to update when
adding functionality to the code and because there is no way to check
programs' behaviour in xenstore against the spec. I think the
xenstore part of interface.tex should be replaced with a new document
in a simpler format, which should amonst other things be sufficiently
machine-readable that automatic testing could reveal at least basic
out-of-spec behaviours like setting or using undocumented keys.

This new document ought to specify the allowable character set of both
keys and values, and ought to specify the xenstored protocol as well.

It seems to me that the appropriate character set for xenstore values
is 7-bit printing ASCII (0x20..0x7e). Values should not have a
trailing nul byte `on the wire' but of course the xs library interface
should continue to add an additional nul beyond the quoted length for
the convenience of callers.

That is consistent with nearly all of the existing uses and makes the
whole system much more tractable compared to an explicit expectation
that binary data will be stored. (For example, if we like binary data
in xenstore, why are uuids represented in their printable hex
encoding?) xenstore data is supposedly non-performance-critical
metadata for use by control plane machinery so the overhead of
printing and parsing text strings is hardly a problem.

Applications which set binary values should be deprecated but to avoid
breaking those applications xenstored should continue indefinitely to
be binary-transparent.

Under these circumstances it can only be regarded as a bug that the
current command-line tools are lossy in the presence of binary data.
Not only does this make them break for those now-deprecated uses, but
it also prevents them from being used to detect and debug problems
relating to the exact byte strings being recorded in xenstore.

As a first step towards the utopia I describe above, this patch
causes xenstore-read and -ls to \-escape the values of xenstore
keys, and xenstore-write to un-\-escape them. The escaping is a
subset of that permitted by C89; only \t \r \n \\ and hex and octal
are used and recognised. (So no \f, \a etc.)

This change will not change the representation by these tools of
values which contain only 7-bit printing ASCII characters unless they
contain \'s.

Values which contain \'s will need to be quoted on entry and dequoted
on exit if being manipulated by xenstore-*. The only values likely to
be affected are paths in Windows guest filesystems and in practice we
believe that any such filename which is actually relevant to anything
will be set other than via xenstore-write.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 05 11:05:47 2007 +0000 (2007-12-05)
parents 32237d8517b1
children b6fb8b4dc261
files tools/xenstore/xenstore_client.c tools/xenstore/xs_lib.c tools/xenstore/xs_lib.h tools/xenstore/xsls.c
line diff
     1.1 --- a/tools/xenstore/xenstore_client.c	Wed Dec 05 10:53:47 2007 +0000
     1.2 +++ b/tools/xenstore/xenstore_client.c	Wed Dec 05 11:05:47 2007 +0000
     1.3 @@ -138,19 +138,25 @@ perform(int optind, int argc, char **arg
     1.4  {
     1.5      while (optind < argc) {
     1.6  #if defined(CLIENT_read)
     1.7 -	char *val = xs_read(xsh, xth, argv[optind], NULL);
     1.8 +	struct expanding_buffer ebuf;
     1.9 +	unsigned len;
    1.10 +	char *val = xs_read(xsh, xth, argv[optind], &len);
    1.11  	if (val == NULL) {
    1.12  	    warnx("couldn't read path %s", argv[optind]);
    1.13  	    return 1;
    1.14  	}
    1.15  	if (prefix)
    1.16  	    output("%s: ", argv[optind]);
    1.17 -	output("%s\n", val);
    1.18 +	output("%s\n", sanitise_value(&ebuf, val, len));
    1.19  	free(val);
    1.20  	optind++;
    1.21  #elif defined(CLIENT_write)
    1.22 -	if (!xs_write(xsh, xth, argv[optind], argv[optind + 1],
    1.23 -                      strlen(argv[optind + 1]))) {
    1.24 +	struct expanding_buffer ebuf;
    1.25 +	char *val_spec = argv[optind + 1];
    1.26 +	unsigned len;
    1.27 +	expanding_buffer_ensure(&ebuf, strlen(val_spec)+1);
    1.28 +	unsanitise_value(ebuf.buf, &len, val_spec);
    1.29 +	if (!xs_write(xsh, xth, argv[optind], ebuf.buf, len)) {
    1.30  	    warnx("could not write path %s", argv[optind]);
    1.31  	    return 1;
    1.32  	}
    1.33 @@ -179,9 +185,10 @@ perform(int optind, int argc, char **arg
    1.34              slash = strrchr(p, '/');
    1.35              if (slash) {
    1.36                  char *val;
    1.37 +                unsigned len;
    1.38                  *slash = '\0';
    1.39 -                val = xs_read(xsh, xth, p, NULL);
    1.40 -                if (val && strlen(val) == 0) {
    1.41 +                val = xs_read(xsh, xth, p, &len);
    1.42 +                if (val && len == 0) {
    1.43                      unsigned int num;
    1.44                      char ** list = xs_directory(xsh, xth, p, &num);
    1.45  
     2.1 --- a/tools/xenstore/xs_lib.c	Wed Dec 05 10:53:47 2007 +0000
     2.2 +++ b/tools/xenstore/xs_lib.c	Wed Dec 05 11:05:47 2007 +0000
     2.3 @@ -22,6 +22,7 @@
     2.4  #include <string.h>
     2.5  #include <stdlib.h>
     2.6  #include <errno.h>
     2.7 +#include <assert.h>
     2.8  #include "xs_lib.h"
     2.9  
    2.10  /* Common routines for the Xen store daemon and client library. */
    2.11 @@ -181,3 +182,114 @@ unsigned int xs_count_strings(const char
    2.12  
    2.13  	return num;
    2.14  }
    2.15 +
    2.16 +char *expanding_buffer_ensure(struct expanding_buffer *ebuf, int min_avail)
    2.17 +{
    2.18 +	int want;
    2.19 +	char *got;
    2.20 +
    2.21 +	if (ebuf->avail >= min_avail)
    2.22 +		return ebuf->buf;
    2.23 +
    2.24 +	if (min_avail >= INT_MAX/3)
    2.25 +		return 0;
    2.26 +
    2.27 +	want = ebuf->avail + min_avail + 10;
    2.28 +	got = realloc(ebuf->buf, want);
    2.29 +	if (!got)
    2.30 +		return 0;
    2.31 +
    2.32 +	ebuf->buf = got;
    2.33 +	ebuf->avail = want;
    2.34 +	return ebuf->buf;
    2.35 +}
    2.36 +
    2.37 +char *sanitise_value(struct expanding_buffer *ebuf,
    2.38 +		     const char *val, unsigned len)
    2.39 +{
    2.40 +	int used, remain, c;
    2.41 +	unsigned char *ip;
    2.42 +
    2.43 +#define ADD(c) (ebuf->buf[used++] = (c))
    2.44 +#define ADDF(f,c) (used += sprintf(ebuf->buf+used, (f), (c)))
    2.45 +
    2.46 +	assert(len < INT_MAX/5);
    2.47 +
    2.48 +	ip = (unsigned char *)val;
    2.49 +	used = 0;
    2.50 +	remain = len;
    2.51 +
    2.52 +	if (!expanding_buffer_ensure(ebuf, remain + 1))
    2.53 +		return NULL;
    2.54 +
    2.55 +	while (remain-- > 0) {
    2.56 +		c= *ip++;
    2.57 +
    2.58 +		if (c >= ' ' && c <= '~' && c != '\\') {
    2.59 +			ADD(c);
    2.60 +			continue;
    2.61 +		}
    2.62 +
    2.63 +		if (!expanding_buffer_ensure(ebuf, used + remain + 5))
    2.64 +			/* for "<used>\\nnn<remain>\0" */
    2.65 +			return 0;
    2.66 +
    2.67 +		ADD('\\');
    2.68 +		switch (c) {
    2.69 +		case '\t':  ADD('t');   break;
    2.70 +		case '\n':  ADD('n');   break;
    2.71 +		case '\r':  ADD('r');   break;
    2.72 +		case '\\':  ADD('\\');  break;
    2.73 +		default:
    2.74 +			if (c < 010) ADDF("%03o", c);
    2.75 +			else         ADDF("x%02x", c);
    2.76 +		}
    2.77 +	}
    2.78 +
    2.79 +	ADD(0);
    2.80 +	assert(used <= ebuf->avail);
    2.81 +	return ebuf->buf;
    2.82 +
    2.83 +#undef ADD
    2.84 +#undef ADDF
    2.85 +}
    2.86 +
    2.87 +void unsanitise_value(char *out, unsigned *out_len_r, const char *in)
    2.88 +{
    2.89 +	const char *ip;
    2.90 +	char *op;
    2.91 +	unsigned c;
    2.92 +	int n;
    2.93 +
    2.94 +	for (ip = in, op = out; (c = *ip++); *op++ = c) {
    2.95 +		if (c == '\\') {
    2.96 +			c = *ip++;
    2.97 +
    2.98 +#define GETF(f) do {					\
    2.99 +		        n = 0;				\
   2.100 +                        sscanf(ip, f "%n", &c, &n);	\
   2.101 +			ip += n;			\
   2.102 +		} while (0)
   2.103 +
   2.104 +			switch (c) {
   2.105 +			case 't':              c= '\t';            break;
   2.106 +			case 'n':              c= '\n';            break;
   2.107 +			case 'r':              c= '\r';            break;
   2.108 +			case '\\':             c= '\\';            break;
   2.109 +			case 'x':                    GETF("%2x");  break;
   2.110 +			case '0': case '4':
   2.111 +			case '1': case '5':
   2.112 +			case '2': case '6':
   2.113 +			case '3': case '7':    --ip; GETF("%3o");  break;
   2.114 +			case 0:                --ip;               break;
   2.115 +			default:;
   2.116 +			}
   2.117 +#undef GETF
   2.118 +		}
   2.119 +	}
   2.120 +
   2.121 +	*op = 0;
   2.122 +
   2.123 +	if (out_len_r)
   2.124 +		*out_len_r = op - out;
   2.125 +}
     3.1 --- a/tools/xenstore/xs_lib.h	Wed Dec 05 10:53:47 2007 +0000
     3.2 +++ b/tools/xenstore/xs_lib.h	Wed Dec 05 11:05:47 2007 +0000
     3.3 @@ -67,4 +67,19 @@ bool xs_perm_to_string(const struct xs_p
     3.4  /* Given a string and a length, count how many strings (nul terms). */
     3.5  unsigned int xs_count_strings(const char *strings, unsigned int len);
     3.6  
     3.7 +/* Sanitising (quoting) possibly-binary strings. */
     3.8 +struct expanding_buffer {
     3.9 +	char *buf;
    3.10 +	int avail;
    3.11 +};
    3.12 +
    3.13 +/* Ensure that given expanding buffer has at least min_avail characters. */
    3.14 +char *expanding_buffer_ensure(struct expanding_buffer *, int min_avail);
    3.15 +
    3.16 +/* sanitise_value() may return NULL if malloc fails. */
    3.17 +char *sanitise_value(struct expanding_buffer *, const char *val, unsigned len);
    3.18 +
    3.19 +/* *out_len_r on entry is ignored; out must be at least strlen(in)+1 bytes. */
    3.20 +void unsanitise_value(char *out, unsigned *out_len_r, const char *in);
    3.21 +
    3.22  #endif /* _XS_LIB_H */
     4.1 --- a/tools/xenstore/xsls.c	Wed Dec 05 10:53:47 2007 +0000
     4.2 +++ b/tools/xenstore/xsls.c	Wed Dec 05 11:05:47 2007 +0000
     4.3 @@ -19,6 +19,7 @@ static int desired_width = 60;
     4.4  
     4.5  void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
     4.6  {
     4.7 +    static struct expanding_buffer ebuf;
     4.8      char **e;
     4.9      char newpath[STRING_MAX], *val;
    4.10      int newpath_len;
    4.11 @@ -60,11 +61,13 @@ void print_dir(struct xs_handle *h, char
    4.12          }
    4.13          else {
    4.14              if (max_width < (linewid + len + TAG_LEN)) {
    4.15 -                printf(" = \"%.*s...\"",
    4.16 -                       (int)(max_width - TAG_LEN - linewid), val);
    4.17 +                printf(" = \"%.*s\\...\"",
    4.18 +                       (int)(max_width - TAG_LEN - linewid),
    4.19 +		       sanitise_value(&ebuf, val, len));
    4.20              }
    4.21              else {
    4.22 -                linewid += printf(" = \"%s\"", val);
    4.23 +                linewid += printf(" = \"%s\"",
    4.24 +				  sanitise_value(&ebuf, val, len));
    4.25                  if (show_perms) {
    4.26                      putchar(' ');
    4.27                      for (linewid++;