From: Paul Floyd Date: Sun, 16 Oct 2022 16:58:52 +0000 (+0200) Subject: Improve FreeBSD handling of W^X setting. X-Git-Tag: VALGRIND_3_20_0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b31ff3321e57b774b48c6b547045cb8b089d152a;p=thirdparty%2Fvalgrind.git Improve FreeBSD handling of W^X setting. FreeBSD has a sysctl that prevents mmapping regions that are both Write and Exec. This setting prevents Valgrind from workig correctly. This change uses ELF tags to turn off W^X control, and also removes a sysctl check for the same flag. Patch contributor: https://github.com/semihalf-Duleba-Kornel/valgrind/commit/0a3a794143f7c451a5a00e598854ae8d813f8f17 --- diff --git a/coregrind/m_debuginfo/readelf.c b/coregrind/m_debuginfo/readelf.c index 25d237b7de..6cf08f666f 100644 --- a/coregrind/m_debuginfo/readelf.c +++ b/coregrind/m_debuginfo/readelf.c @@ -1171,7 +1171,7 @@ void read_and_set_osrel(DiImage* img) DiOffT name_ioff = note_ioff + sizeof(ElfXX_Nhdr); //DiOffT desc_ioff = name_ioff + ((note[0].n_namesz + 3) & ~3); if (ML_(img_strcmp_c)(img, name_ioff, "FreeBSD") == 0 - && note[0].n_type == NT_FREEBSD_ABI_TAG) { + && note[0].n_type == VKI_NT_FREEBSD_ABI_TAG) { u_int32_t osrel = note[1].n_type; int name[4]; diff --git a/coregrind/m_main.c b/coregrind/m_main.c index 25882e7cea..200da407fb 100644 --- a/coregrind/m_main.c +++ b/coregrind/m_main.c @@ -1373,50 +1373,6 @@ Int valgrind_main ( Int argc, HChar **argv, HChar **envp ) VG_(exit)(1); } - - //-------------------------------------------------------------- - // FreeBSD also check for sysctl kern.elf64.allow_wx=0 - // This is a sysctl that prevents applications from mmap'ing - // segments that are writeable and executable - //-------------------------------------------------------------- -#if defined(VGP_amd64_freebsd) - error = VG_(sysctlbyname)("kern.elf64.allow_wx", &val, &len, 0, 0); - if (error != -1 && val != 1) { - VG_(debugLog)(0, "main", "Valgrind: FATAL:\n"); - VG_(debugLog)(0, "main", "sysctl kern.elf64.allow_wx sysctl is 0.\n"); - VG_(debugLog)(0, "main", " Set this sysctl with\n"); - VG_(debugLog)(0, "main", " 'sysctl kern.elf64.allow_wx sysctl=1'.\n"); - // the below code doesn't work as I expected - // the proccontrol command doesn't cause sysctlbyname to get a modified value - // which means that valgrind will still detect allow_wx == 0 and exit here -//#if (FREEBSD_VERS >= FREEBSD_13_1) -// VG_(debugLog)(0, "main", " Or, alternatively, run valgrind with\n"); -// VG_(debugLog)(0, "main", " 'proccontrol -m wxmap -s enable valgrind [options] prog-and-args'\n"); -//#endif - VG_(debugLog)(0, "main", " Cannot continue.\n"); - - VG_(exit)(1); - } - -#endif - - /* also 323bit version */ -#if defined(VGP_x86_freebsd) - error = VG_(sysctlbyname)("kern.elf32.allow_wx", &val, &len, 0, 0); - if (error != -1 && val != 1) { - VG_(debugLog)(0, "main", "Valgrind: FATAL:\n"); - VG_(debugLog)(0, "main", "sysctl kern.elf32.allow_wx sysctl is 0.\n"); - VG_(debugLog)(0, "main", " Set this sysctl with\n"); - VG_(debugLog)(0, "main", " 'sysctl kern.elf32.allow_wx sysctl=1'.\n"); -//#if (FREEBSD_VERS >= FREEBSD_13_1) -// VG_(debugLog)(0, "main", " Or, alternatively, run valgrind with\n"); -// VG_(debugLog)(0, "main", " 'proccontrol -m wxmap -s enable valgrind [options] prog-and-args'\n"); -//#endif - VG_(debugLog)(0, "main", " Cannot continue.\n"); - - VG_(exit)(1); - } -#endif #endif @@ -3351,6 +3307,47 @@ void _start_in_C_solaris ( UWord* pArgc ) /*====================================================================*/ #elif defined(VGO_freebsd) +/* + * Could probably extract __FreeBSD_version at configure time + */ +/* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ +#include /* __FreeBSD_version */ +/* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ + +/* + * We need to add two elf notes in order for image activator to parse + * additional binary properites. + * First note declares the ABI, second is the feature note. + * This is primarly used to turn off W^X policy for all valgrind tools, + * as they don't work with it enabled. + */ + +/* Based on FreeBSD sources: lib/csu/common/crtbrand.S */ +asm("\n" + ".section .note.tag,\"aG\",%note,.freebsd.noteG,comdat\n" + ".p2align 2\n" + ".4byte 2f-1f\n" + ".4byte 4f-3f\n" + ".4byte "VG_STRINGIFY(VKI_NT_FREEBSD_ABI_TAG)"\n" +"1: .asciz \"FreeBSD\"\n" +"2: .p2align 2\n" +"3: .4byte "VG_STRINGIFY(__FreeBSD_version)"\n" +"4: .previous\n" +); + +/* Based on FreeBSD sources: lib/csu/common/feature_note.S */ +asm("\n" + ".section .note.tag,\"a\",%note\n" + ".p2align 2\n" + ".4byte 2f-1f\n" + ".4byte 4f-3f\n" + ".4byte "VG_STRINGIFY(VKI_NT_FREEBSD_FEATURE_CTL)"\n" +"1: .asciz \"FreeBSD\"\n" +"2: .p2align 2\n" +"3: .4byte "VG_STRINGIFY(VKI_NT_FREEBSD_FCTL_WXNEEDED)"\n" +"4: .previous\n" +); + #if defined(VGP_x86_freebsd) asm("\n" ".text\n" diff --git a/include/vki/vki-freebsd.h b/include/vki/vki-freebsd.h index c2234664d6..c7481f31c7 100644 --- a/include/vki/vki-freebsd.h +++ b/include/vki/vki-freebsd.h @@ -200,7 +200,7 @@ typedef __vki_fd_set vki_fd_set; #endif //---------------------------------------------------------------------- -// linux and freebsd version hacks +// freebsd version hacks //---------------------------------------------------------------------- #ifndef ELFMAG #define ELFMAG "\177ELF" /* magic string */ @@ -214,10 +214,6 @@ typedef __vki_fd_set vki_fd_set; #define ELF_NOTE_GNU "GNU" #endif -// This is not defined on FreeBSD 10.4 -#if !defined(NT_FREEBSD_ABI_TAG) -#define NT_FREEBSD_ABI_TAG 1 -#endif #define VKI_ELF_NOTE_ROUNDSIZE 4 @@ -2485,6 +2481,10 @@ struct vki_ps_strings { #define VKI_AT_NULL 0 #define VKI_AT_PS_STRINGS 32 +#define VKI_NT_FREEBSD_ABI_TAG 1 +#define VKI_NT_FREEBSD_FEATURE_CTL 4 +#define VKI_NT_FREEBSD_FCTL_WXNEEDED 0x00000008 + // See syswrap-freebsd.c PRE/POST(sys_ioctl) #if 0