Skip to contentSkip to conversation

Add long running thread support to fuzzy sync and update atomics

PATCH Superseded Richard Palethorpe 2 participants 9 messages Aug 24, 2017

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Add long running thread support to fuzzy sync and update atomics

Hello,

There is still atleast one feature I would like to add to the fuzzy sync library (sliding window for the delay), but I think this is enough for now. I have made quite a number of additions to the atomics fallbacks. I have updated all the existing architectures, except ARC, and added SPARC. I have nothing against ARC, I just don't have any hardware or an emulator to hand.

Thank you,
Richard.

Richard Palethorpe (6):
tst_atomic: Add load, store, cmpxchg and use __atomic builtins
tst_atomic: Add atomic store, load and cmpxchg tests
fzsync: Add long running thread support and deviation stats Convert cve-2016-7117 test to use long running threads Convert cve-2014-0196 to use long running threads Convert cve-2017-2671 to use long running threads

 configure.ac                  |   1 +
 include/tst_atomic.h          | 381 ++++++++++++++++++++++++++++++++++++++----
 include/tst_fuzzy_sync.h      | 224 ++++++++++++++++++++++---
 lib/newlib_tests/.gitignore   |   1 +
 lib/newlib_tests/Makefile     |   1 +
 lib/newlib_tests/test15.c     | 160 ++++++++++++++++++
 m4/ltp-atomic.m4              |  34 ++++
 testcases/cve/cve-2014-0196.c |  50 +++---
 testcases/cve/cve-2016-7117.c |  43 +++--
 testcases/cve/cve-2017-2671.c |  30 ++--
 10 files changed, 826 insertions(+), 99 deletions(-)
 create mode 100644 lib/newlib_tests/test15.c
 create mode 100644 m4/ltp-atomic.m4
Signature · 1 line
2.14.1
#1 lore raw

PATCH 1/1 CVE: Add some existing tests to runtest file

Add some CVE tests to the CVE runtest file from before it was created.

---
 runtest/cve | 4 ++++
 1 file changed, 4 insertions(+)
runtest/cve +4 -0
index 6e3e52d3a..6d7cb3f76 100644
@@ -1,6 +1,10 @@
 # Tests which check for vulnerabilities by CVE number
+cve-2011-0999 thp01 -I 120
+cve-2011-2183 ksm05 -I 10
+cve-2011-2496 vma03
 cve-2012-0957 cve-2012-0957
 cve-2014-0196 cve-2014-0196
+cve-2015-0235 gethostbyname_r01
 cve-2016-4997 cve-2016-4997
 cve-2016-5195 dirtyc0w
 cve-2016-7117 cve-2016-7117
Signature · 1 line
2.13.3
#3 lore raw

PATCH 1/6 tst_atomic: Add load, store, cmpxchg and use __atomic builtins

In reply to Richard Palethorpe

Also add more fallback inline assembly for the existing architectures and add SPARC64. Use the __atomic_* compiler intrinsics when available and provide the option to use the C++11 memory model.

---
 configure.ac             |   1 +
 include/tst_atomic.h     | 381 +++++++++++++++++++++++++++++++++++++++++++----
 include/tst_fuzzy_sync.h |   1 +
 m4/ltp-atomic.m4         |  34 +++++
 4 files changed, 389 insertions(+), 28 deletions(-)
 create mode 100644 m4/ltp-atomic.m4
configure.ac +1 -0
index 23e583dd8..e4f26fe88 100644
@@ -194,5 +194,6 @@ LTP_CHECK_BUILTIN_CLEAR_CACHE
 LTP_CHECK_MMSGHDR
 LTP_CHECK_UNAME_DOMAINNAME
 LTP_CHECK_X_TABLES
+LTP_CHECK_ATOMIC_MEMORY_MODEL
 
 AC_OUTPUT
include/tst_atomic.h +353 -28

434 lines not on this page · raw message

include/tst_fuzzy_sync.h +1 -0
index 229217495..f97137c35 100644
@@ -32,6 +32,7 @@
 
 #include <sys/time.h>
 #include <time.h>
+#include "tst_atomic.h"
 
 #ifndef CLOCK_MONOTONIC_RAW
 # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
m4/ltp-atomic.m4 new file +34 -0
new file mode 100644
index 000000000..165456de5
@@ -0,0 +1,34 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2016
+dnl
+dnl This program is free software;  you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY;  without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+dnl the GNU General Public License for more details.
+dnl
+
+AC_DEFUN([LTP_CHECK_ATOMIC_MEMORY_MODEL],[dnl
+	AC_MSG_CHECKING([for __atomic_* compiler builtins])
+	AC_LINK_IFELSE([AC_LANG_SOURCE([
+int main(void) {
+	int i = 0, j = 0;
+	__atomic_add_fetch(&i, 1, __ATOMIC_ACQ_REL);
+	__atomic_load_n(&i, __ATOMIC_SEQ_CST);
+	__atomic_compare_exchange_n(&i, &j, 0, 0, __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
+	__atomic_store_n(&i, 0, __ATOMIC_RELAXED);
+	return i;
+}])],[has_saac="yes"])
+
+if test "x$has_saac" = xyes; then
+	AC_DEFINE(HAVE_ATOMIC_MEMORY_MODEL,1,
+	          [Define to 1 if you have the __atomic_* compiler builtins])
+	AC_MSG_RESULT(yes)
+else
+	AC_MSG_RESULT(no)
+fi
+])
Signature · 1 line
2.14.1
#4 lore raw

PATCH 2/6 tst_atomic: Add atomic store, load and cmpxchg tests

In reply to Richard Palethorpe

Similar to test09.

---
 lib/newlib_tests/.gitignore |   1 +
 lib/newlib_tests/Makefile   |   1 +
 lib/newlib_tests/test15.c   | 160 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+)
 create mode 100644 lib/newlib_tests/test15.c
lib/newlib_tests/.gitignore +1 -0
index 8e120074e..7d47a6531 100644
@@ -12,6 +12,7 @@ test11
 test12
 test13
 test14
+test15
 tst_device
 tst_safe_fileops
 tst_res_hexd
lib/newlib_tests/Makefile +1 -0
index e9976e5ba..e86d8f23a 100644
@@ -7,6 +7,7 @@ LDLIBS			+= -lltp
 
 test08: CFLAGS+=-pthread
 test09: CFLAGS+=-pthread
+test15: CFLAGS+=-pthread
 
 ifeq ($(ANDROID),1)
 FILTER_OUT_MAKE_TARGETS	+= test08
lib/newlib_tests/test15.c new file +160 -0

160 lines not on this page · raw message

Signature · 1 line
2.14.1
#5 lore raw

PATCH 3/6 fzsync: Add long running thread support and deviation stats

In reply to Richard Palethorpe

Introduce a new synchronisation method which causes two threads to stop and wait for each other. Continuing only when both threads have signaled their readiness. Previously we would start a new thread in each iteration which creates a variable delay in either the parent or child.

Using two long running threads significantly reduces the time needed to perform a single race and may also increase timing stability on some architectures.

This does not replace the delay method of synchronisation which can be used in addition to the new method.

---
 include/tst_fuzzy_sync.h | 223 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 203 insertions(+), 20 deletions(-)
