[master] c4ceb2d01 Make all mutexen PTHREAD_MUTEX_ERRORCHECK because PTHREAD_MUTEX_NORMAL is still deranged. (See: CERT secure coding POS04-C)

Poul-Henning Kamp phk at FreeBSD.org
Tue Oct 12 15:33:07 UTC 2021


commit c4ceb2d017f1230b21ecc649a1fc692b97e939bb
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Oct 12 13:04:28 2021 +0000

    Make all mutexen PTHREAD_MUTEX_ERRORCHECK because PTHREAD_MUTEX_NORMAL
    is still deranged. (See: CERT secure coding POS04-C)
    
    Also prepare a global condattr_monotime, which will soon be used.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 90b9d781e..14b5c0c09 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -685,6 +685,8 @@ int Lck__Trylock(struct lock *lck, const char *p,  int l);
 void Lck__New(struct lock *lck, struct VSC_lck *, const char *);
 int Lck__Held(const struct lock *lck);
 int Lck__Owned(const struct lock *lck);
+extern pthread_condattr_t condattr_monotime;
+extern pthread_mutexattr_t mtxattr_errorcheck;
 
 /* public interface: */
 void Lck_Delete(struct lock *lck);
diff --git a/bin/varnishd/cache/cache_lck.c b/bin/varnishd/cache/cache_lck.c
index 32b326ee1..af3f306ed 100644
--- a/bin/varnishd/cache/cache_lck.c
+++ b/bin/varnishd/cache/cache_lck.c
@@ -58,8 +58,6 @@ struct ilck {
 	struct VSC_lck		*stat;
 };
 
-static pthread_mutexattr_t attr;
-
 /*--------------------------------------------------------------------*/
 
 static void
@@ -267,7 +265,7 @@ Lck__New(struct lock *lck, struct VSC_lck *st, const char *w)
 	ilck->w = w;
 	ilck->stat = st;
 	ilck->stat->creat++;
-	AZ(pthread_mutex_init(&ilck->mtx, &attr));
+	AZ(pthread_mutex_init(&ilck->mtx, &mtxattr_errorcheck));
 	lck->priv = ilck;
 }
 
@@ -302,8 +300,6 @@ void
 LCK_Init(void)
 {
 
-	AZ(pthread_mutexattr_init(&attr));
-	AZ(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
 #define LOCK(nam)	lck_##nam = Lck_CreateClass(NULL, #nam);
 #include "tbl/locks.h"
 }
diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index 4b98a8a0f..0e81c3928 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -57,6 +57,9 @@ int cache_shutdown = 0;
 volatile struct params		*cache_param;
 static pthread_mutex_t		cache_vrnd_mtx;
 
+pthread_mutexattr_t mtxattr_errorcheck;
+pthread_condattr_t condattr_monotime;
+
 static void
 cache_vrnd_lock(void)
 {
@@ -362,6 +365,12 @@ child_main(int sigmagic, size_t altstksz)
 	malloc_message = child_malloc_fail;
 #endif
 
+	/* Before anything uses pthreads in anger */
+	AZ(pthread_mutexattr_init(&mtxattr_errorcheck));
+	AZ(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK));
+	AZ(pthread_condattr_init(&condattr_monotime));
+	AZ(pthread_condattr_setclock(&condattr_monotime, CLOCK_MONOTONIC));
+
 	cache_param = heritage.param;
 
 	AZ(pthread_key_create(&req_key, NULL));
@@ -372,7 +381,7 @@ child_main(int sigmagic, size_t altstksz)
 
 	THR_SetName("cache-main");
 
-	AZ(pthread_mutex_init(&cache_vrnd_mtx, NULL));
+	AZ(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck));
 	VRND_Lock = cache_vrnd_lock;
 	VRND_Unlock = cache_vrnd_unlock;
 
diff --git a/bin/varnishd/cache/cache_panic.c b/bin/varnishd/cache/cache_panic.c
index 3d01e6090..9123f1ce8 100644
--- a/bin/varnishd/cache/cache_panic.c
+++ b/bin/varnishd/cache/cache_panic.c
@@ -842,7 +842,7 @@ void
 PAN_Init(void)
 {
 
-	AZ(pthread_mutex_init(&panicstr_mtx, NULL));
+	AZ(pthread_mutex_init(&panicstr_mtx, &mtxattr_errorcheck));
 	VAS_Fail_Func = pan_ic;
 	pan_vsb = &pan_vsb_storage;
 	AN(heritage.panic_str);
diff --git a/bin/varnishd/cache/cache_shmlog.c b/bin/varnishd/cache/cache_shmlog.c
index 4fc746eee..fbda72f0b 100644
--- a/bin/varnishd/cache/cache_shmlog.c
+++ b/bin/varnishd/cache/cache_shmlog.c
@@ -614,9 +614,9 @@ VSM_Init(void)
 
 	assert(UINT_MAX % VSL_SEGMENTS == VSL_SEGMENTS - 1);
 
-	AZ(pthread_mutex_init(&vsl_mtx, NULL));
-	AZ(pthread_mutex_init(&vsc_mtx, NULL));
-	AZ(pthread_mutex_init(&vsm_mtx, NULL));
+	AZ(pthread_mutex_init(&vsl_mtx, &mtxattr_errorcheck));
+	AZ(pthread_mutex_init(&vsc_mtx, &mtxattr_errorcheck));
+	AZ(pthread_mutex_init(&vsm_mtx, &mtxattr_errorcheck));
 
 	vsc_lock = vsm_vsc_lock;
 	vsc_unlock = vsm_vsc_unlock;
diff --git a/bin/varnishd/storage/stevedore.c b/bin/varnishd/storage/stevedore.c
index a35e9eb7a..ccacac33c 100644
--- a/bin/varnishd/storage/stevedore.c
+++ b/bin/varnishd/storage/stevedore.c
@@ -175,7 +175,7 @@ STV_open(void)
 	char buf[1024];
 
 	ASSERT_CLI();
-	AZ(pthread_mutex_init(&stv_mtx, NULL));
+	AZ(pthread_mutex_init(&stv_mtx, &mtxattr_errorcheck));
 
 	/* This string was prepared for us before the fork, and should
 	 * point to a configured stevedore. */


More information about the varnish-commit mailing list