From 6a8a18c73e73a338283dfbade0a7d83e5cfafe3b Mon Sep 17 00:00:00 2001
From: Norbert Pocs <norbertpocs0@gmail.com>
Date: Tue, 28 Nov 2023 15:27:31 +0100
Subject: [PATCH 9/9] CVE-2023-6004: torture_misc: Add tests for ipv6
 link-local

Signed-off-by: Norbert Pocs <norbertpocs0@gmail.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>

Conflict: NA
Reference:https://git.libssh.org/projects/libssh.git/patch/?id=6a8a18c73e73a338283dfbade0a7d83e5cfafe3b
---
 tests/unittests/torture_misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/tests/unittests/torture_misc.c b/tests/unittests/torture_misc.c
index 073bc54c..f16b766e 100644
--- a/tests/unittests/torture_misc.c
+++ b/tests/unittests/torture_misc.c
@@ -17,7 +17,14 @@
 #include "misc.c"
 #include "error.c"
 
+#ifdef _WIN32
+#include <netioapi.h>
+#else
+#include <net/if.h>
+#endif
+
 #define TORTURE_TEST_DIR "/usr/local/bin/truc/much/.."
+#define TORTURE_IPV6_LOCAL_LINK "fe80::98e1:82ff:fe8d:28b3%%%s"
 
 const char template[] = "temp_dir_XXXXXX";
 
@@ -730,14 +737,27 @@ static void torture_ssh_check_hostname_syntax(void **state)
 
 static void torture_ssh_is_ipaddr(void **state) {
     int rc;
+    char *interf = malloc(64);
+    char *test_interf = malloc(128);
     (void)state;
 
+    assert_non_null(interf);
+    assert_non_null(test_interf);
     rc = ssh_is_ipaddr("201.255.3.69");
     assert_int_equal(rc, 1);
     rc = ssh_is_ipaddr("::1");
     assert_int_equal(rc, 1);
     rc = ssh_is_ipaddr("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
     assert_int_equal(rc, 1);
+    if_indextoname(1, interf);
+    assert_non_null(interf);
+    rc = sprintf(test_interf, TORTURE_IPV6_LOCAL_LINK, interf);
+    /* the "%%s" is not written */
+    assert_int_equal(rc, strlen(interf) + strlen(TORTURE_IPV6_LOCAL_LINK) - 3);
+    rc = ssh_is_ipaddr(test_interf);
+    assert_int_equal(rc, 1);
+    free(interf);
+    free(test_interf);
 
     rc = ssh_is_ipaddr("..");
     assert_int_equal(rc, 0);
-- 
2.33.0

