-
Notifications
You must be signed in to change notification settings - Fork 4
feat: sanitize paths generated by calling d_path #181
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "self-deleter" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
|
|
||
| [workspace] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| FROM rust:1.84-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY . . | ||
| RUN cargo build --release | ||
|
|
||
| FROM alpine:3.23 | ||
|
|
||
| COPY --from=builder /app/target/release/self-deleter /usr/local/bin | ||
|
|
||
| ENTRYPOINT ["self-deleter"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use std::{ | ||
| fs::{remove_file, File}, | ||
| io::Write, | ||
| }; | ||
|
|
||
| fn main() { | ||
| let exe_path = std::env::current_exe().expect("Failed to get executable path"); | ||
| println!("Removing executable: {}", exe_path.display()); | ||
| remove_file(exe_path).expect("Failed to remove executable"); | ||
|
|
||
| let mut args = std::env::args(); | ||
| let path = args.nth(1).expect("File to modify not provided"); | ||
|
|
||
| println!("Opening file: {path}"); | ||
| let mut f = File::create(path).expect("Failed to create test file"); | ||
| f.write_all(b"This is a test") | ||
| .expect("Failed to write to test file"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import os | ||
|
|
||
| from conftest import dump_logs | ||
| from event import Event, EventType, Process | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def build_self_deleter(docker_client): | ||
| image, _ = docker_client.images.build( | ||
| path='containers/self-deleter', | ||
| tag='self-deleter:latest', | ||
| dockerfile='Containerfile' | ||
| ) | ||
| return image | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def run_self_deleter(fact, monitored_dir, logs_dir, docker_client, build_self_deleter): | ||
| image = build_self_deleter.tags[0] | ||
| container = docker_client.containers.run( | ||
| image, | ||
| '/mounted/test.txt', | ||
| detach=True, | ||
| volumes={ | ||
| monitored_dir: { | ||
| 'bind': '/mounted', | ||
| 'mode': 'z', | ||
| }, | ||
| }, | ||
| name='self-deleter', | ||
| ) | ||
|
|
||
| yield container | ||
|
|
||
| container_log = os.path.join(logs_dir, 'self-deleter.log') | ||
| container.stop(timeout=1) | ||
| dump_logs(container, container_log) | ||
| container.remove() | ||
|
|
||
|
|
||
| def test_d_path_sanitization(fact, monitored_dir, server, run_self_deleter, docker_client): | ||
| """ | ||
| Ensure the sanitization of paths obtained by calling the bpf_d_path | ||
| helper don't include the " (deleted)" suffix when the file is | ||
| removed. | ||
| """ | ||
| # File Under Test | ||
| fut = '/mounted/test.txt' | ||
| host_path = os.path.join(monitored_dir, 'test.txt') | ||
|
|
||
| container = run_self_deleter | ||
|
|
||
| process = Process(pid=None, | ||
| uid=0, | ||
| gid=0, | ||
| exe_path='/usr/local/bin/self-deleter', | ||
| args=f'self-deleter {fut}', | ||
| name='self-deleter', | ||
| container_id=container.id[:12], | ||
| loginuid=pow(2, 32)-1) | ||
| event = Event(process=process, event_type=EventType.OPEN, | ||
| file=fut, host_path=host_path) | ||
| print(f'Waiting for event: {event}') | ||
|
|
||
| server.wait_events([event]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name is quite misleading, since no parsing is happening. How about
sanitize_d_path?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Also added a small paragraph here for files ending with
(deleted)being an edge case that we don't cover.