26 Commits
Author SHA1 Message Date
Jing Chen a093ad0450 Simplify and format gVisor codebase.
The changes are just output of `gofmt -s -w .`.
2024-10-13 00:50:32 -07:00
Kevin KrakauerandgVisor bot 9488cfcf0b nftables: use a secure RNG
Linux implements the random operation with a cryptographically secure RNG. We
must do the same.

PiperOrigin-RevId: 678301194
2024-09-24 10:22:10 -07:00
Jayden NyamiakaandgVisor bot b2340af8a7 Implement Meta Set operation (parsing, interpretation, evaluation, tests).
The set of meta fields for meta set is much more limited than meta load.
Currently supports setting a limited set of meta fields: pkttype.
It should also be able to support: mark, priority, nftrace, and secmark,
but these have yet to be implemented.

PiperOrigin-RevId: 674426635
2024-09-13 14:07:41 -07:00
Jayden NyamiakaandgVisor bot 53af6d6ee6 Improve conciseness of parsing keywords and String func for enum constants.
Replaced overly wordy switch statements with concise maps.
Also makes the style consistent with how meta enum constants are processed.

PiperOrigin-RevId: 674402548
2024-09-13 12:58:51 -07:00
Jayden NyamiakaandgVisor bot ab0c794f9b Implement Meta Load operation (parsing, interpretation, evaluation, tests).
Currently supports a limited set of meta fields. The supported fields are
len, protocol, nfproto, l4proto, skiud, skgid, rtclassid (only for ipv6),
pkttype, prandom, time, day, and hour.

PiperOrigin-RevId: 674392201
2024-09-13 12:29:52 -07:00
Jayden NyamiakaandgVisor bot 548733574e Implement Byteorder operation (parsing, interpretation, evaluation, tests).
Uses binary.NativeEndian & binary.BigEndian for all byteorder operations.

Also removes all magic number instances of 4 and 16 as register sizes.

PiperOrigin-RevId: 674065001
2024-09-12 17:08:39 -07:00
Jayden NyamiakaandgVisor bot 684a1c8665 Implement Route operation (parsing, interpretation, evaluation, tests).
Out of the 5 route keys, we currently support all but IPsec/XRFM key.
However, classid key is only supported for IPv6 (needs support for IPv4 too).

Also removes repetition in validatePayloadBase & String for payloadBase.

PiperOrigin-RevId: 674044789
2024-09-12 16:03:41 -07:00
Jayden NyamiakaandgVisor bot b050c045d1 Implement Last operation (construction, evaluation, tests, NO interpretation).
Last operation is thread-safe.

Includes minor state/structure changes that enable time tracking:
- Adds clock & start time to NFTables state (tcpip Standard Clock as default).
- Passes rule as argument to operation evaluation for access to NFTables state.

PiperOrigin-RevId: 674021450
2024-09-12 14:57:42 -07:00
Jayden NyamiakaandgVisor bot be59d8a1c5 Implement Counter operation (parsing, interpretation, evaluation, tests).
Counter operation is thread-safe.

PiperOrigin-RevId: 673993509
2024-09-12 13:53:46 -07:00
Jayden NyamiakaandgVisor bot 12fb7f25d2 Implement Range operation (construction, evaluation, tests, NO interpretation).
The linux kernel defines the range operation, but its use has not been observed
in the nft binary debug output; commands use two comparison operations instead.
Thus, no interpretation of range operation via the nft binary debug output, but
it's fully supported. Similar comment has been left under operation definition.

Operation named "ranged"  because "range" is a keyword in golang.
Also includes small change to the comparison operation evaluation; no
change in behavior.

PiperOrigin-RevId: 673932318
2024-09-12 11:35:42 -07:00
Jayden NyamiakaandgVisor bot 115723cc9f Change registerData that must be bytesData to explicit bytesData within ops.
Change resulted from realization that it is currently unnecessary and extra to
store all bytesData within operations generically. If this is needed in the
future, we can change it then, but for now, a more direct and as-necessary
implementation is preferred.

