Commit graph

11634 commits

Author SHA1 Message Date
Max Kellermann
7be3641f1d Var: copy Location to stack, to fix use-after-free crash bug
The AssignExpr constructor may free the "init" pointer via
AssignExpr::TypeCheck(), resulting in a crash due to use-after-free.
To fix the crash bug, this patch copies the `Location` to the stack
instead of using a potentially-dangling pointer.
2020-02-27 12:06:34 +01:00
Max Kellermann
528cf11a5c Scope: lookup_ID() and install_ID() return IntrusivePtr<ID>
This fixes several memory leaks and double free bugs.
2020-02-27 12:02:55 +01:00
Max Kellermann
cbb6f09726 Scope: delete duplicate locals 2020-02-27 12:02:55 +01:00
Max Kellermann
783f833d5f EventRegistry: automatically delete EventHandlers
Fixes memory leak.
2020-02-27 12:02:55 +01:00
Max Kellermann
56e8a5f116 main: destroy event_registry after iosource_mgr
Fixes use-after-free bugs because PcapSource::Close() queues an event.
2020-02-27 12:02:55 +01:00
Max Kellermann
b60cccc637 zeekygen/IdentifierInfo: delete duplicate fields
Fixes memory leak.
2020-02-27 12:02:55 +01:00
Max Kellermann
b654a68c86 main: free the global scope in terminate_bro()
Make valgrind a bit happier.
2020-02-27 12:02:55 +01:00
Max Kellermann
875bfc09a2 Scope: pop_scope() returns IntrusivePtr<>
Make sure unused scopes are freed to fix memory leaks.

The comment inside pop_scope() is now obsolete and I deleted it,
because this commit implements the real solution.

Note that this requires us to add a reference to the
push_existing_scope() call in dbg_eval_expr(), because it never owned
the reference.
2020-02-27 12:02:55 +01:00
Max Kellermann
8ea1d89529 Scope: unref all inits in destructor
Fixes memory leak.

Proof that the Scope class is responsible for freeing those objects
can be found infunction_ingredients::~function_ingredients().
2020-02-27 12:02:55 +01:00
Max Kellermann
2c0ece7376 Var: pass IntrusivePtr to add_global(), add_local() etc.
Those functions don't have a well-defined reference passing API, and
we had lots of memory leaks here.  By using IntrusivePtr, reference
ownership is well-defined.
2020-02-27 11:59:37 +01:00
Max Kellermann
17a2f1a3ab plugin/ComponentManager: hold a reference to the EnumType
Use class IntrusivePtr<>.

This reference is necessary because add_type() may discard the
reference and hold only a reference to a clone of our EnumType.
2020-02-27 11:59:37 +01:00
Max Kellermann
db4f449382 Type: fix use-after-free bug in VectorType::ShallowClone()
The new VectorType instance needs a new reference to the `yield_type`.
2020-02-27 11:59:37 +01:00
Max Kellermann
7c80b577f1 Var: fix crash when redeclaring a function with different args
The following source code:

 function foo(foo: int) {}
 function foo() {}

... first produces this error:

 error in crash.zeek, line 1 and crash.zeek, line 2: incompatible types (function(foo:int;) : void and function() : void)

... and then crashes:

 Thread 1 "zeek" received signal SIGSEGV, Segmentation fault.
 0x000055555617d970 in RecordType::FieldDecl (this=0x555557cbdd80, field=0) at ../src/Type.cc:735
 735		return (*types)[field];
 (gdb) bt
 #0  0x000055555617d970 in RecordType::FieldDecl (this=0x555557cbdd80, field=0) at ../src/Type.cc:735
 #1  0x000055555619c0e2 in transfer_arg_defaults (args=0x555557cbf270, recv=0x555557cbdd80) at ../src/Var.cc:315
 #2  0x000055555619c40c in begin_func (id=0x555557cbf070, module_name=0x5555579dd4a0 "GLOBAL", flavor=FUNC_FLAVOR_FUNCTION, is_redef=0, t=0x555557cbde00,
     attrs=0x0) at ../src/Var.cc:371
 #3  0x0000555555f5723b in yyparse () at parse.y:1174
 #4  0x0000555556038bf6 in main (argc=5, argv=0x7fffffffe658) at ../src/main.cc:646

This is because `begin_func()` checks if the old and new functions
have the same type via same_type(), but continues anyway, and then
transfer_arg_defaults() crashes because both `Args()` have different
lengths.
2020-02-27 11:50:25 +01:00
Jon Siwek
36557f3086 Merge remote-tracking branch 'origin/topic/johanna/global_frame_stack'
Additionally changed to pop call_stack which tracks related data.

* origin/topic/johanna/global_frame_stack:
  Pop global frame stack on exception.
2020-02-26 19:56:52 -08:00
Johanna Amann
9ff6584953 Pop global frame stack on exception.
When changing this code for 3.0.0, the code-flow changed significantly.
One of the changes lead to exception being re-thrown from Call - which
leads to the function cleanup not always being run. While the Unref of
the framepointer was (correctly) moved into the catch block, the global
frame stack was not cleaned up - and contains an invalid pointer after
encountering an interpreter exception.

This small commit pops the stack and removes the framepointer that was
pushed onto it before re-throwing the exception.

I also think that this change (re-throwing the exception) breaks the
Zeek debugger in significant ways (you cannot debug a script that causes
a Runtime error anymore).
2020-02-26 15:58:49 -08:00
Jon Siwek
597407a708 Merge branch 'refactor' of https://github.com/MaxKellermann/zeek
- Minor code style/format tweaks in merge

* 'refactor' of https://github.com/MaxKellermann/zeek:
  IntrusivePtr: overload std::swap()
  IntrusivePtr: eliminate setPtr()
  IntrusivePtr: optimize release() using std::exchange()
  Expr: remove unused default constructors and `friend` declarations
  Expr: remove useless override
  Type: don't pass reference to pointer to MatchesIndex()
  Expr: don't pass reference to pointer to check_and_promote*()
  ID: remove unused default constructor
  Scope: simplify PList access, use pop_back() and back()
  List: add method empty()
2020-02-26 10:07:31 -08:00
Jon Siwek
502a0db96b Merge branch 'double_free' of https://github.com/MaxKellermann/zeek
* 'double_free' of https://github.com/MaxKellermann/zeek:
  Type: fix double free bug in SetType::ShallowClone()
2020-02-26 09:40:37 -08:00
Max Kellermann
ae2bd7928c Type: fix double free bug in SetType::ShallowClone()
The type declaration `addr_set` happens to result in a `SetType` with
`elements`, but no `indices`, and so ShallowClone() does not increment
any reference counter.  However, the `SetType` constructor passed
ownership of a non-existing reference to `TableType`, resulting in a
double free bug (and Zeek crash).

At first, I tried to fix this by obeying the code comment in
SetType::ShallowClone() and pass `indices=nullptr`, but that led to a
crash inside IndexType::IsSubNetIndex().  So this patch attempts to
make the symptom go away by pretending the code comment is simply
wrong, and only corrects the reference counters.
2020-02-26 05:34:40 +01:00
Jon Siwek
84e3e6c619 Merge remote-tracking branch 'origin/topic/timw/faster-hashing'
- Minor cleanups in siphash24.h (code style, header include)
- Updated COPYING.3rdparty with new license info

* origin/topic/timw/faster-hashing:
  Add a faster siphash24 implementation than the reference one
2020-02-25 19:56:17 -08:00
Jon Siwek
5a6799dd34 Update submodules 2020-02-25 19:29:06 -08:00
Jon Siwek
9b259019d9 Merge remote-tracking branch 'origin/topic/jsiwek/broker-submodule-build-merge'
* origin/topic/jsiwek/broker-submodule-build-merge:
  Update cmake submodule
  Update broker/cmake modules
