Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zsock_getaddrinfo() returns garbage values if IPv4 address is passed and hints->ai_family == AF_INET6 #18870

Closed
kFYatek opened this issue Sep 3, 2019 · 0 comments · Fixed by #18890
Assignees
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Milestone

Comments

@kFYatek
Copy link

kFYatek commented Sep 3, 2019

Describe the bug
When zsock_getaddrinfo() is passed a node value that is already a valid stringified IPv4 address, but hints->ai_family is configured to AF_INET6, the returned address is unusable.

To Reproduce

  1. Build and run the following application:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(pollout_bug_demo)
target_sources(app PRIVATE main.c)

prj.conf:

CONFIG_NEWLIB_LIBC=y
CONFIG_MAIN_STACK_SIZE=16384
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=y
CONFIG_NET_SOCKETS=y
CONFIG_DNS_RESOLVER=y
CONFIG_DNS_SERVER_IP_ADDRESSES=y
CONFIG_DNS_SERVER1="8.8.8.8"
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"

main.c:

#include <assert.h>
#include <stdio.h>
#include <net/socket.h>

void main(void) {
    struct zsock_addrinfo *result = NULL;
    int ret = zsock_getaddrinfo("192.168.1.1", NULL,
                                &(struct zsock_addrinfo) {
                                    .ai_family = AF_INET6
                                }, &result);
    assert(!ret);
    assert(result);
    assert(result->ai_addr->sa_family == AF_INET6);
    
    char buf[256];
    puts(zsock_inet_ntop(
            AF_INET6,
            &((const struct sockaddr_in6 *) result->ai_addr)->sin6_addr,
            buf, sizeof(buf)));
}
  1. Observe a garbage address being printed out, for example c0a8:101:196:8:5830:8:0:41. Note that the first two segments, c0a8:101, is 192.168.1.1 printed in IPv6 notation. The rest are random bytes from uninitialized memory.

Expected behavior
zsock_getaddrinfo() in this usage shall fail.

If AI_V4MAPPED flag is passed through hints->ai_flags, and if Zephyr supported IPv4-mapped IPv6 addresses, then returning a proper IPv4-mapped IPv6 address would also be acceptable, but AFAIK, Zephyr does not support IPv4-mapped IPv6 addresses.

Impact
If both IPv4 and IPv6 are configured, zsock_getaddrinfo() prefers IPv4 addresses (i.e., returns them first). If an application wants to support IPv4/v6 dual stack, wants to support both domain names and numeric addresses, and wants to prefer IPv6 addresses, one possible approach is to call getaddrinfo() with AF_INET6 hint first, and then fall back to AF_INET if that does not yield any results. This approach does not work on Zephyr because of this bug.

A different approach is necessary as a workaround, e.g. attempting to use inet_pton() first, or using getaddrinfo() without family hint and reordering the results. However, this can be an obstacle when attempting to use existing code that would otherwise be trivially portable to Zephyr.

Environment (please complete the following information):

  • OS: Ubuntu 18.04
  • Toolchain: Zephyr SDK 0.10.3
  • Commit SHA or Version used: aa9b762d12
@kFYatek kFYatek added the bug The issue is a bug, or the PR is fixing a bug label Sep 3, 2019
@jukkar jukkar self-assigned this Sep 3, 2019
jukkar added a commit to jukkar/zephyr that referenced this issue Sep 4, 2019
If we are returned IPv4 address but the hints says IPv6, then
return error as currently we do not support AI_V4MAPPED addresses.
Same check for IPv6 if we want only IPv4 address.

Fixes zephyrproject-rtos#18870

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
@jukkar jukkar added this to the v2.1.0 milestone Sep 4, 2019
@jukkar jukkar added the priority: low Low impact/importance bug label Sep 4, 2019
jukkar added a commit that referenced this issue Sep 11, 2019
If we are returned IPv4 address but the hints says IPv6, then
return error as currently we do not support AI_V4MAPPED addresses.
Same check for IPv6 if we want only IPv4 address.

Fixes #18870

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants