[master] e7ed02c Enforce proper symbol names for stevedores

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Jun 27 12:03:06 CEST 2017


commit e7ed02c9ae8d4b15053ae1b1e0c043d2326a13cd
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Thu Jun 22 10:34:07 2017 +0200

    Enforce proper symbol names for stevedores
    
    For that a new VCT_invalid_name function is here to complain about
    malformed symbol names. It is used for both storage backends and VCL
    names at the moment and will also be used for named listen addresses
    when it comes to that, and symbols in general for libvcc.
    
    Refs #2325
    Refs #2354

diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 27ee0cb..afe8532 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -116,28 +116,21 @@ mcf_vcl_byname(const char *name)
 static int
 mcf_invalid_vclname(struct cli *cli, const char *name)
 {
-	const char *p;
-	int bad = 0;
+	const char *bad;
 
 	AN(name);
-	p = name;
-	if (!vct_isalpha(*p))
-		bad = *p;
-	for (p++; bad == 0 && *p != '\0'; p++)
-		if (!vct_isalpha(*p) &&
-		    !vct_isdigit(*p) &&
-		    *p != '_' &&
-		    *p != '-')
-			bad = *p;
-	if (bad) {
+	bad = VCT_invalid_name(name, NULL);
+
+	if (bad != NULL) {
 		VCLI_SetResult(cli, CLIS_PARAM);
 		VCLI_Out(cli, "Illegal character in VCL name ");
-		if (bad > 0x20 && bad < 0x7f)
-			VCLI_Out(cli, "('%c')", bad);
+		if (*bad > 0x20 && *bad < 0x7f)
+			VCLI_Out(cli, "('%c')", *bad);
 		else
-			VCLI_Out(cli, "(0x%02x)", bad & 0xff);
+			VCLI_Out(cli, "(0x%02x)", *bad & 0xff);
+		return (-1);
 	}
-	return (bad);
+	return (0);
 }
 
 static struct vclprog *
diff --git a/bin/varnishd/storage/mgt_stevedore.c b/bin/varnishd/storage/mgt_stevedore.c
index b05ecf8..0b0dac2 100644
--- a/bin/varnishd/storage/mgt_stevedore.c
+++ b/bin/varnishd/storage/mgt_stevedore.c
@@ -42,6 +42,7 @@
 
 #include "storage/storage.h"
 #include "vav.h"
+#include "vct.h"
 
 static VTAILQ_HEAD(, stevedore) stevedores =
     VTAILQ_HEAD_INITIALIZER(stevedores);
@@ -189,6 +190,9 @@ STV_Config(const char *spec)
 	if (p == NULL)
 		bprintf(stv->ident, "s%u", seq++);
 	else {
+		if (VCT_invalid_name(spec, p) != NULL)
+			ARGV_ERR("invalid storage name (-s %s)\n", spec);
+		/* XXX: no need for truncation once VSM ident becomes dynamic */
 		l = p - spec;
 		if (l > sizeof stv->ident - 1)
 			l = sizeof stv->ident - 1;
diff --git a/bin/varnishtest/tests/r02325.vtc b/bin/varnishtest/tests/r02325.vtc
new file mode 100644
index 0000000..0c6ad4d
--- /dev/null
+++ b/bin/varnishtest/tests/r02325.vtc
@@ -0,0 +1,5 @@
+varnishtest "validate storage identifiers"
+
+shell -err -expect "Error: invalid storage name (-s ...=malloc)" {
+	varnishd -a :0 -n ${tmpdir} -F -f '' -s ...=malloc
+}
diff --git a/include/vct.h b/include/vct.h
index 5e1af85..86b30cf 100644
--- a/include/vct.h
+++ b/include/vct.h
@@ -44,6 +44,8 @@
 
 extern const uint16_t vct_typtab[256];
 
+const char *VCT_invalid_name(const char *b, const char *e);
+
 static inline int
 vct_is(int x, uint16_t y)
 {
diff --git a/lib/libvarnish/vct.c b/lib/libvarnish/vct.c
index 3ba4046..5610566 100644
--- a/lib/libvarnish/vct.c
+++ b/lib/libvarnish/vct.c
@@ -30,8 +30,11 @@
 
 #include "config.h"
 
+#include <stdlib.h>
 #include <stdint.h>
+#include <string.h>
 
+#include "vas.h"
 #include "vct.h"
 
 /* NB: VCT always operate in ASCII, don't replace 0x0d with \r etc. */
@@ -232,3 +235,25 @@ const uint16_t vct_typtab[256] = {
 	[0xfe]	=	VCT_XMLNAMESTART,
 	[0xff]	=	VCT_XMLNAMESTART,
 };
+
+const char *
+VCT_invalid_name(const char *b, const char *e)
+{
+
+	AN(b);
+	if (e == NULL)
+		e = strchr(b, '\0');
+	assert(b < e);
+
+	if (!vct_isalpha(*b))
+		return (b);
+
+	for (; b < e; b++)
+		if (!vct_isalpha(*b) &&
+		    !vct_isdigit(*b) &&
+		    *b != '_' &&
+		    *b != '-')
+			return (b);
+
+	return (NULL);
+}



More information about the varnish-commit mailing list