include/tst_fuzzy_sync.h +203 -20
index f97137c35..eefff700d 100644
@@ -17,23 +17,35 @@
 /*
  * Fuzzy Synchronisation - abreviated to fzsync
  *
- * This library is intended to help reproduce race conditions while running in
- * a loop. You can use it to measure the time at which two functions are
- * called in different threads. Then calculate the average time gap between
- * the function calls and introduce a delay in one thread to synchronise the
- * calls.
+ * This library is intended to help reproduce race conditions by providing two
+ * thread synchronisation mechanisms. The first is a 'checkpoint' system where
+ * each thread will wait indefinitely for the other to enter a checkpoint
+ * before continuing. This is acheived by calling tst_fzsync_wait() and/or
+ * tst_fzsync_wait_update() at the points you want to synchronise in each
+ * thread.
  *
- * It is called 'fuzzy' synchronisation because the time gap will naturally vary
- * due to environmental factors. It is not a 'hard' synchronisation mechanism
- * like lockstepping.
+ * The second takes the form a of a delay which is calculated by measuring the
+ * time difference between two points in each thread and comparing it to the
+ * desired difference (default is zero). Using a delay allows you to
+ * synchronise the threads at a point outside of your direct control
+ * (e.g. inside the kernel) or just increase the accuracy for the first
+ * mechanism. It is acheived using tst_fzsync_delay_{a,b}(),
+ * tst_fzsync_time_{a,b}() and tst_fzsync[_wait_]update().
  *
- * For a usage example see testcases/cve/cve-2017-2671.c
+ * For a usage example see testcases/cve/cve-2016-7117.c or just run
+ * 'git grep tst_fuzzy_sync.h'
  */
 
 #include <sys/time.h>
 #include <time.h>
+#include <math.h>
 #include "tst_atomic.h"
 
+#ifdef LTP_FZSYNC_USE_FUTEX
+# include <sys/syscall.h>
+# include <linux/futex.h>
+#endif
+
 #ifndef CLOCK_MONOTONIC_RAW
 # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
 #endif
@@ -44,12 +56,17 @@
  * @avg_diff_trgt: The desired average time difference, defaults to 0
  * @avg_alpha: The rate at which old diff samples are forgotten,
  *             defaults to 0.25
+ * @avg_dev: Absolute average deviation
  * @a: The time at which call site A was last passed
  * @b: The time at which call site B was last passed
  * @delay: The size of the delay, positive to delay A, negative to delay B
  * @delay_inc: The step size of a delay increment, defaults to 10
  * @update_gap: The number of iterations between recalculating the delay.
  *              Defaults to 0xF and must be of the form $2^n - 1$
+ * @info_gap: The number of iterations between printing some statistics.
+ *            Defaults to 0x7FFFF and must also be one less than a power of 2.
+ * @entry: Used by fzsync_pair_wait()
+ * @exit: Used by fzsync_pair_wait()
  *
  * This contains all the necessary state for synchronising two points A and
  * B. Where A is the time of an event in one process and B is the time of an
@@ -59,11 +76,18 @@ struct tst_fzsync_pair {
 	double avg_diff;
 	double avg_diff_trgt;
 	double avg_alpha;
+	double avg_dev;
 	struct timespec a;
 	struct timespec b;
 	long delay;
 	long delay_inc;
 	int update_gap;
+	int info_gap;
+	int entry;
+	int exit;
+#ifdef LTP_FZSYNC_USE_FUTEX
+	int exit;
+#endif
 };
 
 /**
@@ -71,14 +95,19 @@ struct tst_fzsync_pair {
  */
 #define TST_FZSYNC_PAIR_INIT {	\
 	.avg_alpha = 0.25,	\
-	.delay_inc = 10,	\
-	.update_gap = 0xF	\
+	.delay_inc = 1,	        \
+	.update_gap = 0xF,	\
+	.info_gap = 0x7FFFF     \
 }
 
+/**
+ * tst_fzsync_pair_info - Print some synchronisation statistics
+ */
 static void tst_fzsync_pair_info(struct tst_fzsync_pair *pair)
 {
-	tst_res(TINFO, "avg_diff = %.5gns, delay = %05ld loops",
-		pair->avg_diff, pair->delay);
+	tst_res(TINFO,
+		"avg_diff = %.0fns, avg_dev = %.0fns, delay = %05ld loops",
+		pair->avg_diff, pair->avg_dev, pair->delay);
 }
 
 /**
@@ -133,18 +162,15 @@ static inline void tst_fzsync_time_b(struct tst_fzsync_pair *pair)
 }
 
 /**
- * tst_exp_moving_avg - Exponential moving average
+ * TST_EXP_MOVING_AVG - Exponential moving average
  * @alpha: The preference for recent samples over old ones.
  * @sample: The current sample
  * @prev_avg: The average of the all the previous samples
  *
  * Returns average including the current sample.
  */
