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

Implement json frontend #9

Merged
merged 12 commits into from
Sep 23, 2021
Merged

Conversation

alindima
Copy link
Collaborator

Adds the feature-guarded JSON frontend for compiling BPF filters.

@alindima alindima self-assigned this Aug 16, 2021
@alindima alindima force-pushed the json_frontend branch 3 times, most recently from 92c9c98 to cf1da46 Compare August 16, 2021 13:30
Copy link
Member

@andreeaflorescu andreeaflorescu left a comment

Choose a reason for hiding this comment

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

Review part 1. I just went through the first 3 commits. Will continue the review in the next couple of days.

tools/generate_syscall_tables.sh Outdated Show resolved Hide resolved
tools/generate_syscall_tables.sh Outdated Show resolved Hide resolved
tools/generate_syscall_tables.sh Show resolved Hide resolved
tools/generate_syscall_tables.sh Outdated Show resolved Hide resolved
tools/generate_syscall_tables.sh Outdated Show resolved Hide resolved
src/syscall_table/aarch64.rs Outdated Show resolved Hide resolved
src/syscall_table/mod.rs Outdated Show resolved Hide resolved
src/syscall_table/mod.rs Outdated Show resolved Hide resolved
@@ -256,7 +256,7 @@ let filters: BpfMap = seccompiler::compile_from_json(
categories to BPF programs.

```rust
pub type BpfMap = HashMap<String, Arc<BpfProgram>>;
pub type BpfMap = HashMap<String, BpfProgram>;
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you add some details about what is changed in the documentation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This type was not exported beforehand, it's introduced by this PR.
It was using an Arc in the beginning as a leftover from the use case we have in Firecracker.
But it's actually an implementation detail that needs to be handled in Firecracker (i.e. Map the BpfProgram to an Arc), since it's usecase-specific.

Copy link
Member

Choose a reason for hiding this comment

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

I meant to add some details in the commit message, sorry about that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Cargo.toml Outdated Show resolved Hide resolved
@@ -26,7 +27,7 @@ use bpf::{
pub use bpf::{sock_filter, BpfProgram, BpfProgramRef};

/// Backend Result type.
pub type Result<T> = std::result::Result<T, Error>;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this now private?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well it was not required to be public.
Users of the library will not interact directly with the Backend and therefore do not need access to this type.
I think there should only be one Result type exported and that is the Result from src/lib.rs

Cargo.toml Show resolved Hide resolved
@@ -101,7 +102,8 @@ impl TryFrom<&str> for TargetArch {
}

/// Comparison to perform when matching a condition.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(rename_all = "snake_case")]
Copy link
Member

Choose a reason for hiding this comment

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

Why? Can you also add the response to this question as a comment in the code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

well it's just more idiomatic for JSON to name properties in snake_case format. That's the only reason, syntactic sugar. Do you still think it's worth adding this as a comment?

src/frontend/json.rs Show resolved Hide resolved
#[serde(deny_unknown_fields)]
pub(crate) struct JsonCondition {
/// Index of the argument that is to be compared.
#[serde(rename = "index")]
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for the renames?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the JSON filter, a sycall rule object uses an args property for specifying a condition vector.
Therefore, the arg_* names in this case are a little redundant, since it's obvious that they refer to arguments.

For the other fields, they are usually abbreviations so that the filters are easier to read&write.

src/frontend/json.rs Outdated Show resolved Hide resolved
tools/generate_syscall_tables.sh Outdated Show resolved Hide resolved
src/frontend/json.rs Show resolved Hide resolved
src/frontend/json.rs Outdated Show resolved Hide resolved
src/syscall_table/aarch64.rs Outdated Show resolved Hide resolved
tests/json.rs Outdated Show resolved Hide resolved
tests/json.rs Outdated Show resolved Hide resolved

- label: "build-musl-arm-json"
commands:
- cargo build --release --features=json --target aarch64-unknown-linux-musl
Copy link
Member

Choose a reason for hiding this comment

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

We should also add a cargo check with the json feature on both platforms.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yes, what is not checked is the reversed I think, that there are no warnings with no features.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe this should be done by adding the -D warnings flag to the cargo build command, in the default rust-vmm-ci pipeline. WDYT?

This way we'd make this a default in rust-vmm, instead of having a custom step

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me, do you want to open a PR with your suggestion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep, will do that

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Signed-off-by: alindima <alindima@amazon.com>
Also adds a test that validates that the committed
syscall tables were not manually altered.

Signed-off-by: alindima <alindima@amazon.com>
Also add a custom pipeline that runs the syscall
table validations.

Signed-off-by: alindima <alindima@amazon.com>
- Modify the type declaration of BpfMap to not
be tied to `Arc` filter ownership, since this is
consumer-specific. Also removed the associated
documentation note.
- Fix broken link in the SeccompFilter cargo docs.

Signed-off-by: alindima <alindima@amazon.com>
This enables compiling BPF filters from JSON code
as an alternative of expressing the filters.
This is done via a new library method:
compile_from_json.

Signed-off-by: alindima <alindima@amazon.com>
Signed-off-by: alindima <alindima@amazon.com>
Signed-off-by: alindima <alindima@amazon.com>
Signed-off-by: alindima <alindima@amazon.com>
Signed-off-by: alindima <alindima@amazon.com>
Added a type that is used in the integration tests
for expressing whether the last reported errno
should be equal or not equal to a value or ignored.

Signed-off-by: alindima <alindima@amazon.com>
Signed-off-by: alindima <alindima@amazon.com>
petreeftime
petreeftime previously approved these changes Sep 23, 2021
use std::collections::HashMap;

pub(crate) fn make_syscall_table() -> HashMap<&'static str, i64> {
vec![
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think these are easier to read in syscall number order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They're sorted alphabetically. I don't have strong feelings about doing this either way, I think it depends on preference

Cargo.toml Outdated
@@ -10,4 +10,6 @@ license = "Apache-2.0 OR BSD-3-Clause"
edition = "2018"

[dependencies]
libc = ">=0.2.98"
libc = ">=0.2.39"
serde = { version = ">=1.0.27", features = ["derive"] }
Copy link
Collaborator

Choose a reason for hiding this comment

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

This matches also version 2.0.0 or larger, I think you'd want to specify it as ^1.0.27, which is equivalent to >=1.0.27 <2.0.0, so at least the major version wouldn't change. Alternatively, you could specify it as ~1.0.27 or >=1.0.27 <1.1.0

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good call! will do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

// Need the 'newtype' pattern so that we can implement a custom deserializer.
pub(crate) struct JsonFilterMap(pub HashMap<String, JsonFilter>);

// Implement a custom deserializer, that returns an error for duplicate thread keys.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I was also thinking about that, but serde_with pulls even more dependencies that we can otherwise avoid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep, I considered that as well, but I think it's best to avoid dependencies that we can otherwise work around quite easily

@@ -3,7 +3,7 @@ updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
interval: weekly
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why?

Copy link
Member

Choose a reason for hiding this comment

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

There are too many ops related PRs otherwise. This is a change at the rust-vmm org level.

Signed-off-by: alindima <alindima@amazon.com>
Copy link

@lauralt lauralt left a comment

Choose a reason for hiding this comment

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

Rubber stamp.

@andreeaflorescu andreeaflorescu merged commit c262897 into rust-vmm:main Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants