Skip to contentSkip to conversation

netns: more input validation

PATCH In review Matteo Croce 3 participants 4 messages Jul 25, 2017 – Jul 27, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netns: more input validation

ip netns accepts invalid input as namespace name like an empty string or a string longer than the maximum file name length.
Check that the netns name is not empty and less than or equal to NAME_MAX.

---
 ip/ipnetns.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
ip/ipnetns.c +2 -1
index 42549944..198e9de8 100644
@@ -768,7 +768,8 @@ static int netns_monitor(int argc, char **argv)
 
 static int invalid_name(const char *name)
 {
-	return strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, "..");
+	return !*name || strlen(name) > NAME_MAX ||
+		strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, "..");
 }
 
 int do_netns(int argc, char **argv)
Signature · 1 line
2.11.0
#1 lore raw

From: Matteo Croce

Quoted 15 lines

Sent: 25 July 2017 14:31
ip netns accepts invalid input as namespace name like an empty string or a string longer than the maximum file name length.
Check that the netns name is not empty and less than or equal to NAME_MAX.

---
 ip/ipnetns.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
ip/ipnetns.c +0 -0
index 42549944..198e9de8 100644

ip/ipnetns.c static int netns_monitor(int argc, char **argv) · line 772

Quoted 6 lines
ip/ipnetns.c +2 -1
@@ -768,7 +768,8 @@ static int netns_monitor(int argc, char **argv)
 
 static int invalid_name(const char *name)
 {
-	return strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, "..");
+	return !*name || strlen(name) > NAME_MAX ||
+		strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, "..");

Think I'd check:

	!name[0] || !memchr(name, 0, NAME_MAX) || strchr(name, '/') ||
		(name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))

	David
#2 lore raw

Il giorno mar, 25/07/2017 alle 13.47 +0000, David Laight ha scritto:

Quoted 6 lines

Think I'd check:

	!name[0] || !memchr(name, 0, NAME_MAX) || strchr(name, '/') ||
		(name[0] == '.' && (!name[1] || (name[1] == '.' &&

!name[2])))

David

Nice optimization, but as strchr() and strcmp() are builtin functions, at least in GCC, I don't know if there is any real advantage.