mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Test case for 'false ownership' of mutex via TID reuse
Date: Sat, 16 Aug 2014 19:06:11 -0400	[thread overview]
Message-ID: <20140816230611.GA1241@brightrain.aerifal.cx> (raw)

[-- Attachment #1: Type: text/plain, Size: 672 bytes --]

The attached test case exhibits a bug I'm about to fix in musl, and
which also exists in glibc, whereby happening to get the same TID as a
previous mutex owner who exited with the mutex locked allows the new
thread to unlock the mutex. See also:

http://austingroupbugs.net/view.php?id=755

Sadly it's rather slow and impractical to add to libc-test. I'm not
sure if it's possible to run it in a container with a smaller pid/tid
space to get the reuse to happen sooner. It can also have false
negatives if some other process claims the tid before it gets reused
for a new thread in the test process. Ideas to make the test more
practical and reliable would be nice.

Rich

[-- Attachment #2: false_ownership_2.c --]
[-- Type: text/plain, Size: 621 bytes --]

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *f(void *p)
{
	static int init;
	static pthread_mutex_t m;
	pthread_mutexattr_t ma;
	if (!init) {
		init = 1;
		pthread_mutexattr_init(&ma);
		pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK);
		pthread_mutex_init(&m, &ma);
		pthread_mutex_lock(&m);
		pthread_mutexattr_destroy(&ma);
		return 0;
	}
	if (!pthread_mutex_unlock(&m)) {
		printf("unlocked mutex via false ownership\n");
		exit(1);
	}
	return 0;
}


int main()
{
	int i;
	pthread_t td;
	for (i=0; i<100000; i++) {
		pthread_create(&td, 0, f, 0);
		pthread_join(td, 0);
	}
	return 0;
}

             reply	other threads:[~2014-08-16 23:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-16 23:06 Rich Felker [this message]
2014-08-17 10:11 ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140816230611.GA1241@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).