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

pgsql: don't error out with PDU parsing errors - v2 #12609

Closed
wants to merge 11 commits into from

Conversation

jufajardini
Copy link
Contributor

Previous PR: #12543

Link to ticket: https://redmine.openinfosecfoundation.org/issues/
https://redmine.openinfosecfoundation.org/issues/5524
https://redmine.openinfosecfoundation.org/issues/5566

It seemed to me that it made sense to have the events as part of this work

Describe changes:

  • Add events, and SID range, and pgsql-events.rules file
  • Add PgsqlParseError(s) -- based on work started by @jasonish
  • fix commit messages
  • sometimes still return error - for instance, when the error is for length parsing
  • always use functions for PDU length parsing
  • other changes tangential to the overall work

Provide values to any of the below to override the defaults.

SV_BRANCH=OISF/suricata-verify#2299

jufajardini and others added 11 commits February 17, 2025 16:52
Some backend messages can be the shortest pgsql length possible,
4 bytes, but the parser expectd all messages to be longer than that.

Related to
Bug OISF#5524
The initial parsing for message type checking was more complex than
needed be.

Related to
Bug OISF#5524
Building on top of work done by Jason Ish.

Related to
Bug OISF#5524
Some inner parsers were using it, some weren't. Better to standardize
this. Also take the time to avoid magic numbers for representing the
expected lengths for pgsql PDUs.
Also throwing PgsqlParseError and allowing for incomplete results.

Related to
Task OISF#5566
Bug OISF#5524
Even if unknown, if the message is properly parsed, allow the parser to
proceed.

Related to
Bug OISF#5524
This allows the app-proto to continue onto parsing next PDUs, if
possible.

Bug OISF#5524
Events for:
- parsing error when parsing pgsql packet length
- parsing error for pgsql requests (post length parsing)
- parsing error for pgsql responses (post length parsing)
- too many transactions

Include `pgsql-events.rules` file, and PGSQL events SID range definition

Task OISF#5566
No state change, but since we added Unknown responses, we should handle
that case -- should we have a specific state for such cases?

Related to
Bug OISF#5524
Task OISF#5566
This may happen in some situations if the app-layer parser only sees
unknown messages and sets an event: there will be an empty transaction,
but nothing to log.

Related to
Task OISF#5566
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

Attention: Patch coverage is 81.36646% with 30 lines in your changes missing coverage. Please review.

Project coverage is 80.75%. Comparing base (10ede91) to head (9581d24).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12609      +/-   ##
==========================================
+ Coverage   80.74%   80.75%   +0.01%     
==========================================
  Files         931      931              
  Lines      259144   259209      +65     
==========================================
+ Hits       209242   209332      +90     
+ Misses      49902    49877      -25     
Flag Coverage Δ
fuzzcorpus 56.99% <82.85%> (+0.02%) ⬆️
livemode 19.37% <1.42%> (-0.01%) ⬇️
pcap 44.13% <1.42%> (-0.03%) ⬇️
suricata-verify 63.45% <79.85%> (+0.02%) ⬆️
unittests 58.34% <63.35%> (+<0.01%) ⬆️

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 24761

Copy link
Member

@victorjulien victorjulien left a comment

Choose a reason for hiding this comment

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

CI is unhappy. Looks pretty good to me. A few nits/questions inline.

let (i, identifier) = verify(be_u8, |&x| x == b'T')(i)?;
let (i, length) = verify(be_u32, |&x| x > 6)(i)?;
//let (i, length) = parse_gt_length(i, 6)?;
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove commented out code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh, that's what I get when having so much back and forth fixing commit history x___x sorry about that and the rust issue x___x

// TODO Log anomaly event instead?
js.set_bool("request", false)?;
js.set_bool("response", false)?;
// TODO Log anomaly event?
Copy link
Member

Choose a reason for hiding this comment

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

in what case can this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, we can have empty transactions for instance if we get parsing error(s) for the msg length from both response and request (or if there's only one pgsql message in the transaction). This happens with the pgsql-events test that's present in the SV PR that goes with this one:

{
  "timestamp": "2022-01-04T03:54:04.788066+0000",
  "flow_id": 1132919115245510,
  "event_type": "pgsql",
  "src_ip": "167.248.133.59",
  "src_port": 47478,
  "dest_ip": "10.44.3.101",
  "dest_port": 5432,
  "proto": "TCP",
  "pkt_src": "stream (flow timeout)",
  "pgsql": {
    "tx_id": 1
  }
}

On the one hand, if we know we can "cause" this, I'm not sure if it makes sense to consider this an anomaly. On the other, I guess that ideally the nodes wouldn't send messages with unparsable length, which could indicate an anomaly...

Copy link
Member

Choose a reason for hiding this comment

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

Hmm this record doesn't look very useful. There is no indication of an error or that an event has been set. How are other parsers handling this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SSH will return false if the transaction is empty, I could do that for PGSQL. The app-layer event is still created and the stats will still register that PGSQL was seen.

@jufajardini
Copy link
Contributor Author

CI fixed with #12614

@jufajardini
Copy link
Contributor Author

CI fixed with #12614

Or not. One issue remaining, still.

@jufajardini jufajardini deleted the 5524-pgsql/v2 branch February 19, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants