Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / init_fds.c
1 /*
2  * Copyright (C) 2014 Nikos Mavrogiannopoulos
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <gnutls/gnutls.h>
30 #include <gnutls/crypto.h>
31
32 #include "utils.h"
33
34 /* See <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760476>. */
35
36 void doit(void)
37 {
38 #ifndef _WIN32
39         int res;
40         unsigned i;
41         int serial = 0;
42         char buf[128];
43
44         res = read(3, buf, 16);
45         if (res == 16)
46                 serial = 1;
47
48         /* close all descriptors */
49         for (i=3;i<1024;i++)
50                 close(i);
51
52         res = gnutls_global_init();
53         if (res != 0)
54                 fail("global_init\n");
55
56         if (serial != 0) {
57                 res = read(3, buf, 16);
58                 if (res != 16) {
59                         fail("could not open fd, or OS doesn't assign fds in a serial way (%d)\n", res);
60                 }
61         }
62
63         res = gnutls_global_init();
64         if (res != 0)
65                 fail("global_init2\n");
66
67         gnutls_rnd_refresh();
68
69         res = gnutls_rnd(GNUTLS_RND_RANDOM, buf, sizeof(buf));
70         if (res != 0)
71                 fail("gnutls_rnd\n");
72
73         gnutls_global_deinit();
74
75         if (debug)
76                 success("init-close success\n");
77 #else
78         return;
79 #endif
80 }