Skip to content

Commit 066bde3

Browse files
ummakynesJiri Slaby
authored and
Jiri Slaby
committed
netlink: don't compare the nul-termination in nla_strcmp
[ Upstream commit 8b7b932 ] 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]> Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Jiri Slaby <[email protected]>
1 parent 60fe3f6 commit 066bde3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/nlattr.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,15 @@ int nla_memcmp(const struct nlattr *nla, const void *data,
303303
*/
304304
int nla_strcmp(const struct nlattr *nla, const char *str)
305305
{
306-
int len = strlen(str) + 1;
307-
int d = nla_len(nla) - len;
306+
int len = strlen(str);
307+
char *buf = nla_data(nla);
308+
int attrlen = nla_len(nla);
309+
int d;
308310

311+
if (attrlen > 0 && buf[attrlen - 1] == '\0')
312+
attrlen--;
313+
314+
d = attrlen - len;
309315
if (d == 0)
310316
d = memcmp(nla_data(nla), str, len);
311317

0 commit comments

Comments
 (0)