Skip to contentSkip to conversation

crypto/af_alg02: fixed read() being stuck

PATCH Superseded Christian Amann 2 participants 3 messages May 7, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 crypto/af_alg02: fixed read() being stuck

On kernels < 4.14 (missing commit 2d97591ef43d) reading from the request socket does not return and the testcase does not finish.

This fix moves the logic to a child thread in order for the parent to handle the timeout and report a message to the user.

---
 testcases/kernel/crypto/Makefile   |  2 ++
 testcases/kernel/crypto/af_alg02.c | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)
testcases/kernel/crypto/Makefile +2 -0
index 76f9308c2..6547e1cb6 100644
@@ -20,3 +20,5 @@ include $(top_srcdir)/include/mk/testcases.mk
 CFLAGS			+= -D_GNU_SOURCE
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+af_alg02: CFLAGS += -pthread
testcases/kernel/crypto/af_alg02.c +28 -1
index a9e820423..7f8c81b66 100644
@@ -7,12 +7,20 @@
  * Regression test for commit ecaaab564978 ("crypto: salsa20 - fix
  * blkcipher_walk API usage"), or CVE-2017-17805.  This test verifies that an
  * empty message can be encrypted with Salsa20 without crashing the kernel.
+ *
+ * read() fix:
+ * Calls read() in child thread in order to manually kill it after a timeout.
+ * With kernels missing commit 2d97591ef43d ("crypto: af_alg - consolidation
+ * of duplicate code") read() does not return.
  */
 
 #include "tst_test.h"
 #include "tst_af_alg.h"
+#include "tst_safe_pthread.h"
+#include <pthread.h>
+#include <errno.h>
 
-static void run(void)
+void *verify_encrypt(void *arg)
 {
 	char buf[16];
 	int reqfd = tst_alg_setup_reqfd("skcipher", "salsa20", NULL, 16);
@@ -22,8 +30,27 @@ static void run(void)
 		tst_res(TPASS, "Successfully \"encrypted\" an empty message");
 	else
 		tst_res(TBROK, "read() didn't return 0");
+	return arg;
+}
+
+static void run(void)
+{
+	pthread_t thr;
+
+	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+	SAFE_PTHREAD_CREATE(&thr, NULL, verify_encrypt, NULL);
+
+	while (pthread_kill(thr, 0) != ESRCH) {
+		if (tst_timeout_remaining() <= 10) {
+			pthread_cancel(thr);
+			tst_brk(TBROK,
+				"Timed out while reading from request socket.");
+		}
+		usleep(1000);
+	}
 }
 
 static struct tst_test test = {
 	.test_all = run,
+	.timeout = 20,
 };
Signature · 1 line
2.16.4
#1 lore raw

PATCH v2 1/1 crypto/af_alg02: fixed read() being stuck

@ Li Wang

Okay, I think I understand it now. It looks much cleaner when done like you suggested. Thanks!

Regards,

Christian

Quoted 77 lines · Christian Amann

On kernels < 4.14 (missing commit 2d97591ef43d) reading from the request socket does not return and the testcase does not finish.

This fix moves the logic to a child thread in order for the parent to handle the timeout and report a message to the user.

---
 testcases/kernel/crypto/Makefile   |  2 ++
 testcases/kernel/crypto/af_alg02.c | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)
testcases/kernel/crypto/Makefile +2 -0
index 76f9308c2..6547e1cb6 100644
@@ -20,3 +20,5 @@ include $(top_srcdir)/include/mk/testcases.mk
 CFLAGS			+= -D_GNU_SOURCE
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+af_alg02: CFLAGS += -pthread
testcases/kernel/crypto/af_alg02.c +28 -1
index a9e820423..7f8c81b66 100644
@@ -7,12 +7,20 @@
  * Regression test for commit ecaaab564978 ("crypto: salsa20 - fix
  * blkcipher_walk API usage"), or CVE-2017-17805.  This test verifies that an
  * empty message can be encrypted with Salsa20 without crashing the kernel.
+ *
+ * read() fix:
+ * Calls read() in child thread in order to manually kill it after a timeout.
+ * With kernels missing commit 2d97591ef43d ("crypto: af_alg - consolidation
+ * of duplicate code") read() does not return.
  */
 
 #include "tst_test.h"
 #include "tst_af_alg.h"
+#include "tst_safe_pthread.h"
+#include <pthread.h>
+#include <errno.h>
 
-static void run(void)
+void *verify_encrypt(void *arg)
 {
 	char buf[16];
 	int reqfd = tst_alg_setup_reqfd("skcipher", "salsa20", NULL, 16);
@@ -22,8 +30,27 @@ static void run(void)
 		tst_res(TPASS, "Successfully \"encrypted\" an empty message");
 	else
 		tst_res(TBROK, "read() didn't return 0");
+	return arg;
+}
+
+static void run(void)
+{
+	pthread_t thr;
+
+	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+	SAFE_PTHREAD_CREATE(&thr, NULL, verify_encrypt, NULL);
+
+	while (pthread_kill(thr, 0) != ESRCH) {
+		if (tst_timeout_remaining() <= 10) {
+			pthread_cancel(thr);
+			tst_brk(TBROK,
+				"Timed out while reading from request socket.");
+		}
+		usleep(1000);
+	}
 }
 
 static struct tst_test test = {
 	.test_all = run,
+	.timeout = 20,
 };
#2 lore raw

PATCH v2 1/1 crypto/af_alg02: fixed read() being stuck

Hi Christian,

Quoted 5 lines · Christian Amann

@ Li Wang

Okay, I think I understand it now. It looks much cleaner when done like you suggested. Thanks!

Thanks for your update, you could try add these additional info in patch note via git note add next time.

  # man git-notes
Quoted 39 lines

Regards,

Christian

Quoted 7 lines · Christian Amann

On kernels < 4.14 (missing commit 2d97591ef43d) reading from the request socket does not return and the testcase does not finish.

This fix moves the logic to a child thread in order for the parent to handle the timeout and report a message to the user.

Review

Quoted 7 lines
---
 testcases/kernel/crypto/Makefile   |  2 ++
 testcases/kernel/crypto/af_alg02.c | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/testcases/kernel/crypto/Makefile

Review

Quoted 10 lines

index 76f9308c2..6547e1cb6 100644

testcases/kernel/crypto/Makefile +2 -0
@@ -20,3 +20,5 @@ include $(top_srcdir)/include/mk/testcases.mk
 CFLAGS                       += -D_GNU_SOURCE
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+af_alg02: CFLAGS += -pthread
(quoted diff) +0 -0

b/testcases/kernel/crypto/af_alg02.c

testcases/kernel/crypto/af_alg02.c line 8

Quoted 6 lines

index a9e820423..7f8c81b66 100644

testcases/kernel/crypto/af_alg02.c +0 -0
@@ -7,12 +7,20 @@
  * Regression test for commit ecaaab564978 ("crypto: salsa20 - fix
  * blkcipher_walk API usage"), or CVE-2017-17805.  This test verifies

that an

Quoted 7 lines

testcases/kernel/crypto/af_alg02.c line 9

Quoted 1 line
testcases/kernel/crypto/af_alg02.c +0 -0
  * empty message can be encrypted with Salsa20 without crashing the

kernel.

testcases/kernel/crypto/af_alg02.c

Quoted 3 lines
testcases/kernel/crypto/af_alg02.c +3 -0
+ *
+ * read() fix:
+ * Calls read() in child thread in order to manually kill it after a

timeout.

The comments should be updated too.

Quoted 5 lines

testcases/kernel/crypto/af_alg02.c line 13

Quoted 1 line
testcases/kernel/crypto/af_alg02.c +1 -0
+ * With kernels missing commit 2d97591ef43d ("crypto: af_alg -

consolidation

Quoted 3 lines
testcases/kernel/crypto/af_alg02.c +1 -0
+ * of duplicate code") read() does not return.
  */
 
Quoted 9 lines

testcases/kernel/crypto/af_alg02.c line 23

Quoted 8 lines
testcases/kernel/crypto/af_alg02.c +4 -1
 #include "tst_test.h"
 #include "tst_af_alg.h"
+#include "tst_safe_pthread.h"
+#include <pthread.h>
+#include <errno.h>
 
-static void run(void)
+void *verify_encrypt(void *arg)

We could set LTP_ATTRIBUTE_UNUSED at the behind of unused parameter to get rid of compiling warning:

void *verify_encrypt(void *arg LTP_ATTRIBUTE_UNUSED)

Quoted 3 lines

testcases/kernel/crypto/af_alg02.c line 25

Quoted 2 lines
testcases/kernel/crypto/af_alg02.c +0 -0
 {
      char buf[16];

TST_CHECKPOINT_WAKE(0);

Quoted 7 lines

testcases/kernel/crypto/af_alg02.c static void run(void) · line 30

Quoted 3 lines
testcases/kernel/crypto/af_alg02.c +0 -0
      int reqfd = tst_alg_setup_reqfd("skcipher", "salsa20", NULL, 16);
@@ -22,8 +30,27 @@ static void run(void)
              tst_res(TPASS, "Successfully \"encrypted\" an empty

message");

Quoted 2 lines
testcases/kernel/crypto/af_alg02.c +0 -0
      else
              tst_res(TBROK, "read() didn't return 0");

Should use 'TFAIL' but 'TBROK' here.

See test-writing-guidelines.txt:

406
-------------------------------------------------------------------------------
407 void tst_res(int ttype, char *arg_fmt, ...);
408
-------------------------------------------------------------------------------
409
410 Printf-like function to report test result, it's mostly used with
ttype:
411

   412  |==============================
   413  | 'TPASS' | Test has passed.
   414  | 'TFAIL' | Test has failed.
   415  | 'TINFO' | General message.
   416  |==============================

...
422
-------------------------------------------------------------------------------
423 void tst_brk(int ttype, char *arg_fmt, ...);
424
-------------------------------------------------------------------------------
425
426 Printf-like function to report error and exit the test, it can be
used with ttype:
427

   428  |============================================================
   429  | 'TBROK' | Something has failed in test preparation phase.
   430  | 'TCONF' | Test is not appropriate for current configuration

431 (syscall not implemented, unsupported arch, ...)
432 |============================================================

Quoted 11 lines

+ return arg;

testcases/kernel/crypto/af_alg02.c static void run(void) · line 42

Quoted 9 lines
testcases/kernel/crypto/af_alg02.c +9 -0
+}
+
+static void run(void)
+{
+     pthread_t thr;
+
+     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+     SAFE_PTHREAD_CREATE(&thr, NULL, verify_encrypt, NULL);
+

A checkpoint is neccessary here, because we'd better confirm the thread has already running before check it by pthread_kill(). TST_CHECKPOINT_WAIT(0);

Quoted 19 lines

testcases/kernel/crypto/af_alg02.c static void run(void)

Quoted 5 lines
testcases/kernel/crypto/af_alg02.c +5 -0
+     while (pthread_kill(thr, 0) != ESRCH) {
+             if (tst_timeout_remaining() <= 10) {
+                     pthread_cancel(thr);
+                     tst_brk(TBROK,
+                             "Timed out while reading from request

socket.");

testcases/kernel/crypto/af_alg02.c static void run(void) · line 56

Quoted 9 lines
testcases/kernel/crypto/af_alg02.c +4 -0
+             }
+             usleep(1000);
+     }
 }
 
 static struct tst_test test = {
      .test_all = run,
+     .timeout = 20,
 };

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20190507/0db6e2b4/attachment-0001.html>