-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathflake.nix
More file actions
301 lines (262 loc) · 11.5 KB
/
Copy pathflake.nix
File metadata and controls
301 lines (262 loc) · 11.5 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
inputs = {
# pinned on fix for https://github.com/NixOS/nixpkgs/pull/166977
nixpkgs.url = "github:peperunas/nixpkgs/llvm-i686-cross-fix";
flake-utils.url = "github:numtide/flake-utils";
librop-git = { url = "github:ropfuscator/librop"; };
ropfuscator-utils = {
url = "github:ropfuscator/utilities";
flake = false;
};
fmt = {
url = "github:fmtlib/fmt/7bdf0628";
flake = false;
};
tinytoml = {
url = "github:mayah/tinytoml/ea34092";
flake = false;
};
};
outputs = { self, nixpkgs, flake-utils, librop-git, ropfuscator-utils
, tinytoml, fmt }:
flake-utils.lib.eachDefaultSystem (system:
let
zlib-fix = import ./zlib-fix.nix;
ropfuscatorLibcOverlay = (self: super: {
stdenv = super.overrideCC super.stdenv (super.stdenv.cc.override
(old: {
extraBuildCommands = old.extraBuildCommands
+ "echo '-mllvm --ropfuscator-library=${self.glibc}/lib/libc.so.6' >> $out/nix-support/cc-cflags";
}));
});
ropfuscatorLibropOverlay = (self: super:
let librop-path = "${librop}/lib/librop.so";
in {
stdenv = super.overrideCC super.stdenv (super.stdenv.cc.override
(old: {
extraBuildCommands = old.extraBuildCommands
+ "echo '-mllvm --ropfuscator-library=${librop-path} ${librop-path}' >> $out/nix-support/cc-cflags";
}));
});
localSystem = { inherit system; };
crossSystem = {
config = "i686-unknown-linux-gnu";
linker = "bfd";
useLLVM = true;
};
# vanilla upstream nix packages
pkgs = import nixpkgs {
inherit localSystem crossSystem;
overlays = [ zlib-fix ];
};
# upstream nix packages that use ROPfuscator as default compiler
# the pass is disabled, though, because no library is defined
pkgsRopfuscator = import nixpkgs {
inherit localSystem crossSystem;
overlays = [
zlib-fix
(import ./ropfuscator.nix { inherit tinytoml fmt lib; })
];
};
# upstream nix packages that use ROPfuscator as default compiler
# with libc as default library
pkgsRopfuscatorLibc = import nixpkgs {
inherit localSystem crossSystem;
overlays = [
zlib-fix
(import ./ropfuscator.nix { inherit tinytoml fmt lib; })
];
crossOverlays = [ ropfuscatorLibcOverlay ];
};
# upstream nix packages that use ROPfuscator as default compiler
# with librop as default library
pkgsRopfuscatorLibrop = import nixpkgs {
inherit localSystem crossSystem;
overlays = [
zlib-fix
(import ./ropfuscator.nix { inherit tinytoml fmt lib; })
];
crossOverlays = [ ropfuscatorLibropOverlay ];
};
lib = nixpkgs.lib;
librop = librop-git.packages.${system}.librop;
# helper functions
benchmarkPhases = { deriv }:
let
obfuscation_stats_file_header = "ropfuscator_obfuscation_stats";
ropfuscator_dir = "$TMPDIR/ropfuscator";
performance_stats_file =
"${ropfuscator_dir}/ropfuscator_performance_stats.log";
aggregated_obfuscation_stats_file =
"${ropfuscator_dir}/ropfuscator_obfuscation_stats-aggregated.log";
in deriv.overrideAttrs (old: {
nativeBuildInputs = (old.nativeBuildInputs or [ ])
++ [ pkgs.bc pkgs.datamash ];
preConfigure = ''
if [ ! -d ${ropfuscator_dir} ]; then
echo "[*] Creating ROPfuscator directory in output store"
mkdir -p ${ropfuscator_dir}
fi
echo "[*] Touching performance statistics file"
touch ${performance_stats_file}
export ROPFUSCATOR_CONFIGURE_START=`date +%s.%N`
'' + (old.preConfigure or "");
postConfigure = ''
export ROPFUSCATOR_CONFIGURE_END=`date +%s.%N`
export ROPFUSCATOR_CONFIGURE_DURATION=`echo "$ROPFUSCATOR_CONFIGURE_END - $ROPFUSCATOR_CONFIGURE_START" | bc`
echo "[*] Writing configure phase duration into performance statistics file"
printf "CONFIGURE_DURATION = %.3f\n" $ROPFUSCATOR_CONFIGURE_DURATION >> ${performance_stats_file}
'' + (old.postConfigure or "");
preBuild = ''
export ROPFUSCATOR_BUILD_START=`date +%s.%N`
'' + (old.preBuild or "");
postBuild = ''
export ROPFUSCATOR_BUILD_END=`date +%s.%N`
export ROPFUSCATOR_BUILD_DURATION=`echo "$ROPFUSCATOR_BUILD_END - $ROPFUSCATOR_BUILD_START" | bc`
echo "[*] Writing build time into performance statistics file"
printf "BUILD_DURATION = %.3f\n" $ROPFUSCATOR_BUILD_DURATION >> ${performance_stats_file}
'' + (old.postBuild or "");
preCheck = ''
# allow phase to fail
set +e
export ROPFUSCATOR_CHECK_START=`date +%s.%N`
'' + (old.preCheck or "");
postCheck = ''
set -e
export ROPFUSCATOR_CHECK_END=`date +%s.%N`
export ROPFUSCATOR_CHECK_DURATION=`echo "$ROPFUSCATOR_CHECK_END - $ROPFUSCATOR_CHECK_START" | bc`
echo "[*] Writing check phase duration into performance statistics file"
printf "CHECK_DURATION = %.3f\n" $ROPFUSCATOR_CHECK_DURATION >> ${performance_stats_file}
'' + (old.postCheck or "");
postInstall = ''
# find and move obfuscation stats into ropfuscator out folder
find /build -type f -name "${obfuscation_stats_file_header}*" -exec sh -c "([[ ! -f ${ropfuscator_dir}/tmp ]] && cat {} > ${ropfuscator_dir}/tmp || tail -n +2 {} >> ${ropfuscator_dir}/tmp) && mv {} ${ropfuscator_dir}" \;
# process and prettify obfuscation stats, if present
if [ -f ${ropfuscator_dir}/tmp ]; then cat ${ropfuscator_dir}/tmp | (sed -u 1q; sort) | datamash -HW groupby 1 sum 2,3,4,5,6,7,8,9 | tr "\\t" "," > ${aggregated_obfuscation_stats_file} && rm ${ropfuscator_dir}/tmp; fi
echo "[*] Moving ROPfuscator stats folder to output"
mv "${ropfuscator_dir}" $out
'' + (old.postInstall or "");
});
forceTests = { deriv }:
# since ROPfuscator works only on x86_32,
# we should execute the tests only if the host
# is x86
if (pkgs.stdenv.hostPlatform.isx86_32 or pkgs.stdenv.hostPlatform.isx86_64) then
deriv.overrideAttrs (old: {
# forcing the derivation to run tests (if any)
doCheck = true;
# allow phase to fail
preCheck = "set +e;" + (old.preCheck or "");
postCheck = "set -e;" + (old.postCheck or "");
postPatch = (old.postPatch or "") + "export doCheck=1;";
})
else
deriv;
benchmarkPhasesAndForceTests = { deriv }:
forceTests { deriv = benchmarkPhases { inherit deriv; }; };
noOptimize = { deriv }:
deriv.overrideAttrs (old: {
pname = old.pname + "-ozero";
NIX_CFLAGS_COMPILE = "-O0";
NIX_CXXFLAGS_COMPILE = "-O0";
});
optimize = { deriv }:
deriv.overrideAttrs (old: {
pname = old.pname + "-othree";
NIX_CFLAGS_COMPILE = "-O3";
NIX_CXXFLAGS_COMPILE = "-O3";
});
ropfuscate = { deriv, stdenv, config ? "" }:
let
stdenv_ = if config == "" then
stdenv
else
pkgs.buildPackages.overrideCC stdenv (stdenv.cc.override (old: {
extraBuildCommands = old.extraBuildCommands + ''
echo "-mllvm --ropfuscator-config=${config}" >> $out/nix-support/cc-cflags
'';
}));
config_name = if (config == "") then
""
else
lib.removeSuffix ".toml"
(lib.lists.last (lib.splitString "/" config));
in (deriv.override { stdenv = stdenv_; }).overrideAttrs (old: {
pname = old.pname + "-ropfuscated"
+ lib.optionalString (config != "") "-${config_name}";
dontStrip = false;
});
ropfuscateRopOnly = { deriv, stdenv }:
ropfuscate {
inherit deriv stdenv;
config = "${ropfuscator-utils}/configs/roponly.toml";
};
ropfuscateHalfAddresses = { deriv, stdenv }:
ropfuscate {
inherit deriv stdenv;
config = "${ropfuscator-utils}/configs/halfaddresses.toml";
};
ropfuscateAllAddresses = { deriv, stdenv }:
ropfuscate {
inherit deriv stdenv;
config = "${ropfuscator-utils}/configs/alladdresses.toml";
};
ropfuscateFull = { deriv, stdenv }:
ropfuscate {
inherit deriv stdenv;
config = "${ropfuscator-utils}/configs/full.toml";
};
in rec {
# expose packages
inherit pkgs pkgsRopfuscator pkgsRopfuscatorLibc pkgsRopfuscatorLibrop;
# expose helper functions
inherit ropfuscate ropfuscateRopOnly ropfuscateHalfAddresses
ropfuscateAllAddresses ropfuscateFull;
inherit benchmarkPhases benchmarkPhasesAndForceTests forceTests;
inherit optimize noOptimize;
defaultPackage = packages.clang;
# development shell
devShell = packages.llvmDebug.overrideAttrs (_: {
shellHook = ''
# move to temporary directory
cd `mktemp -d`
# unpack and configure project
echo "Preparing LLVM source tree..."
eval "$unpackPhase" && cd llvm && runHook patchPhase && eval "$configurePhase"
# get compile_commands.json and put them in root of LLVM tree
cd .. && mv build/compile_commands.json .
'';
});
# exposed packages
packages = flake-utils.lib.flattenTree rec {
llvm = pkgsRopfuscator.buildPackages.ropfuscator-llvm;
llvmDebug = pkgsRopfuscator.buildPackages.ropfuscator-llvm-debug;
clang = pkgsRopfuscator.buildPackages.ropfuscator-clang;
vanillaRopStdenv = pkgs.overrideCC pkgs.stdenv clang;
libcRopStdenv = pkgs.overrideCC vanillaRopStdenv
(vanillaRopStdenv.cc.override (old: {
extraBuildCommands = old.extraBuildCommands
+ "echo '-mllvm --ropfuscator-library=${pkgs.glibc}/lib/libc.so' >> $out/nix-support/cc-cflags";
}));
libropRopStdenv = pkgs.overrideCC vanillaRopStdenv
(let librop-path = "${librop}/lib/librop.so";
in (vanillaRopStdenv.cc.override (old: {
extraBuildCommands = old.extraBuildCommands
+ "echo '-mllvm --ropfuscator-library=${librop-path} ${librop-path}' >> $out/nix-support/cc-cflags";
})));
tests = pkgs.callPackage ./tests.nix {
inherit ropfuscator-utils librop;
stdenv = vanillaRopStdenv;
};
helloVanilla = pkgs.hello;
helloRopOnly = ropfuscateRopOnly {
deriv = pkgs.hello;
stdenv = libropRopStdenv;
};
helloFull = ropfuscateFull {
deriv = pkgs.hello;
stdenv = libropRopStdenv;
};
};
});
}