util: add bounds check to util_get_{domain,username}
PATCH v2 1/1 util: add bounds check to util_get_{domain,username}
Two cases were using strcpy, and the other two were using strncpy. Instead all cases can use l_strlcpy which guarentees NULL termination.
--- src/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
src/util.c +4 -4
@@ -173,10 +173,10 @@ const char *util_get_domain(const char *identity)for (c = identity; *c; c++) {switch (*c) {case '\\' : -strncpy (domain, identity, c - identity); +l_strlcpy (domain, identity, c - identity);return domain;case '@' : -strcpy (domain, c +1 ); +l_strlcpy (domain, c +1 ,sizeof (domain));return domain;default :continue ;
@@ -197,10 +197,10 @@ const char *util_get_username(const char *identity)for (c = identity; *c; c++) {switch (*c) {case '\\' : -strcpy (username, c +1 ); +l_strlcpy (username, c +1 ,sizeof (username));return username;case '@' : -strncpy (username, identity, c - identity); +l_strlcpy (username, identity, c - identity);return username;default :continue ;
Signature · 1 line
2.17.1
Hi James,
Quoted 11 lines · James Prestwood
Two cases were using strcpy, and the other two were using strncpy. Instead all cases can use
l_strlcpywhich guarentees NULL termination.--- src/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)src/util.c +0
-0
src/util.c const char *util_get_domain(const char *identity) · line 176
Quoted 5 lines
src/util.c +1
-1@@ -173,10 +173,10 @@ const char *util_get_domain(const char *identity)for (c = identity; *c; c++) {switch (*c) {case '\\' : -strncpy (domain, identity, c - identity); +l_strlcpy (domain, identity, c - identity);
Using l_strlcpy here is wrong. You want to copy a substrict of c - identity bytes into domain where identity does not have any embedded nulls. But passing c - identity into l_strlcpy would not work the way
you think. Also, the existing code has an inherent assumption that c -
identity is always <= 253 bytes. So really, nothing here is dangerous...
If anything we should be using memcpy to make this more clear. If you want to be pedantic, then you need to check that c - identity is < sizeof(domain).
src/util.c const char *util_get_domain(const char *identity) · line 179
Quoted 4 lines
src/util.c +1
-1return domain;case '@' : -strcpy (domain, c +1 ); +l_strlcpy (domain, c +1 ,sizeof (domain));
Here we do have a terminating NULL, so using l_strlcpy is fine.
src/util.c const char *util_get_username(const char *identity) · line 200
Quoted 9 lines
src/util.c +1
-1return domain;default :continue ;@@ -197,10 +197,10 @@ const char *util_get_username(const char *identity)for (c = identity; *c; c++) {switch (*c) {case '\\' : -strcpy (username, c +1 ); +l_strlcpy (username, c +1 ,sizeof (username));
as above
src/util.c const char *util_get_username(const char *identity) · line 203
Quoted 4 lines
src/util.c +1
-1return username;case '@' : -strncpy (username, identity, c - identity); +l_strlcpy (username, identity, c - identity);
The very first comment applies here as well
src/util.c const char *util_get_username(const char *identity) · line 206
Quoted 4 lines
src/util.c +0
-0return username;default :continue ;
Regards,
-Denis
Quoted 31 lines · Denis Kenzior
Hi James,
Quoted 12 lines · James Prestwood
Two cases were using strcpy, and the other two were using strncpy. Instead all cases can use
l_strlcpywhich guarentees NULL termination.--- src/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)src/util.c +0
-0src/util.c const char *util_get_domain(const char · line 177
Quoted 6 lines
src/util.c +1
-1@@ -173,10 +173,10 @@ const char *util_get_domain(const char *identity)for (c = identity; *c; c++) {switch (*c) {case '\\' : -strncpy (domain, identity, c - identity); +l_strlcpy (domain, identity, c - identity);Using
l_strlcpyhere is wrong. You want to copy a substrict of c - identity bytes into domain where identity does not have any embedded nulls. But passing c - identity intol_strlcpywould not work the way
you think. Also, the existing code has an inherent assumption that
c -
identity is always <= 253 bytes. So really, nothing here is dangerous...
Quoted 4 lines
If anything we should be using memcpy to make this more clear. If you
want to be pedantic, then you need to check that c - identity is < sizeof(domain).
Ok, since there is actually a comment explaining the identity length assumption I will just do memcpy and not bother checking the length.
Quoted 7 lines
Quoted 1 line
Regards,
-Denis
_______________________________________________
iwd mailing list -- iwd(a)lists.01.org
To unsubscribe send an email to iwd-leave(a)lists.01.org