~ubuntu-branches/ubuntu/wily/zeromq3/wily-proposed

« back to all changes in this revision

Viewing changes to tests/test_security_null.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2015-05-05 21:06:02 UTC
  • Revision ID: package-import@ubuntu.com-20150505210602-7nmw5z31bei3puj6
Tags: 4.0.5+dfsg-3
V3 protocol handler vulnerable to downgrade attacks, use upstream
backported fix for this issue (closes: #784366).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
 
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3
3
 
4
4
    This file is part of 0MQ.
5
5
 
18
18
*/
19
19
 
20
20
#include "testutil.hpp"
 
21
#if defined (ZMQ_HAVE_WINDOWS)
 
22
#   include <winsock2.h>
 
23
#   include <ws2tcpip.h>
 
24
#   include <stdexcept>
 
25
#   define close closesocket
 
26
#else
 
27
#   include <sys/socket.h>
 
28
#   include <netinet/in.h>
 
29
#   include <arpa/inet.h>
 
30
#   include <unistd.h>
 
31
#endif
21
32
 
22
33
static void
23
34
zap_handler (void *handler)
27
38
        char *version = s_recv (handler);
28
39
        if (!version)
29
40
            break;          //  Terminating
 
41
 
30
42
        char *sequence = s_recv (handler);
31
43
        char *domain = s_recv (handler);
32
44
        char *address = s_recv (handler);
57
69
        free (identity);
58
70
        free (mechanism);
59
71
    }
60
 
    zmq_close (handler);
 
72
    close_zero_linger (handler);
61
73
}
62
74
 
63
75
int main (void)
76
88
    void *zap_thread = zmq_threadstart (&zap_handler, handler);
77
89
 
78
90
    //  We bounce between a binding server and a connecting client
 
91
    
 
92
    //  We first test client/server with no ZAP domain
 
93
    //  Libzmq does not call our ZAP handler, the connect must succeed
79
94
    void *server = zmq_socket (ctx, ZMQ_DEALER);
80
95
    assert (server);
81
96
    void *client = zmq_socket (ctx, ZMQ_DEALER);
82
97
    assert (client);
83
 
    
84
 
    //  We first test client/server with no ZAP domain
85
 
    //  Libzmq does not call our ZAP handler, the connect must succeed
86
98
    rc = zmq_bind (server, "tcp://127.0.0.1:9000");
87
99
    assert (rc == 0);
88
 
    rc = zmq_connect (client, "tcp://localhost:9000");
 
100
    rc = zmq_connect (client, "tcp://127.0.0.1:9000");
89
101
    assert (rc == 0);
90
102
    bounce (server, client);
91
 
    zmq_unbind (server, "tcp://127.0.0.1:9000");
92
 
    zmq_disconnect (client, "tcp://localhost:9000");
93
 
    
 
103
    close_zero_linger (client);
 
104
    close_zero_linger (server);
 
105
 
94
106
    //  Now define a ZAP domain for the server; this enables 
95
107
    //  authentication. We're using the wrong domain so this test
96
108
    //  must fail.
97
 
    //  **************************************************************
98
 
    //  PH: the following causes libzmq to get confused, so that the
99
 
    //  next step fails. To reproduce, uncomment this block. Note that
100
 
    //  even creating a new client/server socket pair, the behaviour
101
 
    //  does not change.
102
 
    //  **************************************************************
103
 
    //  Destroying the old sockets and creating new ones isn't needed,
104
 
    //  but it shows that the problem isn't related to specific sockets.
105
 
    //close_zero_linger (client);
106
 
    //close_zero_linger (server);
107
 
    //server = zmq_socket (ctx, ZMQ_DEALER);
108
 
    //assert (server);
109
 
    //client = zmq_socket (ctx, ZMQ_DEALER);
110
 
    //assert (client);
111
 
    ////  The above code should not be required
112
 
    //rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
113
 
    //assert (rc == 0);
114
 
    //rc = zmq_bind (server, "tcp://127.0.0.1:9001");
115
 
    //assert (rc == 0);
116
 
    //rc = zmq_connect (client, "tcp://localhost:9001");
