malloc(0) returns an invalid address

Dan Nelson dnelson at allantgroup.com
Tue Nov 30 23:07:01 PST 2004


In the last episode (Dec 01), JINMEI Tatuya / ?$B?@L at C#:H said:
> The first call to sysctl sets 'l' to 0, since the list is empty.  Then
> the malloc returns '0x800' as a *valid pointer*.  But in the second
> call to sysctl, kernel rejects this pointer at line 1299 of
> sys/kern/kern_sysctl.c:
> 
> userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
>     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
> {
> 
> 	(...)
> 
> 	if (old) {
> -->		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
> -->			return (EFAULT);
> 
> and so we'll see

The bug is in useracc, I think.   It should probably return true
immediately if len is zero, since it's okay to read or write zero bytes
from any pointer.  A workaround would be to just skip the sysctl if
there is nothing to read.

-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-current mailing list