I'm trying to write a sample code and see how it works practically.
As said here and discussed here.
If everything is correct the output should be:
$ cat foo1
this is foo1 content
$ cat foo2
this is foo2 content
$ sudo bcc_mangle_open.py &
[1] 63453
$ cat foo1
this is foo2 content
I wrote a sample in BCC, which looks like this:
from bcc import BPF
# define BPF program
prog = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
int trace_entry(struct pt_regs *ctx)
{
char buf[10];
char foo2[] = "foo2";
char *fname = (char *) PT_REGS_PARM1(ctx);
bpf_probe_read_str(buf, sizeof(buf), fname);
if (buf[0] != 'f' || buf[1] != 'o' || buf[2] != 'o' || buf[3] != '1') {
return 0;
}
bpf_probe_write_user(fname, foo2, sizeof(foo2));
return 0;
};
"""
# load BPF program
b = BPF(text=prog)
b.attach_kprobe("do_sys_open", fn_name="trace_entry")
The content of foo1
is supposed to change, but it's not happening.
And I have tried to print fname
and even buf
using bpf_trace_printk()
, but I got nothing in my screen.
Any idea why the content is not changing?
update-1
As suggested by @Queole
.It worked..So we have to give (char *) PT_REGS_PARM2(ctx)
instead of (char *) PT_REGS_PARM1(ctx)
. I got the output but a bit weirdly. after some 3-4 cat foo1
, the content is changing.
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo2 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo1 content
$ cat foo1
this is foo2 content
What is the reason for this behavior?