Skip to content

Commit

Permalink
Fix memory overrun when adding a single line and b64-encode inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Gauci committed May 17, 2018
1 parent ec125e1 commit b8124da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions htm/HtmServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void HtmServer::run() {
RawSocketUtils::readAll(endpointFd, &uid[0], uid.length());
length -= uid.length();
LOG(ERROR) << "READING FROM " << uid << ":" << length;
string data(length, '\0');
RawSocketUtils::readAll(endpointFd, &data[0], length);
string data;
RawSocketUtils::readB64EncodedLength(endpointFd, &data, length);
LOG(ERROR) << "READ FROM " << uid << ":" << data << " " << length;
state.appendData(uid, data);
break;
Expand Down
4 changes: 3 additions & 1 deletion htm/TerminalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ string TerminalHandler::pollUserTerminal() {
buffer.insert(buffer.end(), tokens.begin(), tokens.end());
} else {
buffer.back().append(tokens.front());
buffer.insert(buffer.end(), tokens.begin() + 1, tokens.end());
if (tokens.size() > 1) {
buffer.insert(buffer.end(), tokens.begin() + 1, tokens.end());
}
}
if (buffer.size() > MAX_BUFFER_LINES) {
int amountToErase = buffer.size() - MAX_BUFFER_LINES;
Expand Down
16 changes: 14 additions & 2 deletions terminal/RawSocketUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ class RawSocketUtils {
static inline void writeB64(int fd, const char* buf, size_t count) {
int encodedLength = base64::Base64::EncodedLength(count);
string s(encodedLength, '\0');
base64::Base64::Encode(buf, count, &s[0], s.length());
if (!base64::Base64::Encode(buf, count, &s[0], s.length())) {
throw runtime_error("b64 decode failed");
}
writeAll(fd, &s[0], s.length());
}

static inline void readB64(int fd, char* buf, size_t count) {
int encodedLength = base64::Base64::EncodedLength(count);
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length());
base64::Base64::Decode((const char*)&s[0], s.length(), buf, count);
if(!base64::Base64::Decode((const char*)&s[0], s.length(), buf, count)) {
throw runtime_error("b64 decode failed");
}
}

static inline void readB64EncodedLength(int fd, string* out, size_t encodedLength) {
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length());
if(!base64::Base64::Decode(s, out)) {
throw runtime_error("b64 decode failed");
}
}

static inline string readMessage(int fd) {
Expand Down

0 comments on commit b8124da

Please sign in to comment.