1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Add more assertions and checks for cross-thread socket use. See #1233

This commit is contained in:
dmiller
2018-07-07 21:13:46 +00:00
parent cdae588782
commit 6d72dbb9df

View File

@@ -329,6 +329,7 @@ static void status (lua_State *L, enum nse_status status)
switch (status) switch (status)
{ {
case NSE_STATUS_SUCCESS: case NSE_STATUS_SUCCESS:
assert(lua_status(L) == LUA_YIELD);
lua_pushboolean(L, true); lua_pushboolean(L, true);
nse_restore(L, 1); nse_restore(L, 1);
break; break;
@@ -339,6 +340,7 @@ static void status (lua_State *L, enum nse_status status)
case NSE_STATUS_ERROR: case NSE_STATUS_ERROR:
case NSE_STATUS_TIMEOUT: case NSE_STATUS_TIMEOUT:
case NSE_STATUS_PROXYERROR: case NSE_STATUS_PROXYERROR:
assert(lua_status(L) == LUA_YIELD);
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, nse_status2str(status)); lua_pushstring(L, nse_status2str(status));
nse_restore(L, 2); nse_restore(L, 2);
@@ -354,6 +356,7 @@ static void callback (nsock_pool nsp, nsock_event nse, void *ud)
{ {
nse_nsock_udata *nu = (nse_nsock_udata *) ud; nse_nsock_udata *nu = (nse_nsock_udata *) ud;
lua_State *L = nu->thread; lua_State *L = nu->thread;
assert(nse_type(nse) != NSE_TYPE_READ);
if (lua_status(L) == LUA_OK && nse_status(nse) == NSE_STATUS_ERROR) { if (lua_status(L) == LUA_OK && nse_status(nse) == NSE_STATUS_ERROR) {
// Sometimes Nsock fails immediately and callback is called before // Sometimes Nsock fails immediately and callback is called before
// l_connect has a chance to yield. We'll use nu->action to signal // l_connect has a chance to yield. We'll use nu->action to signal
@@ -375,6 +378,10 @@ static int yield (lua_State *L, nse_nsock_udata *nu, const char *action,
lua_pushthread(L); lua_pushthread(L);
lua_rawseti(L, -2, THREAD_I); lua_rawseti(L, -2, THREAD_I);
lua_pop(L, 1); /* nsock udata environment */ lua_pop(L, 1); /* nsock udata environment */
if (nu->thread != NULL && nu->thread != L)
{
luaL_error(L, "Invalid reuse of a socket from one thread to another.");
}
nu->thread = L; nu->thread = L;
nu->action = action; nu->action = action;
nu->direction = direction; nu->direction = direction;
@@ -609,9 +616,10 @@ static void receive_callback (nsock_pool nsp, nsock_event nse, void *udata)
{ {
nse_nsock_udata *nu = (nse_nsock_udata *) udata; nse_nsock_udata *nu = (nse_nsock_udata *) udata;
lua_State *L = nu->thread; lua_State *L = nu->thread;
assert(lua_status(L) == LUA_YIELD); assert(nse_type(nse) == NSE_TYPE_READ);
if (nse_status(nse) == NSE_STATUS_SUCCESS) if (nse_status(nse) == NSE_STATUS_SUCCESS)
{ {
assert(lua_status(L) == LUA_YIELD);
int len; int len;
const char *str = nse_readbuf(nse, &len); const char *str = nse_readbuf(nse, &len);
trace(nse_iod(nse), hexify((const unsigned char *) str, len).c_str(), FROM); trace(nse_iod(nse), hexify((const unsigned char *) str, len).c_str(), FROM);