Commit Graph

129 Commits (6c88cc1e0c7f8ef0c2518cb00e58b834b862f1e6)

Author SHA1 Message Date
Jason Ish 6f20d87ba1 lua: don't accept a table as a return value from match
Remove the half finished support for accepting a table returned from a
Lua rule's match function. This is not documented, not tested, and not
really implemented.

Also, use lua_tointeger to get the return value from the match function
as an integer instead of a float.

Ticket: #6941
1 year ago
Jason Ish e5faedf7e6 lua/util: move SCThreadInfo into suricata.util lib
Move the SCThreadInfo global function into the suricata.util library as
thread_info().

This is the last global function to be registered, so remove the
supporting functions.
1 year ago
Jason Ish 11b3ebcb5f lua/bytevar: convert SCByteVar to Lua lib
Similar to flowvars and flowints, but a byte var cannot be registered
from a Lua script, but it still needs to be setup. Instead provide an
"map" function that sets it up, or errors out if the byte var is
unknown.

This also required passing the signature into the Lua init method, as
the state of the Signature object and the time of loading the Lua
keyword is required.
1 year ago
Philippe Antoine a2b5225612 rust: bindgen SCSigMatchAppendSMToList
Ticket: 7667
1 year ago
Jeff Lucovsky 6ed386082e detect/luaxform: initial lua transform support
Adds a new lua script capability to use a script as a buffer transform
keyword.

It uses a `transform` lua function that returns the input buffer after
modifying it.

Issue: 2290
1 year ago
Philippe Antoine 7e78ad944c lua: convert ja3 function into suricata.ja3 lib
Ticket: 7605
1 year ago
Philippe Antoine c578015edf lua: remove unused code
Since hooks, we do not need a specific SMTP buffer list id.
1 year ago
Jason Ish 8a5bc12156 lua: convert lua flowint functions to lib: suricata.flowintlib
Ticket: #7487
2 years ago
Philippe Antoine 0024de5e9e src: new file detect-engine-buffer.h
Taken from detect-engine.h so that we can bindgen this smaller
unit.
2 years ago
Jason Ish 35b03b4077 lua: convert flowvar functions to lib
New Lua lib, "suricata.flowvar" for working with flowvars from Lua.

Replaces functions:
- SCFlowvarGet (and ScFlowvarGet)
- SCFlowvarSet (and SCFlowvarSet)

Of note, the DetectLuaData has been made available to the init and
thread_init methods, instead of just the match. This is due to an
issue that if a flow variable is not registered in init, it will not
be logged, registering in thread_init is too late.

Ticket: #7486
2 years ago
Victor Julien 57c73880db lua: enable lua rules by default
Now that sandboxing is in place, lua rule support is enabled by default.
2 years ago
Victor Julien 592444cdc4 detect/lua: use rule hook instead of init() 'needs' table
For registration of app-layer inspection, no longer use the 'needs'
table from the script, but instead use the rule hook setting.

Ticket: #4783.
2 years ago
Jason Ish 92d5a3fd0a lua/detect: remove rule sid, rev and gid globals
For some rules the values are always 0, for others they get set, so not
consistent. Also not documented and not tested and should be replaced by
a "suricata.rule" lib.

For example, these globals would be set for this rule:

alert tcp any any -> any any (flow:to_server;
flowbits:isnotset,dataset_added; lua:dataset.lua;
flowbits:set,dataset_added;sid:1;rev:99;)

But not this rule:

alert dns any any -> any any (msg:"TEST DNS LUA dns.rrname"; \
      content:"org"; lua:test-rrname.lua; sid:1; rev:1;)

Related to ticket
https://redmine.openinfosecfoundation.org/issues/7490 (but not a fix)

This does not affect the functions with similar names for getting rule
details in output context.
2 years ago
Jason Ish 22b77b0c56 conf: prefix conf API with SC 2 years ago
Jason Ish 4b8ef41df8 lua: remove buffer type dns.rrname, not needed
DNS is transaction based, Lua scripts need only express interest in
the request or the response.
2 years ago
Jason Ish 7b3763ecad lua: allow for real booleans in "needs" expression
In addition to

    function init (args)
        local needs = {}
        needs["dnp3"] = tostring(true)
        return needs
    end

allow for

    function init (args)
        return {dnp3=true}
    end

with the idea that the former will be completely removed for 8.0.

This works with all existing SV tests and as a string value, any value
is considered "truthy".
2 years ago
Victor Julien fbfeea752a lua: remove script_api_ver
Not documented and never set to new values despite updates.

Ticket: #7492.
2 years ago
Victor Julien 7226a0b2e7 detect/lua: register built-in libs also for open setup
Register internal libs for the case where loading external modules is allowed.
2 years ago
Victor Julien 48ef7aab64 lua: remove old lua payload/packet calls
Moving forward the packetlib is to be used.

Ticket: #7488.
2 years ago
Victor Julien 4dbd6ccd2c detect/lua: minor code cleanup 2 years ago
Victor Julien 3f53d657a9 detect/lua: remove unused tls flag 2 years ago
Philippe Antoine 9a8e377a7a http: include htp-libhtp.h from header
Instead of C files requiring it.
In preparation of libhtp rust, to minimize the final commit.
2 years ago
Philippe Antoine 23050d70ed http: aliases for htp opaque htp_tx_t
In preparation of libhtp rust
2 years ago
Victor Julien e89ab4f88a detect/lua: add thread_init
Add optional `thread_init` function support. This function is called per
script, per thread to allow a user to initialize the lua state.
2 years ago
Victor Julien c3e61e07f0 detect/lua: improve stack dumping
Only useful when debugging. Add a prefix and a stack size indication.
2 years ago
Philippe Antoine 20423fdd38 style: remove some useless return
and remove empty line before end of function
2 years ago
Jason Ish 10e6028175 lua: track memory limit exceede errors
Update the Lua allocated to set a code on memory allocation limit
exceeded errors so an appropriate error message can be logged and a
state incremented.

