Overview
Features
Download
Documentation
Community
Add-Ons & Services

SecureServer + DialogSocket + Timeout = SIGSEGV

Please post support and help requests here.

SecureServer + DialogSocket + Timeout = SIGSEGV

Postby cristiantm » 07 Feb 2014, 19:45

I might have found a bug on Poco, but I would like to discuss it here first. Maybe I did something that I should not.

I´m using a DialogSocket w/ SSL, using a setup like the example above.

If i set a readtimout to the dialog (or server) socket, when the timeout happens, the server crashes with SIGSEGV

DialogSocket + SSL works fine, DialogSocket + Timeout also works fine, the crash only happens with DialogSocket + SSL + Timeout set.

Compiled with Poco 1.5.2

Code: Select all
stack trace: Thread [2] 6671 [core: 0] (Suspended : Signal : SIGSEGV:Segmentation fault)   
   Poco::Net::DialogSocket::get() at 0x7ffff78ede1b   
   Poco::Net::DialogSocket::receiveLine() at 0x7ffff78ee07f   
   MyConnection::run() at TimeoutDialog.cpp:49 0x4032d8   
   Poco::Net::TCPServerConnection::start() at 0x7ffff79072f7   
   Poco::Net::TCPServerDispatcher::run() at 0x7ffff7916e00   
   Poco::PooledThread::run() at 0x7ffff75c7bb7   
   Poco::ThreadImpl::runnableEntry() at 0x7ffff75c4db9   
   start_thread() at pthread_create.c:311 0x7ffff6317f6e   
   clone() at clone.S:113 0x7ffff6cd79cd   


Code: Select all
#include <iostream>
using namespace std;

#include "Poco/SharedPtr.h"
using Poco::SharedPtr;

#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerConnection.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Net/TCPServerParams.h"
#include "Poco/Net/DialogSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/SSLManager.h"
#include <iostream>
#include "Poco/Net/KeyConsoleHandler.h"
#include "Poco/Net/ConsoleCertificateHandler.h"
#include "Poco/Net/Context.h"
using namespace Poco::Net;

class MyConnection: public TCPServerConnection {
public:
   MyConnection(const StreamSocket& s):
      TCPServerConnection(s)
   {
      std::cout << "MYCON INIT" << std::endl;
   }

   ~MyConnection() {
   }

   void run()
   {
      std::cout << "MYCON RUN" << std::endl;
      try
      {
         DialogSocket ds = DialogSocket(socket());
         std::string command;
         while(ds.receiveMessage(command))
         {
            std::cout << command << std::endl;
         }
         std::cout << "Connection closed" << std::endl;
         ds.close();
      }
      catch (Poco::Exception& exc)
      {
         std::cout << "ERROR:" << exc.what() << std::endl;
      }
   }
};

class MyConnectionFactory: public TCPServerConnectionFactory{
public:
   MyConnectionFactory(): TCPServerConnectionFactory()
   {
      std::cout << "MYCON FACTORY INIT" << std::endl;
   }

   MyConnection* createConnection(const StreamSocket& socket)
   {
      return new MyConnection(socket);
   }
};

int main() {
   try{
   SharedPtr<PrivateKeyPassphraseHandler> pConsoleHandler = new KeyConsoleHandler(true);
    SharedPtr<InvalidCertificateHandler> pInvalidCertHandler = new ConsoleCertificateHandler(true);
    Context::Ptr pContext = new Context(Context::SERVER_USE, "server.pem", "server.pem", "trusted.pem", Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    SSLManager::instance().initializeServer(pConsoleHandler, pInvalidCertHandler, pContext);
   std::cout << "INIT" << std::endl;
   std::cout << "SSL INIT" << std::endl;
   Poco::Net::initializeSSL();
   std::cout << "SOCKET INIT" << std::endl;
   SecureServerSocket svs(12345);
   svs.setReceiveTimeout(Poco::Timespan(0,0,0,10,0));
   std::cout << "SERVER INIT" << std::endl;
   TCPServer* server = new TCPServer(new MyConnectionFactory(), svs);
   server->start();
   Poco::Thread thread;
   thread.start(*server);
   thread.join();
   std::cout << "END" << std::endl;
   Poco::Net::uninitializeSSL();
   }
   catch(std::exception &e)
   {
      std::cout << "ERROR:" << e.what() << std::endl;
   }
   return 0;
}
cristiantm
 
Posts: 11
Joined: 05 Mar 2013, 13:58

Re: SecureServer + DialogSocket + Timeout = SIGSEGV

Postby cristiantm » 19 Feb 2014, 15:14

Looks like this may be related:

https://github.com/pocoproject/poco/issues/334

But Im still wondering if I´m not doing it wrong by using a DialogSocket over a SecureStreamSocket... anyone?
cristiantm
 
Posts: 11
Joined: 05 Mar 2013, 13:58

Re: SecureServer + DialogSocket + Timeout = SIGSEGV

Postby cristiantm » 19 Feb 2014, 16:06

Ok, I´ve decided to try to fix that myself and contribute back to the community a little bit ;)

And a workaround was easy, thanks to the very readable Poco code :)

What is happening:

When there is a timeout, as in the bug report cited, receiveBytes returns -2 (Poco::Net::SecureStreamSocket::ERR_SSL_WANT_READ).

The SEGV is because refill will then "refill" the DialogSocket with -2 bytes, and crash.

I´ve created a issue report on GitHub for it:
https://github.com/pocoproject/poco/issues/380
cristiantm
 
Posts: 11
Joined: 05 Mar 2013, 13:58


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests

cron