libxl: config parser: warn about arbitrary python
cover libxl: config parser: warn about arbitrary python
In this series we arrange to print a special warning about things in xl config files which look like arbitrary python code.
There are three small patches:
1/3 Fix an unrelated bug which can cause a segfault if config parsing fails 2/3 Move some boilerplate code into subfunctions 3/3 Add the new functionality to the lexer and to those subfunctions
PATCH 1/3 libxl: config parser: do not segfault in some error paths
In error paths, xlu__cfg_set_free can be called on NULL. So check for that rather than segfaulting.
--- tools/libxl/libxlu_cfg.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
tools/libxl/libxlu_cfg.c +1 -0
@@ -93,6 +93,7 @@ int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {void xlu__cfg_set_free (XLU_ConfigSetting *set) {int i; +if (!set)return ;free (set->name);for (i=0 ; i<set->nvalues; i++)free (set->values[i]);
Signature · 1 line
1.5.6.5
PATCH 2/3 libxl: config parser: make CfgParseContext initialisation common
xlu_cfg_readfile and xlu_cfg_readdata had some somewhat-boilerplate code for initialisation, parsing, and cleanup. Make that common.
No functional change.
--- tools/libxl/libxlu_cfg.c | 82 ++++++++++++++++++++++++-------------------- tools/libxl/libxlu_cfg_l.c | 30 ++++++++++++---- tools/libxl/libxlu_cfg_l.h | 18 ++++++++- 3 files changed, 83 insertions(+), 47 deletions(-)
tools/libxl/libxlu_cfg.c +45 -37
@@ -18,59 +18,68 @@ XLU_Config *xlu_cfg_init(FILE *report, const char *report_filename) {return cfg; } +static int ctx_prep (CfgParseContext *ctx,XLU_Config *cfg) { +int e; + + ctx->cfg= cfg; + ctx->err=0 ; + ctx->lexerrlineno=-1 ; + ctx->scanner=0 ; + + e=xlu__cfg_yylex_init_extra (ctx, &ctx->scanner); +if (e) { +fprintf (cfg->report,"%s: unable to create scanner: %s\n" , + cfg->filename,strerror (e)); +return e; + } +return 0 ; +} + +static void ctx_dispose (CfgParseContext *ctx) { +if (ctx->scanner)xlu__cfg_yylex_destroy (ctx->scanner); +} + +static void parse (CfgParseContext *ctx) { +/* On return, ctx.err will be updated with the error status. */ +int r; + r=xlu__cfg_yyparse (ctx); +if (r)assert (ctx->err); +} +int xlu_cfg_readfile (XLU_Config *cfg,const char *real_filename) { +FILE *f =0 ; +int e; +CfgParseContext ctx; -FILE *f; -int e, r; + e =ctx_prep (&ctx, cfg); +if (e) { ctx.err= e; goto xe; } - ctx.cfg= cfg; - ctx.err=0 ; - ctx.lexerrlineno=-1 ; - f=fopen (real_filename,"r" );if (!f) { - e= errno; + ctx.err = errno;fprintf (cfg->report,"%s: unable to open configuration file: %s\n" , real_filename,strerror (e)); -return e; - } - - e=xlu__cfg_yylex_init_extra (&ctx, &ctx.scanner); -if (e) { -fprintf (cfg->report,"%s: unable to create scanner: %s\n" , - cfg->filename,strerror (e)); -return e; + goto xe; }xlu__cfg_yyrestart (f, ctx.scanner); - r=xlu__cfg_yyparse (&ctx); -if (r)assert (ctx.err); +parse (&ctx); -xlu__cfg_yylex_destroy (ctx.scanner); -fclose (f); + xe: +ctx_dispose (&ctx); +if (f)fclose (f);return ctx.err; }int xlu_cfg_readdata (XLU_Config *cfg,const char *data,int length) { -CfgParseContext ctx; -int e, r; +int e;YY_BUFFER_STATE buf=0 ; - ctx.scanner=0 ; - ctx.cfg= cfg; - ctx.err=0 ; - ctx.lexerrlineno=-1 ; - - e=xlu__cfg_yylex_init_extra (&ctx, &ctx.scanner); -if (e) { -fprintf (cfg->report,"%s: unable to create scanner: %s\n" , - cfg->filename,strerror (e)); - ctx.err= e; - ctx.scanner=0 ; - goto xe; - } +CfgParseContext ctx; + e=ctx_prep (&ctx, cfg); +if (e) { ctx.err= e; goto xe; } buf =xlu__cfg_yy_scan_bytes (data, length, ctx.scanner);if (!buf) {
@@ -80,12 +89,11 @@ int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) { goto xe; } - r=xlu__cfg_yyparse (&ctx); -if (r)assert (ctx.err); +parse (&ctx); xe:if (buf)xlu__cfg_yy_delete_buffer (buf, ctx.scanner); -if (ctx.scanner)xlu__cfg_yylex_destroy (ctx.scanner); +ctx_dispose (&ctx);return ctx.err; }
tools/libxl/libxlu_cfg_l.c +22 -8
@@ -54,7 +54,6 @@ typedef int flex_int32_t;typedef unsigned char flex_uint8_t ;typedef unsigned short int flex_uint16_t ;typedef unsigned int flex_uint32_t ; -#endif /* ! C99 */ /* Limits of integral types. */ #ifndef INT8_MIN
@@ -85,6 +84,8 @@ typedef unsigned int flex_uint32_t;#define UINT32_MAX (4294967295U)#endif +#endif /* ! C99 */ +#endif /* ! FLEXINT_H */ #ifdef __cplusplus
@@ -158,7 +159,15 @@ typedef void* yyscan_t;/* Size of default input buffer. */ #ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. +* Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. +* Ditto for the __ia64__ case accordingly. +*/ +#define YY_BUF_SIZE 32768 +#else #define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ #endif /* The state buf must be large enough to hold one state per character in the main buffer.
@@ -487,7 +496,7 @@ int xlu__cfg_yyget_column(yyscan_t yyscanner);void xlu__cfg_yyset_column (int column_no,yyscan_t yyscanner); -#line 491 "libxlu_cfg_l.c" +#line 500 "libxlu_cfg_l.c"#define INITIAL 0#define lexerr 1
@@ -623,7 +632,12 @@ static int input (yyscan_t yyscanner );/* Amount of stuff to slurp up with each read. */ #ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else #define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ #endif /* Copy whatever the last rule matched to the standard output. */
@@ -631,7 +645,7 @@ static int input (yyscan_t yyscanner );/* This used to be an fputs(), but since the string might contain NUL's, * we now use fwrite(). */ -#define ECHO fwrite( yytext, yyleng, 1, yyout ) +#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)#endif /* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@@ -642,7 +656,7 @@ static int input (yyscan_t yyscanner );if (YY_CURRENT_BUFFER_LVALUE ->yy_is_interactive ) \ { \int c ='*' ; \ -int n; \ +size_t n; \for ( n =0 ; n < max_size && \ (c =getc ( yyin )) !=EOF && c !='\n' ; ++n ) \ buf[n] = (char ) c; \
@@ -730,7 +744,7 @@ YY_DECL#line 37 "libxlu_cfg_l.l" -#line 734 "libxlu_cfg_l.c" +#line 748 "libxlu_cfg_l.c" yylval = yylval_param;
@@ -930,7 +944,7 @@ YY_RULE_SETUP#line 82 "libxlu_cfg_l.l"YY_FATAL_ERROR ("flex scanner jammed" ); YY_BREAK -#line 934 "libxlu_cfg_l.c" +#line 948 "libxlu_cfg_l.c"case YY_STATE_EOF (INITIAL ):case YY_STATE_EOF (lexerr):yyterminate ();
@@ -1673,8 +1687,8 @@ YY_BUFFER_STATE xlu__cfg_yy_scan_string (yyconst char * yystr , yyscan_t yyscann/** Setup the input buffer state to scan the given bytes. The next call to xlu__cfg_yylex() will * scan from a @e copy of @a bytes. -* @param bytes the byte buffer to scan -* @param len the number of bytes in the buffer pointed to by @a bytes. +* @param yybytes the byte buffer to scan +* @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. * @param yyscanner The scanner object. * @return the newly allocated buffer state object. */
tools/libxl/libxlu_cfg_l.h +16 -2
@@ -58,7 +58,6 @@ typedef int flex_int32_t;typedef unsigned char flex_uint8_t ;typedef unsigned short int flex_uint16_t ;typedef unsigned int flex_uint32_t ; -#endif /* ! C99 */ /* Limits of integral types. */ #ifndef INT8_MIN
@@ -89,6 +88,8 @@ typedef unsigned int flex_uint32_t;#define UINT32_MAX (4294967295U)#endif +#endif /* ! C99 */ +#endif /* ! FLEXINT_H */ #ifdef __cplusplus
@@ -131,7 +132,15 @@ typedef void* yyscan_t;/* Size of default input buffer. */ #ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. +* Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. +* Ditto for the __ia64__ case accordingly. +*/ +#define YY_BUF_SIZE 32768 +#else #define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ #endif #ifndef YY_TYPEDEF_YY_BUFFER_STATE
@@ -301,7 +310,12 @@ static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner);/* Amount of stuff to slurp up with each read. */ #ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else #define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ #endif /* Number of entries by which start-condition stack grows. */
@@ -338,6 +352,6 @@ extern int xlu__cfg_yylex \#line 82 "libxlu_cfg_l.l" -#line 342 "libxlu_cfg_l.h" +#line 356 "libxlu_cfg_l.h"#undef xlu__cfg_yyIN_HEADER#endif /* xlu__cfg_yyHEADER_H */
Signature · 1 line
1.5.6.5
PATCH 3/3 libxl: config parser: print warning for apparent arbitrary python
The characters - + . ( ) : are not legal in xl config files but are valid Python and use of at least one of them is almost essential for writing arbitrary Python in the config file.
So if we see one of these during lexing, note it, and then after the parse is complete if it failed we print a special extra warning.
Currently this warning refers to the nonexistent wiki page
http://wiki.xen.org/xenwiki/PythonInXlConfig
which will have to be written (and/or given a better name) before the actual 4.1 release.
--- tools/libxl/libxlu_cfg.c | 9 +++ tools/libxl/libxlu_cfg_i.h | 2 + tools/libxl/libxlu_cfg_l.c | 129 ++++++++++++++++++++++------------------- tools/libxl/libxlu_cfg_l.h | 2 +- tools/libxl/libxlu_cfg_l.l | 6 ++ tools/libxl/libxlu_internal.h | 2 +- 6 files changed, 89 insertions(+), 61 deletions(-)
tools/libxl/libxlu_cfg.c +9 -0
@@ -24,6 +24,7 @@ static int ctx_prep(CfgParseContext *ctx, XLU_Config *cfg) { ctx->cfg= cfg; ctx->err=0 ; ctx->lexerrlineno=-1 ; + ctx->likely_python=0 ; ctx->scanner=0 ; e=xlu__cfg_yylex_init_extra (ctx, &ctx->scanner);
@@ -44,6 +45,14 @@ static void parse(CfgParseContext *ctx) {int r; r=xlu__cfg_yyparse (ctx);if (r)assert (ctx->err); + +if (ctx->err && ctx->likely_python) { +fputs ( +"warning: Config file looks like it contains Python code.\n" +"warning: Arbitrary Python is no longer supported.\n" +"warning: See http://wiki.xen.org/xenwiki/PythonInXlConfig\n" , + ctx->cfg->report); + } }int xlu_cfg_readfile (XLU_Config *cfg,const char *real_filename) {
tools/libxl/libxlu_cfg_i.h +2 -0
@@ -16,6 +16,8 @@ char *xlu__cfgl_dequote(CfgParseContext*, const char *src);void xlu__cfg_yyerror (YYLTYPE *locp,CfgParseContext *,char const *msg);void xlu__cfgl_lexicalerror (CfgParseContext *,char const *msg); +void xlu__cfgl_likely_python (CfgParseContext *ctx); +/* Why oh why does bison not declare this in its autogenerated .h ? */
tools/libxl/libxlu_cfg_l.c +70 -59
244 lines not on this page · raw message
tools/libxl/libxlu_cfg_l.h +1 -1
@@ -350,7 +350,7 @@ extern int xlu__cfg_yylex \#undef YY_DECL#endif -#line 82 "libxlu_cfg_l.l" +#line 88 "libxlu_cfg_l.l"#line 356 "libxlu_cfg_l.h"#undef xlu__cfg_yyIN_HEADER
tools/libxl/libxlu_cfg_l.l +6 -0
@@ -64,6 +64,12 @@ void xlu__cfg_yyset_column(int column_no, yyscan_t yyscanner); GOT(STRING); } +[+-.():] { + ctx->likely_python= 1; + BEGIN(lexerr); + yymore(); + } + . { BEGIN(lexerr); yymore();
tools/libxl/libxlu_internal.h +1 -1
@@ -41,7 +41,7 @@ struct XLU_Config {typedef struct {XLU_Config *cfg; -int err, lexerrlineno; +int err, lexerrlineno, likely_python;void *scanner; }CfgParseContext ;
Signature · 1 line
1.5.6.5
Re: [PATCH 2/3] libxl: config parser: make CfgParseContext initialisation common
In reply to Ian JacksonIan Jackson writes ("[PATCH 2/3] libxl: config parser: make CfgParseContext initialisation common"):
Quoted 2 lines
xlu_cfg_readfileandxlu_cfg_readdatahad some somewhat-boilerplate code for initialisation, parsing, and cleanup. Make that common.
This patch mistakenly includes changes to libxlu_cfg_l.[ch] which are due purely to rerunning flex. I did wonder what had become of them.
If this patchset gets suitable acks I will apply these
Quoted 2 lines
tools/libxl/libxlu_cfg_l.c | 30 ++++++++++++---- tools/libxl/libxlu_cfg_l.h | 18 ++++++++-
hunks as a separate commit.
That way the patch 3/3 (or 4/4 if you prefer) contains only changes to libxlu_cfg.[ch] which are actually due to the change to libxl_cfg_l.l.
Ian.