Search code examples
linuxsystem-callsbpfebpf

Can eBPF modify the return value or parameters of a syscall?


To simulate some behavior I would like to attach a probe to a syscall and modify the return value when certain parameters are passed. Alternatively, it would also be enough to modify the parameters of the function before they are processes.

Is this possible with BPF?


Solution

  • I believe that attaching eBPF to kprobes/kretprobes gives you read access to function arguments and return values, but that you cannot tamper with them. I am NOT 100% sure; good places to ask for confirmation would be the IO Visor project mailing list or IRC channel (#iovisor at irc.oftc.net).

    As an alternative solution, I know you can at least change the return value of a syscall with strace, with the -e option. Quoting the manual page:

    -e inject=set[:error=errno|:retval=value][:signal=sig][:when=expr]
           Perform syscall tampering for the specified set of syscalls.
    

    Also, there was a presentation on this, and fault injection, at Fosdem 2017, if it is of any interest to you. Here is one example command from the slides:

    strace -P precious.txt -efault=unlink:retval=0 unlink precious.txt
    

    Edit: As stated by Ben, eBPF on kprobes and tracepoints is definitively read only, for tracing and monitoring use cases. I also got confirmation about this on IRC.