Skip to contentSkip to conversation

_chk_fail and _chk canaries for minios and newlib

RFC PATCH In review George S. Coker, II 3 participants 6 messages Mar 9, 2009 – Mar 25, 2009

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 _chk_fail and _chk canaries for minios and newlib

Samuel,

I've made a small patch (attached) to minios and newlib that addresses long standing linking issues for ocaml stubdomains on non-debian distros. While minios and associated libraries are compiled with fno-stack-protector and no fortify buffer overflow protections, this doesn't produce a stubdom free of these dependencies when linking against third party libraries, e.g. Libasmrun for ocaml. It seems impractical to keep building minios specific libraries given that these options are common on all distros (now) and a potential impediment to creating stubdomains out of existing system libraries.

This patch implements a minios version of the stack_chk_fail from glibc. fprintf_chk and sprintf_chk functions have been added to newlib. This split was made to ensure that minios would dump the stack and exit on a stack_chk_fail but avoid a cross-dependency between minios and newlib. If anyone has other suggestions, let me know.

The _chk functions are just pass through stubs because the actual fortify implementation is not trivial for newlib. It's also not clear that minios domains benefit much from the fortify protections. This patch supports the needs of the ocaml stubdomain, other stubdomains using existing system libraries may need additional _chk stubs.

George

Signature · 1 line
George S. Coker, II <[email protected]>
extras/mini-os/lib/stack_chk_fail.c new file +8 -0
diff -r f8187a343ad2 extras/mini-os/lib/stack_chk_fail.c
@@ -0,0 +1,8 @@
+#include <kernel.h>
+#include <console.h>
+
+void __stack_chk_fail(void)
+{
+    printk("stack smashing detected\n");
+    do_exit();
+}
stubdom/Makefile +1 -0
diff -r f8187a343ad2 stubdom/Makefile
@@ -93,6 +93,7 @@
 newlib-$(NEWLIB_VERSION): newlib-$(NEWLIB_VERSION).tar.gz
 	tar xzf $<
 	patch -d $@ -p0 < newlib.patch
+	patch -d $@ -p0 < newlib-chk.patch
 	touch $@
 
 NEWLIB_STAMPFILE=$(CROSS_ROOT)/$(GNU_TARGET_ARCH)-xen-elf/lib/libc.a
stubdom/newlib-chk.patch new file +159 -0

159 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Hello,

Quoted 2 lines · George S. Coker, II, le Mon 09 Mar 2009 13:08:04 -0500, a écrit :

This patch implements a minios version of the stack_chk_fail from glibc. fprintf_chk and sprintf_chk functions have been added to newlib.

