Skip to contentSkip to conversation

netlink: don't compare the nul-termination in nla_strcmp

PATCH In review Pablo Neira Ayuso 2 participants 2 messages Apr 1, 2014

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

PATCH v2 1/1 netlink: don't compare the nul-termination in nla_strcmp

From: Pablo Neira <[email protected]>

nla_strcmp compares the string length plus one, so it's implicitly including the nul-termination in the comparison.

int nla_strcmp(const struct nlattr *nla, const char *str)

 {
        int len = strlen(str) + 1;
        ...
                d = memcmp(nla_data(nla), str, len);

However, if NLA_STRING is used, userspace can send us a string without the nul-termination. This is a problem since the string comparison will not match as the last byte may be not the nul-termination.

Fix this by skipping the comparison of the nul-termination if the attribute data is nul-terminated. Suggested by Thomas Graf.

Cc Florian Westphal <[email protected]>
Cc Thomas Graf <[email protected]>
---
 lib/nlattr.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/nlattr.c +8 -2
index 18eca78..fc67547 100644
@@ -303,9 +303,15 @@ int nla_memcmp(const struct nlattr *nla, const void *data,
  */
 int nla_strcmp(const struct nlattr *nla, const char *str)
 {
-	int len = strlen(str) + 1;
-	int d = nla_len(nla) - len;
+	int len = strlen(str);
+	char *buf = nla_data(nla);
+	int attrlen = nla_len(nla);
+	int d;
 
+	if (attrlen > 0 && buf[attrlen - 1] == '\0')
+		attrlen--;
+
+	d = attrlen - len;
 	if (d == 0)
 		d = memcmp(nla_data(nla), str, len);
 
Signature · 1 line
1.7.10.4
maintainer
#1 lore raw

From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 1 Apr 2014 19:38:44 +0200

Quoted 16 lines

From: Pablo Neira <[email protected]>

nla_strcmp compares the string length plus one, so it's implicitly including the nul-termination in the comparison.

int nla_strcmp(const struct nlattr *nla, const char *str)

 {
        int len = strlen(str) + 1;
        ...
                d = memcmp(nla_data(nla), str, len);

However, if NLA_STRING is used, userspace can send us a string without the nul-termination. This is a problem since the string comparison will not match as the last byte may be not the nul-termination.

Quoted 6 lines

Fix this by skipping the comparison of the nul-termination if the attribute data is nul-terminated. Suggested by Thomas Graf.

Cc Florian Westphal <[email protected]>
Cc Thomas Graf <[email protected]>

Applied and queued up for -stable, thanks Pablo.