Skip to content

Commit 5eb3f8c

Browse files
committed
bugfix: tcpsock:sslhandshake(): memory invalid read and/or bogus "lua ssl ceritficate does not match host" errors might happen when checking certificates with the SNI name.
1 parent da31a5e commit 5eb3f8c

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,43 @@ ngx_http_lua_socket_tcp_sslhandshake(lua_State *L)
13141314
}
13151315
}
13161316

1317-
u->ssl_name = name;
1317+
dd("found sni name: %.*s %p", (int) name.len, name.data, name.data);
1318+
1319+
if (name.len == 0) {
1320+
u->ssl_name.len = 0;
1321+
1322+
} else {
1323+
if (u->ssl_name.data) {
1324+
/* buffer already allocated */
1325+
1326+
if (u->ssl_name.len >= name.len) {
1327+
/* reuse it */
1328+
ngx_memcpy(u->ssl_name.data, name.data, name.len);
1329+
u->ssl_name.len = name.len;
1330+
1331+
} else {
1332+
ngx_free(u->ssl_name.data);
1333+
goto new_ssl_name;
1334+
}
1335+
1336+
} else {
1337+
1338+
new_ssl_name:
1339+
1340+
u->ssl_name.data = ngx_alloc(name.len, ngx_cycle->log);
1341+
if (u->ssl_name.data == NULL) {
1342+
u->ssl_name.len = 0;
1343+
1344+
lua_pushnil(L);
1345+
lua_pushliteral(L, "no memory");
1346+
return 2;
1347+
}
1348+
1349+
ngx_memcpy(u->ssl_name.data, name.data, name.len);
1350+
u->ssl_name.len = name.len;
1351+
}
1352+
}
1353+
13181354
u->write_co_ctx = coctx;
13191355

13201356
rc = ngx_ssl_handshake(c);
@@ -3314,6 +3350,12 @@ ngx_http_lua_socket_tcp_finalize(ngx_http_request_t *r,
33143350
u->peer.free(&u->peer, u->peer.data, 0);
33153351
}
33163352

3353+
if (u->ssl_name.data) {
3354+
ngx_free(u->ssl_name.data);
3355+
u->ssl_name.data = NULL;
3356+
u->ssl_name.len = 0;
3357+
}
3358+
33173359
c = u->peer.connection;
33183360
if (c) {
33193361
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,

0 commit comments

Comments
 (0)