-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathGdbLibraryInjector.cpp
195 lines (165 loc) · 5.16 KB
/
GdbLibraryInjector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "GdbLibraryInjector.h"
#include <QtCore/QFileInfo>
#include <QtCore/QLibrary>
#include <QtCore/QProcess>
#include <QtCore/QTextStream>
#include <QtDebug>
#include <dlfcn.h>
void GdbLibraryInjector::logToFile(QProcess* gdbProcess, QTextStream* stream)
{
QFile gdbLogFile("gdb.log");
gdbLogFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream logStream(&gdbLogFile);
logStream << QString("== gdb output for process %1 ==\n").arg(gdbProcess->pid());
logStream << stream->readAll();
}
bool GdbLibraryInjector::readGdbResponse(QTextStream* gdbStream, const QString& command, QStringList* lines)
{
QString commandStart = QString("&\"%1").arg(command);
bool readingCommand = false;
while (true)
{
if (gdbStream->atEnd())
{
if (!gdbStream->device()->waitForReadyRead(5000))
{
qWarning() << "Failed to read stream";
return false;
}
}
QString line = gdbStream->readLine();
if (line.startsWith(commandStart))
{
readingCommand = true;
}
else if (line.startsWith('^') && readingCommand)
{
return true;
}
if (readingCommand)
{
*lines << line;
}
}
}
bool GdbLibraryInjector::startAndInject(const QString& program, const QStringList& args,
const QString& libraryPath, const QString& entryPoint,
int* outPid)
{
TargetInfo target;
target.program = program;
target.args = args;
return inject(target,libraryPath,entryPoint,outPid);
}
bool GdbLibraryInjector::inject(int pid, const QString& libraryPath, const QString& entryPoint)
{
TargetInfo target;
target.pid = pid;
return inject(target,libraryPath,entryPoint,0);
}
bool GdbLibraryInjector::inject(const TargetInfo& target, const QString& libraryPath, const QString& entryPoint,
int* outPid)
{
int pid = target.pid;
QString fullLibraryPath;
QFileInfo libraryInfo(libraryPath);
if (libraryInfo.exists()) {
fullLibraryPath = libraryInfo.absoluteFilePath();
} else {
fullLibraryPath = libraryPath;
}
QLibrary library(fullLibraryPath);
if (!library.load())
{
qWarning() << "Failed to load" << fullLibraryPath << ":" << library.errorString();
return false;
}
QStringList programArgs;
if (target.pid == 0)
{
programArgs << "--args";
programArgs << target.program;
programArgs += target.args;
}
// start gdb in MI mode with .gdbinit commands ignored so
// that we have a clean state to attach to the process, inject the helper
// library and invoke the entry point
QScopedPointer<QProcess> gdbProcess(new QProcess);
QStringList gdbArgs;
gdbArgs << "--interpreter=mi";
gdbArgs << "-nx";
gdbArgs += programArgs;
gdbProcess->start("gdb",gdbArgs);
if (!gdbProcess->waitForStarted())
{
qWarning() << "Failed to start gdb helper to attach to" << pid;
return false;
}
QTextStream gdbStream(gdbProcess.data());
gdbStream << "set auto-solib-add off\n"; // disable automatic shared library loading for
// faster attaching
gdbStream << "set unwindonsignal on\n"; // in the event of the helper causing a segfault
// or uncaught exception,
// roll back to the previous state
gdbStream << "set unwind-on-terminating-exception on\n";
if (target.pid != 0)
{
gdbStream << "attach " << QString::number(pid) << '\n';
}
else
{
gdbStream << "br _start\n";
gdbStream << "run\n";
gdbStream << "info proc\n";
gdbStream.flush();
QStringList procInfo;
if (!readGdbResponse(&gdbStream,"info proc",&procInfo))
{
qWarning() << "Unable to get information from newly minted process";
return false;
}
QRegExp processLine("process ([0-9]+)");
Q_FOREACH(const QString& line, procInfo)
{
if (processLine.indexIn(line) != -1)
{
*outPid = processLine.capturedTexts()[1].toInt();
pid = *outPid;
}
}
// wait for the application to reach the Qt event
// loop before we try to inject the helper library
gdbStream << "sharedlibrary QtCore\n";
gdbStream << "br QCoreApplication::exec\n";
gdbStream << "continue\n";
}
gdbStream << "sharedlibrary libc\n";
gdbStream << "sharedlibrary libdl\n"; // load libdl library for dlopen()
// load the library into the process and invoke the entry point
QString flag_RTLD_NOW = QString::number(RTLD_NOW);
gdbStream << "call dlopen(\"" << fullLibraryPath << "\"," << flag_RTLD_NOW << ")\n";
// call dlerror() so that any problems loading the injected library
// are logged
gdbStream << "print ((char*)(dlerror()))\n";
// load symbols for the helper library so that the entry point can
// be invoked
gdbStream << "sharedlibrary " << fullLibraryPath << '\n';
gdbStream << "call " << entryPoint << "()\n";
gdbStream << "detach\n";
gdbStream << "quit\n";
gdbStream.flush();
if (!gdbProcess->waitForFinished())
{
qWarning() << "Failed to kill gdb helper attached to" << pid;
logToFile(gdbProcess.data(),&gdbStream);
return false;
}
if (gdbProcess->exitStatus() != QProcess::NormalExit)
{
qWarning() << "gdb helper process attached to" << pid << "crashed";
logToFile(gdbProcess.data(),&gdbStream);
return false;
}
logToFile(gdbProcess.data(),&gdbStream);
return true;
}