From 9da08590438f76fdffe4118ad749db5ecbdda105 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 16 Jul 2024 07:39:27 +0200 Subject: [PATCH 1/4] Bugfix: hard casts between "mbed::SocketAdrress *" and "struct sockaddr *" caus memory overwrites in random locations as the size of those structures are not the same. --- src/arch/posix/mbed_tcp.cpp | 53 +++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/arch/posix/mbed_tcp.cpp b/src/arch/posix/mbed_tcp.cpp index 5f940c3..6446a4c 100644 --- a/src/arch/posix/mbed_tcp.cpp +++ b/src/arch/posix/mbed_tcp.cpp @@ -1,5 +1,7 @@ #include "mbed_tcp.h" +#include "PortentaEthernet.h" + int mbed_send(UA_FD fd, const void * data, size_t size, int ignored) { TCPSocket * sock = (TCPSocket *)fd; @@ -82,22 +84,45 @@ int mbed_getnameinfo(struct sockaddr* fd, size_t sa_sz, char* name, size_t host_ return 0; } -int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hints, struct addrinfo** info) { +int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hints, struct addrinfo** info) +{ + if (hostname == NULL) + { + static const char * localhost = "localhost"; + hostname = localhost; + } + + SocketAddress mbed_hints(hostname, atoi(portstr)); + mbed_hints.set_ip_address(Ethernet.localIP().toString().c_str()); + SocketAddress * mbed_res; - if (hostname == NULL) { - static const char* localhost = "localhost"; - hostname = localhost; - static SocketAddress _hints("localhost", atoi(portstr)); - _hints.set_ip_address("127.0.0.1"); - hints = (struct addrinfo*)&_hints; - } + /* rc either holds the number of results uncovered or a negative error code. */ + auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); + if (rc < 0) + { + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) failed with %d", rc); + return UA_STATUSCODE_BAD; + } + + int const addr_cnt = rc; + if (addr_cnt == 0) + { + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) found no addresses"); + return UA_STATUSCODE_BAD; + } + + /* Note: we currently support only a single address result. */ + static struct sockaddr ai_addr; + ai_addr.ai = mbed_res[0]; + + static struct addrinfo res; + memcpy(&res, hints, sizeof(struct addrinfo)); + + res.ai_addr = &ai_addr; + res.ai_next = NULL; + info[0] = &res; - auto ret = NetworkInterface::get_default_instance()->getaddrinfo(hostname, (SocketAddress*)hints, (SocketAddress**)info); - hints->ai_addr = (struct sockaddr*)hints; - hints->ai_next = NULL; - info[0] = (struct addrinfo*)hints; - // Always return 0 - return UA_STATUSCODE_GOOD; + return UA_STATUSCODE_GOOD; } int mbed_listen(UA_FD fd, int ignored) { From 5204ec42b508dd3ccb18d584c082c477de638bae Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 16 Jul 2024 07:44:27 +0200 Subject: [PATCH 2/4] Quickfix: getaddrinfo always returns -3009 - this has been the case for the previous code version too but has been ignored. --- src/arch/posix/mbed_tcp.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/arch/posix/mbed_tcp.cpp b/src/arch/posix/mbed_tcp.cpp index 6446a4c..f3e025b 100644 --- a/src/arch/posix/mbed_tcp.cpp +++ b/src/arch/posix/mbed_tcp.cpp @@ -96,24 +96,25 @@ int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hi mbed_hints.set_ip_address(Ethernet.localIP().toString().c_str()); SocketAddress * mbed_res; - /* rc either holds the number of results uncovered or a negative error code. */ - auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); - if (rc < 0) - { - UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) failed with %d", rc); - return UA_STATUSCODE_BAD; - } - - int const addr_cnt = rc; - if (addr_cnt == 0) - { - UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) found no addresses"); - return UA_STATUSCODE_BAD; - } +// /* rc either holds the number of results uncovered or a negative error code. */ +// auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); +// if (rc < 0) +// { +// UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) failed with %d", rc); +// return UA_STATUSCODE_BAD; +// } +// +// int const addr_cnt = rc; +// if (addr_cnt == 0) +// { +// UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) found no addresses"); +// return UA_STATUSCODE_BAD; +// } /* Note: we currently support only a single address result. */ static struct sockaddr ai_addr; - ai_addr.ai = mbed_res[0]; + //ai_addr.ai = mbed_res[0]; + ai_addr.ai = mbed_hints; static struct addrinfo res; memcpy(&res, hints, sizeof(struct addrinfo)); From e203e51e586f260b7f7cef3dc107ffc7eff7f1b0 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 17 Jul 2024 05:46:46 +0200 Subject: [PATCH 3/4] Revert "Quickfix: getaddrinfo always returns -3009 - this has been the case for the previous code version too but has been ignored." This reverts commit 5204ec42b508dd3ccb18d584c082c477de638bae. --- src/arch/posix/mbed_tcp.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/arch/posix/mbed_tcp.cpp b/src/arch/posix/mbed_tcp.cpp index f3e025b..6446a4c 100644 --- a/src/arch/posix/mbed_tcp.cpp +++ b/src/arch/posix/mbed_tcp.cpp @@ -96,25 +96,24 @@ int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hi mbed_hints.set_ip_address(Ethernet.localIP().toString().c_str()); SocketAddress * mbed_res; -// /* rc either holds the number of results uncovered or a negative error code. */ -// auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); -// if (rc < 0) -// { -// UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) failed with %d", rc); -// return UA_STATUSCODE_BAD; -// } -// -// int const addr_cnt = rc; -// if (addr_cnt == 0) -// { -// UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) found no addresses"); -// return UA_STATUSCODE_BAD; -// } + /* rc either holds the number of results uncovered or a negative error code. */ + auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); + if (rc < 0) + { + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) failed with %d", rc); + return UA_STATUSCODE_BAD; + } + + int const addr_cnt = rc; + if (addr_cnt == 0) + { + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "NetworkInterface::get_default_instance()->getaddrinfo(...) found no addresses"); + return UA_STATUSCODE_BAD; + } /* Note: we currently support only a single address result. */ static struct sockaddr ai_addr; - //ai_addr.ai = mbed_res[0]; - ai_addr.ai = mbed_hints; + ai_addr.ai = mbed_res[0]; static struct addrinfo res; memcpy(&res, hints, sizeof(struct addrinfo)); From a4cf7abc1d34e4318d5655b1d1333d933db8cdea Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 17 Jul 2024 05:55:24 +0200 Subject: [PATCH 4/4] Quickfix: bypass faulty DNS by using default values for localhost. --- src/arch/posix/mbed_tcp.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/arch/posix/mbed_tcp.cpp b/src/arch/posix/mbed_tcp.cpp index 6446a4c..db232f9 100644 --- a/src/arch/posix/mbed_tcp.cpp +++ b/src/arch/posix/mbed_tcp.cpp @@ -86,16 +86,35 @@ int mbed_getnameinfo(struct sockaddr* fd, size_t sa_sz, char* name, size_t host_ int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hints, struct addrinfo** info) { + static struct sockaddr ai_addr; + static struct addrinfo res; + bool is_localhost = false; + if (hostname == NULL) { static const char * localhost = "localhost"; hostname = localhost; + is_localhost = true; } SocketAddress mbed_hints(hostname, atoi(portstr)); mbed_hints.set_ip_address(Ethernet.localIP().toString().c_str()); SocketAddress * mbed_res; + /* Bypass faulty DNS lookup on localhost. */ + if (is_localhost) + { + ai_addr.ai = mbed_hints; + + memcpy(&res, hints, sizeof(struct addrinfo)); + + res.ai_addr = &ai_addr; + res.ai_next = NULL; + info[0] = &res; + + return UA_STATUSCODE_GOOD; + } + /* rc either holds the number of results uncovered or a negative error code. */ auto rc = NetworkInterface::get_default_instance()->getaddrinfo(hostname, &mbed_hints, &mbed_res); if (rc < 0) @@ -112,10 +131,8 @@ int mbed_addrinfo(const char* hostname, const char* portstr, struct addrinfo* hi } /* Note: we currently support only a single address result. */ - static struct sockaddr ai_addr; ai_addr.ai = mbed_res[0]; - static struct addrinfo res; memcpy(&res, hints, sizeof(struct addrinfo)); res.ai_addr = &ai_addr;