Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a FreeBSD feature note to tools
The crt1 provided for FreeBSD binaries contains a feature note section.
It's used to specify OS specific elf properties that are later parsed
in the kernel image activator.
For example one could use it to opt-out of ASLR, or W^X policy.

Since valgrind tools use a custom _start method it has to be added manually.
By default we opt-out from W^X policy, since it's incompatible with
how the profiled programs are loaded.
All features can be modified with elfctl tool that is shipped with
the base system.
  • Loading branch information
semihalf-Duleba-Kornel committed May 25, 2022
1 parent f6316d1 commit 0a3a794
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions coregrind/m_main.c
Expand Up @@ -1366,50 +1366,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


Expand Down Expand Up @@ -3332,6 +3288,43 @@ void _start_in_C_solaris ( UWord* pArgc )
/*====================================================================*/
#elif defined(VGO_freebsd)

#include <sys/param.h> /* __FreeBSD_version */
#include <sys/elf_common.h> /* NT_FREEBSD_* */

/*
* 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(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(NT_FREEBSD_FEATURE_CTL)"\n"
"1: .asciz \"FreeBSD\"\n"
"2: .p2align 2\n"
"3: .4byte "VG_STRINGIFY(NT_FREEBSD_FCTL_WXNEEDED)"\n"
"4: .previous\n"
);

#if defined(VGP_x86_freebsd)
asm("\n"
".text\n"
Expand Down

0 comments on commit 0a3a794

Please sign in to comment.