1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/* ISC license. */
#include <skalibs/nonposix.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>
#include <tls.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/error.h>
#include <skalibs/buffer.h>
#include <skalibs/sig.h>
#include <skalibs/selfpipe.h>
#include <skalibs/strerr2.h>
#include <skalibs/tai.h>
#include <skalibs/iopause.h>
#include <skalibs/djbunix.h>
#include <s6-networking/stls.h>
typedef struct tlsbuf_s tlsbuf_t, *tlsbuf_t_ref ;
struct tlsbuf_s
{
buffer b ;
unsigned char blockedonother : 1 ;
char buf[STLS_BUFSIZE] ;
} ;
static inline int buffer_tls_flush (struct tls *ctx, tlsbuf_t *b)
{
struct iovec v[2] ;
ssize_t r, w ;
buffer_rpeek(&b[0].b, v) ;
r = tls_write(ctx, v[0].iov_base, v[0].iov_len) ;
switch (r)
{
case -1 : return -1 ;
case TLS_WANT_POLLIN :
if (b[1].blockedonother) strerr_dief1x(101, "TLS deadlock") ;
b[0].blockedonother = 1 ;
case TLS_WANT_POLLOUT : return 0 ;
default : break ;
}
w = r ;
if ((size_t)w == v[0].iov_len && v[1].iov_len)
{
r = tls_write(ctx, v[1].iov_base, v[1].iov_len) ;
switch (r)
{
case TLS_WANT_POLLIN :
if (b[1].blockedonother) strerr_dief1x(101, "TLS deadlock") ;
b[0].blockedonother = 1 ;
case -1 :
case TLS_WANT_POLLOUT :
buffer_rseek(&b[0].b, w) ;
return 0 ;
default : break ;
}
w += r ;
}
buffer_rseek(&b[0].b, w) ;
return 1 ;
}
static inline int buffer_tls_fill (struct tls *ctx, tlsbuf_t *b)
{
struct iovec v[2] ;
ssize_t r, w ;
int ok = 1 ;
buffer_wpeek(&b[1].b, v) ;
r = tls_read(ctx, v[0].iov_base, v[0].iov_len) ;
switch (r)
{
case 0 : return -2 ;
case -1 : return -1 ;
case TLS_WANT_POLLOUT :
if (b[0].blockedonother) strerr_dief1x(101, "TLS deadlock") ;
b[1].blockedonother = 1 ;
case TLS_WANT_POLLIN : return 0 ;
default : break ;
}
w = r ;
if ((size_t)w == v[0].iov_len && v[1].iov_len)
{
r = tls_read(ctx, v[1].iov_base, v[1].iov_len) ;
switch (r)
{
case TLS_WANT_POLLOUT :
if (b[0].blockedonother) strerr_dief1x(101, "TLS deadlock") ;
b[1].blockedonother = 1 ;
case -1 :
case TLS_WANT_POLLIN :
buffer_wseek(&b[1].b, w) ;
return 0 ;
case 0 : ok = -1 ; errno = EPIPE ;
default : break ;
}
w += r ;
}
buffer_wseek(&b[1].b, w) ;
return ok ;
}
static void send_closenotify (struct tls *ctx, int const *fds)
{
iopause_fd x = { .fd = fds[3], .events = IOPAUSE_WRITE } ;
while (tls_close(ctx) == TLS_WANT_POLLOUT)
iopause_g(&x, 1, 0) ;
}
static void closeit (struct tls *ctx, int *fds, int brutal)
{
if (brutal) shutdown(fds[3], SHUT_WR) ;
else if (fds[2] >= 0) send_closenotify(ctx, fds) ;
fd_close(fds[3]) ; fds[3] = -1 ;
}
static void handle_signals (pid_t pid, int *e)
{
for (;;) switch (selfpipe_read())
{
case -1 : strerr_diefu1sys(111, "read selfpipe") ;
case 0 : return ;
case SIGCHLD :
{
int wstat ;
if (wait_pid_nohang(pid, &wstat) == pid)
{
*e = wstat ;
return ;
}
}
}
}
int stls_run (struct tls *ctx, int *fds, pid_t pid, unsigned int verbosity, uint32_t options, tain_t const *tto)
{
tlsbuf_t b[2] = { { .blockedonother = 0 }, { .blockedonother = 0 } } ;
iopause_fd x[5] = { { .fd = fds[4], .events = IOPAUSE_READ } } ;
unsigned int xindex[4] ;
unsigned int i = 0 ;
int e = -1 ;
for (; i < 2 ; i++)
{
buffer_init(&b[i].b, i ? &buffer_write : &buffer_read, fds[i], b[i].buf, STLS_BUFSIZE) ;
if (ndelay_on(fds[2+i]) < 0) strerr_diefu1sys(111, "set fds non-blocking") ;
}
if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ;
tain_now_g() ;
for (;;)
{
tain_t deadline ;
unsigned int xlen = 1 ;
int r ;
tain_add_g(&deadline, fds[0] >= 0 && fds[2] >= 0 && buffer_isempty(&b[0].b) && buffer_isempty(&b[1].b) ? tto : &tain_infinite_relative) ;
/* poll() preparation */
if (fds[0] >= 0 && buffer_isreadable(&b[0].b))
{
x[xlen].fd = fds[0] ;
x[xlen].events = IOPAUSE_READ ;
xindex[0] = xlen++ ;
}
else xindex[0] = 5 ;
if (fds[1] >= 0 && buffer_iswritable(&b[1].b))
{
x[xlen].fd = fds[1] ;
x[xlen].events = IOPAUSE_WRITE ;
xindex[1] = xlen++ ;
}
else xindex[1] = 5 ;
if (fds[2] >= 0 && !b[1].blockedonother && buffer_isreadable(&b[1].b))
{
x[xlen].fd = fds[2] ;
x[xlen].events = IOPAUSE_READ ;
xindex[2] = xlen++ ;
}
else xindex[2] = 5 ;
if (fds[3] >= 0 && !b[0].blockedonother && buffer_iswritable(&b[0].b))
{
x[xlen].fd = fds[3] ;
x[xlen].events = IOPAUSE_WRITE ;
xindex[3] = xlen++ ;
}
else xindex[3] = 5 ;
if (xlen == 1) break ;
/* poll() */
r = iopause_g(x, xlen, &deadline) ;
if (r < 0) strerr_diefu1sys(111, "iopause") ;
else if (!r)
{
fd_close(fds[0]) ; fds[0] = -1 ;
closeit(ctx, fds, options & 1) ;
if (e >= 0) break ;
continue ;
}
while (xlen--)
if (x[xlen].revents & IOPAUSE_EXCEPT)
x[xlen].revents |= IOPAUSE_READ | IOPAUSE_WRITE ;
/* Signal */
if (x[0].revents & IOPAUSE_READ)
{
handle_signals(pid, &e) ;
if (e >= 0 && xindex[0] == 5 && xindex[3] == 5) break ;
}
/* Flush to local */
if (xindex[1] < 5 && x[xindex[1]].revents & IOPAUSE_WRITE)
{
r = buffer_flush(&b[1].b) ;
if (!r && !error_isagain(errno))
{
strerr_warnwu1sys("write to application") ;
if (fds[2] >= 0)
{
if (options & 1) shutdown(fds[2], SHUT_RD) ;
fd_close(fds[2]) ; fds[2] = -1 ;
xindex[2] = 4 ;
}
r = 1 ;
}
if (r && fds[2] < 0)
{
fd_close(fds[1]) ; fds[1] = -1 ;
}
}
/* Flush to remote */
if (xindex[3] < 5 && x[xindex[3]].revents & IOPAUSE_WRITE)
{
r = buffer_tls_flush(ctx, b) ;
if (r < 0)
{
strerr_warnwu2x("write to peer: ", tls_error(ctx)) ;
fd_close(fds[0]) ; fds[0] = -1 ;
}
if (r && fds[0] < 0)
{
closeit(ctx, fds, options & 1) ;
if (e >= 0) break ;
}
}
/* Fill from local */
if (xindex[0] < 5 && x[xindex[0]].revents & IOPAUSE_READ)
{
r = sanitize_read(buffer_fill(&b[0].b)) ;
if (r < 0)
{
if (errno != EPIPE) strerr_warnwu1sys("read from application") ;
fd_close(fds[0]) ; fds[0] = -1 ;
if (buffer_isempty(&b[0].b))
{
closeit(ctx, fds, options & 1) ;
if (e >= 0) break ;
}
}
}
/* Fill from remote */
if (xindex[2] < 5 && x[xindex[2]].revents & IOPAUSE_READ)
{
r = buffer_tls_fill(ctx, b) ;
if (r < 0)
{
if (r == -1) strerr_warnwu2x("read from peer: ", tls_error(ctx)) ;
if (options & 1) shutdown(fds[2], SHUT_RD) ;
/*
XXX: We need a way to detect when we've received a close_notify,
because then we need to trigger a write and then shut the engine
down. This is orthogonal to options&1, it only means that the
peer sent a close_notify.
As for now, libtls doesn't offer an API to detect that, so we
do nothing special - we just wait until our app sends EOF.
*/
fd_close(fds[2]) ; fds[2] = -1 ;
if (buffer_isempty(&b[1].b))
{
fd_close(fds[1]) ; fds[1] = -1 ;
}
}
}
}
if (fds[1] >= 0) fd_close(fds[1]) ;
if (fds[0] >= 0) fd_close(fds[0]) ;
if (fds[3] >= 0) fd_close(fds[3]) ;
if (fds[2] >= 0) fd_close(fds[2]) ;
return e ;
}
|