From: Paul Floyd Date: Sun, 3 Apr 2022 13:50:38 +0000 (+0200) Subject: Bug 451843 - valgrind fails to start on a FreeBSD system which enforces W^X X-Git-Tag: VALGRIND_3_19_0~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbc3bcab0ae7aa01a116c05c52c66a6714a7df12;p=thirdparty%2Fvalgrind.git Bug 451843 - valgrind fails to start on a FreeBSD system which enforces W^X Also add FreeBSD 13.1 to configure.ac --- diff --git a/NEWS b/NEWS index 2b74b58644..23caf25025 100644 --- a/NEWS +++ b/NEWS @@ -85,6 +85,7 @@ are not entered into bugzilla tend to get forgotten about or ignored. 450536 Powerpc: valgrind throws 'facility scv unavailable exception' 451626 Syscall param bpf(attr->raw_tracepoint.name) points to unaddressable byte(s) 451827 [ppc64le] VEX temporary storage exhausted with several vbpermq instructions +451843 valgrind fails to start on a FreeBSD system which enforces W^X To see details of a given bug, visit https://bugs.kde.org/show_bug.cgi?id=XXXXXX diff --git a/configure.ac b/configure.ac index 4559028f92..04c306efbb 100755 --- a/configure.ac +++ b/configure.ac @@ -394,8 +394,10 @@ case "${host_os}" in freebsd_12=1200 AC_DEFINE([FREEBSD_12_2], 1220, [FREEBSD_VERS value for FreeBSD 12.2]) freebsd_12_2=1220 - AC_DEFINE([FREEBSD_13], 1300, [FREEBSD_VERS value for FreeBSD 13.x]) - freebsd_13=1300 + AC_DEFINE([FREEBSD_13_0], 1300, [FREEBSD_VERS value for FreeBSD 13.0]) + freebsd_13_0=1300 + AC_DEFINE([FREEBSD_13_1], 1310, [FREEBSD_VERS value for FreeBSD 13.1+]) + freebsd_13_1=1310 AC_DEFINE([FREEBSD_14], 1400, [FREEBSD_VERS value for FreeBSD 14.x]) freebsd_14=1400 @@ -428,9 +430,18 @@ case "${host_os}" in esac ;; 13.*) - AC_MSG_RESULT([FreeBSD 13.x (${kernel})]) - AC_DEFINE([FREEBSD_VERS], FREEBSD_13, [FreeBSD version]) - freebsd_vers=$freebsd_13 + case "${kernel}" in + 13.0-*) + AC_MSG_RESULT([FreeBSD 13.0 (${kernel})]) + AC_DEFINE([FREEBSD_VERS], FREEBSD_13_0, [FreeBSD version]) + freebsd_vers=$freebsd_13_0 + ;; + *) + AC_MSG_RESULT([FreeBSD 13.1+ (${kernel})]) + AC_DEFINE([FREEBSD_VERS], FREEBSD_13_1, [FreeBSD version]) + freebsd_vers=$freebsd_13_1 + ;; + esac ;; 14.*) AC_MSG_RESULT([FreeBSD 14.x (${kernel})]) @@ -4625,7 +4636,7 @@ fi # test "$VGCONF_OS" = "solaris" if test "$VGCONF_OS" = "freebsd" ; then -AM_CONDITIONAL(FREEBSD_VERS_13_PLUS, test $freebsd_vers -ge $freebsd_13) +AM_CONDITIONAL(FREEBSD_VERS_13_PLUS, test $freebsd_vers -ge $freebsd_13_0) else diff --git a/coregrind/m_initimg/initimg-freebsd.c b/coregrind/m_initimg/initimg-freebsd.c index 71fb8add17..10d034eab6 100644 --- a/coregrind/m_initimg/initimg-freebsd.c +++ b/coregrind/m_initimg/initimg-freebsd.c @@ -578,7 +578,7 @@ Addr setup_client_stack( void* init_sp, /* --- auxv --- */ auxv = (struct auxv *)ptr; *client_auxv = (UInt *)auxv; -#if defined(VGP_x86_freebsd) && (VGO_freebsd <= FREEBSD_13) +#if defined(VGP_x86_freebsd) && (VGO_freebsd <= FREEBSD_13_0) int* pagesizes = NULL; #endif @@ -660,7 +660,7 @@ Addr setup_client_stack( void* init_sp, // case AT_CANARYLEN: // case AT_EXECPATH: // case AT_CANARY: -#if defined(VGP_x86_freebsd) && (VGO_freebsd <= FREEBSD_13) +#if defined(VGP_x86_freebsd) && (VGO_freebsd <= FREEBSD_13_0) case AT_PAGESIZESLEN: if (!VG_(is32on64)()) { VG_(debugLog)(2, "initimg", @@ -685,7 +685,7 @@ Addr setup_client_stack( void* init_sp, // case AT_TIMEKEEP: break; -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) case AT_BSDFLAGS: case AT_ARGC: // case AT_ARGV: diff --git a/coregrind/m_main.c b/coregrind/m_main.c index 70b6c05495..2a7712b3ce 100644 --- a/coregrind/m_main.c +++ b/coregrind/m_main.c @@ -1348,14 +1348,14 @@ Int valgrind_main ( Int argc, HChar **argv, HChar **envp ) VG_(exit)(1); } +#if defined(VGO_freebsd) + Int val; + SizeT len = sizeof(val); //-------------------------------------------------------------- // FreeBSD check security.bsd.unprivileged_proc_debug sysctl // This needs to be done before aspacemgr starts, otherwise that // will fail with mysterious error codes //-------------------------------------------------------------- -#if defined(VGO_freebsd) - Int val; - SizeT len = sizeof(val); Int error = VG_(sysctlbyname)("security.bsd.unprivileged_proc_debug", &val, &len, 0, 0); if (error != -1 && val != 1) { VG_(debugLog)(0, "main", "Valgrind: FATAL:\n"); @@ -1366,6 +1366,50 @@ 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 diff --git a/coregrind/m_stacktrace.c b/coregrind/m_stacktrace.c index 9467f3caf6..4c45b97b9f 100644 --- a/coregrind/m_stacktrace.c +++ b/coregrind/m_stacktrace.c @@ -283,7 +283,7 @@ UInt VG_(get_StackTrace_wrk) ( ThreadId tid_if_known, if (do_stats) stats.nr++; // Does this apply to macOS 10.14 and earlier? -# if defined(VGO_freebsd) && (FREEBSD_VERS < FREEBSD_13) +# if defined(VGO_freebsd) && (FREEBSD_VERS < FREEBSD_13_0) if (VG_(is_valid_tid)(tid_if_known) && VG_(is_in_syscall)(tid_if_known) && i < max_n_ips) { diff --git a/coregrind/m_syswrap/priv_syswrap-freebsd.h b/coregrind/m_syswrap/priv_syswrap-freebsd.h index e7b66b17d6..db0b647f53 100644 --- a/coregrind/m_syswrap/priv_syswrap-freebsd.h +++ b/coregrind/m_syswrap/priv_syswrap-freebsd.h @@ -524,7 +524,7 @@ DECL_TEMPLATE(freebsd, sys_fhreadlink) // 567 // unimpl __NR_copy_file_range 569 DECL_TEMPLATE(freebsd, sys___sysctlbyname) // 570 -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) // looks like close_range got backported // to 12.2 leaving these 4 marked as UNIMPL in 12.2 // unimpl __NR_shm_open2 571 @@ -537,7 +537,7 @@ DECL_TEMPLATE(freebsd, sys___realpathat) // 574 #endif -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) // unimpl __NR_rpctls_syscall 576 DECL_TEMPLATE(freebsd, sys___specialfd) // 577 diff --git a/coregrind/m_syswrap/syswrap-freebsd.c b/coregrind/m_syswrap/syswrap-freebsd.c index 3fe6c0eb24..6088d89d70 100644 --- a/coregrind/m_syswrap/syswrap-freebsd.c +++ b/coregrind/m_syswrap/syswrap-freebsd.c @@ -6208,7 +6208,7 @@ POST(sys___sysctlbyname) #endif // (FREEBSD_VERS >= FREEBSD_12_2) -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) // SYS___realpathat 474 // from syscalls.master @@ -6940,7 +6940,7 @@ const SyscallTableEntry ML_(syscall_table)[] = { // unimpl __NR_copy_file_range 569 BSDXY(__NR___sysctlbyname, sys___sysctlbyname), // 570 -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) // unimpl __NR_shm_open2 571 // unimpl __NR_shm_rename 572 // unimpl __NR_sigfastblock 573 @@ -6949,7 +6949,7 @@ const SyscallTableEntry ML_(syscall_table)[] = { // unimpl __NR_close_range 575 #endif -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) // unimpl __NR_rpctls_syscall 576 BSDX_(__NR___specialfd, sys___specialfd), // 577 // unimpl __NR_aio_writev 578 diff --git a/include/vki/vki-scnums-freebsd.h b/include/vki/vki-scnums-freebsd.h index f3247e5376..2bce8b523d 100644 --- a/include/vki/vki-scnums-freebsd.h +++ b/include/vki/vki-scnums-freebsd.h @@ -640,7 +640,7 @@ #endif -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) #define __NR_rpctls_syscall 576 #define __NR___specialfd 577 diff --git a/memcheck/tests/freebsd/scalar.c b/memcheck/tests/freebsd/scalar.c index 140ee82568..50c70e4f23 100644 --- a/memcheck/tests/freebsd/scalar.c +++ b/memcheck/tests/freebsd/scalar.c @@ -1615,7 +1615,7 @@ int main(void) SY(SYS_thr_kill2, x0-1, x0-1, x0+9999); FAIL; /* SYS_shm_open 482 */ -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) GO(SYS_freebsd12_shm_open, "(SHM_ANON) 3s 0m"); SY(SYS_freebsd12_shm_open, x0+SHM_ANON, x0+2, x0+9); SUCC; #else @@ -1748,7 +1748,7 @@ int main(void) SY(SYS_jail_remove, x0+1); FAIL; /* SYS_closefrom 509 */ -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) GO(SYS_freebsd12_closefrom, "1s 0m"); SY(SYS_freebsd12_closefrom, x0+100000); SUCC; #else diff --git a/none/tests/freebsd/auxv.c b/none/tests/freebsd/auxv.c index a6d0f45244..689fb3d113 100644 --- a/none/tests/freebsd/auxv.c +++ b/none/tests/freebsd/auxv.c @@ -39,7 +39,7 @@ Elf_AuxStr aux_map[AT_COUNT] = { {"AT_HWCAP2", 26}, // FreeBSD 12 and 11 // {"AT_COUNT", 27}, -#if (FREEBSD_VERS >= FREEBSD_13) +#if (FREEBSD_VERS >= FREEBSD_13_0) {"AT_BSDFLAGS", 27}, {"AT_ARGC", 28}, {"AT_ARGV", 29},