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

Skeleton headers for host isolation #130

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion non-GPL/Events/EventsTrace/EventsTrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ int main(int argc, char **argv)
if (g_features_autodetect)
ebpf_detect_system_features(&opts.features);

err = ebpf_event_ctx__new(&ctx, event_ctx_callback, opts);
err = ebpf_event_ctx__new(&ctx, event_ctx_callback, opts, /* dry_run= */ false);

if (err < 0) {
fprintf(stderr, "Could not create event context: %d %s\n", err, strerror(-err));
Expand Down
12 changes: 10 additions & 2 deletions non-GPL/Events/Lib/EbpfEvents.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ int ebpf_set_verbose_logging()

int ebpf_event_ctx__new(struct ebpf_event_ctx **ctx,
ebpf_event_handler_fn cb,
struct ebpf_event_ctx_opts opts)
struct ebpf_event_ctx_opts opts,
bool dry_run)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This addition doesn't really change functionality but I wanted to make this option more explicit in the code to prevent bugs or relying on a certain behavior in the function. We did in fact had bugs like this in this function before. The Host Isolation version of this also has a dryRun parameter.

{
struct EventProbe_bpf *probe = NULL;
struct btf *btf = NULL;
Expand Down Expand Up @@ -469,8 +470,15 @@ int ebpf_event_ctx__new(struct ebpf_event_ctx **ctx,
if (err != 0)
goto out_destroy_probe;

if (!ctx)
if (dry_run) {
err = 0;
goto out_destroy_probe;
}

if (!ctx) {
err = -ENOENT;
goto out_destroy_probe;
}

*ctx = calloc(1, sizeof(struct ebpf_event_ctx));
if (*ctx == NULL) {
Expand Down
9 changes: 5 additions & 4 deletions non-GPL/Events/Lib/EbpfEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ int ebpf_detect_system_features(uint64_t *features);

/* Allocates a new context based on requested events and capabilities.
*
* If ctx is NULL, the function returns right after loading and attaching the
* libbpf skeleton.
* If dry_run is true, the function only tests load and attach to
* verify host compatibility.
*
* Returns a positive int that represents an fd, which can be used with epoll
* on success. Returns an error on failure. If ctx is NULL,
* on success. Returns an error on failure. If dry_run is true,
* returns 0 on success or less than 0 on failure.
*/
int ebpf_event_ctx__new(struct ebpf_event_ctx **ctx,
ebpf_event_handler_fn cb,
struct ebpf_event_ctx_opts opts);
struct ebpf_event_ctx_opts opts,
bool dry_run);

/* Consumes as many events as possible from the event context and returns the
* number consumed.
Expand Down
19 changes: 16 additions & 3 deletions non-GPL/HostIsolation/Lib/TcLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,22 +473,35 @@ int netlink_filter_add_begin(struct netlink_ctx *ctx, const char *ifname)
int netlink_filter_add_end(int fd, struct netlink_ctx *ctx, const char *ebpf_obj_filename)
{
struct nlmsghdr *nl = NULL;
struct bpf_prog_info info = {};
unsigned int info_len = sizeof(info);
char buf[128];
int rv = -1;
int len = 0;

if (!ctx || !ebpf_obj_filename) {
if (!ctx) {
ebpf_log("netlink_filter_add_end error: NULL parameter\n");
rv = -1;
goto out;
}

rv = bpf_obj_get_info_by_fd(fd, &info, &info_len);
if (rv < 0) {
ebpf_log("netlink_filter_add_end error: failed to get info by fd\n");
goto out;
}

nl = &ctx->msg.n;
memset(buf, 0, sizeof(buf));

len = snprintf(buf, sizeof(buf), "%s:[.text]", ebpf_obj_filename);
if (ebpf_obj_filename) {
len = snprintf(buf, sizeof(buf), "%s:[.text]", ebpf_obj_filename);
} else {
len = snprintf(buf, sizeof(buf), "%s:[%u]", info.name, info.id);
}

if (len < 0 || len >= sizeof(buf)) {
ebpf_log("netlink_filter_add_end error: filename too long\n");
ebpf_log("netlink_filter_add_end error: name too long\n");
rv = -1;
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion non-GPL/HostIsolation/Lib/TcLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int netlink_filter_add_begin(struct netlink_ctx *ctx, const char *ifname);
* @param[in] fd eBPF program file descriptor
* @param[in] ctx Context containing netlink state (from previous add_begin()
* call) - passed by caller
* @param[in] ebpf_obj_filename eBPF object filename
* @param[in] ebpf_obj_filename eBPF object filename (NULL if using skeleton header)
* @return Error value (0 for success)
*/
int netlink_filter_add_end(int fd, struct netlink_ctx *ctx, const char *ebpf_obj_filename);
Expand Down