Branch data Line data Source code
1 : : /* readnz.c --- Check out-of-bounds reads on non-zero terminated strings.
2 : : * Copyright (C) 2010-2012 Simon Josefsson
3 : : *
4 : : * This file is part of GNU SASL.
5 : : *
6 : : * This program is free software: you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation, either version 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : */
20 : :
21 : : #ifdef HAVE_CONFIG_H
22 : : #include "config.h"
23 : : #endif
24 : :
25 : : #include <stdio.h>
26 : : #include <stdarg.h>
27 : : #include <stdlib.h>
28 : : #include <string.h>
29 : : #include <stdbool.h>
30 : :
31 : : #include "utils.h"
32 : :
33 : : static void
34 : 2 : doit2 (bool server_p)
35 : : {
36 : 2 : Gsasl *ctx = NULL;
37 : 2 : Gsasl_session *session = NULL;
38 : : char *mechs;
39 : 2 : char *mech, *ptrptr = NULL;
40 : : char *s1;
41 : : size_t s1len;
42 : : int res;
43 : : size_t i;
44 : :
45 : 2 : res = gsasl_init (&ctx);
46 [ - + ]: 2 : if (res != GSASL_OK)
47 : : {
48 : 0 : fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
49 : 0 : return;
50 : : }
51 : :
52 [ + + ]: 2 : if (server_p)
53 : 1 : res = gsasl_server_mechlist (ctx, &mechs);
54 : : else
55 : 1 : res = gsasl_client_mechlist (ctx, &mechs);
56 [ - + ]: 2 : if (res != GSASL_OK)
57 : : {
58 : 0 : fail ("mechlist() failed (%d):\n%s\n", res, gsasl_strerror (res));
59 : 0 : return;
60 : : }
61 : :
62 [ + + ][ + + ]: 22 : for (i = 0; (mech = strtok_r (i == 0 ? mechs : NULL, " ", &ptrptr)); i++)
63 : : {
64 : : size_t len;
65 : :
66 [ + + ]: 120 : for (len = 0; len < 5; len++)
67 : : {
68 : : char *p;
69 : :
70 [ + + ]: 100 : if (server_p)
71 : 50 : res = gsasl_server_start (ctx, mech, &session);
72 : : else
73 : 50 : res = gsasl_client_start (ctx, mech, &session);
74 [ - + ]: 100 : if (res != GSASL_OK)
75 : : {
76 : 0 : fail ("start(%s) failed (%d):\n%s\n", mech,
77 : : res, gsasl_strerror (res));
78 : 0 : return;
79 : : }
80 : :
81 : 100 : p = malloc (len);
82 [ - + ]: 100 : if (!p)
83 : : {
84 : 0 : fail ("out of memory\n");
85 : 0 : return;
86 : : }
87 : :
88 : 100 : memset (p, 42, len);
89 : :
90 : 100 : res = gsasl_step (session, p, len, &s1, &s1len);
91 [ + + ][ + + ]: 100 : if (res == GSASL_OK || res == GSASL_NEEDS_MORE)
92 : 23 : gsasl_free (s1);
93 : :
94 : 100 : gsasl_free (p);
95 : :
96 : 100 : gsasl_finish (session);
97 : : }
98 : : }
99 : :
100 : 2 : gsasl_free (mechs);
101 : :
102 : 2 : gsasl_done (ctx);
103 : : }
104 : :
105 : : void
106 : 1 : doit (void)
107 : : {
108 : 1 : doit2 (true);
109 : 1 : doit2 (false);
110 : 1 : }
|