Fix possible buffer overflow in Linux Updater.

Fixes #5227.
This commit is contained in:
John Preston 2018-10-09 12:13:41 +03:00
parent f43752b3ad
commit f9632d5c43

View File

@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <pwd.h>
#include <string>
#include <deque>
#include <vector>
#include <cstring>
#include <cerrno>
#include <algorithm>
@ -23,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
using std::string;
using std::deque;
using std::vector;
using std::cout;
bool do_mkdir(const char *path) { // from http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux
@ -439,43 +441,39 @@ int main(int argc, char *argv[]) {
writeLog("Error: short exe name!");
}
static const int MaxLen = 65536, MaxArgsCount = 128;
auto fullBinaryPath = exePath + exeName;
const auto path = fullBinaryPath.c_str();
char path[MaxLen] = {0};
string fullBinaryPath = exePath + exeName;
strcpy(path, fullBinaryPath.c_str());
char *args[MaxArgsCount] = { 0 };
char p_noupdate[] = "-noupdate";
char p_autostart[] = "-autostart";
char p_debug[] = "-debug";
char p_tosettings[] = "-tosettings";
char p_key[] = "-key";
char p_startintray[] = "-startintray";
char p_testmode[] = "-testmode";
char p_externalupdater[] = "-externalupdater";
char p_workdir[] = "-workdir";
int argIndex = 0;
args[argIndex++] = path;
args[argIndex++] = p_noupdate;
if (autostart) args[argIndex++] = p_autostart;
if (debug) args[argIndex++] = p_debug;
if (startintray) args[argIndex++] = p_startintray;
if (testmode) args[argIndex++] = p_testmode;
if (externalupdater) args[argIndex++] = p_externalupdater;
if (tosettings) args[argIndex++] = p_tosettings;
auto args = vector<const char*>();
const auto push = [&](const char *arg) {
args.push_back(arg);
};
push(path);
push("-noupdate");
if (autostart) push("-autostart");
if (debug) push("-debug");
if (startintray) push("-startintray");
if (testmode) push("-testmode");
if (externalupdater) push("-externalupdater");
if (tosettings) push("-tosettings");
if (key) {
args[argIndex++] = p_key;
args[argIndex++] = key;
push("-key");
push(key);
}
if (customWorkingDir && workdir) {
args[argIndex++] = p_workdir;
args[argIndex++] = workdir;
push("-workdir");
push(workdir);
}
push(nullptr);
pid_t pid = fork();
switch (pid) {
case -1: writeLog("fork() failed!"); return 1;
case 0: execv(path, args); return 1;
case -1:
writeLog("fork() failed!");
return 1;
case 0:
execv(path, args.data());
return 1;
}
writeLog("Executed Telegram, closing log and quitting..");