Commit graph

310 commits

Author SHA1 Message Date
Christian Kreibich
6108b18a3b Normalize file paths in find_file_in_path()
This renders script file names more nicely, for example when printed by the
reporter. It also avoids redundant prefixing of "./" to local scripts. I'm not
adding unit tests since normalize_path() already has them. A btest follows in
the next commit.
2023-10-03 22:14:40 -07:00
Tim Wojtulewicz
de13bb6361 Avoid unnecessary type names in return statements 2023-07-07 09:17:05 -07:00
Arne Welzel
bc8b5f5830 Rename util-config.h to zeek-config-paths.h and install it
The util-config.h has never been installed previously. Skimming the history,
it was only meant for inclusion from util.cc, hence the name. Now that it's
included from some other headers, rename it to align with what it
contains and install it, too.
2023-05-17 15:01:57 +02:00
Arne Welzel
b2d934226f Introduce ZEEK_SEED_VALUES environment variable
For "individually different but deterministic" runs specifying Zeek's
seed as an environment variable eases setups as one can avoid creating
extra seed files for each of the individual processes.

It is an error to specify the new ZEEK_SEED_VALUES variable together
with the existing ZEEK_SEED_FILE and -G. ZEEK_SEED takes precedence over
deterministic mode (-D) like ZEEK_SEED_FILE does today already.
2023-05-12 19:50:37 +02:00
Tim Wojtulewicz
f2a3e23dfa Add ifdef'd implementation of setvbuf to zeek::util 2023-05-02 10:28:50 -07:00
Tim Wojtulewicz
35ee96c4cb A bunch of other minor issues reported by PVS-Studio 2023-01-27 13:03:52 -07:00
Tim Wojtulewicz
c1e5389929 Merge remote-tracking branch 'dop/topic/dopheide/vfmt-buf_len'
* dop/topic/dopheide/vfmt-buf_len:
  Fix logic error in vfmt() when growing the buffer
2022-11-21 09:10:37 -07:00
Michael Dopheide
e47b055769 Fix logic error in vfmt() when growing the buffer 2022-11-18 17:46:03 +00:00
Tim Wojtulewicz
ca8f82a574 Fix the strcasestr unit test 2022-11-17 11:21:40 -07:00
Tim Wojtulewicz
51cdbbd59d Re-enable strcasestr on Windows, fix linking error 2022-11-14 09:13:31 -07:00
Tim Wojtulewicz
a8fc63e182 Merge remote-tracking branch 'microsoft/master'
* microsoft/master: (71 commits)
  Clang formatting
  Mask ports before inserting them into the map
  Fix compiler warning from applied patch
  Remove statistics plugin in favor of stats bif
  Add EventHandler version of stats plugin
  Mark a few EventHandler methods const
  Changed implementation from std::map to std::unordered_map of Val.cc
  Removed const, Windows build is now working
  Added fixes suggested in PR
  Update src/packet_analysis/protocol/ip/IP.cc
  Apply suggestions from code review
  Clang format again but now with v13.0.1
  Rewrote usages of define(_MSC_VER) to ifdef _MSC_VER
  Clang format it all
  Fixed initial CR comments
  Add NEWS entry about Windows port
  Add a couple of extra unistd.h includes to fix a build failure
  Use std::chrono instead of gettimeofday
  Update libkqueue submodule [nomail]
  Don't call tokenize_string if the input string is empty
  ...
2022-11-11 15:23:21 -07:00
Tomer Lev
e2be5ddc0c Added fixes suggested in PR 2022-11-10 19:01:29 +02:00
Tomer Lev
73e749a162 Clang format again but now with v13.0.1 2022-11-09 18:56:00 +02:00
Tomer Lev
a105ea9d80 Rewrote usages of define(_MSC_VER) to ifdef _MSC_VER 2022-11-09 18:56:00 +02:00
Tomer Lev
5cdc6e150e Clang format it all 2022-11-09 18:55:51 +02:00
Tim Wojtulewicz
2e457eb3ea Fix a few compiler warnings from MSVC 2022-11-09 18:17:07 +02:00
Tim Wojtulewicz
6bf469b7a8 Remove extra <filesystem> include from util.cc 2022-11-09 18:16:13 +02:00
Tim Wojtulewicz
baee0d8026 Replace uses of std::filesystem with zeek::filesystem 2022-11-09 18:16:13 +02:00
Tim Wojtulewicz
5494e4e6fd Fix relative path checking in get_exe_path, plus a signed comparison 2022-11-09 18:16:13 +02:00
Tim Wojtulewicz
bf06cc9c2f Turn on unit tests by default, and ifdef out a few of them 2022-11-09 18:16:13 +02:00
Tim Wojtulewicz
78fb845f63 Rework change to util::vfmt to fix strings getting truncated 2022-11-09 18:16:13 +02:00
Tim Wojtulewicz
77c555a3a8 Fixing some issues from rebasing 2022-11-09 18:16:13 +02:00
Elad Solomon
dfd6cbe6b4 Fixed usages of path_list_separator 2022-11-09 18:15:34 +02:00
Tomer Lev
d7734ca783 Return the actual error code instead of a message when compiling to windows 2022-11-09 18:15:34 +02:00
Tomer Lev
d2f625c115 Replaced weakly_canonical with lexically_normal in normalize_path func.
Replaced weakly_canonical with lexically_normal in normalize_path func. Normalizing the path now does not access the filesystem.
2022-11-09 18:15:34 +02:00
Elad Solomon
3f349b8a37 Optimize initial memory consumption 2022-11-09 18:15:34 +02:00
Guy Magen
fbf5b68d6f Fixed ftp bug
Fixed ftp bug
2022-11-09 18:15:33 +02:00
Elad Solomon
3a80b79497 Compile Zeek with MSVC
Allow Zeek to be embedded in another project
2022-11-09 18:15:30 +02:00
Josh Soref
90f1da4fe7 spelling: deterministic
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-11-07 20:05:24 -05:00
Tim Wojtulewicz
701fc3654a Make char*/wchar_t* split methods use string_view/wstring_view 2022-07-21 11:53:47 -07:00
Tim Wojtulewicz
510dd1cf71 Add util::split methods for splitting strings 2022-07-21 11:53:47 -07:00
Tim Wojtulewicz
9f97a91d39 Remove unnecssary includes of Dict.h 2022-07-05 13:33:34 -07:00
Arne Welzel
b77f5fa14c Add gulrak/filesystem to auxil, expose via zeek::filesystem
This adds https://github.com/gulrak/filesystem as a submodule into auxil
as a compiler-independent std::filesystem replacement.

The ghc::filesystem namespace is exposed as zeek::filesystem in util.h.

In the build directory, we add 3rdparty/ghc as a symlink to auxil in
order to support building from the build tree.

    <build_dir>/src/3rdparty/ghc -> /path/to/zeek/src/auxil/filesystem/include/ghc

In the installation tree, the headers are installed into include/zeek/3rdparty:

    <install_dir>/include/zeek/3rdparty/ghc

Note, this differs from how we approached rapidjson which isn't included
using a zeek/3rdparty and instead requires an additional include path of
the following form for external plugins to find and use it.

    <install_dir>/include/zeek/3rdparty/rapidjson/include/

We diverge from this approach. Placing ghc directly into 3rdparty appears
nicer and avoids changing external components (DynamicPlugin.cmake / spicyc)
2022-07-04 13:32:18 +02:00
Tim Wojtulewicz
ef659b8e82 Remove unused util::detail::rand64bit method 2022-07-01 14:10:33 -07:00
Tim Wojtulewicz
1c3c88fd2a Merge remote-tracking branch 'origin/topic/johanna/rand64bit'
* origin/topic/johanna/rand64bit:
  Optimize 64 bit random number generation
2022-06-30 13:37:49 -07:00
Tim Wojtulewicz
fb16ce3711 Remove other general deprecations 2022-06-30 19:17:13 +00:00
Johanna Amann
31cf270565 Optimize 64 bit random number generation
rand64bit called random 4 times to generate one 64 bit number. There is
no reason to do this - random() is basically guaranteed to return a 32
bit number.

This also adds a static check to make sure that it does.
2022-06-29 14:37:39 +02:00
Tim Wojtulewicz
7c4fd382d9 Code modernization: Convert from deprecated C standard library headers 2022-06-27 09:47:31 -07:00
Vern Paxson
41aee03c17 make curr_CPU_time() broadly available rather than just isolated to ZAM 2022-05-03 10:38:32 -07:00
Tim Wojtulewicz
3f06986896 Add unit test for other get_word() version 2022-04-12 15:30:13 -07:00
Tim Wojtulewicz
a7d3cb48ef Add concept of "parent" tag namespaces
This allows us to create an EnumType that groups all of the analyzer
tag values into a single type, while still having the existing types
that split them up. We can then use this for certain events that benefit
from taking all of the tag types at once.
2021-11-23 19:36:49 -07:00
Benjamin Bannier
72cbc7cd13 Move 3rdparty source files to 3rdparty/
This patch moves in-tree 3rdparty source files to `3rdparty/`. With that
we can remove special treatment of these files for `run-clang-format`.
2021-11-09 07:20:18 +01:00
Johanna Amann
1b3b9a3cfc Merge branch 'fsync-shadow-files-before-rename' of https://github.com/awelzel/zeek
* 'fsync-shadow-files-before-rename' of https://github.com/awelzel/zeek:
  logging/writers/ascii: shadow files: Add fsync() before rename()