Fixes the tracking of the allocated size by using the difference
between original size, and new size and toss in some debug
validations.
2 years ago
Jason Ish 5a1cba72f0 lua: add logging and counter for instruction limit being exceeded 2 years ago
Jason Ish c8fa454cb2 lua: add blocked functions as a special log type plus stat
Distinguish between a generic Lua script error and an error created by a
function being blocked, so each is logged once respective of each other.

Also add a stat that is incremented when a script fails due to a
blocked function.

NOTE: This does not catch calls to functions that are blocked by not
having the library loaded, such as "io.open", as they are blocked by
not even loading the "io" library.
2 years ago
Jason Ish 86f9e43068 lua: use a function allow list instead of a deny list
The Lua library surface area is small enough to manage an allow list,
which is generally better than a deny list, as we'll explicitly need
to opt-in to new functions provided by the Lua runtime.
2 years ago
Jason Ish 2e440169d6 lua: remove lua as a compile time feature
Its always built-in. However, can be disabled at runtime.
2 years ago
Jason Ish afb705d278 lua: reset instruction counter before calling script 2 years ago
Jason Ish 1f05a17fb9 lua: misc cleanups in sandbox implementation
Including:
- rename guards
- SCMalloc to SCCalloc
- remove unused enum
- rename public functions to our naming standard
2 years ago
Jo Johnson 04adb0c0f6 lua: Add config to allow sandbox bypass 2 years ago
Jo Johnson e946b20e0f lua: Add config override for lua sandbox limits 2 years ago
Jo Johnson 8428b0b9d7 lua: Add lua sandbox for detection rules 2 years ago
Jo Johnson 01c8af766c lua: remove internal references to luajit 2 years ago
Jo Johnson 712496bb3f lua: Remove luajit support
lua 5.4 support is not available in luajit

Ticket: #4776
2 years ago
Jason Ish 6d2d8c26d3 detect-lua: small cleanups
- remove unused headers
- cleanup/rename flags
2 years ago
Jason Ish 224f55ba21 detect/lua: don't treat a crashed script as no match
If a rule script crashed, the return value was treated as a no
match. This would make a negation of the rule match and alert.

Instead cleanup and exit early if the rule script crashed and don't
run negation logic.

A stat, detect.lua.errors has been added to count how many times a
script crashes.

Also consolidates the running of the Lua script and return value
handling to a common function.

Bug: #6940
2 years ago
Jason Ish b11bb1c412 detect: rename DetectAppLayerInspectEngineRegister2
Rename DetectAppLayerInspectEngineRegister2 to
DetectAppLayerInspectEngineRegister as there is no other variant of
this function, and the versioning with lack of supporting
documentation can lead to confusion.
3 years ago
Jeff Lucovsky 193e0ea1a9 memory/alloc: Use SCCalloc instead of malloc/memset 3 years ago
Philippe Antoine c272a646c5 detect: SigMatchAppendSMToList can fail
Ticket: #6104

And failures should be handled to say that the rule failed to load

Reverts the fix by 299ee6ed55
that was simple, but not complete (memory leak),
to have this bigger API change which simplifies code.
3 years ago
Victor Julien b130234b26 var-names: reimplement var name handling
Implement a new design for handling var name id's. The old logic
was aware of detection engine versions and generally didn't work
well for multi-tenancy cases. Other than memory leaks and crashes,
logging of var names worked or failed based on which tenant was
loaded last.

This patch implements a new approach, where there is a global store
of vars and their id's for the lifetime of the program.

Overall Design:

Base Store: "base"

Used during keyword registration. Operates under lock. Base is shared
between all detect engines, detect engine versions and tenants.
Each variable name is ref counted.

During the freeing of a detect engine / tenant, unregistration decreases
the ref cnt.

Base has both a string to id and a id to string hash table. String to
id is used during parsing/registration. id to string during unregistration.

Active Store Pointer (atomic)

The "active" store atomic pointer points to the active lookup store. The call
to `VarNameStoreActivate` will build a new lookup store and hot swap
the pointer.

Ensuring memory safety. During the hot swap, the pointer is replaced, so
any new call to the lookup functions will automatically use the new store.
This leaves the case of any lookup happening concurrently with the pointer
swap. For this case we add the old store to a free list. It gets a timestamp
before which it cannot be freed.

Free List

The free list contains old stores that are waiting to get removed. They
contain a timestamp that is checked before they are freed.

Bug: #6044.
Bug: #6201.
3 years ago
Jason Ish f119b29701 lua: disable lua rules by default
To protect against possible supply chain attacks, disable Lua rules by
default. They can be enabled under the "security" section of
suricata.yaml.

Ticket: #6122
3 years ago
Gabriel Lima Luz b43357eb26 detect-lua: Convert unittests to FAIL/PASS API
Ticket: 4051
4 years ago
Victor Julien b31ffde6f4 output: remove error codes from output 4 years ago
Victor Julien 39f5c7f56a error: use SC_EINVAL for invalid input 4 years ago
Philippe Antoine 1f066cbbe8 unittest: fix unneeded includes as per cppclean
Especially because there is conditional inclusion from a header
4 years ago
Victor Julien e250ef6402 debug: remove empty header 4 years ago