PiperOrigin-RevId: 673908276
2024-09-12 10:39:17 -07:00
gVisor bot 3c4b246cf2 Fix printf violations inside of the gvisor code
Recently printf.Analyzer has become stricter
(https://github.com/golang/go/issues/60529)
which led to new findings.
gvisor nogo tests run this analyzer and fail if it produces findings.

PiperOrigin-RevId: 671657227
2024-09-06 00:45:23 -07:00
Jayden NyamiakaandgVisor bot 3fbdd4a142 Implement Bitwise operation (evaluation, tests, partial interpretation).
PiperOrigin-RevId: 670691161
2024-09-03 13:50:05 -07:00
Jayden NyamiakaandgVisor bot 341a018fd3 Minor refactor of constructor for comparison operator.
Justification: Since the comparison operator is only valid for bytesData, the
constructor should recieve a byte slice as input and then convert it to
registerData. It makes less sense to have the caller create the registerData
from the byte slice if bytes data is the only type of registerData, we accept.

This change removes unnecessary checks and makes the code a lot less wordy by
removing the profuse amount of newBytesData calls for comparison construction.

PiperOrigin-RevId: 670652077
2024-09-03 12:08:01 -07:00
Jayden NyamiakaandgVisor bot bd89a24410 Implement PayloadSet operation (parsing, interpretation, evaluation, tests).
Tests payload set evaluation for all basic fields of IP, IPv6, & TCP headers.
Similar to PayloadLoad, these headers were prioritized. Setting other headers
should still work but wasn't explicitly tested. Tests for other packets should
be added later.

PiperOrigin-RevId: 670636213
2024-09-03 11:28:54 -07:00
Jayden NyamiakaandgVisor bot 33dc9383dc Implement PayloadLoad operation (parsing, interpretation, evaluation, tests).
Tests payload load evaluation for all basic fields of IP, IPv6, & TCP headers.
These are the main headers that are encountered for gVisor, so their
implementation was prioritized. Loading for other packet headers should work
as expected since raw/general payload loading works, but it has not been
explicitly tested. These tests should be added later and a TODO has been left
noting this.

PiperOrigin-RevId: 668202252
2024-08-27 17:24:28 -07:00
Jayden NyamiakaandgVisor bot 99745eb79e Changes interpreter data representation from Little to Big Endian.
Previously, it was thought that the interpreter stored data and did evaluations
in Little Endian. The confusion originated from the nft binary's debug output,
which is the primary artifact guiding the interpreter's implementation. The
debug output of the nft binary prints data in Little Endian although the data
is actually being stored in Big Endian. Since this is the only reference we
have to implement the interpreter, other than the Linux kernel, we thought the
data matched the debug output directly such that the interpreter's host
representation was Little Endian. The mistake became clear when looking at the
Linux kernel code for the byteorder operation and realizing the apparent
inconsistencies.

This also came with a few other changes as a result of switching endianness.
Allows data of any number of bytes (0,16]
Converts data in nftinterp from Little to Big Endian to account for the debug
output's transformation.

PiperOrigin-RevId: 668151185
2024-08-27 15:00:31 -07:00
Jayden NyamiakaandgVisor bot bc4a23853e Hide all implementation details deeper than Rule abstraction.
PiperOrigin-RevId: 667367125
2024-08-25 13:48:17 -07:00
Jayden NyamiakaandgVisor bot 452ecde42e Make BytesData generic to support all multiples of 4 bytes (up to 16).
Removes Data- types and switches to Go's built-in type system.
Modifies tests to allow for creation and interpretation of 8- and 12-byte data.

Change became necessary when it was noticed that some nft binary operations
allowed for 8-byte data. Previously believed data was restricted to 4 or 16
bytes, but turned out the restriction is for data to be given in multiples of 4
bytes. This change corrects that.

PiperOrigin-RevId: 666542163
2024-08-22 16:54:35 -07:00
Jayden NyamiakaandgVisor bot b508258e39 Implement Comparison operation (parsing, interpretation, evaluation, tests).
Also improves the conciseness and consistency of the nftinterp_test file.

PiperOrigin-RevId: 663200389
2024-08-15 00:16:41 -07:00
Jayden NyamiakaandgVisor bot 0f4d195bff Implement Loop Checking for Jump & Goto operations with minor rule restructure.
Loop checking is done whenever a rule is registered to a chain.
Includes restructuring how rules are added/registered to chains to make loop
checking consistent and restrict adding operations to already registered rules.

Change also performs minor code re-org, moving evaluation methods to the top.

PiperOrigin-RevId: 663039449
2024-08-14 13:46:45 -07:00
Jayden NyamiakaandgVisor bot fa6e6811ed Implement base rule interpreter structure & Immediate operation interpretation.
Implement the necessary structure for rules to be parsed and interpreted.
The interpreter parses one line at a time with each line corresponding
to a single operation in a rule.

PiperOrigin-RevId: 660575983
2024-08-07 16:17:20 -07:00
Jayden NyamiakaandgVisor bot 2e8244c61b Implement Immediate operation and test evaluation from top level.
Currently, the Immediate operation is the only supported operation.

PiperOrigin-RevId: 660512320
2024-08-07 13:22:25 -07:00
Jayden NyamiakaandgVisor bot 7e665a50ec Implement complete logic for packet evaluation from hook to operation level.
Change flushes out the Rule type and defines Operation interface.
Logic incorporates the RegisterSet and a pseudo jumpstack and supports jumping
between chains via jump and goto Verdicts starting from a base chain.
Individual operations have left to be implemented.

PiperOrigin-RevId: 660470222
2024-08-07 11:35:43 -07:00
Jayden NyamiakaandgVisor bot dc3506fbee Implement RegisterSet & RegisterData and redefine Verdict for later use.
RegisterSet will be modified by each operation and will hold resulting verdicts
in the verdict register.
RegisterData will be stored in operations and used to set the data of specific
registers within the RegisterSet.
Both will be used mainly during packet evaluation.

PiperOrigin-RevId: 659637471
2024-08-05 12:12:03 -07:00