Skip to content

Commit

Permalink
Socket buffer size fix (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sami Vänttinen authored and droidmonkey committed Apr 3, 2018
1 parent 0650b30 commit 3a92e4a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/browser/BrowserAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QJsonParseError>
#include "BrowserAction.h"
#include "BrowserSettings.h"
#include "NativeMessagingBase.h"
#include "sodium.h"
#include "sodium/crypto_box.h"
#include "sodium/randombytes.h"
Expand Down Expand Up @@ -456,7 +457,7 @@ QString BrowserAction::encrypt(const QString plaintext, const QString nonce)
std::vector<unsigned char> sk(sa.cbegin(), sa.cend());

std::vector<unsigned char> e;
e.resize(max_length);
e.resize(NATIVE_MSG_MAX_LENGTH);

if (m.empty() || n.empty() || ck.empty() || sk.empty()) {
return QString();
Expand Down Expand Up @@ -484,7 +485,7 @@ QByteArray BrowserAction::decrypt(const QString encrypted, const QString nonce)
std::vector<unsigned char> sk(sa.cbegin(), sa.cend());

std::vector<unsigned char> d;
d.resize(max_length);
d.resize(NATIVE_MSG_MAX_LENGTH);

if (m.empty() || n.empty() || ck.empty() || sk.empty()) {
return QByteArray();
Expand Down
2 changes: 0 additions & 2 deletions src/browser/BrowserService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "gui/DatabaseTabWidget.h"
#include "core/Entry.h"

enum { max_length = 16*1024 };

class BrowserService : public QObject
{
Q_OBJECT
Expand Down
7 changes: 7 additions & 0 deletions src/browser/NativeMessagingBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#include <iostream>
#include <unistd.h>

#ifndef Q_OS_WIN
#include <sys/types.h>
#include <sys/socket.h>
#endif

static const int NATIVE_MSG_MAX_LENGTH = 1024*1024;

class NativeMessagingBase : public QObject
{
Q_OBJECT
Expand Down
8 changes: 7 additions & 1 deletion src/browser/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ void NativeMessagingHost::newLocalConnection()
void NativeMessagingHost::newLocalMessage()
{
QLocalSocket* socket = qobject_cast<QLocalSocket*>(QObject::sender());

if (!socket || socket->bytesAvailable() <= 0) {
return;
}

socket->setReadBufferSize(NATIVE_MSG_MAX_LENGTH);
int socketDesc = socket->socketDescriptor();
if (socketDesc) {
int max = NATIVE_MSG_MAX_LENGTH;
setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, &max, sizeof(max));
}

QByteArray arr = socket->readAll();
if (arr.isEmpty()) {
return;
Expand Down
7 changes: 7 additions & 0 deletions src/proxy/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ NativeMessagingHost::NativeMessagingHost() : NativeMessagingBase()
{
m_localSocket = new QLocalSocket();
m_localSocket->connectToServer(getLocalServerPath());
m_localSocket->setReadBufferSize(NATIVE_MSG_MAX_LENGTH);

int socketDesc = m_localSocket->socketDescriptor();
if (socketDesc) {
int max = NATIVE_MSG_MAX_LENGTH;
setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, &max, sizeof(max));
}
#ifdef Q_OS_WIN
m_running.store(true);
m_future = QtConcurrent::run(this, static_cast<void(NativeMessagingHost::*)()>(&NativeMessagingHost::readNativeMessages));
Expand Down

0 comments on commit 3a92e4a

Please sign in to comment.