2021-10-15 09:47:08 +01:00
Arne Welzel
dc6e21d6ae logging/writers/ascii: shadow files: Add fsync() before rename()
We're using shadow files for log rotation on systems with ext4 running
Linux 4.19. We've observed zero-length shadow files in the logger's working
directory after a power-outage. This leads to a broken/stuck logger
process due to empty shadow files being considered invalid and the
process exiting:

    error: failed to process leftover log 'conn.log.gz': Found leftover log, 'conn.log.gz', but the associated shadow  file, '.shadow.conn.log.gz', required to process it is invalid

PR #1137 introduced atomic renaming of shadow files and was supposed to
handle this. However, after more investigation, the rename() has to be
preceded by an fsync() in order to avoid zero-length files in the presence
of hard-crashes or power-failures. This is generally operating system
and filesystem dependent, but should not hurt to add. The performance impact
can likely be neglected due to the low frequency and limited number of
log streams.

This has happened to others, too. Some references around this issue:

* https://stackoverflow.com/questions/7433057/is-rename-without-fsync-safe
* https://unix.stackexchange.com/questions/464382/which-filesystems-require-fsync-for-crash-safety-when-replacing-an-existing-fi
* https://bugzilla.kernel.org/show_bug.cgi?id=15910

Reproducer

This issue was reproduced artificially on Linux using the sysrq-trigger
functionality to hard-reset the system shortly after a .shadow file was
renamed to it's final destination with the following script watching for
.shadow.conn.log.gz:

    #!/bin/bash
    set -eu
    dir=/data/logger-01/

    # Allow everything via /proc/sysrq-trigger
    echo "1" > /proc/sys/kernel/sysrq

    inotifywait -m -e MOVED_TO --format '%e %w%f' "${dir}" | while read -r line; do
        if echo "${line}" | grep -q '^MOVED_TO .*/.shadow.conn.log.gz$'; then
            echo "RESET: $line"
            sleep 4
            # Trigger a hard-reset without sync/unmount
            echo "b" > /proc/sysrq-trigger
        fi
    done

This quite reliably (4 out of 4 times) yielded a system with zero-length
shadow files and a broken logger after it came back online:

    $ ls -lha /data/logger-01/.shadow.*
    -rw-r--r-- 1 bro bro 0 Oct 14 02:26 .shadow.conn.log.gz
    -rw-r--r-- 1 bro bro 0 Oct 14 02:26 .shadow.dns.log.gz
    -rw-r--r-- 1 bro bro 0 Oct 14 02:26 .shadow.files.log.gz

After this change while running the reproducer, the shadow files always
contained content after a hard-reset.

Rework with util::safe_fsync helper
2021-10-14 15:54:45 +02:00
Tim Wojtulewicz
9af6b2f48d clang-format: Set penalty for breaking after assignment operator 2021-09-27 10:49:48 -07:00
Tim Wojtulewicz
4423574d26 clang-format: Set IndentCaseBlocks to false 2021-09-27 10:49:48 -07:00
Christian Kreibich
2585ccd873 Add unit tests for memory helpers 2021-09-20 17:51:43 -07:00
Christian Kreibich
c5cceaf5ad Add memory sizing/alignment helpers to util.cc/h
This functionality previously lived in the CompHash class, with one difference:
this removes a discrepancy between the offset aligner and the memory pointer
aligner/padder. The size aligner used to align the provided offset and then add an
additional alignment size (for example, 1 aligned to 4 wouldn't yield 4 but 8).
Like the memory aligners it now only rounds up as needed.

Includes unit tests.
2021-09-20 17:51:43 -07:00
Tim Wojtulewicz
b2f171ec69 Reformat the world 2021-09-16 15:35:39 -07:00
Tim Wojtulewicz
58cb9163d1 Fix mis-usage of string::append that leads to an overflow 2021-09-07 09:16:53 -07:00