2020-02-25 19:22:34 -08:00
Jon Siwek
b3d668e08f Update cmake submodule 2020-02-25 18:38:02 -08:00
Jon Siwek
2654ed2777 Update broker/cmake modules 2020-02-25 18:01:54 -08:00
Jon Siwek
9a75d04af5 Updating submodule(s).
[nomail]
2020-02-25 17:11:40 -08:00
Max Kellermann
5ed69edb7e IntrusivePtr: overload std::swap() 2020-02-25 21:05:59 +01:00
Max Kellermann
4dbc224e21 IntrusivePtr: eliminate setPtr()
There are only two call sites, and those hard-code the `add_ref`
parameter.
2020-02-25 21:05:53 +01:00
Max Kellermann
6f3e9d2c4c IntrusivePtr: optimize release() using std::exchange() 2020-02-25 21:05:42 +01:00
Max Kellermann
3bc33fe7f6 Expr: remove unused default constructors and friend declarations 2020-02-25 21:05:14 +01:00
Max Kellermann
138868747c Expr: remove useless override
All it does is call the base class.
2020-02-25 21:05:03 +01:00
Max Kellermann
8f3a712f74 Type: don't pass reference to pointer to MatchesIndex()
No implementation modifies the pointer value.  To guard against this,
this commit changes `&` to `const`.
2020-02-25 21:04:29 +01:00
Max Kellermann
0520d715c3 Expr: don't pass reference to pointer to check_and_promote*()
The function never modifies the pointer value.

check_and_promote_expr() is left untouched because it really does
modify the pointer.
2020-02-25 21:04:20 +01:00
Max Kellermann
3ccda03a54 ID: remove unused default constructor 2020-02-25 21:03:53 +01:00
Max Kellermann
ee21f29173 Scope: simplify PList access, use pop_back() and back() 2020-02-25 21:03:27 +01:00
Max Kellermann
cdf4362c7d List: add method empty() 2020-02-25 21:03:07 +01:00
Jon Siwek
4c7b1fa619 Merge branch 'leaks' of https://github.com/MaxKellermann/zeek
- Various minor code formatting/styling during the merge

* 'leaks' of https://github.com/MaxKellermann/zeek:
  parse.y: fix memory leak in FieldAssignExpr call
  parse.y: fix use-after-free bug in open-ended index_slice
  Type: fix use-after-free bug in init_type()
  Expr: fix memory leak in RecordCoerceExpr::Fold()
  Expr: fix memory leak in RecordCoerceExpr::InitVal()
  zeekygen/IdentifierInfo: fix memory leak in operator=()
  Func: fix memory leaks in get_func_priority()
  parse.y: fix several memory leaks after lookup_ID()
  Func: fix memory leaks in check_built_in_call()
  Var: fix memory leaks in add_global() and add_local()
  Var: add missing references to `init` in add{,_and_assign}_local()
  parse.y: hold reference on init_expr for zeekygen::Manager::Redef()
  Expr: fix two memory leaks in AssignExpr::InitVal()
  parse.y: fix memory leak after "&derepcated" without string
  RuleMatcher: delete PatternSet instances in destructor (memleak)
  option.bif: fix crash bug by referencing `Func`, not `Val`
2020-02-24 20:51:31 -08:00
Tim Wojtulewicz
7726558151 Add a faster siphash24 implementation than the reference one
- Average of 10 runs of 2009-M57-day11-18.trace (release build at -O3):
  - Master: 6.027s  93650 bytes max RSS
  - Commit: 5.950s  93271 bytes max RSS

- Hashing a fixed 32-byte payload 10 million times with a fixed key:
  - Master: 1.397411s
  - Commit: 0.998211s
2020-02-24 21:03:13 -07:00
Jon Siwek
a15f471a87 Update zeek-testing commit 2020-02-24 18:13:17 -08:00
Jon Siwek
1f4dca04d4 Merge branch 'ExpandAndInit_IntrusivePtr' of https://github.com/MaxKellermann/zeek
- Minor whitespace tweak in merge

