Skip to content

Commit e89bb81

Browse files
committed
Add TLS connection error callback function.
For sendlog, this will just break out of the event loop. For a logsrvd relay, it will try the next relay server (if any) and return an error to the client if no connection succeeds. Note that this only affects TLS negotiation (which may happen asynchronously) after the actual socket has been connected. Found by the ZeroPath AI Security Engineer <https://zeropath.com>
1 parent 96b16aa commit e89bb81

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

logsrvd/logsrvd_relay.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static void relay_client_msg_cb(int fd, int what, void *v);
6262
static void relay_server_msg_cb(int fd, int what, void *v);
6363
static void connect_cb(int sock, int what, void *v);
6464
static bool start_relay(int sock, struct connection_closure *closure);
65+
static void tls_connect_error_fn(struct tls_client_closure *tls_client);
6566

6667
/*
6768
* Free a struct relay_closure container and its contents.
@@ -286,6 +287,7 @@ connect_relay_tls(struct connection_closure *closure)
286287
sudo_timespecclear(&tls_client->connect_timeout);
287288
}
288289
tls_client->start_fn = tls_client_start_fn;
290+
tls_client->connect_error_fn = tls_connect_error_fn;
289291
if (!tls_ctx_client_setup(ssl_ctx, closure->relay_closure->sock, tls_client))
290292
goto bad;
291293

@@ -498,6 +500,27 @@ connect_relay(struct connection_closure *closure)
498500
debug_return_bool(true);
499501
}
500502

503+
/* Called on TLS connection error. */
504+
static void
505+
tls_connect_error_fn(struct tls_client_closure *tls_client)
506+
{
507+
struct connection_closure *closure = tls_client->parent_closure;
508+
int res;
509+
510+
/* TLS connection failed, try next relay (if any). */
511+
while ((res = connect_relay_next(closure)) == -1) {
512+
if (errno == ENOENT || errno == EINPROGRESS) {
513+
/* Out of relays or connecting asynchronously. */
514+
break;
515+
}
516+
}
517+
if (res == -1 && errno != EINPROGRESS) {
518+
closure->errstr = _("unable to connect to relay host");
519+
if (!schedule_error_message(closure->errstr, closure))
520+
connection_close(closure);
521+
}
522+
}
523+
501524
/*
502525
* Respond to a ServerHello message from the relay.
503526
* Returns true on success, false on error.

logsrvd/sendlog.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ tls_start_fn(struct tls_client_closure *tls_client)
394394
{
395395
return fmt_client_hello(tls_client->parent_closure);
396396
}
397+
398+
/* Called on TLS connection error. */
399+
static void
400+
tls_connect_error_fn(struct tls_client_closure *tls_client)
401+
{
402+
sudo_ev_loopbreak(tls_client->evbase);
403+
}
397404
#endif /* HAVE_OPENSSL */
398405

399406
static void
@@ -1688,6 +1695,7 @@ client_closure_alloc(int sock, struct sudo_event_base *base,
16881695
closure->tls_client.peer_name = &server_info;
16891696
closure->tls_client.connect_timeout.tv_sec = TLS_HANDSHAKE_TIMEO_SEC;
16901697
closure->tls_client.start_fn = tls_start_fn;
1698+
closure->tls_client.connect_error_fn = tls_connect_error_fn;
16911699
}
16921700
#endif
16931701

logsrvd/tls_client.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ tls_connect_cb(int sock, int what, void *v)
187187
debug_return;
188188

189189
bad:
190-
sudo_ev_loopbreak(evbase);
190+
tls_client->connect_error_fn(tls_client);
191191
debug_return;
192192
}
193193

@@ -199,6 +199,7 @@ tls_ctx_client_setup(SSL_CTX *ssl_ctx, int sock,
199199
bool ret = false;
200200
debug_decl(tls_ctx_client_setup, SUDO_DEBUG_UTIL);
201201

202+
SSL_free(closure->ssl);
202203
if ((closure->ssl = SSL_new(ssl_ctx)) == NULL) {
203204
errstr = ERR_reason_error_string(ERR_get_error());
204205
sudo_warnx(U_("unable to allocate ssl object: %s"),

logsrvd/tls_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ struct tls_client_closure {
3434
void *parent_closure;
3535
struct sudo_event_base *evbase; /* duplicated */
3636
struct sudo_event *tls_connect_ev;
37-
struct peer_info *peer_name;
37+
struct peer_info *peer_name; /* duplicated */
3838
struct timespec connect_timeout;
3939
bool (*start_fn)(struct tls_client_closure *);
40+
void (*connect_error_fn)(struct tls_client_closure *);
4041
bool tls_connect_state;
4142
};
4243

0 commit comments

Comments
 (0)