117
 
    //assert (rc == 0);
118
 
    //expect_bounce_fail (server, client);
119
 
    //zmq_unbind (server, "tcp://127.0.0.1:9001");
120
 
    //zmq_disconnect (client, "tcp://localhost:9001");
121
 
    
 
109
    server = zmq_socket (ctx, ZMQ_DEALER);
 
110
    assert (server);
 
111
    client = zmq_socket (ctx, ZMQ_DEALER);
 
112
    assert (client);
 
113
    rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
 
114
    assert (rc == 0);
 
115
    rc = zmq_bind (server, "tcp://127.0.0.1:9001");
 
116
    assert (rc == 0);
 
117
    rc = zmq_connect (client, "tcp://127.0.0.1:9001");
 
118
    assert (rc == 0);
 
119
    expect_bounce_fail (server, client);
 
120
    close_zero_linger (client);
 
121
    close_zero_linger (server);
 
122
 
122
123
    //  Now use the right domain, the test must pass
 
124
    server = zmq_socket (ctx, ZMQ_DEALER);
 
125
    assert (server);
 
126
    client = zmq_socket (ctx, ZMQ_DEALER);
 
127
    assert (client);
123
128
    rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4);
124
129
    assert (rc == 0);
125
130
    rc = zmq_bind (server, "tcp://127.0.0.1:9002");
126
131
    assert (rc == 0);
127
 
    rc = zmq_connect (client, "tcp://localhost:9002");
 
132
    rc = zmq_connect (client, "tcp://127.0.0.1:9002");
128
133
    assert (rc == 0);
129
 
    //  **************************************************************
130
 
    //  PH: it fails here; though the ZAP reply is 200 OK, and
131
 
    //  null_mechanism.cpp correctly parses that, the connection
132
 
    //  never succeeds and the test hangs.
133
 
    //  **************************************************************
134
134
    bounce (server, client);
135
 
    zmq_unbind (server, "tcp://127.0.0.1:9002");
136
 
    zmq_disconnect (client, "tcp://localhost:9002");
137
 
    
 
135
    close_zero_linger (client);
 
136
    close_zero_linger (server);
 
137
 
 
138
    // Unauthenticated messages from a vanilla socket shouldn't be received
 
139
    server = zmq_socket (ctx, ZMQ_DEALER);
 
140
    assert (server);
 
141
    rc = zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5);
 
142
    assert (rc == 0);
 
143
    rc = zmq_bind (server, "tcp://127.0.0.1:9003");
 
144
    assert (rc == 0);
 
145
 
 
146
    struct sockaddr_in ip4addr;
 
147
    int s;
 
148
 
 
149
    ip4addr.sin_family = AF_INET;
 
150
    ip4addr.sin_port = htons(9003);
 
151
    inet_pton(AF_INET, "127.0.0.1", &ip4addr.sin_addr);
 
152
 
 
153
    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
154
    rc = connect (s, (struct sockaddr*) &ip4addr, sizeof ip4addr);
 
155
    assert (rc > -1);
 
156
    // send anonymous ZMTP/1.0 greeting
 
157
    send (s, "\x01\x00", 2, 0);
 
158
    // send sneaky message that shouldn't be received
 
159
    send (s, "\x08\x00sneaky\0", 9, 0);
 
160
    int timeout = 150;
 
161
    zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
 
162
    char *buf = s_recv (server);
 
163
    if (buf != NULL) {
 
164
        printf ("Received unauthenticated message: %s\n", buf);
 
165
        assert (buf == NULL);
 
166
    }
 
167
    close (s);
 
168
    close_zero_linger (server);
 
169
 
138
170
    //  Shutdown
139
 
    close_zero_linger (client);
140
 
    close_zero_linger (server);
141
171
    rc = zmq_ctx_term (ctx);
142
172
    assert (rc == 0);
143
 
 
144
 
    //  Wait until ZAP handler terminates.
 
173
    //  Wait until ZAP handler terminates
145
174
    zmq_threadclose (zap_thread);
146
175
 
147
176
    return 0;