尝试运行示例时出现GnuTLS错误

时间:2012-03-01 21:29:57

标签: c compilation makefile

我正在尝试在VPS服务器上运行GnuTLS示例。我收到这个错误:

[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]# 

我是C编程的新手。你能告诉我这意味着什么吗?

p.s这是代码:

/* 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;

}

我使用此命令编译代码

gcc -o server ex-serv-srp.c -lgnutls 

此致

1 个答案:

答案 0 :(得分:1)

“double free”表示您试图两次释放()一段数据。

如果没有看到您正在编译的代码,很难说更多。

作为一般规则:如果你可以使用调试符号编译程序(它现在似乎没有它们)并从程序中获取核心转储,你可以使用gdb来获取堆栈跟踪并找出违规代码的确切行。 (你也可以直接在gdb下运行程序。)

在这种情况下确保你的代码在使用-Wall和-Werror运行gcc时能够干净地编译是很有价值的 - 它们会遇到很多问题。