-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfix.patch
More file actions
146 lines (144 loc) · 5.36 KB
/
fix.patch
File metadata and controls
146 lines (144 loc) · 5.36 KB
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
diff --git a/lib/src/SharedLibManager.cc b/lib/src/SharedLibManager.cc
index bf67b4f2..7a035a48 100644
--- a/lib/src/SharedLibManager.cc
+++ b/lib/src/SharedLibManager.cc
@@ -17,10 +17,49 @@
#include <dirent.h>
#include <dlfcn.h>
#include <fstream>
+#include <sstream>
#include <sys/types.h>
+#include <sys/wait.h>
#include <trantor/utils/Logger.h>
#include <unistd.h>
+// Safe exec helper: runs a program with explicit argv, no shell involved.
+// Returns the exit status, or -1 on fork/exec failure.
+static int safeExec(const std::vector<std::string> &args)
+{
+ if (args.empty())
+ return -1;
+
+ std::vector<char *> argv;
+ argv.reserve(args.size() + 1);
+ for (auto &a : args)
+ argv.push_back(const_cast<char *>(a.c_str()));
+ argv.push_back(nullptr);
+
+ pid_t pid = fork();
+ if (pid == -1)
+ {
+ perror("fork");
+ return -1;
+ }
+ if (pid == 0)
+ {
+ // Child: replace image with the target program.
+ execvp(argv[0], argv.data());
+ // execvp only returns on error.
+ perror("execvp");
+ _exit(127);
+ }
+ // Parent: wait for child.
+ int status = 0;
+ if (waitpid(pid, &status, 0) == -1)
+ {
+ perror("waitpid");
+ return -1;
+ }
+ return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+}
+
static void forEachFileIn(
const std::string &path,
const std::function<void(const std::string &, const struct stat &)> &cb)
@@ -153,20 +192,18 @@ void SharedLibManager::managerLibs()
else
{
// generate source code and compile it.
- std::string cmd = "drogon_ctl create view ";
- if (!outputPath_.empty())
- {
- cmd.append(filename).append(" -o ").append(
- outputPath_);
- }
- else
- {
- cmd.append(filename).append(" -o ").append(
- libPath);
- }
+ const std::string &outDir =
+ !outputPath_.empty() ? outputPath_ : libPath;
+ std::vector<std::string> genArgs = {"drogon_ctl",
+ "create",
+ "view",
+ filename,
+ "-o",
+ outDir};
srcFile.append(".cc");
- LOG_TRACE << cmd;
- auto r = system(cmd.c_str());
+ LOG_TRACE << "drogon_ctl create view " << filename
+ << " -o " << outDir;
+ auto r = safeExec(genArgs);
// TODO: handle r
(void)(r);
dlStat.handle =
@@ -203,24 +240,45 @@ void *SharedLibManager::compileAndLoadLib(const std::string &sourceFile,
void *oldHld)
{
LOG_TRACE << "src:" << sourceFile;
- std::string cmd = COMPILER_COMMAND;
- cmd.append(" ")
- .append(sourceFile)
- .append(" ")
- .append(COMPILATION_FLAGS)
- .append(" ")
- .append(INCLUDING_DIRS);
- if (std::string(COMPILER_ID).find("Clang") != std::string::npos)
- cmd.append(" -shared -fPIC -undefined dynamic_lookup -o ");
- else
- cmd.append(" -shared -fPIC --no-gnu-unique -o ");
auto pos = sourceFile.rfind('.');
auto soFile = sourceFile.substr(0, pos);
soFile.append(".so");
- cmd.append(soFile);
- LOG_TRACE << cmd;
- if (system(cmd.c_str()) == 0)
+ // Build argv without invoking a shell so that metacharacters in
+ // sourceFile or soFile cannot be interpreted by /bin/sh.
+ std::vector<std::string> compileArgs;
+ compileArgs.push_back(COMPILER_COMMAND);
+
+ // COMPILATION_FLAGS and INCLUDING_DIRS are baked in at build time from
+ // trusted CMake variables; split them on whitespace into separate tokens.
+ auto splitIntoArgs = [&](const std::string &s) {
+ std::istringstream iss(s);
+ std::string token;
+ while (iss >> token)
+ compileArgs.push_back(token);
+ };
+ compileArgs.push_back(sourceFile);
+ splitIntoArgs(COMPILATION_FLAGS);
+ splitIntoArgs(INCLUDING_DIRS);
+ if (std::string(COMPILER_ID).find("Clang") != std::string::npos)
+ {
+ compileArgs.push_back("-shared");
+ compileArgs.push_back("-fPIC");
+ compileArgs.push_back("-undefined");
+ compileArgs.push_back("dynamic_lookup");
+ }
+ else
+ {
+ compileArgs.push_back("-shared");
+ compileArgs.push_back("-fPIC");
+ compileArgs.push_back("--no-gnu-unique");
+ }
+ compileArgs.push_back("-o");
+ compileArgs.push_back(soFile);
+
+ LOG_TRACE << COMPILER_COMMAND << " " << sourceFile << " ... -o " << soFile;
+
+ if (safeExec(compileArgs) == 0)
{
LOG_TRACE << "Compiled successfully:" << soFile;
return loadLib(soFile, oldHld);