-static inline double tst_exp_moving_avg(double alpha, long sample,
-					double prev_avg)
-{
-	return alpha * sample + (1.0 - alpha) * prev_avg;
-}
+#define TST_EXP_MOVING_AVG(alpha, sample, prev_avg)\
+	(alpha * sample + (1.0 - alpha) * prev_avg)
 
 /**
  * tst_fzsync_pair_update - Recalculate the delay
@@ -169,8 +195,16 @@ static void tst_fzsync_pair_update(int loop_index, struct tst_fzsync_pair *pair)
 	double target = pair->avg_diff_trgt;
 	double avg = pair->avg_diff;
 
+	if (pair->a.tv_sec > pair->b.tv_sec)
+		pair->a.tv_nsec += 1000000000;
+	else if (pair->a.tv_sec < pair->b.tv_sec)
+		pair->b.tv_nsec += 1000000000;
+
 	diff = pair->a.tv_nsec - pair->b.tv_nsec;
-	avg = tst_exp_moving_avg(pair->avg_alpha, diff, avg);
+	avg = TST_EXP_MOVING_AVG(pair->avg_alpha, diff, avg);
+	pair->avg_dev = TST_EXP_MOVING_AVG(pair->avg_alpha,
+					   fabs(diff - avg),
+					   pair->avg_dev);
 
 	if (!(loop_index & pair->update_gap)) {
 		if (avg > target)
@@ -179,5 +213,154 @@ static void tst_fzsync_pair_update(int loop_index, struct tst_fzsync_pair *pair)
 			pair->delay += inc;
 	}
 
+	if (!(loop_index & pair->info_gap))
+		tst_fzsync_pair_info(pair);
+
 	pair->avg_diff = avg;
 }
+
+/**
+ * tst_fzsync_pair_wait - Wait for the other thread
+ *
+ * Use this if you need an additional synchronisation point in a thread. See
+ * tst_fzsync_pair_wait_update().
+ *
+ * Returns a non-zero value if the thread should continue otherwise the
+ * calling thread should exit.
+ */
+static inline int tst_fzsync_pair_wait(struct tst_fzsync_pair *pair)
+{
+	int n = tst_atomic_add_return_(1, &pair->entry, LTP_ACQUIRE);
+
+#ifndef LTP_FZSYNC_USE_FUTEX
+	do {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->entry, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+	} while (n == 1);
+
+	tst_atomic_add_return_(1, &pair->exit, LTP_ACQUIRE);
+	do {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->exit, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+	} while (n == 1);
+
+#else
+	if (n == 1) {
+		syscall(SYS_futex, &pair->entry, FUTEX_WAIT, 1, 0, 0);
+	} else {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->entry, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+		syscall(SYS_futex, &pair->entry, FUTEX_WAKE, 1, 0, 0);
+	}
+
+	n = tst_atomic_add_return_(1, &pair->exit, LTP_ACQUIRE);
+	if (n == 1) {
+		syscall(SYS_futex, &pair->exit, FUTEX_WAIT, 1, 0, 0);
+	} else {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->exit, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+		syscall(SYS_futex, &pair->exit, FUTEX_WAKE, 1, 0, 0);
+	}
+#endif
+	return n < 3;
+}
+
+/**
+ * tst_fzsync_pair_wait_update - Wait for the other thread and then recalculate
+ *
+ * This allows you to have two long running threads which wait for each other
+ * every iteration. So each thread will exit this function@approximately
+ * the same time. It also updates the delay values in a thread safe manner.
+ *
+ * You must call this function in both threads the same number of times each
+ * iteration. So a call in one thread must match with a call in the
+ * other. Make sure that calls to tst_fzsync_pair_wait() and
+ * tst_fzsync_pair_wait_update() happen in the same order in each thread. That
+ * is, make sure that a call to tst_fzsync_pair_wait_update() in one thread
+ * corresponds to a call to tst_fzsync_pair_wait_update() in the other.
+ *
+ * Implementation notes: [ Because we expect to run in a loop, we have to
+ * prevent the scenario where one thread 'overlaps' the other. For example
+ * thread A may be spinning inside the wait function, when it is then put to
+ * sleep by the kernel. Thread B then enters the wait function, resets the
+ * atomic variable to zero and exits again. Then, before A is woken, B
+ * re-enters the wait function on the next iteration and increments the atomic
+ * variable to one.
+ *
+ * A then wakes and continues to spin because the atomic variable is set to
+ * one. B is also spinning waiting for A to increment the atomic variable. In
+ * order to prevent this we use two atomic variables; entry and exit. If one
+ * thread begins to race ahead while the other is sleeping in the entry spin
+ * state then it will transition to the exit spin state and stay there until
+ * the other thread joins it.
+ *
+ * When setting the atomic variables to zero we use cmpxchg to prevent the
+ * exit signal (the atomic variables are set to any value > 2) from being
+ * overwritten. ]
+ *
+ * Returns a non-zero value if the calling thread should continue to loop. If
+ * it returns zero then tst_fzsync_exit() has been called and you must exit
+ * the thread.
+ */
+static inline int tst_fzsync_pair_wait_update(struct tst_fzsync_pair *pair)
+{
+	static int loop_index;
+	int n = tst_atomic_add_return_(1, &pair->entry, LTP_ACQ_REL);
+
+#ifndef LTP_FZSYNC_USE_FUTEX
+	if (n == 2) {
+		loop_index++;
+		tst_fzsync_pair_update(loop_index, pair);
+		tst_atomic_cmpxchg_(&pair->entry, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+	}
+
+	do {
+		n = tst_atomic_load_(&pair->entry, LTP_ACQUIRE);
+	} while (n && n < 3);
+
+	tst_atomic_add_return_(1, &pair->exit, LTP_ACQUIRE);
+	do {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->exit, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+	} while (n == 1);
+#else
+	if (n == 2) {
+		loop_index++;
+		tst_fzsync_pair_update(loop_index, pair);
+		tst_atomic_cmpxchg_(&pair->entry, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+		syscall(SYS_futex, &pair->entry, FUTEX_WAKE, 1, 0, 0);
+	} else {
+		syscall(SYS_futex, &pair->entry, FUTEX_WAIT, 1, 0, 0);
+	}
+
+	n = tst_atomic_add_return_(1, &pair->exit, LTP_ACQUIRE);
+	if (n == 1) {
+		syscall(SYS_futex, &pair->exit, FUTEX_WAIT, 1, 0, 0);
+	} else {
+		n = 2;
+		tst_atomic_cmpxchg_(&pair->exit, &n, 0,
+				    LTP_RELEASE, LTP_ACQUIRE);
+		syscall(SYS_futex, &pair->exit, FUTEX_WAKE, 1, 0, 0);
+	}
+#endif
+	return n < 3;
+}
+
+/**
+ * tst_fzsync_pair_exit - Signal that the other thread should exit
+ *
+ * Causes tst_fzsync_pair_wait() and tst_fzsync_pair_wait_update() to return
+ * 0.
+ */
+static inline void tst_fzsync_pair_exit(struct tst_fzsync_pair *pair)
+{
+	tst_atomic_store_(3, &pair->exit, LTP_RELEASE);
+	tst_atomic_store_(3, &pair->entry, LTP_RELEASE);
+}
Signature · 1 line
2.14.1
#6 lore raw

PATCH 4/6 Convert cve-2016-7117 test to use long running threads

In reply to Richard Palethorpe
---
 testcases/cve/cve-2016-7117.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)
testcases/cve/cve-2016-7117.c +25 -18
index deb667761..e08f8cd20 100644
@@ -94,30 +94,44 @@ static struct mmsghdr msghdrs[2] = {
 static char rbuf[sizeof(MSG)];
 static struct timespec timeout = { .tv_sec = RECV_TIMEOUT };
 static struct tst_fzsync_pair fzsync_pair = TST_FZSYNC_PAIR_INIT;
+static pthread_t pt_send;
+static void *send_and_close(void *);
+
+static void setup(void)
+{
+	SAFE_PTHREAD_CREATE(&pt_send, 0, send_and_close, 0);
+}
 
 static void cleanup(void)
 {
 	close(socket_fds[0]);
 	close(socket_fds[1]);
+
+	if (pt_send) {
+		tst_fzsync_pair_exit(&fzsync_pair);
+		SAFE_PTHREAD_JOIN(pt_send, 0);
+	}
 }
 
 static void *send_and_close(void *arg)
 {
-	send(socket_fds[0], MSG, sizeof(MSG), 0);
-	send(socket_fds[0], MSG, sizeof(MSG), 0);
-
-	tst_fzsync_delay_b(&fzsync_pair);
+	while(tst_fzsync_pair_wait_update(&fzsync_pair)) {
+		send(socket_fds[0], MSG, sizeof(MSG), 0);
+		send(socket_fds[0], MSG, sizeof(MSG), 0);
 
-	close(socket_fds[0]);
-	close(socket_fds[1]);
-	tst_fzsync_time_b(&fzsync_pair);
+		tst_fzsync_delay_b(&fzsync_pair);
 
+		close(socket_fds[0]);
+		close(socket_fds[1]);
+		tst_fzsync_time_b(&fzsync_pair);
+		if (!tst_fzsync_pair_wait(&fzsync_pair))
+			break;
+	}
 	return arg;
 }
 
 static void run(void)
 {
-	pthread_t pt_send;
 	int i, stat, too_early_count = 0;
 
 	msghdrs[0].msg_hdr.msg_iov->iov_base = (void *)&rbuf;
@@ -126,8 +140,7 @@ static void run(void)
 		if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, socket_fds))
 			tst_brk(TBROK | TERRNO, "Socket creation failed");
 
-		SAFE_PTHREAD_CREATE(&pt_send, 0, send_and_close, 0);
-
+		tst_fzsync_pair_wait_update(&fzsync_pair);
 		tst_fzsync_delay_a(&fzsync_pair);
 
 		stat = tst_syscall(__NR_recvmmsg,
@@ -140,20 +153,14 @@ static void run(void)
 		else if (stat < 0)
 			tst_res(TWARN | TERRNO, "recvmmsg failed unexpectedly");
 
-		SAFE_PTHREAD_JOIN(pt_send, 0);
-
-		tst_fzsync_pair_update(i, &fzsync_pair);
-		if (!(i & 0x7FFFF)) {
-			tst_res(TINFO, "Too early: %.1f%%",
-				100 * too_early_count / (float)i);
-			tst_fzsync_pair_info(&fzsync_pair);
-		}
+		tst_fzsync_pair_wait(&fzsync_pair);
 	}
 
 	tst_res(TPASS, "Nothing happened after %d attempts", ATTEMPTS);
 }
 
 static struct tst_test test = {
+	.setup = setup,
 	.test_all = run,
 	.cleanup = cleanup,
 	.min_kver = "2.6.33",
Signature · 1 line
2.14.1
#7 lore raw

PATCH 5/6 Convert cve-2014-0196 to use long running threads

In reply to Richard Palethorpe
---
 testcases/cve/cve-2014-0196.c | 50 ++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 22 deletions(-)
testcases/cve/cve-2014-0196.c +28 -22
index 4e2b3f582..ffb0aad3f 100644
@@ -51,11 +51,13 @@
 #define ATTEMPTS      0x7000
 #define BUFLEN        512
 
-static int master_fd, slave_fd;
+static volatile int master_fd, slave_fd;
 static int filler_ptys[ONEOFF_ALLOCS * 2];
 static int target_ptys[RUN_ALLOCS * 2];
 static char buf[BUFLEN];
 
+static pthread_t overwrite_thread;
+static void *overwrite_thread_fn(void *);
 static struct tst_fzsync_pair fzsync_pair = TST_FZSYNC_PAIR_INIT;
 
 static void create_pty(int *amaster, int *aslave)
@@ -68,35 +70,40 @@ static void setup(void)
 {
 	int i;
 
-	fzsync_pair.delay_inc = 100;
 	for (i = 0; i < ONEOFF_ALLOCS; i++) {
 		create_pty(&filler_ptys[i],
 			   &filler_ptys[i + ONEOFF_ALLOCS]);
 	}
+
+	fzsync_pair.info_gap = 0xFFF;
+	SAFE_PTHREAD_CREATE(&overwrite_thread, NULL,
+			    overwrite_thread_fn, NULL);
 }
 
-static void *overwrite_thread_fn(void *p)
+static void *overwrite_thread_fn(void *p LTP_ATTRIBUTE_UNUSED)
 {
-	tst_fzsync_delay_b(&fzsync_pair);
-	tst_fzsync_time_b(&fzsync_pair);
-
-	SAFE_WRITE(0, slave_fd, buf, BUFLEN - 1);
-	SAFE_WRITE(0, slave_fd, buf, BUFLEN - 1);
-	SAFE_WRITE(0, slave_fd, buf, BUFLEN);
-
-	return p;
+	while(tst_fzsync_pair_wait_update(&fzsync_pair)) {
+		tst_fzsync_delay_b(&fzsync_pair);
+		tst_fzsync_time_b(&fzsync_pair);
+
+		SAFE_WRITE(0, slave_fd, buf, BUFLEN - 1);
+		SAFE_WRITE(0, slave_fd, buf, BUFLEN - 1);
+		SAFE_WRITE(0, slave_fd, buf, BUFLEN);
+		if (!tst_fzsync_pair_wait(&fzsync_pair))
+			break;
+	}
+	return 0;
 }
 
 static void run(void)
 {
 	struct termios t;
-	pthread_t overwrite_thread;
 	int i, j;
 
 	tst_res(TINFO, "Attempting to overflow into a tty_struct...");
 
 	for (i = 0; i < ATTEMPTS; i++) {
-		create_pty(&master_fd, &slave_fd);
+		create_pty((int *)&master_fd, (int *)&slave_fd);
 
 		for (j = 0; j < RUN_ALLOCS; j++)
 			create_pty(&target_ptys[j],
@@ -111,20 +118,12 @@ static void run(void)
 		t.c_lflag |= ECHO;
 		tcsetattr(master_fd, TCSANOW, &t);
 
-		SAFE_PTHREAD_CREATE(&overwrite_thread, NULL,
-				    overwrite_thread_fn, NULL);
+		tst_fzsync_pair_wait_update(&fzsync_pair);
 
 		tst_fzsync_delay_a(&fzsync_pair);
 		tst_fzsync_time_a(&fzsync_pair);
 		SAFE_WRITE(0, master_fd, "A", 1);
 
-		SAFE_PTHREAD_JOIN(overwrite_thread, NULL);
-
-		tst_fzsync_pair_update(i, &fzsync_pair);
-
-		if (!(i & 0x1FFF))
-			tst_fzsync_pair_info(&fzsync_pair);
-
 		for (j = 0; j < RUN_ALLOCS; j++) {
 			if (j == RUN_ALLOCS / 2)
 				continue;
@@ -139,6 +138,8 @@ static void run(void)
 		ioctl(slave_fd, 0xdeadbeef);
 		SAFE_CLOSE(master_fd);
 		SAFE_CLOSE(slave_fd);
+
+		tst_fzsync_pair_wait(&fzsync_pair);
 	}
 
 	tst_res(TPASS, "Nothing bad happened, probably.");
@@ -148,6 +149,11 @@ static void cleanup(void)
 {
 	int i;
 
+	if (overwrite_thread) {
+		tst_fzsync_pair_exit(&fzsync_pair);
+		SAFE_PTHREAD_JOIN(overwrite_thread, NULL);
+	}
+
 	for (i = 0; i < ONEOFF_ALLOCS * 2; i++)
 		close(filler_ptys[i]);
 	close(master_fd);
Signature · 1 line
2.14.1
#8 lore raw

PATCH 6/6 Convert cve-2017-2671 to use long running threads

In reply to Richard Palethorpe
---
 testcases/cve/cve-2017-2671.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
testcases/cve/cve-2017-2671.c +19 -11
index 77744db42..ce4e3f002 100644
@@ -49,13 +49,15 @@
 
 #include "tst_fuzzy_sync.h"
 
-#define ATTEMPTS 0xFFFF
+#define ATTEMPTS 0x80000
 #define PING_SYSCTL_PATH "/proc/sys/net/ipv4/ping_group_range"
 
 static int sockfd;
 static unsigned int ping_min_grp, ping_max_grp;
 static struct tst_fzsync_pair fzsync_pair = TST_FZSYNC_PAIR_INIT;
 static struct sockaddr_in iaddr, uaddr;
+static pthread_t thrd;
+static void *connect_b(void *);
 
 static void setup(void)
 {
@@ -73,13 +75,20 @@ static void setup(void)
 	SAFE_FILE_PRINTF(PING_SYSCTL_PATH, "0 0");
 
 	sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
+	SAFE_PTHREAD_CREATE(&thrd, 0, connect_b, 0);
 	tst_res(TINFO, "Created ping socket, attempting to race...");
 }
 
 static void cleanup(void)
 {
+	if (thrd) {
+		tst_fzsync_pair_exit(&fzsync_pair);
+		SAFE_PTHREAD_JOIN(thrd, NULL);
+	}
+
 	if (sockfd > 0)
 		SAFE_CLOSE(sockfd);
+
 	if (ping_min_grp | ping_max_grp)
 		SAFE_FILE_PRINTF(PING_SYSCTL_PATH, "%u %u",
 				 ping_min_grp, ping_max_grp);
@@ -87,32 +96,31 @@ static void cleanup(void)
 
 static void *connect_b(void * param LTP_ATTRIBUTE_UNUSED)
 {
-	tst_fzsync_delay_b(&fzsync_pair);
-	connect(sockfd, (struct sockaddr *)&uaddr, sizeof(uaddr));
-	tst_fzsync_time_b(&fzsync_pair);
+	while(tst_fzsync_pair_wait_update(&fzsync_pair)) {
+		tst_fzsync_delay_b(&fzsync_pair);
+		connect(sockfd, (struct sockaddr *)&uaddr, sizeof(uaddr));
+		tst_fzsync_time_b(&fzsync_pair);
+		if (!tst_fzsync_pair_wait(&fzsync_pair))
+			break;
+	}
 
 	return 0;
 }
 
 static void run(void)
 {
-	pthread_t thrd;
 	int i;
 
 	for (i = 0; i < ATTEMPTS; i++) {
 		SAFE_CONNECT(sockfd,
 			     (struct sockaddr *)&iaddr, sizeof(iaddr));
-		SAFE_PTHREAD_CREATE(&thrd, 0, connect_b, 0);
 
+		tst_fzsync_pair_wait_update(&fzsync_pair);
 		tst_fzsync_delay_a(&fzsync_pair);
 		connect(sockfd, (struct sockaddr *)&uaddr, sizeof(uaddr));
 		tst_fzsync_time_a(&fzsync_pair);
 
-		SAFE_PTHREAD_JOIN(thrd, 0);
-		tst_fzsync_pair_update(i, &fzsync_pair);
-
-		if (!(i & 0x7FFF))
-			tst_fzsync_pair_info(&fzsync_pair);
+		tst_fzsync_pair_wait(&fzsync_pair);
 	}
 
 	tst_res(TPASS, "We didn't crash");
Signature · 1 line
2.14.1