-
Notifications
You must be signed in to change notification settings - Fork 1
ROX-30437: resolve host path via inode tracking #158
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #pragma once | ||
|
|
||
| #include "file.h" | ||
| #include "d_path.h" | ||
| #include "maps.h" | ||
| #include "types.h" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| name = "fact-ffi" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| license.workspace = true | ||
|
|
||
| [lib] | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| aya = { workspace = true } | ||
|
|
||
| [build-dependencies] | ||
| cc = { workspace = true } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| fn main() { | ||
| println!("cargo::rerun-if-changed=src/c/"); | ||
| cc::Build::new() | ||
| .file("src/c/inode.c") | ||
| .opt_level(2) | ||
| .warnings_into_errors(true) | ||
| .warnings(true) | ||
| .compile("inode"); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||
| #include <errno.h> | ||||||
| #include <fcntl.h> | ||||||
| #include <stdint.h> | ||||||
| #include <stdio.h> | ||||||
| #include <string.h> | ||||||
| #include <unistd.h> | ||||||
|
|
||||||
| #include <linux/bpf.h> | ||||||
| #include <sys/syscall.h> | ||||||
| #include <sys/types.h> | ||||||
|
|
||||||
| int32_t add_path(int32_t map_fd, const char* path, const char* host_path) { | ||||||
| int fd = open(path, O_RDONLY); | ||||||
| if (fd <= 0) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| fprintf(stderr, "%s:%d - open error: %d\n", __FILE__, __LINE__, errno); | ||||||
| return errno; | ||||||
| } | ||||||
|
|
||||||
| char buf[4096]; | ||||||
| snprintf(buf, 4096, "%s", host_path); | ||||||
|
|
||||||
| union bpf_attr attr; | ||||||
| memset(&attr, 0, sizeof(attr)); | ||||||
| attr.map_fd = map_fd; | ||||||
| attr.key = (unsigned long long)&fd; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get it, we search in inode_store by the inode: but store by the file descriptor?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's how the inode storage works. When accessing from userspace via the bpf syscall you don't have a way to access the inode pointer itself (because it lives on kernel memory), so you give it the file descriptor to the file and the kernel resolves the inode under the hood: But on kernel side, you do have the inode pointer, so you can use that directly: |
||||||
| attr.value = (unsigned long long)buf; | ||||||
| attr.flags = BPF_NOEXIST; | ||||||
|
|
||||||
| long res = syscall(SYS_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); | ||||||
| if (res == -EEXIST) { | ||||||
| res = 0; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some debug log output in this case? |
||||||
| } | ||||||
|
|
||||||
| close(fd); | ||||||
| return res; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| use std::{ | ||
| ffi::{c_char, CString}, | ||
| os::fd::{AsFd, AsRawFd}, | ||
| path::Path, | ||
| }; | ||
|
|
||
| use aya::maps::MapData; | ||
|
|
||
| #[link(name = "inode")] | ||
| unsafe extern "C" { | ||
| fn add_path(map_fd: i32, path: *const c_char, host_path: *const c_char) -> i32; | ||
| } | ||
|
|
||
| fn path_to_cstring(path: &Path) -> anyhow::Result<CString> { | ||
| let path = path.as_os_str().to_string_lossy(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like Regarding lossy part -- I assume it will cause problems with non-UTF8 file names, correct? |
||
| Ok(CString::new(path.to_string())?) | ||
| } | ||
|
|
||
| pub fn try_add_path( | ||
| inode_store: &mut MapData, | ||
| path: &Path, | ||
| host_path: &Path, | ||
| ) -> anyhow::Result<()> { | ||
| let path = path_to_cstring(path)?; | ||
| let host_path = path_to_cstring(host_path)?; | ||
| let fd = inode_store.fd().as_fd().as_raw_fd(); | ||
| let res = unsafe { add_path(fd, path.as_ptr(), host_path.as_ptr()) }; | ||
|
|
||
| if res != 0 { | ||
| anyhow::bail!("Failed to add inode: {res}"); | ||
| } | ||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod inode_store; |
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.
Why should we ignore events if the host_path was not resolved?