]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Improve FreeBSD handling of W^X setting.
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 16 Oct 2022 16:58:52 +0000 (18:58 +0200)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 16 Oct 2022 16:58:52 +0000 (18:58 +0200)
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

coregrind/m_debuginfo/readelf.c
coregrind/m_main.c
include/vki/vki-freebsd.h

index 25d237b7de88f0965aa5051758e9148679a63a1f..6cf08f666f02f4de64642d0a6383bc0ee1fb5df6 100644 (file)
@@ -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];
index 25882e7cea49a96a951048a61fb8c41f7d7d41e2..200da407fba09a685f172ec0e28ab5ed030b43e6 100644 (file)
@@ -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 <sys/param.h>       /* __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"
index c2234664d6b4d5b1948dbbd4a3317a91fc314e8a..c7481f31c74e977808220374603973b9410ea1ca 100644 (file)
@@ -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