hardening: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | **Hardening - PIE or PIC - applies to Linux**. PIE Hardening applies also to Windows, and its probably useful on Windows. The same FPC command line switches apply on Windows. | |
| 2 | 2 | ||
| 3 | 3 | ## Overview | |
| 4 | 4 | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 5 | + | For our purpose here, "hardening" means making your binary harder to hack while running. Key is [PIE](<https://en.wikipedia.org/wiki/Position-independent_code>), Position Independent Executable. FPC supports PIE with a couple of options on most platforms. Some environments require it and it makes good sense, especially in a server application. Its suggested that PIE Hardening increases the size of an executable and slows it down marginally. | |
| 8 | 6 | ||
| 9 | - | ||
| 7 | + | Most projects only require the following extra FPC command line switches - | |
| 8 | + | ``` | |
| 10 | 9 | -Cg -k-pie -znow | |
| 11 | 10 | ``` | |
| 12 | 11 | ||
| 13 | - | ||
| 12 | + | However, there are some corner cases - | |
| 14 | 13 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 14 | + | * Lazarus application will probably already have the "-Cg" in its compile line. | |
| 15 | + | * Small, command line applications that would generate a **statically linked binary** will compile fine but not run ("No such file or directory"). The difficulty is that FPC does not bother to embed instructions about portable linking in such a binary because it appears unnecessary. The solution is to add "{$linklib c}" without the inverted commas to your source. This requests LibC be linked, forcing a dynamically linked binary and FPC provides the necessary information for the linker. | |
| 16 | + | * At present, late 2021, PIE Hardening does not seem to work on PowerPC64le systems, see [this bug report](<https://gitlab.com/freepascal.org/fpc/source/-/issues/39451>) | |
| 17 | + | * Some linux operating systems have a _file_ command that mentions the word 'pie' in its output when directed to a PIE Hardened binary, but some don't. But all recent OSs will, with a PIE Hardened binary, list something like _interpreter /lib64/ld-linux-x86-64.so.2_ \- that library must exist, if not, you have the _statically linked binary_ problem mentioned above. | |
| 19 | 18 | ||
| 20 | - | ||
| 19 | + | ||
| 21 | 20 | ||
| 22 | - | ||
| 21 | + | ## Example | |
| 23 | 22 | ||
| 24 | - | ||
| 23 | + | Following is an example test file - | |
| 24 | + | ``` | |
| 25 | 25 | program Test; | |
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 26 | + | {$linklib c} | |
| 27 | + | begin | |
| 28 | + | writeln('test'); | |
| 29 | + | end. | |
| 30 | 30 | ``` | |
| 31 | 31 | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 32 | + | Compile with this - | |
| 33 | + | ``` | |
| 35 | 34 | fpc -Cg -k-pie -k-znow test.pas | |
| 36 | 35 | ``` | |
| 37 | 36 | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 37 | + | And examine the result - | |
| 38 | + | ``` | |
| 41 | 39 | $> file test [enter] | |
| 42 | - | ||
| 40 | + | test: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.4.0, stripped | |
| 43 | 41 | ``` | |
| 44 | 42 | ||
| 43 | + | ||
| 44 | + | ||
| 45 | 45 | ## See Also | |
| 46 | 46 | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 47 | + | * <https://wiki.freepascal.org/Hardened_runtime_for_macOS> | |
| 48 | + | * <https://wiki.debian.org/Hardening> | |
| 49 | + | * <https://wiki.freepascal.org/Debian_package_structure> | |
| 50 | + | * This [[GDB Debugger Tips|gdb page]] mentions a problem debugging PIE. | |
| 51 | 51 | ||
| 52 | 52 | --- | |
| 53 | - | ||
| 53 | + | *From the Free Pascal wiki, page "hardening", as the Internet Archive kept it on 2022-07-07. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
