Bug 119129

Summary: [libc] [patch] __stack_chk_guard setup is bogus in src/lib/libc/sys/stack_protector.c
Product: Base System Reporter: Antoine Brodin <antoine.brodin>
Component: kernAssignee: Antoine Brodin <antoine>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 8.0-CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
kern_mib.c.diff none

Description Antoine Brodin 2007-12-29 13:10:01 UTC
When compiling with -fstack-protector-all and executing a binary,
__stack_chk_guard is always initialized to 0xff0a0000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000.
(at least on i386).

Fix: 

There is a bug in either src/lib/libc/sys/stack_protector.c:__guard_setup(),
or in src/sys/kern/kern_mib.c:sysctl_kern_arnd().
sysctl_kern_arnd() generates a random long, while __guard_setup assumes it
generates a random buffer.
On OpenBSD, src/lib/libc/sys/stack_protector.c is the same but
src/sys/kern/kern_sysctl.c initializes a buffer for KERN_ARND
( http://fxr.watson.org/fxr/source//kern/kern_sysctl.c?v=OPENBSD#L394 )
How-To-Repeat: %%%
cat > b.c << EOF

#include <stdio.h>

extern long __stack_chk_guard[8];

int
main(void)
{
	int i;

	for (i = 0; i < 8; i++)
		printf("%lx\n", __stack_chk_guard[i]);
	return 0;
}

EOF
gcc -fstack-protector-all b.c
./a.out
%%%

It gives:
./a.out 
ff0a0000
0
0
0
0
0
0
0


Where is the problem ?
The length returned by sysctl(mib, 2, __stack_chk_guard, &len, NULL, 0)
is not sizeof(__stack_chk_guard) so the default canary is used:

%%%
cat > a.c << EOF

#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>

int
main(void)
{
	long stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
	int mib[2];
	size_t len;
	int i, ret;

	mib[0] = CTL_KERN;
	mib[1] = KERN_ARND;
	len = sizeof(stack_chk_guard);
	ret = sysctl(mib, 2, stack_chk_guard, &len, NULL, 0);
	if (ret == -1)
		printf("-1\n");
	if (len != sizeof(stack_chk_guard))
		printf("%d != %d\n", len, sizeof(stack_chk_guard));
	if (ret == -1 || len != sizeof(stack_chk_guard))
	{
		((unsigned char *)(void *)stack_chk_guard)[0] = 0;
		((unsigned char *)(void *)stack_chk_guard)[1] = 0;
		((unsigned char *)(void *)stack_chk_guard)[2] = '\n';
		((unsigned char *)(void *)stack_chk_guard)[3] = 255;
	}
	for (i = 0; i < 8; i++)
		printf("%lx\n", stack_chk_guard[i]);
	return 0;
}

EOF
gcc a.c
./a.out
%%%

It gives:
./a.out
4 != 32
ff0a0000
0
0
0
0
0
0
0
Comment 1 Antoine Brodin 2007-12-29 14:23:44 UTC
As the problem is more likely in
src/sys/kern/kern_mib.c:sysctl_kern_arnd(), this PR may be in "kern"
category.
Comment 2 Antoine Brodin 2007-12-30 11:21:47 UTC
Here is a patch, tested on i386 only.
I tried to mimic what OpenBSD does.
Comment 3 Antoine Brodin freebsd_committer freebsd_triage 2008-02-10 19:17:15 UTC
Responsible Changed
From-To: freebsd-bugs->antoine

Take.
Comment 4 dfilter service freebsd_committer freebsd_triage 2008-02-17 16:44:56 UTC
antoine     2008-02-17 16:44:48 UTC

  FreeBSD src repository

  Modified files:
    sys/kern             kern_mib.c 
  Log:
  Make sysctl_kern_arnd return a random buffer instead of a random long,
  as it is expected by userland (stack protector guard setup for example).
  
  PR:             119129
  Approved by:    rwatson (mentor)
  MFC after:      1 month
  
  Revision  Changes    Path
  1.88      +9 -5      src/sys/kern/kern_mib.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 5 Antoine Brodin freebsd_committer freebsd_triage 2008-02-17 16:50:25 UTC
State Changed
From-To: open->patched

Patched in revision 1.88 of src/sys/kern/kern_mib.c
Comment 6 dfilter service freebsd_committer freebsd_triage 2008-03-24 14:28:39 UTC
antoine     2008-03-24 14:28:33 UTC

  FreeBSD src repository

  Modified files:        (Branch: RELENG_7)
    sys/kern             kern_mib.c 
  Log:
  MFC to RELENG_7:
    Make sysctl_kern_arnd return a random buffer instead of a random long,
    as it is expected by userland (stack protector guard setup for example).
  
    PR:             119129
    Approved by:    rwatson (mentor)
    MFC after:      1 month
  
  Revision  Changes    Path
  1.84.2.2  +9 -5      src/sys/kern/kern_mib.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 7 Antoine Brodin freebsd_committer freebsd_triage 2008-03-24 14:50:25 UTC
State Changed
From-To: patched->closed

Close: committed in HEAD and RELENG_7.