2 ms·
> If you're going to align anything, you will (probably) need to write some code to compute the right alignment. At that point, you will need to split your prin
by saagarjha 9y ago
> If you're going to align anything, you will (probably) need to write some code to compute the right alignment. At that point, you will need to split your printf into pieces, so why not just use printf's return code, which tells you the number of characters written?
The child comment provides a use case for %n, so I won't go into this here.
> glibc on Linux started hardening printf by replacing calls to printf with calls to __printf_chk
Only with -D_FORTIFY_SOURCE=2 set when compiling. Swapping printf calls with __printf_chk is off by default.
> The implementation is somewhat horrific - printf opens /proc/self/maps and range-checks the format string's address, but it works and does provide the necessary security
Luckily, macOS has a nicer Mach API for this, namely the vm_region/vm_region_64 family of functions: https://opensource.apple.com/source/Libc/Libc-1244.30.3/stdio/FreeBSD/vfprintf.c.auto.html https://opensource.apple.com/source/Libc/Libc-1244.30.3/stdi... (search for __printf_is_memory_read_only).
> By the way, which version of macOS did they introduce the SIGILL hardening, and is it documented? I'm on 10.12.6
You're off by a version; this was introduced in 10.13. Your program crashes nicely on with Apple LLVM version 9.1.0 (clang-902.0.39.2) on macOS High Sierra 10.13.5 (17F70a). Documented? Ha, I wish. It just ended up breaking a couple of programs that relied on this behavior, since it would just call os_crash with "%n used in a non-immutable format string".