Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Do I need -KPIC -DPIC and -ztype=pie for full ASLR support?

Hi,
do I have to compile my programs with -KPIC -DPIC and -ztype=pie so that -zaslr=enable has any effect?
$ cc -KPIC -DPIC -ztype=pie -zaslr=enable helloworld.c
$ file a.out
a.out: ELF 32-bit LSB dynamic lib 80386 Version 1 [SSE], position-independent executable, dynamically linked, not stripped
Also it seems like nothing in the base OS is compiled with PIC/PIE, why is that so?
$ file /usr/bin/* | grep position
$ file /usr/sbin/* | grep position
$
Best Answer
-
The short answer is: Yes, you do.
As you undoubtedly know, ASLR works by altering the mapped addresses of
mapped objects.
ASLR has always been able to work on the shared objects in a process, because
shared objects are ET_DYN, built to be mapped at arbitrary addresses. Shared
objects should always be built PIC, because otherwise, they end up containing code
that needs to be fixed up at load time, imposing unnecessary startup costs.
ASLR has traditionally not able to work on the main "a.out" object, because such
objects were built as ET_EXEC, fixed to a known address. The initial ASLR rollout
was therefore not "full", because it ignored the a.out. Hence, the invention
of PIE. PIE are "executables" built as ET_DYN, which can be mapped arbitrarily.
With PIE, you can have "full ASLR"
Hence, for "full ASLR", you need -ztype=PIE, and if you define "full" as also meaning
"high quality", then you should also compile it as PIC.
I'm not really sure what -DPIC does, if anything, so I'll leave that to a compiler expert
to answer.
If you were running the latest development bits, as I am, you'd see a
different result:
[email protected]% file /usr/bin/* | grep position | wc -l
385
These things take time to work through the pipeline, but you will eventually see
PIE in the system.
Answers
-
The short answer is: Yes, you do.
As you undoubtedly know, ASLR works by altering the mapped addresses of
mapped objects.
ASLR has always been able to work on the shared objects in a process, because
shared objects are ET_DYN, built to be mapped at arbitrary addresses. Shared
objects should always be built PIC, because otherwise, they end up containing code
that needs to be fixed up at load time, imposing unnecessary startup costs.
ASLR has traditionally not able to work on the main "a.out" object, because such
objects were built as ET_EXEC, fixed to a known address. The initial ASLR rollout
was therefore not "full", because it ignored the a.out. Hence, the invention
of PIE. PIE are "executables" built as ET_DYN, which can be mapped arbitrarily.
With PIE, you can have "full ASLR"
Hence, for "full ASLR", you need -ztype=PIE, and if you define "full" as also meaning
"high quality", then you should also compile it as PIC.
I'm not really sure what -DPIC does, if anything, so I'll leave that to a compiler expert
to answer.
If you were running the latest development bits, as I am, you'd see a
different result:
[email protected]% file /usr/bin/* | grep position | wc -l
385
These things take time to work through the pipeline, but you will eventually see
PIE in the system.
-
-DPIC is not needed by Studio compilers or by Solaris. An application might use a macro PIC for its own purposes.
-
On the current Solaris 11.3 bits, you can run
$ elfdump -d /usr/bin/* /usr/sbin/* 2>&1 | grep SUNW_ASLR | grep -c ENABLE
and see a large number of files that use ASLR. Shared libraries also use ASLR.
-
-DPIC does exactly what you'd expect, it #define's PIC, which is commonly used with inline assembly to choose between PIC & non-PIC versions, and because GNU autoconf defines it when building shared libraries, is sometimes used when code differs between shared & static libraries. I'm not aware of anything in the core Solaris headers that checks for #ifdef PIC, but I wouldn't be surprised if some of our bundled FOSS libraries have such checks in headers your program may be including.