xdp-project / xdp-tutorial

XDP tutorial
2.33k stars 562 forks source link

How to run AF_XDP program without root privileges? #361

Closed wshwb closed 1 year ago

wshwb commented 1 year ago

I want to run a userspace program to create AF_XDP socket and receive packet through this socket, But I must to use "sudo" to start my userspace program to load a kernelspace XDP filter program and create a MUM and AF_XDP socket with libbpf library now. I want to know How to start userspace program which dont need "sudo" everytimes to receive packets? Please give me some advice!! Thanks a lot!!!

IlievIliya92 commented 1 year ago

You can consider using extended Linux capabilities in order to allow your process to be granted with the required privileges to load & run your BPF program. The setcap command will help you to configure the capabilities, here is an example command line how to do that:

$ sudo setcap cap_sys_admin,cap_dac_override,cap_sys_resource,cap_net_admin,cap_net_raw=eip /path/to/your/binary

where /path/to/your/binary is the path to the binary that you are currently executing using sudo. To verify that the operation was successful use the getcap utility:

$ getcap /path/to/your/binary
/path/to/your/binary cap_dac_override,cap_net_admin,cap_net_raw,cap_sys_resource,cap_bpf=eip

The cap_sys_admin is heavily loaded with privileges. You can try solely using the cap_bpf option instead. The choice of capabilities that you want to grant to your process are application specific and related to the sys calls and resources used by your program.

Note Please note that the Linux capabilities can introduce security risks if not properly managed and assigned. While they provide finer-grained control over privileges and reduce the need for processes to run with full root privileges, they also increase the attack surface and potential impact if misused.

wshwb commented 1 year ago

Thank you very much for your very good suggestions! 1.I have try you solutions in advanced03-AF_XDP it really works, I can create AF_XDP socket without "sudo". Your kind suggestions are very good. And I got another more general and complete solution https://github.com/xdp-project/xdp-tools/issues/320#issuecomment-1542338789 I will try it in the future.

Baruch-Fridman commented 1 week ago

And I got another more general and complete solution xdp-project/xdp-tools#320 (comment) I will try it in the future.

Have you tried this option? I tried with kernel v6.5 and it doesn't work. I saw that in version 6 the bpf_obj_get and bpf_obj_pin functions have changed, could this be a reason for this? Can it be overcome? Please note that this option also appears here https://next.redhat.com/2023/07/18/using-ebpf-in-unprivileged-pods/ . The option of adding capabilities is not my favorite. @wshwb