* 'ExpandAndInit_IntrusivePtr' of https://github.com/MaxKellermann/zeek:
  Val: pass IntrusivePtr<> to TableVal::ExpandAndInit()
2020-02-24 18:12:58 -08:00
Tim Wojtulewicz
01df20c782 Merge remote-tracking branch 'origin/topic/jsiwek/deprecated-attribute'
* origin/topic/jsiwek/deprecated-attribute:
  Switch to using [[deprecated]] attribute
2020-02-24 18:53:37 -07:00
Tim Wojtulewicz
822567b3f9 Merge remote-tracking branch 'origin/topic/jsiwek/32bit-compat'
* origin/topic/jsiwek/32bit-compat:
  Improve formatting of doubles that are close to integers
  Improve HTTP version number comparisons
  Add a 32-bit task to Cirrus CI config
  Replace va_list fmt() overload with vfmt()
  Format tables indexed by patterns consistently across 32-bit/64-bit
  Format interval values consistently across 32-bit/64-bit platforms
2020-02-24 18:49:33 -07:00
Jon Siwek
15b4eeb085 Merge branch 'opaqueval_intrusiveptr' of https://github.com/MaxKellermann/zeek
- Minor whitespace changes during merge.

* 'opaqueval_intrusiveptr' of https://github.com/MaxKellermann/zeek:
  OpaqueVal: use class IntrusivePtr
2020-02-24 17:25:03 -08:00
Jon Siwek
e0da9fbe82 Merge branch 'scanned-file-device-and-inode' of https://github.com/awelzel/zeek
- Minor whitespace adjustments
- Converted std::pair usage to anonymous struct to improve readability

* 'scanned-file-device-and-inode' of https://github.com/awelzel/zeek:
  scan.l: Actually add <utility> include, too.
  ScannedFile: Identify already scanned files by device and inode
2020-02-24 17:09:30 -08:00
Jon Siwek
302c94299f Updating submodule(s).
[nomail]
2020-02-24 16:42:24 -08:00
Max Kellermann
12b6070b2b parse.y: fix memory leak in FieldAssignExpr call
An unmanaged reference is held on `func_id`, but its `val` reference
is now owned by the `FieldAssignExpr` instance.  The `ID` instance
still feels responsible for releasing the `val` reference, but since
nobody ever frees the `ID`, the conflict never causes a crash.
2020-02-24 19:50:56 +01:00
Max Kellermann
e2bf12d54a Val: pass IntrusivePtr<> to TableVal::ExpandAndInit()
Clarifies ownership and fixes memory leaks.

Closes https://github.com/zeek/zeek/issues/811
2020-02-24 15:55:52 +01:00
Max Kellermann
68b965c299 parse.y: fix use-after-free bug in open-ended index_slice
This code passed the "$1" reference to both `SizeExpr::SizeExpr()` and
`IndexExpr::IndexExpr()`.

Bug introduced by commit 8515d3aa57
2020-02-24 15:51:44 +01:00
Arne Welzel
97cd5c11a4 scan.l: Actually add <utility> include, too. 2020-02-24 15:12:46 +01:00
Max Kellermann
bcac5d3b17 Type: fix use-after-free bug in init_type()
Prior to this, `t` gets assigned from `Expr::InitType()` which returns
a referenced `BroType` to the caller (and init_Type() releases the
reference later).

But `reduce_type()` does not return a referenced `BroType`; so if
`reduce_type()` happens to return a different instance, it will be
released and maybe destroyed, resulting in a use-after-free bug.
2020-02-24 14:28:31 +01:00
Max Kellermann
e557563c69 Expr: fix memory leak in RecordCoerceExpr::Fold()
Don't add a second reference if the `rhs` variable was assigned from
`def->AttrExpr()->Eval(0)`.

Caused by commit af3267acc3
2020-02-24 13:44:59 +01:00
Max Kellermann
46ecbd400e Expr: fix memory leak in RecordCoerceExpr::InitVal()
Caused by commit b1fd161274
2020-02-24 13:37:51 +01:00