NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/57721: pthread_attr_setstack incorrectly adjusts address as if for guard page



>Number:         57721
>Category:       lib
>Synopsis:       pthread_attr_setstack incorrectly adjusts address as if for guard page
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Nov 24 15:45:00 +0000 2023
>Originator:     Taylor R Campbell
>Release:        current
>Organization:
The NetBSD Threadstacktion
>Environment:
>Description:
pthread_attr_setstack is required by POSIX to make any threads with the attribute _not_ have a guard page:

> If the stackaddr attribute has been set (that is, the caller is allocating and managing its own thread stacks), the guardsize attribute shall be ignored and no protection shall be provided by the implementation. It is the responsibility of the application to manage stack overflow along with stack allocation and management in this case.

NetBSD's libpthread correctly avoids mprotecting any pages PROT_NONE for threads with attributes having pthread_attr_setstack, but it incorrectly adjusts the stack address for the (possibly default) guard size.
>How-To-Repeat:
	void *stackaddr = ...;
	size_t stacksize = ...;
	pthread_attr_t attr;
	pthread_t t;
	int error;

	error = pthread_attr_init(&attr);
	if (error)
		errc(EXIT_FAILURE, error, "pthread_attr_init");
	error = pthread_attr_setstack(&attr, stackaddr, stacksize);
	if (error)
		errc(EXIT_FAILURE, error, "pthread_attr_setstack");
	error = pthread_create(&t, &attr, ...);
	if (error)
		errc(EXIT_FAILURE, error, "pthread_create");

A thread created this way will have a pthread_attr_getstack yield a stack address of (char *)stackaddr + sysctl(vm.thread_guard_size), not stackaddr as it should.
>Fix:
Yes, please!



Home | Main Index | Thread Index | Old Index