Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applayer plugin 5053 v11 #12471

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

catenacyber
Copy link
Contributor

Link to ticket: https://redmine.openinfosecfoundation.org/issues/
https://redmine.openinfosecfoundation.org/issues/4102 with all 6 subtickets

Describe changes:

  • add app-layer plugin example with template protocol
  • document app-layer plugins

@jufajardini what do you think about the doc ?

#12467 rebased to get green CI

There are also #12470 and #12466 which will require a rebase of this PR if they get merged first (or the other way around)

cc @jasonish

and use generic logger callback prototype with later cast

and do some other small modifications so that the plugin
has less diff
so that it can be used by plugins

Avoid export by cbindgen as this constant is also defined in C
so that it can be used by plugins that want to call SCLogError
and such
Comment on lines +31 to +39
// Struct definitions
#[repr(C)]
#[allow(non_snake_case)]
pub struct SCPlugin {
pub name: *const libc::c_char,
pub license: *const libc::c_char,
pub author: *const libc::c_char,
pub Init: extern "C" fn(),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of a data structure that should remain in C, and be provided to Rust via bindgen instead of continuing the pattern of creating C data structure in Rust for the convenience of write once.

@@ -83,7 +83,7 @@ extern crate suricata_derive;
pub mod core;

#[macro_use]
pub(crate) mod debug;
pub mod debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also need 8b12670

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I broke out the fixes from my previous PR to expose SCPlugin to Rust to just a minimal one to fix logging from plugins:

#12472

Copy link

codecov bot commented Jan 24, 2025

Codecov Report

Attention: Patch coverage is 84.09091% with 7 lines in your changes missing coverage. Please review.

Project coverage is 80.55%. Comparing base (d63ad75) to head (b35cd6d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12471      +/-   ##
==========================================
+ Coverage   80.52%   80.55%   +0.02%     
==========================================
  Files         923      923              
  Lines      259176   259543     +367     
==========================================
+ Hits       208708   209067     +359     
- Misses      50468    50476       +8     
Flag Coverage Δ
fuzzcorpus 56.16% <79.54%> (+0.09%) ⬆️
livemode 19.36% <4.54%> (-0.05%) ⬇️
pcap 44.27% <43.18%> (+0.07%) ⬆️
suricata-verify 63.34% <83.33%> (+0.01%) ⬆️
unittests 58.37% <29.54%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 24348

jasonish and others added 6 commits January 25, 2025 08:23
With the recent refactor, the log level as seen by plugins was not
being updated when being set through the C interface, so just set it
directly upon plugin initialization.
so that they can be used by rust plugins
Ticket: 7151
Ticket: 7152
Ticket: 7154
Ticket: 7149
Ticket: 7150
Ticket: 7153
@catenacyber catenacyber force-pushed the applayer-plugin-5053-v11 branch from d08fb5c to b35cd6d Compare January 25, 2025 07:26
Copy link
Contributor

@jufajardini jufajardini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc is mostly ready, imho.
Pointed out nits and typos, mostly. 🙇🏽

- log.rs
- parser.rs

These files will have different ``use`` statements, targetting the suricata crate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
These files will have different ``use`` statements, targetting the suricata crate.
These files will have different ``use`` statements, targeting the suricata crate.

- plugin.rs : defines the entry point of the plugin ``SCPluginRegister``

``SCPluginRegister`` should register callback that should then call ``SCPluginRegisterAppLayer``
passing a ``SCAppLayerPlugin`` structure to suricata.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
passing a ``SCAppLayerPlugin`` structure to suricata.
passing a ``SCAppLayerPlugin`` structure to Suricata.

- Plugins can only use simple logging as defined by ``EveJsonSimpleTxLogFunc``
without suricata.yaml configuration, see https://github.com/OISF/suricata/pull/11160
- Keywords cannot use validate callbacks, see https://redmine.openinfosecfoundation.org/issues/5634
- Plugins cannot have keywords matching on mulitple protocols (like ja4),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Plugins cannot have keywords matching on mulitple protocols (like ja4),
- Plugins cannot have keywords matching on multiple protocols (like ja4),

A related work are Suricata plugins, also in progress and tracked by Redmine
Ticket #4101: https://redmine.openinfosecfoundation.org/issues/4101.

Plugins can be used by modifying suricata.yaml ``plugins`` section to include
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
Plugins can be used by modifying suricata.yaml ``plugins`` section to include
Plugins can be used by modifying the suricata.yaml ``plugins`` section to include

Comment on lines +32 to +36
- alname.rs
- detect.rs
- lib.rs
- log.rs
- parser.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add oneliner descriptions for each file?

The solution is to go through the ``JsonBuilder`` C API which uses an opaque pointer.

And the plugin contains also additional files:
- plugin.rs : defines the entry point of the plugin ``SCPluginRegister``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
- plugin.rs : defines the entry point of the plugin ``SCPluginRegister``
- plugin.rs : defines the entry point of the plugin -- ``SCPluginRegister``

And the plugin contains also additional files:
- plugin.rs : defines the entry point of the plugin ``SCPluginRegister``

``SCPluginRegister`` should register callback that should then call ``SCPluginRegisterAppLayer``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``SCPluginRegister`` should register callback that should then call ``SCPluginRegisterAppLayer``
``SCPluginRegister`` should register a callback that should then call ``SCPluginRegisterAppLayer``

Comment on lines +51 to +54
It should also call ``suricata::plugin::init();`` to ensure the plugin has initialized
its value of the Suricata Context, a structure needed by rust, to call some C functions,
that cannot be found at compile time because of circular dependencies, and are therefore
resolved at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a minor changes (if I understand this portion correctly)

Suggested change
It should also call ``suricata::plugin::init();`` to ensure the plugin has initialized
its value of the Suricata Context, a structure needed by rust, to call some C functions,
that cannot be found at compile time because of circular dependencies, and are therefore
resolved at runtime.
It should also call ``suricata::plugin::init();`` to ensure the plugin has initialized
its value of the Suricata Context. This is a structure needed by rust to call some C functions
that cannot be found at compile time because of circular dependencies and are therefore
resolved at runtime.

.. attention:: A pure rust plugin needs to be compiled with ``RUSTFLAGS=-Clink-args=-Wl,-undefined,dynamic_lookup``

This is because the plugin will link dynamically at runtime the functions defined in Suricata runtime.
You can define this rust flags in a ``.cargo/config.toml`` file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can define this rust flags in a ``.cargo/config.toml`` file.
You can define these rust flags in a ``.cargo/config.toml`` file.

that cannot be found at compile time because of circular dependencies, and are therefore
resolved at runtime.

This ``SCAppLayerPlugin`` begins by a version number ``SC_PLUGIN_API_VERSION`` for runtime compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
This ``SCAppLayerPlugin`` begins by a version number ``SC_PLUGIN_API_VERSION`` for runtime compatibility
The ``SCAppLayerPlugin`` begins by a version number ``SC_PLUGIN_API_VERSION`` for runtime compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

4 participants