Reputation: 1626
I'm trying to run GnuTLS example on VPS server. I get this error:
[root@localhost test]# ./server
Echo Server ready. Listening to port '5556'.
- connection from 127.0.0.1, port 38140
*** glibc detected *** ./server: double free or corruption (fasttop): 0x09a9e5c0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6ea31)[0xc32a31]
/usr/local/lib/libgnutls.so.28(+0x90bd9)[0x6dbbd9]
/usr/local/lib/libgnutls.so.28(+0x9095a)[0x6db95a]
/usr/local/lib/libgnutls.so.28(+0x1e878)[0x669878]
/usr/local/lib/libgnutls.so.28(+0x1c1a8)[0x6671a8]
/usr/local/lib/libgnutls.so.28(gnutls_handshake+0x47)[0x667937]
./server[0x8049040]
/lib/libc.so.6(__libc_start_main+0xe6)[0xbdace6]
./server[0x8048c51]
======= Memory map: ========
00452000-00469000 r-xp 00000000 fd:00 131073 /lib/libpthread-2.12.so
00469000-0046a000 r--p 00016000 fd:00 131073 /lib/libpthread-2.12.so
0046a000-0046b000 rw-p 00017000 fd:00 131073 /lib/libpthread-2.12.so
0046b000-0046d000 rw-p 00000000 00:00 0
0062b000-0062c000 r-xp 00000000 00:00 0 [vdso]
0064b000-00733000 r-xp 00000000 fd:00 18887 /usr/local/lib/libgnutls.so.28.3.0
00733000-00739000 rw-p 000e7000 fd:00 18887 /usr/local/lib/libgnutls.so.28.3.0
00947000-0094e000 r-xp 00000000 fd:00 131077 /lib/librt-2.12.so
0094e000-0094f000 r--p 00006000 fd:00 131077 /lib/librt-2.12.so
0094f000-00950000 rw-p 00007000 fd:00 131077 /lib/librt-2.12.so
00a64000-00a82000 r-xp 00000000 fd:00 131042 /lib/ld-2.12.so
00a82000-00a83000 r--p 0001d000 fd:00 131042 /lib/ld-2.12.so
00a83000-00a84000 rw-p 0001e000 fd:00 131042 /lib/ld-2.12.so
00bc4000-00d4d000 r-xp 00000000 fd:00 131049 /lib/libc-2.12.so
00d4d000-00d4e000 ---p 00189000 fd:00 131049 /lib/libc-2.12.so
00d4e000-00d50000 r--p 00189000 fd:00 131049 /lib/libc-2.12.so
00d50000-00d51000 rw-p 0018b000 fd:00 131049 /lib/libc-2.12.so
00d51000-00d54000 rw-p 00000000 00:00 0
00f13000-00f5f000 r-xp 00000000 fd:00 4823 /usr/lib/sse2/libgmp.so.3.5.0
00f5f000-00f62000 rw-p 0004b000 fd:00 4823 /usr/lib/sse2/libgmp.so.3.5.0
00fbf000-00fdc000 r-xp 00000000 fd:00 131015 /lib/libgcc_s-4.4.6-20110824.so.1
00fdc000-00fdd000 rw-p 0001d000 fd:00 131015 /lib/libgcc_s-4.4.6-20110824.so.1
08048000-0804a000 r-xp 00000000 fd:00 281891 /opt/test/server
0804a000-0804b000 rw-p 00001000 fd:00 281891 /opt/test/server
09a91000-09ab2000 rw-p 00000000 00:00 0 [heap]
b78ea000-b78ec000 rw-p 00000000 00:00 0
b78f0000-b78f5000 rw-p 00000000 00:00 0
bffc6000-bffdb000 rw-p 00000000 00:00 0 [stack]
Aborted
[root@localhost test]#
I'm new to C programming. Can you tell me what this means?
p.s this is the code:
/* This example code is placed in the public domain. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <gnutls/gnutls.h>
#define SRP_PASSWD "/opt/test/tpasswd"
#define SRP_PASSWD_CONF "/opt/test/tpasswd.conf"
#define KEYFILE "/opt/test/key.pem"
#define CERTFILE "/opt/test/cert.pem"
#define CAFILE "/etc/ssl/certs/ca-certificates.crt"
/* This is a sample TLS-SRP echo server.
*/
#define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
#define MAX_BUF 1024
#define PORT 5556 /* listen to 5556 port */
/* These are global */
gnutls_srp_server_credentials_t srp_cred;
gnutls_certificate_credentials_t cert_cred;
static gnutls_session_t
initialize_tls_session (void)
{
gnutls_session_t session;
gnutls_init (&session, GNUTLS_SERVER);
gnutls_priority_set_direct (session, "NORMAL:-KX-ALL:+SRP:+SRP-DSS:+SRP-RSA", NULL);
gnutls_credentials_set (session, GNUTLS_CRD_SRP, srp_cred);
/* for the certificate authenticated ciphersuites.
*/
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cert_cred);
/* request client certificate if any.
*/
gnutls_certificate_server_set_request (session, GNUTLS_CERT_IGNORE);
return session;
}
int
main (void)
{
int err, listen_sd;
int sd, ret;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
socklen_t client_len;
char topbuf[512];
gnutls_session_t session;
char buffer[MAX_BUF + 1];
int optval = 1;
char name[256];
strcpy (name, "Echo Server");
gnutls_global_init ();
/* SRP_PASSWD a password file (created with the included srptool utility)
*/
gnutls_srp_allocate_server_credentials (&srp_cred);
gnutls_srp_set_server_credentials_file (srp_cred, SRP_PASSWD,
SRP_PASSWD_CONF);
gnutls_certificate_allocate_credentials (&cert_cred);
gnutls_certificate_set_x509_trust_file (cert_cred, CAFILE,
GNUTLS_X509_FMT_PEM);
gnutls_certificate_set_x509_key_file (cert_cred, CERTFILE, KEYFILE,
GNUTLS_X509_FMT_PEM);
/* TCP socket operations
*/
listen_sd = socket (AF_INET, SOCK_STREAM, 0);
SOCKET_ERR (listen_sd, "socket");
memset (&sa_serv, '\0', sizeof (sa_serv));
sa_serv.sin_family = AF_INET;
sa_serv.sin_addr.s_addr = INADDR_ANY;
sa_serv.sin_port = htons (PORT); /* Server Port number */
setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
sizeof (int));
err = bind (listen_sd, (struct sockaddr *) & sa_serv, sizeof (sa_serv));
SOCKET_ERR (err, "bind");
err = listen (listen_sd, 1024);
SOCKET_ERR (err, "listen");
printf ("%s ready. Listening to port '%d'.\n\n", name, PORT);
client_len = sizeof (sa_cli);
for (;;)
{
session = initialize_tls_session ();
sd = accept (listen_sd, (struct sockaddr *) & sa_cli, &client_len);
printf ("- connection from %s, port %d\n",
inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
sizeof (topbuf)), ntohs (sa_cli.sin_port));
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
do
{
ret = gnutls_handshake (session);
}
while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
if (ret < 0)
{
close (sd);
gnutls_deinit (session);
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
gnutls_strerror (ret));
continue;
}
printf ("- Handshake was completed\n");
printf ("- User %s was connected\n", gnutls_srp_server_get_username(session));
/* print_info(session); */
for (;;)
{
memset (buffer, 0, MAX_BUF + 1);
ret = gnutls_record_recv (session, buffer, MAX_BUF);
if (ret == 0)
{
printf ("\n- Peer has closed the GnuTLS connection\n");
break;
}
else if (ret < 0)
{
fprintf (stderr, "\n*** Received corrupted "
"data(%d). Closing the connection.\n\n", ret);
break;
}
else if (ret > 0)
{
/* echo data back to the client
*/
gnutls_record_send (session, buffer, strlen (buffer));
}
}
printf ("\n");
/* do not wait for the peer to close the connection. */
gnutls_bye (session, GNUTLS_SHUT_WR);
close (sd);
gnutls_deinit (session);
}
close (listen_sd);
gnutls_srp_free_server_credentials (srp_cred);
gnutls_certificate_free_credentials (cert_cred);
gnutls_global_deinit ();
return 0;
}
I use this command to compile the code
gcc -o server ex-serv-srp.c -lgnutls
Regards
Upvotes: 0
Views: 648
Reputation: 4495
"double free" means you have attempted to free() a piece of data twice.
Without seeing the code you are compiling it is hard to say more.
As a general rule: if you can compile your program with debugging symbols (it doesn't have them right now it seems) and get a core dump out of the program you can use gdb to get a stack trace and find out the exact line where the offending piece of code is. (you can also just run the program directly under gdb).
It is also of value in such instances to make sure your code compiles cleanly when you run gcc with -Wall and -Werror -- they catch lots of problems.
Upvotes: 1