【问题标题】:GnuTLS Error when trying to run example尝试运行示例时出现 GnuTLS 错误
【发布时间】:2012-03-02 06:04:38
【问题描述】:

我正在尝试在 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 

问候

【问题讨论】:

  • 在选项中添加 -Wall 和 -Werror 重新编译,以及 -g 为您提供调试符号。然后修复它给你的任何错误,最后,如果这不能修复它,你现在可以使用 gdb 找到失败的确切行。
  • 这是结果:pastebin.com/NEdNwrGn 我看不到任何有用的东西。
  • 你需要向 gdb 索取回溯。
  • 哦,你可能想告诉你的自动线程调试库在双重释放时导致 abort() 以便你可以调试。
  • pastebin.com/SVvXkmNR 我错过了什么吗?我是第一次使用 gdb。

标签: c compilation makefile


【解决方案1】:

"double free" 表示你尝试了两次 free() 一条数据。

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

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

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

【讨论】:

    猜你喜欢
    • 2011-03-31
    • 2014-03-27
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2017-04-04
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多