suricata
util-lua-common.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef SURICATA_UTIL_LUA_COMMON_H
25 #define SURICATA_UTIL_LUA_COMMON_H
26 
27 #include "app-layer-protos.h"
28 
29 #define DEFAULT_LUA_ALLOC_LIMIT 500000
30 #define DEFAULT_LUA_INSTRUCTION_LIMIT 500000
31 
32 /*
33  * The libs validate their handles with luaL_checkudata(), i.e. by
34  * metatable identity only. In unsandboxed states setmetatable() /
35  * debug.setmetatable() is reachable, so a script can give one lib's
36  * userdata another lib's metatable and the FFI code would re-cast the
37  * payload (type confusion / wrong drop size). Each library userdata
38  * therefore carries an immutable header with a type id that the
39  * helpers verify next to the metatable; a forged handle fails closed.
40  */
41 #define SCLUA_UD_MAGIC 0x534C5541u /* "SLUA" */
42 
43 typedef struct SCLuaUserDataHeader_ {
44  uint32_t magic;
45  uint32_t type_id;
47 
69 };
70 
71 /*
72  * Lifetime anchor for the tx userdata (SCLUA_TUD_*_TX, the TLS state
73  * handle and the DNP3 points object). Each of those payloads starts
74  * with a SCLuaTxRef, filled by SCLuaTxRefInit() at get_tx() time from
75  * the flow the state is running on. The tx (or the whole app-layer
76  * state) can be freed while a script keeps the handle in module
77  * state; SCLuaTxRefResolve() looks the tx up on demand from (flow,
78  * flow identity, alproto, tx id) and is enforced by the
79  * SCLuaCheckUserData / SCLuaTestUserData entry points for every tx
80  * type. The lua layer never stores a parser tx pointer, so there is
81  * no dangling pointer to guard: a miss just means the tx (or its
82  * state) is gone. The alproto pins the protocol against flow-address
83  * reuse by another protocol; the flow identity pins the flow itself
84  * against address reuse by a same-protocol flow.
85  *
86  * The identity is flow_hash plus the full start timestamp - not
87  * FlowGetId(), which truncates the seconds to 16 bits and wraps the
88  * usecs mod 65536: two attacker-influenced flows could digest to the
89  * same id while the raw fields still differ.
90  */
91 struct Flow_;
92 typedef struct SCLuaTxRef_ {
93  struct Flow_ *flow; /**< flow the handle was created on */
94  uint32_t flow_hash; /**< flow hash at creation */
95  uint64_t start_sec; /**< full startts seconds at creation */
96  uint32_t start_usec; /**< startts usecs at creation */
97  AppProto alproto; /**< that flow's app proto at creation */
98  uint64_t tx_id; /**< tx id on that state */
100 
101 /*
102  * Fill the liveness anchor of a freshly created tx userdata. The
103  * flow is taken from the state; the tx id comes with it.
104  */
105 void SCLuaTxRefInit(lua_State *L, SCLuaTxRef *ref, uint64_t tx_id);
106 
107 /*
108  * \brief resolve the tx a (flow, tx id) anchor names, on demand
109  *
110  * The tx is looked up in the flow the state runs on now:
111  * same flow, same protocol (the recorded alproto pins the protocol
112  * against flow-address reuse), a live state, and the tx still on that
113  * state's list (AppLayerParserGetTx). On a hit *tx receives the live
114  * tx pointer - the only pointer ever handed to the accessors, never
115  * stored - so dereferencing is always memory-safe. The handle names
116  * its tx by id: if the state is freed and re-allocated and a fresh tx
117  * reuses the id, a stale handle resolves to the fresh tx (live
118  * memory, possibly different data) instead of failing closed.
119  *
120  * \retval 1 live: *tx set (when non-NULL)
121  * \retval 0 stale: the tx or its state was freed, the handle is from
122  * another flow, or the flow address was reused (another
123  * protocol or a same-protocol flow)
124  */
125 int SCLuaTxRefResolve(lua_State *L, const SCLuaTxRef *ref, void **tx);
126 
127 /* accessor boilerplate: resolve the anchored tx or push nil and return */
128 #define SCLUA_RESOLVE_TX(L, refp, txvar) \
129  void *txvar = NULL; \
130  if (!SCLuaTxRefResolve(L, (refp), &(txvar))) { \
131  lua_pushnil(L); \
132  return 1; \
133  }
134 
135 /*
136  * Allocate a library userdata with the type header. Returns the start
137  * of the payload area (size bytes) and leaves the object on the stack;
138  * set the metatable afterwards.
139  */
140 void *SCLuaNewUserData(lua_State *L, uint32_t type_id, size_t size);
141 
142 /*
143  * Verify the object at idx has metatable mt and type id type_id, error
144  * otherwise. Returns the payload start; the object is left as-is.
145  * For the tx types the tx liveness anchor is verified as well and a
146  * stale handle raises "tx no longer valid" instead of handing out a
147  * dangling pointer.
148  */
149 void *SCLuaCheckUserData(lua_State *L, int idx, const char *mt, uint32_t type_id);
150 
151 /*
152  * Tag-checked payload without the liveness check: raises on a type
153  * mismatch, but a stale tx anchor still yields the payload. Callers
154  * may only touch Lua-owned fields (e.g. a count cached at handle
155  * creation), never the embedded parser-owned pointers.
156  */
157 void *SCLuaCheckUserDataPayload(lua_State *L, int idx, const char *mt, uint32_t type_id);
158 
159 /*
160  * As SCLuaCheckUserData but a well-tagged stale handle yields NULL
161  * instead of a raise, for types whose stale contract is a soft
162  * failure (nil) rather than an error (the dnp3 lazy points object).
163  */
164 void *SCLuaCheckUserDataSoft(lua_State *L, int idx, const char *mt, uint32_t type_id);
165 
166 /*
167  * As SCLuaCheckUserData but returns NULL instead of raising when the
168  * object is absent, untagged or of another type (for __gc handlers,
169  * where a forged handle must be skipped, not raised on). A tx handle
170  * that is well-tagged but stale still raises: following a dangling
171  * tx pointer is never an option.
172  */
173 void *SCLuaTestUserData(lua_State *L, int idx, const char *mt, uint32_t type_id);
174 
175 int LuaCallbackError(lua_State *luastate, const char *msg);
176 const char *LuaGetStringArgument(lua_State *luastate, int argc);
177 
178 void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value);
179 void LuaPushTableKeyValueBoolean(lua_State *luastate, const char *key, bool value);
180 void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value);
182  lua_State *luastate, const char *key, const char *value, size_t len);
184  lua_State *luastate, const char *key, const uint8_t *value, size_t len);
185 
187 
188 #ifdef UNITTESTS
189 void SCLuaCommonRegisterTests(void);
190 #endif /* UNITTESTS */
191 
192 /* hack to please scan-build. Even though LuaCallbackError *always*
193  * returns 2, scan-build doesn't accept it and generates false
194  * positives */
195 #define LUA_ERROR(msg) \
196  LuaCallbackError(luastate, (msg)); \
197  return 2
198 
199 #endif /* SURICATA_UTIL_LUA_COMMON_H */
SCLUA_TUD_INVALID
@ SCLUA_TUD_INVALID
Definition: util-lua-common.h:49
SCLuaCheckUserDataSoft
void * SCLuaCheckUserDataSoft(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:151
SCLuaTxRef
struct SCLuaTxRef_ SCLuaTxRef
SCLuaUserDataHeader_
Definition: util-lua-common.h:43
SCLuaUserDataHeader_::magic
uint32_t magic
Definition: util-lua-common.h:44
len
uint8_t len
Definition: app-layer-dnp3.h:2
SCLUA_TUD_NTP_TX
@ SCLUA_TUD_NTP_TX
Definition: util-lua-common.h:66
SCLUA_TUD_HTTP_TX
@ SCLUA_TUD_HTTP_TX
Definition: util-lua-common.h:62
SCLUA_TUD_DNS_TX
@ SCLUA_TUD_DNS_TX
Definition: util-lua-common.h:63
SCLUA_TUD_DATASET
@ SCLUA_TUD_DATASET
Definition: util-lua-common.h:56
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
SCLuaTxRef_
Definition: util-lua-common.h:92
SCLuaTxRef_::start_usec
uint32_t start_usec
Definition: util-lua-common.h:96
Flow_
Flow data structure.
Definition: flow.h:359
SCLuaCheckUserDataPayload
void * SCLuaCheckUserDataPayload(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:137
SCLuaNewUserData
void * SCLuaNewUserData(lua_State *L, uint32_t type_id, size_t size)
Definition: util-lua-common.c:42
SCLuaUserDataTypes
SCLuaUserDataTypes
Definition: util-lua-common.h:48
SCLuaTestUserData
void * SCLuaTestUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:123
lua_State
struct lua_State lua_State
Definition: suricata-common.h:527
SCLUA_TUD_DNP3_POINTS
@ SCLUA_TUD_DNP3_POINTS
Definition: util-lua-common.h:68
SCLuaTxRef_::tx_id
uint64_t tx_id
Definition: util-lua-common.h:98
SCLuaTxRef_::start_sec
uint64_t start_sec
Definition: util-lua-common.h:95
SCLUA_TUD_FLOWVAR
@ SCLUA_TUD_FLOWVAR
Definition: util-lua-common.h:53
SCLUA_TUD_SSH_TX
@ SCLUA_TUD_SSH_TX
Definition: util-lua-common.h:65
SCLuaTxRef_::flow
struct Flow_ * flow
Definition: util-lua-common.h:93
SCLuaUserDataHeader
struct SCLuaUserDataHeader_ SCLuaUserDataHeader
SCLUA_TUD_FILE
@ SCLUA_TUD_FILE
Definition: util-lua-common.h:58
LuaPushTableKeyValueInt
void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
Definition: util-lua-common.c:238
SCLuaTxRefResolve
int SCLuaTxRefResolve(lua_State *L, const SCLuaTxRef *ref, void **tx)
Definition: util-lua-common.c:182
SCLuaCommonRegisterTests
void SCLuaCommonRegisterTests(void)
Definition: util-lua-common.c:504
SCLUA_TUD_SHA256
@ SCLUA_TUD_SHA256
Definition: util-lua-common.h:50
LuaGetStringArgument
const char * LuaGetStringArgument(lua_State *luastate, int argc)
Definition: util-lua-common.c:225
SCLUA_TUD_RULE
@ SCLUA_TUD_RULE
Definition: util-lua-common.h:57
LuaPushTableKeyValueString
void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value)
Push a key plus string value to the stack.
Definition: util-lua-common.c:256
SCLuaUserDataHeader_::type_id
uint32_t type_id
Definition: util-lua-common.h:45
LuaPushTableKeyValueArray
void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len)
Definition: util-lua-common.c:273
SCLUA_TUD_FLOW
@ SCLUA_TUD_FLOW
Definition: util-lua-common.h:59
SCLuaCheckUserData
void * SCLuaCheckUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:101
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:289
SCLUA_TUD_TLS_STATE
@ SCLUA_TUD_TLS_STATE
Definition: util-lua-common.h:61
SCLuaTxRefInit
void SCLuaTxRefInit(lua_State *L, SCLuaTxRef *ref, uint64_t tx_id)
Definition: util-lua-common.c:164
SCLUA_TUD_SMTP_TX
@ SCLUA_TUD_SMTP_TX
Definition: util-lua-common.h:64
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:218
SCLUA_TUD_MD5
@ SCLUA_TUD_MD5
Definition: util-lua-common.h:52
LuaPushTableKeyValueLString
void LuaPushTableKeyValueLString(lua_State *luastate, const char *key, const char *value, size_t len)
Push a key plus string value with length to the stack.
Definition: util-lua-common.c:265
SCLuaTxRef_::flow_hash
uint32_t flow_hash
Definition: util-lua-common.h:94
app-layer-protos.h
SCLuaTxRef_::alproto
AppProto alproto
Definition: util-lua-common.h:97
LuaPushTableKeyValueBoolean
void LuaPushTableKeyValueBoolean(lua_State *luastate, const char *key, bool value)
Definition: util-lua-common.c:245
SCLUA_TUD_FLOWINT
@ SCLUA_TUD_FLOWINT
Definition: util-lua-common.h:54
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:455
SCLUA_TUD_SHA1
@ SCLUA_TUD_SHA1
Definition: util-lua-common.h:51
SCLUA_TUD_BYTEVAR
@ SCLUA_TUD_BYTEVAR
Definition: util-lua-common.h:55
SCLUA_TUD_PACKET
@ SCLUA_TUD_PACKET
Definition: util-lua-common.h:60
SCLUA_TUD_JA3_TX
@ SCLUA_TUD_JA3_TX
Definition: util-lua-common.h:67