Description

I'm testing kamailio v 5.6.1 in an alpine linux docker with musl libc (x86_64) Version 1.2.3 and I see a kamailio crash just with version option (kamailio --version).
The crash occur if I haven't localhost resolution in /etc/hosts. This isn't a canonical situation, but crash occur!
Running kamailio with gdb I see that the problem is in freeaddrinfo call in log_init function.

Reproduction

cat /etc/hosts
#127.0.0.1 localhost
#::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
#172.30.0.3 bd9375eeb5af
#2001:db8:1111:2222:0:242:ac1e:3 bd9375eeb5af
/ #
/ # kamailio --version
Segmentation fault (core dumped)

Debugging Data

# gdb --args kamailio --version
GNU gdb (GDB) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-alpine-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from kamailio...
(gdb) run
Starting program: /usr/local/sbin/kamailio --version

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f9f5ed in freeaddrinfo () from /lib/ld-musl-x86_64.so.1
(gdb) bt
#0  0x00007ffff7f9f5ed in freeaddrinfo () from /lib/ld-musl-x86_64.so.1
#1  0x000055555559087d in calc_proc_no () at main.c:1976
#2  0x00007fffffffe8d0 in ?? ()
#3  0x00007fffffffed58 in ?? ()
#4  0x000055555561b143 in log_init () at core/dprint.c:483
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

Possible Solutions

I suppose that log_init function needs a check for info pointer. If info pointer is not NULL, then call freeaddrinfo. I see similar protections in other points of code.

void log_init(void)
{
	struct addrinfo hints;
	struct addrinfo *info = NULL;
	int gai_result;
	char hostname[1024];

	hostname[1023] = '\0';
	gethostname (hostname, 1023);

	memset (&hints, 0, sizeof (hints));
	hints.ai_family = AF_UNSPEC;    /*either IPV4 or IPV6 */
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_CANONNAME;

	if ((gai_result = getaddrinfo (hostname, 0, &hints, &info)) != 0) {
		log_fqdn = "?";
	} else if (info == NULL) {
		log_fqdn = "?";
	} else {
		log_fqdn = strdup (info->ai_canonname);
	}
	
	if(info) // add pointer validity
	   freeaddrinfo (info);

	dprint_init_colors();
}

Additional Information

version: kamailio 5.6.2 (x86_64/linux) 54a9c1
flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLOCKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED
ADAPTIVE_WAIT_LOOPS 1024, MAX_RECV_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: 54a9c1 
compiled on 09:26:43 Nov 15 2022 with gcc 9.3.0


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <kamailio/kamailio/issues/3281@github.com>