-
Notifications
You must be signed in to change notification settings - Fork 905
Linux netfilter: Fix a Y2K38_SAFETY Coverity warning #1568
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
base: master
Are you sure you want to change the base?
Conversation
efe2289 to
a29eed3
Compare
This concerns the 'Sequence number' in Netlink messages.
The Linux Documentation/userspace-api/netlink/intro.rst states:
:c:member:`nlmsghdr.nlmsg_seq` should be a set to a monotonically
increasing value. The value gets echoed back in responses and doesn't
matter in practice, but setting it to an increasing value for each
message sent is considered good hygiene. The purpose of the field is
matching responses to requests.
Thus seq_id, used to set it, has no need to be initialized to
time(NULL). It can start at 1.
The Coverity warning was:
CID 1508935: Use of 32-bit time_t (Y2K38_SAFETY)
A time_t value is stored in an integer with too few bits to accommodate
it. The expression time(NULL) is cast to unsigned int.
356 seq_id = time(NULL);
There was also an old clang warning on 64-bit build:
pcap-netfilter-linux.c:356:12: warning: implicit conversion loses
integer precision: 'time_t' (aka 'long') to 'unsigned int'
[-Wshorten-64-to-32]
356 | seq_id = time(NULL);
| ~ ^~~~~~~~~~
It was silenced by 16782c7.
This commit is no longer useful.
The "To be look at before 2038..." in the previous commit message is no
longer relevant.
Which also means it takes longer to roll over (and ceases to be monotonically increasing), so that's an additional improvement. (Unless a lot of messages are sent per second, it'll be a while before it's likely to roll over, but....) |
|
When doing a test with a |
|
Note: |
|
Because "The purpose of the field is matching responses to requests." a rool over should not be a problem. |
|
"Communicating between the kernel and user-space in Linux using Netlink Sockets: Source code reference" says, on page 10:
I presume that "there is no need to use incremental sequence numbers" because, in that code, there's only one message in flight, so you would just use What libnl3 does, as of the current tip of the master branch, is:
I guess there's some concern about collisions between sequence numbers in difference sockets, otherwise I'm not sure why you don't just start them all at 1. |
|
If "there's some concern about collisions between sequence numbers in difference sockets,", there is a design problem. |
|
In https://people.netfilter.org/pablo/netlink/netlink.pdf: With note: Nothing about time() or risk of collisions. |
|
So is there a reason why libnl3 bothers to randomize the sequence numbers? Or did they do that because they mistakenly thought that they had to do that? |
|
It's unclear because there is also: |
|
In libnl, doc/core.txt: |
|
Same file: |
Good questions... |
|
No mention to time() use in https://www.rfc-editor.org/rfc/rfc3549. |
|
https://docs.kernel.org/6.1/userspace-api/netlink/intro.html: Same as in Documentation/userspace-api/netlink/intro.rst. |
|
Their "randomize" is not randomize and is decreasing. |
|
There are two parts to libnl3's sequence number handling:
The random initial sequence number sounds similar to what's done for TCP. That's done to make it harder for an attacker to guess sequence numbers, not to prevent sequence number collisions from different TCP connections. If replies to netlink packets always arrive on the same socket as the one on which the request was sent, that's similar to TCP, so there's no need to prevent collisions from different sockets. It may be different from TCP, however, in that I'm not sure there's a way to attack a netlink socket user by injecting traffic on the socket. If there's a way to do that, randomized sequence numbers might be used as a defense against that. The only way I see avoiding collisions between sockets being useful would be... ...if you're running a program that captures on an nlmon device to monitor netlink traffic, and the program wants to match request and responses. I don't see anything in LINKTYPE_NETLINK to associate a captured netlink packet with a specific socket. |
Thank you for digging deeper.
I think there is a big difference about randomization: On Linux, e.g., the TCP sequence number, (via secure_tcp_seq()) based on adresses, ports, secret, hashes is "unpredictable" (or at least very difficult to predict). The sequence number based on time() seems totally predictable. |
Uh oh!
There was an error while loading. Please reload this page.