Memory leak

d d.avion at slmail.me
Mon Jul 20 16:23:41 UTC 2020


No memory leakage, if I comment out all SSL related constructs from my code.
Thus, the SSL related code parts seem to be the cause of this leak.
What is the issue here?
Makefile:

CC=clang
CFLAGS=-g -Wextra -Wall -Werror -pedantic -std=c89 -lssl -fsanitize=address -fno-omit-frame-pointer

.PHONY: clean

test: main.c
$(CC) $(CFLAGS) $^ -o $@

clean:
rm -rf test

Code:

#define _DEFAULT_SOURCE
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <openssl/ssl.h>

int main(int argc, char **argv)
{
int sfd = 0;
int ret = 0;
struct addrinfo *result = NULL;
struct addrinfo *rp = NULL;
SSL_CTX *ssl_ctx = NULL;
SSL *ssl = NULL;

/* Socket initialization and connection */
sfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sfd != -1);
{
const unsigned int min_argc = 4;
assert(argc == min_argc);
}
ret = getaddrinfo(argv[1], argv[2], NULL, &result);
assert(ret != -1);

for (rp = result; rp != NULL; rp = rp->ai_next) {
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
}

/* SSL initialization */
SSL_load_error_strings();
SSL_library_init();
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
ssl = SSL_new(ssl_ctx);
SSL_set_fd(ssl, sfd);
ret = SSL_connect(ssl);
assert(ret != -1);

/* HTTP requests */
{
const size_t buf_sz = atoi(argv[3]);
const unsigned int max = 30;
char *buf = (char *)calloc(buf_sz + 1, sizeof(char));
strncat(buf, "GET / HTTP/1.1\r\nHost: ", max);
strncat(buf, argv[1], max);
strncat(buf, "\r\n\r\n", max);
ret = SSL_write(ssl, buf, strlen(buf) + 1);
assert(ret != -1);
free(buf);
}


{
const size_t buf_sz = atoi(argv[3]);
char *buf = (char *)calloc(buf_sz + 1, sizeof(char));
assert(buf);
ret = SSL_read(ssl, buf, buf_sz);
assert(ret != -1);
printf("%s\n", buf);
free(buf);
}

/* Clean up */
SSL_free(ssl);
close(sfd);
return 0;
}
Program output:

user at user-ubuntu:~/projects/https$ ./test finance.yahoo.com 443 1024
HTTP/1.1 200 OK
Referrer-Policy: no-referrer-when-downgrade
Strict-Transport-Security: max-age=15552000
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: sandbox allow-downloads allow-forms allow-modals allow-same-origin allow-scripts allow-popups allow-popups-to-escape-sandbox allow-top-navigation-by-user-activation allow-presentation;
Content-Type: text/html; charset=utf-8
Set-Cookie: B=8mraj0lfhbaol&b=3&s=r7; expires=Mon, 20-Jul-2021 14:32:53 GMT; path=/; domain=.yahoo.com
Date: Mon, 20 Jul 2020 14:32:53 GMT
Server: ATS
Cache-Control: max-age=0, private
Expires: -1
Age: 0
Transfer-Encoding: chunked
Connection: keep-alive
Expect-CT: max-age=31536000, report-uri="http://csp.yahoo.com/beacon/csp?src=yahoocom-expect-ct-report-only"
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff



=================================================================
==28089==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1024 byte(s) in 1 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7cf88cd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x17b8cd)
    #2 0x7f59b7e7a0b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

Direct leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7f59be9 in gaih_inet /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:1058:18
    #2 0x7f59b7f5bf48 in getaddrinfo /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:2256:12
    #3 0x442e3a in getaddrinfo (/home/user/projects/https/test+0x442e3a)
    #4 0x4c349c in main /home/user/projects/https/main.c:30:11
    #5 0x7f59b7e7a0b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 1712 byte(s) in 19 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7cf88cd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x17b8cd)

Indirect leak of 504 byte(s) in 1 object(s) allocated from:
    #0 0x493e09 in realloc (/home/user/projects/https/test+0x493e09)
    #1 0x7f59b7d5b464  (/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x1de464)

Indirect leak of 504 byte(s) in 1 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7d5ba48 in OPENSSL_sk_dup (/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x1dea48)

Indirect leak of 456 byte(s) in 6 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7f59a59 in gaih_inet /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:1058:18
    #2 0x7f59b7f5bf48 in getaddrinfo /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:2256:12
    #3 0x442e3a in getaddrinfo (/home/user/projects/https/test+0x442e3a)
    #4 0x4c349c in main /home/user/projects/https/main.c:30:11
    #5 0x7f59b7e7a0b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 320 byte(s) in 5 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7f59be9 in gaih_inet /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:1058:18
    #2 0x7f59b7f5bf48 in getaddrinfo /build/glibc-YYA7BZ/glibc-2.31/posix/../sysdeps/posix/getaddrinfo.c:2256:12
    #3 0x442e3a in getaddrinfo (/home/user/projects/https/test+0x442e3a)
    #4 0x4c349c in main /home/user/projects/https/main.c:30:11
    #5 0x7f59b7e7a0b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x493aed in malloc (/home/user/projects/https/test+0x493aed)
    #1 0x7f59b7d5b9f3 in OPENSSL_sk_dup (/lib/x86_64-linux-gnu/libcrypto.so.1.1+0x1de9f3)

SUMMARY: AddressSanitizer: 4616 byte(s) leaked in 35 allocation(s).


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mta.openssl.org/pipermail/openssl-users/attachments/20200720/5a532566/attachment-0001.html>


More information about the openssl-users mailing list