Skip to contentSkip to conversation

ipaddress: strengthen check on 'label' input

PATCH Superseded Patrick Talbert 2 participants 3 messages May 29, 2018 – Jun 14, 2018

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ipaddress: strengthen check on 'label' input

As mentioned in the ip-address man page, an address label must be equal to the device name or prefixed by the device name followed by a colon. Currently the only check on this input is to see if the device name appears at the beginning of the label string.

This commit adds an additional check to ensure label == dev or continues with a colon.

---
 ip/ipaddress.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
ip/ipaddress.c +19 -2
index 00da14c..fce2008 100644
@@ -2040,6 +2040,22 @@ static bool ipaddr_is_multicast(inet_prefix *a)
 		return false;
 }
 
+static bool is_valid_label(const char *dev, const char *label)
+{
+	char alias[strlen(dev) + 1];
+
+	if (strlen(label) < strlen(dev))
+		return false;
+
+	strcpy(alias, dev);
+	strcat(alias, ":");
+	if (strncmp(label, dev, strlen(dev)) == 0 ||
+	    strncmp(label, alias, strlen(alias)) == 0)
+		return true;
+	else
+		return false;
+}
+
 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
 {
 	struct {
@@ -2174,8 +2190,9 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
 		fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
 		return -1;
 	}
-	if (l && matches(d, l) != 0) {
-		fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
+	if (l && ! is_valid_label(d, l)) {
+		fprintf(stderr, "\"label\" (%s) must match \"dev\" (%s) or be prefixed by"
+			" \"dev\" with a colon.\n", l, d);
 		return -1;
 	}
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 11 lines · On Tue, 29 May 2018 16:57:07 +0200 Patrick Talbert <[email protected]> wrote:

As mentioned in the ip-address man page, an address label must be equal to the device name or prefixed by the device name followed by a colon. Currently the only check on this input is to see if the device name appears at the beginning of the label string.

This commit adds an additional check to ensure label == dev or continues with a colon.

Yes, this looks better but still have some feedback.

Quoted 17 lines
---
 ip/ipaddress.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
ip/ipaddress.c +5 -0
index 00da14c..fce2008 100644
@@ -2040,6 +2040,22 @@ static bool ipaddr_is_multicast(inet_prefix *a)
 		return false;
 }
 
+static bool is_valid_label(const char *dev, const char *label)
+{
+	char alias[strlen(dev) + 1];
+
+	if (strlen(label) < strlen(dev))

ip/ipaddress.c static bool ipaddr_is_multicast(inet_prefix *a) · line 2057

Quoted 10 lines
ip/ipaddress.c +10 -0
+		return false;
+
+	strcpy(alias, dev);
+	strcat(alias, ":");
+	if (strncmp(label, dev, strlen(dev)) == 0 ||
+	    strncmp(label, alias, strlen(alias)) == 0)
+		return true;
+	else
+		return false;
+}

This string copying and comparison still is much more overhead than it needs to be. The following tests out to be equivalent with a single strncmp and strlen.

Why not just:

ip/ipaddress.c +10 -0
index 00da14c6f97c..eac489e94fe4 100644
@@ -2040,6 +2040,16 @@ static bool ipaddr_is_multicast(inet_prefix *a)
 		return false;
 }
 
+static bool is_valid_label(const char *label, const char *dev)
+{
+	size_t len = strlen(dev);
+
+	if (strncmp(label, dev, len) != 0)
+		return false;
+
+	return label[len] == '\0' || label[len] == ':';
+}
+
 
 

Doesn't matter much now, but code seems to get copied.

#2 lore raw
Quoted 58 lines · Stephen Hemminger
Quoted 11 lines · On Tue, 29 May 2018 16:57:07 +0200 Patrick Talbert <[email protected]> wrote:

As mentioned in the ip-address man page, an address label must be equal to the device name or prefixed by the device name followed by a colon. Currently the only check on this input is to see if the device name appears at the beginning of the label string.

This commit adds an additional check to ensure label == dev or continues with a colon.

Yes, this looks better but still have some feedback.

Quoted 17 lines
---
 ip/ipaddress.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
ip/ipaddress.c +5 -0
index 00da14c..fce2008 100644
@@ -2040,6 +2040,22 @@ static bool ipaddr_is_multicast(inet_prefix *a)
              return false;
 }
 
+static bool is_valid_label(const char *dev, const char *label)
+{
+     char alias[strlen(dev) + 1];
+
+     if (strlen(label) < strlen(dev))

ip/ipaddress.c static bool ipaddr_is_multicast(inet_prefix *a) · line 2057

Quoted 10 lines
ip/ipaddress.c +10 -0
+             return false;
+
+     strcpy(alias, dev);
+     strcat(alias, ":");
+     if (strncmp(label, dev, strlen(dev)) == 0 ||
+         strncmp(label, alias, strlen(alias)) == 0)
+             return true;
+     else
+             return false;
+}

This string copying and comparison still is much more overhead than it needs to be. The following tests out to be equivalent with a single strncmp and strlen.

Why not just:

ip/ipaddress.c +0 -0
index 00da14c6f97c..eac489e94fe4 100644
@@ -2040,6 +2040,16 @@ static bool ipaddr_is_multicast(inet_prefix *a)
                return false;
 }
 

ip/ipaddress.c static bool ipaddr_is_multicast(inet_prefix *a) · line 2052

Quoted 10 lines
ip/ipaddress.c +10 -0
+static bool is_valid_label(const char *label, const char *dev)
+{
+       size_t len = strlen(dev);
+
+       if (strncmp(label, dev, len) != 0)
+               return false;
+
+       return label[len] == '\0' || label[len] == ':';
+}
+

Woah. This is way better. v3 coming up....

Thank you for all of your help with this... and by help I mean writing the patch.

Quoted 3 lines

Doesn't matter much now, but code seems to get copied.