The 01/17/2022 17:54, Catalin Marinas wrote: > On Fri, Jan 07, 2022 at 12:01:17PM +0000, Catalin Marinas wrote: > > I think we can look at this from two angles: > > > > 1. Ignoring MDWE, should whoever does the original mmap() also honour > > PROT_BTI? We do this for static binaries but, for consistency, should > > we extend it to dynamic executable? > > > > 2. A 'simple' fix to allow MDWE together with BTI. > > Thinking about it, (1) is not that different from the kernel setting > PROT_EXEC on the main executable when the dynamic loader could've done > it as well. There is a case for making this more consistent: whoever > does the mmap() should use the full attributes. > Yeah that was my original idea that it should be consistent. One caveat is that protection flags are normally specified in the program header, but the BTI marking is in PT_GNU_PROPERTY which is harder to get to, so glibc does not try to get it right for the initial mapping either: it has to re-mmap or mprotect. (In principle we could use read syscalls to parse the ELF headers and notes before mmap, but that's more complicated with additional failure modes.) i.e. if (2) is fixed then mprotect can be used for library mapping too which is simpler than re-mmap. > Question for the toolchain people: would the compiler ever generate > relocations in the main executable that the linker needs to resolve via > an mprotect(READ|WRITE) followed by mprotect(READ|EXEC)? If yes, we'd > better go for a proper MDWE implementation in the kernel. There is some support for text relocations in glibc, but it's not expected to be common (e.g. it is a bug if a distro binary has it). For static PIE we made -z text ldflag the default so text relocs are rejected (i think glibc cannot self-relocate those, so ld.so cannot have them either), but otherwise certain text relocs work (static relocations are not supported). $ cat a.c int x = 42; __attribute__((section(".text"))) int *y = &x; int main(){ return *y; } $ gcc a.c /tmp/ccOrpMPD.s: Assembler messages: /tmp/ccOrpMPD.s:12: Warning: ignoring changed section attributes for .text $ readelf -aW ./a.out |grep TEXTREL 0x0000000000000016 (TEXTREL) 0x0 0x000000000000001e (FLAGS) TEXTREL BIND_NOW $ strace -e mprotect ./a.out mprotect(0xffff839ee000, 65536, PROT_NONE) = 0 mprotect(0xffff839fe000, 12288, PROT_READ) = 0 mprotect(0xaaaac2096000, 4096, PROT_READ|PROT_WRITE|PROT_EXEC) = 0 mprotect(0xaaaac2096000, 4096, PROT_READ|PROT_EXEC) = 0 mprotect(0xaaaac20a6000, 4096, PROT_READ) = 0 mprotect(0xffff83a55000, 4096, PROT_READ) = 0 +++ exited with 42 +++