summary refs log tree commit diff
path: root/pkgs/applications/emulators
diff options
context:
space:
mode:
authorSergei Trofimovich <slyich@gmail.com>2023-11-02 12:13:28 +0000
committerSergei Trofimovich <slyich@gmail.com>2023-11-02 12:40:33 +0000
commit0e0863ee3848fe855fd02e6111b83836e5690021 (patch)
tree215fad50258d3711619b3f857c97c8fa8d3c0c39 /pkgs/applications/emulators
parentef34e71d89c299e7078fe45a5f0be0b522a4a8b1 (diff)
zsnes: fix buffer size marking to avoid crash on _FORTIFY_SOURCE=3
Without the change `zsnesn` startup crashes with:

    $ gdb zsnes
    *** buffer overflow detected ***: terminated
    ...
    #7  0x08057c14 in memset (__len=2, __ch=255, __dest=<optimized out>) at
      ...-glibc-2.38-23-dev/include/bits/string_fortified.h:59
Diffstat (limited to 'pkgs/applications/emulators')
-rw-r--r--pkgs/applications/emulators/zsnes/default.nix5
-rw-r--r--pkgs/applications/emulators/zsnes/fortify3.patch29
2 files changed, 33 insertions, 1 deletions
diff --git a/pkgs/applications/emulators/zsnes/default.nix b/pkgs/applications/emulators/zsnes/default.nix
index 059c069d6069f..8e12d7cd7bb36 100644
--- a/pkgs/applications/emulators/zsnes/default.nix
+++ b/pkgs/applications/emulators/zsnes/default.nix
@@ -23,7 +23,10 @@ in stdenv.mkDerivation {
     sha256 = "1gy79d5wdaacph0cc1amw7mqm7i0716n6mvav16p1svi26iz193v";
   };
 
-  patches = [ ./zlib-1.3.patch ];
+  patches = [
+    ./zlib-1.3.patch
+    ./fortify3.patch
+  ];
 
   buildInputs = [ nasm SDL zlib libpng ncurses libGLU libGL ];
 
diff --git a/pkgs/applications/emulators/zsnes/fortify3.patch b/pkgs/applications/emulators/zsnes/fortify3.patch
new file mode 100644
index 0000000000000..83a67b8b7e879
--- /dev/null
+++ b/pkgs/applications/emulators/zsnes/fortify3.patch
@@ -0,0 +1,29 @@
+pal16bxcl is an array of 256 dwords, not bytes:
+    src/endmem.asm:NEWSYM pal16bxcl, resd 256
+
+While at it fixes off-by-4 out of bounds exit.
+
+Detected by _FORTIFY_SOURCE=3:
+    *** buffer overflow detected ***: terminated
+    #7  0x08057c14 in memset (__len=2, __ch=255, __dest=<optimized out>) at ...-glibc-2.38-23-dev/include/bits/string_fortified.h:59
+#8  clearmem () at initc.c:1461
+--- a/src/initc.c
++++ b/src/initc.c
+@@ -1389,7 +1389,7 @@ extern unsigned char vidmemch8[4096];
+ extern unsigned char pal16b[1024];
+ extern unsigned char pal16bcl[1024];
+ extern unsigned char pal16bclha[1024];
+-extern unsigned char pal16bxcl[256];
++extern unsigned char pal16bxcl[1024];
+ extern unsigned char SPCRAM[65472];
+ unsigned char *SPCState = SPCRAM;
+ 
+@@ -1456,7 +1456,7 @@ void clearmem()
+   memset(pal16b, 0, 1024);
+   memset(pal16bcl, 0, 1024);
+   memset(pal16bclha, 0, 1024);
+-  for (i=0 ; i<1024 ; i+=4)
++  for (i=0 ; i<1024-4 ; i+=4)
+   {
+     memset(pal16bxcl+i, 255, 2);
+     memset(pal16bxcl+i+2, 0, 2);