Cool! That'd be useful indeed. I'm however wondering whether your patch is enough for the stack protection: in my memory, gcc assumes that the glibc is used, and on e.g. i386, it uses gs:(0x14) for the stack canary (see a disassembly of a program compiled with -fstack-protector-all, there's a mov %gs:0x14,%eax lying in functions), and as a result we need to define a proper gs in MiniOS that follows glibc's tcbhead_t.

The fortified printfs should be fine.

Samuel

#2 lore raw
Quoted 6 lines · Samuel Thibault

Hello,

Quoted 2 lines · George S. Coker, II, le Mon 09 Mar 2009 13:08:04 -0500, a écrit :

This patch implements a minios version of the stack_chk_fail from glibc. fprintf_chk and sprintf_chk functions have been added to newlib.

Quoted 8 lines

Cool! That'd be useful indeed. I'm however wondering whether your patch is enough for the stack protection: in my memory, gcc assumes that the glibc is used, and on e.g. i386, it uses gs:(0x14) for the stack canary (see a disassembly of a program compiled with -fstack-protector-all, there's a mov %gs:0x14,%eax lying in functions), and as a result we need to define a proper gs in MiniOS that follows glibc's tcbhead_t.

It probably isn't enough. It's more of a stub to make the linker and libraries happy. I can work on a more proper patch, but I was a little uncertain about the split between minios and newlib. It's just not clear where to add new funcs because of the out-of-tree dependency on newlib.

Quoted 3 lines

The fortified printfs should be fine.

Samuel

Signature · 1 line
George S. Coker, II <[email protected]>
#3 lore raw

boot issues with PV guest OS on Xen-3.4 unstable

Hi,
I am facing some problem in booting PV-Guest OS, can someone please help me out?

I am creating a disk image for PV-Guest as in below link

http://www.virtuatopia.com/index.php/Building_a_Xen_Virtual_Guest_Filesy
stem_on_a_Disk_Image_%28Cloning_Host_System%29

PV-guest configuration files is like below

# Kernel image file.
kernel = "/boot/vmlinuz-2.6.18.8-xen"

# Optional ramdisk.
#ramdisk = "/boot/initrd.gz"
ramdisk = "/boot/initrd-2.6.18.8-xen"
memory = 512
disk = ['tap:aio:/home/xen/PV/XenGuest1.img,xvda1,w', 'tap:aio:/home/xen/PV/XenGuest1.swap,xvda2,w']

extra = "3 xencons=tty"
# Set root device.
root = "/dev/xvda1"

Error messages while booting guest
-----------------------------
xm create -c xmexample_ext3
.
.
.
Waiting for device /dev/xvda1 to appear: ok
rootfs: major=202 minor=1 devn=51713
Mounting root /dev/xvda1
EXT3-fs: INFO: recovery required on readonly filesystem.
EXT3-fs: write access will be enabled during recovery. kjournald starting. Commit interval 5 seconds
EXT3-fs: recovery complete.
EXT3-fs: mounted filesystem with ordered data mode. EXT3 FS on xvda1, internal journal
INIT: version 2.86 booting
INIT: cannot execute "/etc/init.d/boot"
INIT: Entering runlevel: 3
INIT: cannot execute "/etc/init.d/rc"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: Id "1" respawning too fast: disabled for 5 minutes
INIT: Id "2" respawning too fast: disabled for 5 minutes
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: Id "5" respawning too fast: disabled for 5 minutes
INIT: Id "6" respawning too fast: disabled for 5 minutes
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: Id "4" respawning too fast: disabled for 5 minutes
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: cannot execute "/sbin/mingetty"
INIT: Id "3" respawning too fast: disabled for 5 minutes
INIT: no more processes left in this runlevel

#4 lore raw
In reply to George S. Coker, II
Quoted 2 lines · George S. Coker, II, le Mon 09 Mar 2009 14:28:22 -0500, a écrit :

It probably isn't enough. It's more of a stub to make the linker and libraries happy.

Right, in this particular case it's read-only code so it shouldn't harm so much and the code could be checked-in as is. I'm however wondering how these reads do not trigger traps, as we unmap page 0 in clear_bootstrap(), and thus e.g. on i386 gs:0x14 should trap.

Quoted 1 line

I was a little uncertain about the split between minios and newlib.

The way you did it seems right to me: checking printf size is more a matter of newlib, while reacting to stack smashing is a kernel matter.

Samuel

#5 lore raw

PATCH 1/1 minios: _chk_fail and _chk canaries for minios and newlib

In reply to George S. Coker, II

Hello,

Just resending George's patch for inclusion.

Samuel

minios: _chk_fail and _chk canaries for minios and newlib

Add __stack_chk_fail to mini-os and __sprintf_chk __fprintf_chk to newlib, to cope with ocaml runtimes compiled with -fstack-protector.

From: "George S. Coker, II" <[email protected]>

extras/mini-os/lib/stack_chk_fail.c new file +8 -0
diff -r f8187a343ad2 extras/mini-os/lib/stack_chk_fail.c
@@ -0,0 +1,8 @@
+#include <kernel.h>
+#include <console.h>
+
+void __stack_chk_fail(void)
+{
+    printk("stack smashing detected\n");
+    do_exit();
+}
stubdom/Makefile +1 -0
diff -r f8187a343ad2 stubdom/Makefile
@@ -93,6 +93,7 @@
 newlib-$(NEWLIB_VERSION): newlib-$(NEWLIB_VERSION).tar.gz
 	tar xzf $<
 	patch -d $@ -p0 < newlib.patch
+	patch -d $@ -p0 < newlib-chk.patch
 	touch $@
 
 NEWLIB_STAMPFILE=$(CROSS_ROOT)/$(GNU_TARGET_ARCH)-xen-elf/lib/libc.a
stubdom/newlib-chk.patch new file +159 -0

159 lines not on this page · raw message