Project

General

Profile

Actions

Bug #4880

open
ZZ SB

detect: hostbits and xbits rules incorrectly ordered

Bug #4880: detect: hostbits and xbits rules incorrectly ordered

Added by zhenjun zhu almost 5 years ago. Updated 3 days ago.

Status:
Assigned
Priority:
Normal
Target version:
Affected Versions:
Effort:
Difficulty:
low
Label:

Description

The description of hostbits and xbits in the latest document does not match the status quo.
https://suricata.readthedocs.io/en/latest/rules/xbits.html#xbits-keyword

alert ip any any -> any any (msg:"test xbits set fakehost"; xbits:set, fakehost, track ip_dst; sid:1;)
alert ip any any -> any any (msg:"test xbits isset set fakehost and set fakerservice"; xbits:isset, fakehost, track ip_dst; xbits:set, fakeservice, track ip_dst; sid:2;)
alert ip any any -> any any (msg:"test xbits isset set fakeservice"; xbits:isset, fakeservice, track ip_dst; sid:3;)
I can't get an alert triggered by the third rule.

alert ip any any -> any any (msg:"test hostbits set fakehost"; hostbits:set, fakehost, dst; sid:1;)
alert ip any any -> any any (msg:"test hostbits isset hostbits and set fakerservice"; hostbits:isset, fakehost, dst; hostbits:set, fakeservice, dst; sid:2;)
alert ip any any -> any any (msg:"test hostbits isset fakerservice"; hostbits:isset, fakeservice, dst; sid:3;)
I can get an alert triggered by the third rule.


Subtasks 2 (2 open — 0 closed)

Bug #9248: hostbits/xbits: treat hostbits and xbits differently in the rule ordering stage (8.0.x backport)AssignedOISF DevActions
Bug #9340: detect: hostbits and xbits rules incorrectly ordered (8.0.x backport)AssignedShivani BhardwajActions

Related issues 1 (1 open — 0 closed)

Related to Suricata - Bug #1399: flowbits: rules not always evaluated in necessary orderIn ReviewShivani BhardwajActions

PA Updated by Philippe Antoine over 2 years ago Actions #2

  • Related to Bug #1399: flowbits: rules not always evaluated in necessary order added

PA Updated by Philippe Antoine over 2 years ago Actions #3

  • Target version set to TBD

PA Updated by Philippe Antoine over 2 years ago Actions #4

  • Assignee set to OISF Dev

PA Updated by Philippe Antoine over 1 year ago Actions #5

  • Status changed from New to Feedback
  • Label Needs Suricata-Verify test added

Is this still an issue in Suricata 8 ? Would you have a pcap to test the supplied rules ?

JF Updated by Juliana Fajardini Reichow 10 days ago Actions #6

Philippe Antoine wrote in #note-5:

Is this still an issue in Suricata 8 ? Would you have a pcap to test the supplied rules ?

I seem to have run into the same problem while working for fixes for another ticket ( #8459 ).

I'll share one or two SV tests.

JF Updated by Juliana Fajardini Reichow 10 days ago Actions #7

Report from Claude (Opus 5.5):
  1. Rule ordering: xbits setters sort after readers

Found while writing SV test for #8459. Present on `main` (checked
2026-10-21). Independent of firewall mode.

  1. Symptom

Two TD rules matching the same packet, one with `xbits:set,X,track ip_src`
and one with `xbits:isset,X,track ip_src`: the `isset` rule does not alert.
Rule ordering is meant to put setters before readers, so this pattern should
work.

  1. Cause

`SCSigGetXbitsType()` (`src/detect-engine-sigorder.c:406-460`) classifies a
rule for the hostbits / ippairbits ordering, but its two loops check different
keywords:

- MATCH list (readers: `isset` / `isnotset`): counts only `DETECT_XBITS` (`:415`).
- POSTMATCH list (writers: `set` / `unset` / `toggle`): counts only
`DETECT_HOSTBITS` (`:434`).

Result, with `NOT_USED=1 < READ=2 < SET_READ=3 < SET=4` and higher sorting
first (`SCSigLessThan`, `:539-540`):

Rule Classified as Effect
------------------ -------------- -------------------------------
xbits `isset` `READ` (2) sorts before the setter
xbits `set` `NOT_USED` (1) writer never recognised
hostbits `set` `SET` (4) correct
hostbits `isset` `NOT_USED` (1) reader never recognised

hostbits ends up in the right order only because both sides are misclassified
in a way that still puts the setter first. xbits with `track ip_src|ip_dst`
maps to `VAR_TYPE_HOST_BIT` (`src/detect-xbits.c:309-314`), so it goes
through the same hostbits comparison and comes out reversed.

  1. Possible fix

Have both loops count `DETECT_XBITS` and `DETECT_HOSTBITS`. This changes rule
ordering for any ruleset using xbits or hostbits, so it needs its own ticket
and tests (xbits and hostbits same-packet `set` -> `isset`, both `ip_src` and
`ip_pair`).

  1. Not checked

- Whether `ip_pair` xbits (`SCSigOrderByIPPairbitsCompare`) hit the same
reversal; the code path suggests so.

JF Updated by Juliana Fajardini Reichow 10 days ago Actions #8

  • Status changed from Feedback to Triaged
  • Target version changed from TBD to 9.0.0-beta1

JF Updated by Juliana Fajardini Reichow 10 days ago Actions #9

  • Affected Versions 8.0.7, git main added
  • Label Needs backport to 8.0 added

JF Updated by Juliana Fajardini Reichow 10 days ago Actions #10

  • Label deleted (Needs Suricata-Verify test)

OT Updated by OISF Ticketbot 10 days ago Actions #11

  • Subtask #9248 added

OT Updated by OISF Ticketbot 10 days ago Actions #12

  • Label deleted (Needs backport to 8.0)

SB Updated by Shivani Bhardwaj 3 days ago Actions #13

  • Status changed from Triaged to Assigned
  • Assignee changed from OISF Dev to Shivani Bhardwaj

SB Updated by Shivani Bhardwaj 3 days ago Actions #14

  • Difficulty set to low

SB Updated by Shivani Bhardwaj 3 days ago Actions #15

  • Subject changed from hostbits/xbits: treat hostbits and xbits differently in the rule ordering stage to detect: hostbits and xbits rules incorrectly ordered
  • Label Needs backport to 8.0 added

OT Updated by OISF Ticketbot 3 days ago Actions #16

  • Subtask #9340 added

OT Updated by OISF Ticketbot 3 days ago Actions #17

  • Label deleted (Needs backport to 8.0)
Actions

Also available in: PDF Atom