Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

[Question] How to load a xdp program with cargo installed by rustup #288

Closed
Forsworns opened this issue Feb 21, 2022 · 3 comments
Closed

Comments

@Forsworns
Copy link

Forsworns commented Feb 21, 2022

I work on WSL2 and it has support eBPF programs since version 5.10.74.3-microsoft-standard-WSL2.

As the cargo-bpf doc said:

Loading eBPF programs requires admin priviledges, so you'll have to run
load as root or with sudo:

$ sudo cargo bpf load -i eth0 target/bpf/programs/block_http.elf

But my cargo is installed with rustup, which installs cargo in my user home. Therefore, I got error sudo cargo: command not found.

I tried with sudo -E bash -c "cargo bpf load -i eth0 target/bpf/programs/block_http.elf". But the error is the same.

I also tried use ip to load xdp programs, as what is recommended in XDP tutorial

ip link set dev lo xdpgeneric obj block_http.elf sec xdp

However, it reminded me the elf file from redbpf did not has a xdp section and PTR *mut xdp_md type_id=2 Invalid name:

bpf_sentinel/ebpf_sentinel.elf sec xdp

BTF debug data section '.BTF' rejected: Invalid argument (22)!
 - Length:       1344
Verifier analysis:

magic: 0xeb9f
version: 1
flags: 0x0
hdr_len: 24
type_off: 0
type_len: 320
str_off: 320
str_len: 1000
btf_total_size: 1344
[1] PTR *mut xdp_md type_id=2 Invalid name

Program section 'xdp' not found in ELF file!
Error fetching program/map!
@rsdy
Copy link
Collaborator

rsdy commented Feb 21, 2022

I think sudo -E bash -c ... will re-run your /root/.bashrc and reset your PATH. You should try sudo -E cargo ..., or install through sudo rustup.

@Forsworns
Copy link
Author

I think sudo -E bash -c ... will re-run your /root/.bashrc and reset your PATH. You should try sudo -E cargo ...

Tried sudo -E cargo ... and the output is the same

Fine, maybe I need to reinstall the cargo and toolchains :(

@Forsworns
Copy link
Author

It seems tedious to re-install system-wide cargo/rustup.

I change the /etc/passwd file for user privilege escalation. By changing the user id and group id of the user with cargo installed to 0. The user gains root privilege without sudo now. And the XDP program is successfully injected via cargo bpf tool. Don't know if this is a good way. At least, it works without much suffering. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants