]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tests: treat all signals as error
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Thu, 3 Jan 2019 08:51:34 +0000 (09:51 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 4 Jan 2019 16:12:43 +0000 (17:12 +0100)
Previously we were only treating SIGSEGV as error though there is
no reason to treat other signals as success and they may hide an
actual error case (e.g., when SIGPIPE is received). With this change we
treat any signals received by the child except SIGTERM as error, and
we ensure that SIGPIPE is ignored in all tests.

This also updates tests/slow/cipher-api-test.c to test failures with
SIGABRT or otherwise consistently.

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
12 files changed:
tests/alpn-server-prec.c
tests/key-material-set-dtls.c
tests/mini-alpn.c
tests/mini-dtls-discard.c
tests/mini-dtls-heartbeat.c
tests/mini-dtls-record.c
tests/mini-dtls-srtp.c
tests/mini-record-failure.c
tests/prf.c
tests/psk-file.c
tests/slow/cipher-api-test.c
tests/utils.h

index e8da9ebada312ddc8647f11c0b3a4bdf24714697..0461c5d39a0d698ac2e175b2b6aea3d0a9acdbda 100644 (file)
@@ -45,6 +45,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -262,6 +263,8 @@ static void start(const char *p1, const char *p2, const char *cp1, const char *c
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 692c7a6574201d8b24cc2fbe0fc9f876dd4645be..4a569162bb0d8da95ce0a3b80f34ad0662be1dd9 100644 (file)
@@ -42,6 +42,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -354,6 +355,8 @@ static void start(void)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 304d0a171c87a5e086c5b61924a0173958f21632..bed981451a7619b71104378893da0d9db112d0f3 100644 (file)
@@ -41,6 +41,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -264,6 +265,8 @@ static void start(const char *p1, const char *p2)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index e9201acaeaedfe6dc6502c7b873fcb3070e24156..97bc6de29be4ceeaa5e9f14b509f2a43c4ae0e5c 100644 (file)
@@ -44,6 +44,7 @@ int main()
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <errno.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 #include <assert.h>
@@ -266,6 +267,8 @@ void start(const char *prio)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index bb6fa7e528f2dabd30d6d6b459f1e3df3a35e836..cb575f9b2cf51221c0e45a47e5488669524f2f41 100644 (file)
@@ -43,6 +43,7 @@ int main()
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -312,6 +313,8 @@ static void start(int server_initiated)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 63bba89aaf7bc1a01ed7a676de9ace6b091ab45b..4d92d81c54602b999818535a33b60f54214687e1 100644 (file)
@@ -45,6 +45,7 @@ int main()
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -367,6 +368,8 @@ static void start(void)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 93c06820f8104538ce6245cfb97f92eccad4fb81..78c03b3d1e1cd2453f9e3c1294f6116760b59ce1 100644 (file)
@@ -43,6 +43,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -304,6 +305,8 @@ static void start(int profile)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 02b05462702a3626c71c4c94b83a636fc83b830c..46df2a1900e833709a7dad489047087b3cd4cb19 100644 (file)
@@ -331,6 +331,7 @@ static void ch_handler(int sig)
 
 void doit(void)
 {
+       signal(SIGPIPE, SIG_IGN);
        signal(SIGCHLD, ch_handler);
 
        start("aes-cbc", AES_CBC, 1);
index 78526cfba84712211d9b572d60ae484ca51bbb51..ff839fe73ca37bf3aa81ee3871ca1a14d4ff642c 100644 (file)
@@ -41,6 +41,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#include <signal.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/dtls.h>
 
@@ -423,6 +424,8 @@ static void start(void)
        int fd[2];
        int ret;
 
+       signal(SIGPIPE, SIG_IGN);
+
        ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
        if (ret < 0) {
                perror("socketpair");
index 2512086e0fd9daad6d82d818963b801055c7c5fb..0a6eef4b87e4ea9b8eac48e630dda07734a9d1e1 100644 (file)
@@ -48,6 +48,7 @@ int main(int argc, char **argv)
 #include <sys/wait.h>
 #endif
 #include <unistd.h>
+#include <signal.h>
 #include <assert.h>
 #include <gnutls/gnutls.h>
 
@@ -304,6 +305,8 @@ void run_test3(const char *prio, const char *sprio, const char *user, const gnut
        int err;
        int sockets[2];
 
+       signal(SIGPIPE, SIG_IGN);
+
        if (expect_fail_serv || expect_fail_cli) {
                success("ntest %s (user:%s)\n", prio, user);
        } else {
@@ -407,7 +410,7 @@ void doit(void)
        run_test2("NORMAL:-VERS-ALL:+VERS-TLS1.3:+DHE-PSK:-GROUP-ALL:+GROUP-FFDHE2048:+GROUP-FFDHE4096", "NORMAL:-VERS-ALL:+VERS-TLS1.3:+DHE-PSK:-GROUP-ALL:+GROUP-FFDHE4096", "jas", &key, 0, GNUTLS_KX_DHE_PSK, 0, 0);
 
        /* try without server credentials */
-       run_test3("NORMAL:-VERS-ALL:+VERS-TLS1.3:+PSK:+DHE-PSK", NULL, "jas", &key, 1, 0, 0, GNUTLS_E_FATAL_ALERT_RECEIVED, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
+       run_test3("NORMAL:-VERS-ALL:+VERS-TLS1.3:+PSK:+DHE-PSK", NULL, "jas", &key, 1, 0, 0, GNUTLS_E_PUSH_ERROR, GNUTLS_E_INSUFFICIENT_CREDENTIALS);
 }
 
 #endif                         /* _WIN32 */
index 66b164037da4229a445599394e4ba2bada402209..4a9cd1ea3012f0b9d6a2798742a66f8f7cecd0da 100644 (file)
@@ -48,14 +48,54 @@ static void tls_log_func(int level, const char *str)
        fprintf(stderr, "<%d>| %s", level, str);
 }
 
-static unsigned error_detected = 0;
-
-static void custom_abrt(int sig)
+/* Test whether an invalid call to gnutls_cipher_encrypt() is caught */
+static void test_cipher(int algo)
 {
-       error_detected = 1;
+       int ret;
+       gnutls_cipher_hd_t ch;
+       uint8_t key16[64];
+       uint8_t iv16[32];
+       uint8_t data[128];
+       gnutls_datum_t key, iv;
+
+       key.data = key16;
+       key.size = gnutls_cipher_get_key_size(algo);
+       assert(key.size <= sizeof(key16));
+
+       iv.data = iv16;
+       iv.size = gnutls_cipher_get_iv_size(algo);
+       assert(iv.size <= sizeof(iv16));
+
+       memset(iv.data, 0xff, iv.size);
+       memset(key.data, 0xfe, key.size);
+       memset(data, 0xfa, sizeof(data));
+
+       gnutls_global_set_log_function(tls_log_func);
+       if (debug)
+               gnutls_global_set_log_level(4711);
+
+       ret = global_init();
+       if (ret < 0) {
+               fail("Cannot initialize library\n"); /*errcode 1 */
+       }
+
+       ret =
+           gnutls_cipher_init(&ch, algo, &key, &iv);
+       if (ret < 0)
+               fail("gnutls_cipher_init failed\n"); /*errcode 1 */
+
+       /* try encrypting in a way that violates nettle's block conventions */
+       ret = gnutls_cipher_encrypt(ch, data, sizeof(data)-1);
+       if (ret >= 0)
+               fail("succeeded in encrypting partial data on block cipher\n");
+
+       gnutls_cipher_deinit(ch);
+
+       gnutls_global_deinit();
 }
 
-static void test_cipher(int algo, unsigned aead)
+/* Test whether an invalid gnutls_cipher_add_auth() is caught */
+static void test_aead_cipher1(int algo)
 {
        int ret;
        gnutls_cipher_hd_t ch;
@@ -63,10 +103,9 @@ static void test_cipher(int algo, unsigned aead)
        uint8_t iv16[32];
        uint8_t data[128];
        gnutls_datum_t key, iv;
-       unsigned auth = 1;
 
        if (algo == GNUTLS_CIPHER_CHACHA20_POLY1305)
-               auth = 0;
+               return;
 
        key.data = key16;
        key.size = gnutls_cipher_get_key_size(algo);
@@ -94,44 +133,84 @@ static void test_cipher(int algo, unsigned aead)
        if (ret < 0)
                fail("gnutls_cipher_init failed\n"); /*errcode 1 */
 
-       if (aead) {
-               if (auth) {
-                       ret = gnutls_cipher_add_auth(ch, data, sizeof(data)-1);
-                       if (ret < 0)
-                               fail("could not add auth data\n");
-
-                       signal(SIGABRT, custom_abrt);
-                       ret = gnutls_cipher_add_auth(ch, data, 16);
-                       signal(SIGABRT, SIG_DFL);
-                       if (ret >= 0 && error_detected == 0)
-                               fail("succeeded in adding auth data data after partial data were given\n");
-               }
+       ret = gnutls_cipher_add_auth(ch, data, sizeof(data)-1);
+       if (ret < 0)
+               fail("could not add auth data\n");
 
-               /* try encrypting in a way that violates nettle's AEAD conventions */
-               ret = gnutls_cipher_encrypt(ch, data, sizeof(data)-1);
-               if (ret < 0)
-                       fail("could not encrypt data\n");
+       ret = gnutls_cipher_add_auth(ch, data, 16);
+       if (ret >= 0)
+               fail("succeeded in adding auth data data after partial data were given\n");
 
-               signal(SIGABRT, custom_abrt);
-               ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
-               signal(SIGABRT, SIG_DFL);
-               if (ret >= 0 && error_detected == 0)
-                       fail("succeeded in encrypting partial data after partial data were given\n");
+       gnutls_cipher_deinit(ch);
 
-       } else {
-               /* try encrypting in a way that violates nettle's block conventions */
-               signal(SIGABRT, custom_abrt);
-               ret = gnutls_cipher_encrypt(ch, data, sizeof(data)-1);
-               signal(SIGABRT, SIG_DFL);
-               if (ret >= 0 && error_detected == 0)
-                       fail("succeeded in encrypting partial data on block cipher\n");
+       gnutls_global_deinit();
+       return;
+}
+
+/* Test whether an invalid call to gnutls_cipher_encrypt() is caught */
+static void test_aead_cipher2(int algo)
+{
+       int ret;
+       gnutls_cipher_hd_t ch;
+       uint8_t key16[64];
+       uint8_t iv16[32];
+       uint8_t data[128];
+       gnutls_datum_t key, iv;
+
+       key.data = key16;
+       key.size = gnutls_cipher_get_key_size(algo);
+       assert(key.size <= sizeof(key16));
+
+       iv.data = iv16;
+       iv.size = gnutls_cipher_get_iv_size(algo);
+       assert(iv.size <= sizeof(iv16));
+
+       memset(iv.data, 0xff, iv.size);
+       memset(key.data, 0xfe, key.size);
+       memset(data, 0xfa, sizeof(data));
+
+       gnutls_global_set_log_function(tls_log_func);
+       if (debug)
+               gnutls_global_set_log_level(4711);
+
+       ret = global_init();
+       if (ret < 0) {
+               fail("Cannot initialize library\n"); /*errcode 1 */
        }
+
+       ret =
+           gnutls_cipher_init(&ch, algo, &key, &iv);
+       if (ret < 0)
+               fail("gnutls_cipher_init failed\n"); /*errcode 1 */
+
+       /* try encrypting in a way that violates nettle's AEAD conventions */
+       ret = gnutls_cipher_encrypt(ch, data, sizeof(data)-1);
+       if (ret < 0)
+               fail("could not encrypt data\n");
+
+       ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
+       if (ret >= 0)
+               fail("succeeded in encrypting partial data after partial data were given\n");
+
        gnutls_cipher_deinit(ch);
 
        gnutls_global_deinit();
        return;
 }
 
+static void check_status(int status)
+{
+       if (WEXITSTATUS(status) != 0 ||
+           (WIFSIGNALED(status) && WTERMSIG(status) != SIGABRT)) {
+               if (WIFSIGNALED(status)) {
+                       fail("Child died with signal %d\n", WTERMSIG(status));
+               } else {
+                       fail("Child died with status %d\n",
+                            WEXITSTATUS(status));
+               }
+       }
+}
+
 static
 void start(const char *name, int algo, unsigned aead)
 {
@@ -152,9 +231,34 @@ void start(const char *name, int algo, unsigned aead)
                int status;
                /* parent */
                wait(&status);
-               check_wait_status(status);
+               check_status(status);
+       } else {
+               if (!aead)
+                       test_cipher(algo);
+               else
+                       test_aead_cipher1(algo);
+               exit(0);
+       }
+
+       if (!aead)
+               return;
+
+       /* check test_aead_cipher2 */
+
+       child = fork();
+       if (child < 0) {
+               perror("fork");
+               fail("fork");
+               return;
+       }
+
+       if (child) {
+               int status;
+               /* parent */
+               wait(&status);
+               check_status(status);
        } else {
-               test_cipher(algo,aead);
+               test_aead_cipher2(algo);
                exit(0);
        }
 }
index b905065c529e36d22f8e3182238e936e3b9efcad..e5728374fa5ef004f96fdfb3566bf4c06410e65d 100644 (file)
@@ -24,6 +24,7 @@
 #define UTILS_H
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
 #include <string.h>
 #include <stdarg.h>
@@ -148,7 +149,7 @@ inline static void _check_wait_status(int status, unsigned sigonly)
 {
 #if defined WEXITSTATUS && defined WIFSIGNALED
        if (WEXITSTATUS(status) != 0 ||
-           (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
+           (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM)) {
                if (WIFSIGNALED(status)) {
                        fail("Child died with signal %d\n", WTERMSIG(status));
                } else {