]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd-network/dhcp-option.c
dhcp-option: refuse control and non-UTF8 characters in string option
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-option.c
index 4025dd36325e2f8b02be52c1878654c09a50404d..4a6fa462f12f88963da60972134af3d614eac662 100644 (file)
@@ -396,27 +396,27 @@ int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t c
 }
 
 int dhcp_option_parse_string(const uint8_t *option, size_t len, char **ret) {
+        _cleanup_free_ char *string = NULL;
         int r;
 
         assert(option);
         assert(ret);
 
-        if (len <= 0)
-                *ret = mfree(*ret);
-        else {
-                char *string;
+        if (len <= 0) {
+                *ret = NULL;
+                return 0;
+        }
 
-                /*
-                 * One trailing NUL byte is OK, we don't mind. See:
-                 * https://github.com/systemd/systemd/issues/1337
-                 */
-                r = make_cstring((const char *) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
-                if (r < 0)
-                        return r;
+        /* One trailing NUL byte is OK, we don't mind. See:
+         * https://github.com/systemd/systemd/issues/1337 */
+        r = make_cstring((const char *) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
+        if (r < 0)
+                return r;
 
-                free_and_replace(*ret, string);
-        }
+        if (!string_is_safe(string) || !utf8_is_valid(string))
+                return -EINVAL;
 
+        *ret = TAKE_PTR(string);
         return 0;
 }