summaryrefslogtreecommitdiffstats
path: root/samples/bpf/test_cgrp2_sock.c
diff options
context:
space:
mode:
authorDavid Ahern <dsahern@gmail.com>2017-09-01 00:05:50 +0200
committerDavid S. Miller <davem@davemloft.net>2017-09-01 07:05:15 +0200
commit0adc3dd90050538ac543c6c0bea8e8527a9e2e2c (patch)
tree0455bd99d3cc18191b603ef13342b1b4386e38e3 /samples/bpf/test_cgrp2_sock.c
parentsamples/bpf: Update cgrp2 socket tests (diff)
downloadlinux-0adc3dd90050538ac543c6c0bea8e8527a9e2e2c.tar.xz
linux-0adc3dd90050538ac543c6c0bea8e8527a9e2e2c.zip
samples/bpf: Update cgroup socket examples to use uid gid helper
Signed-off-by: David Ahern <dsahern@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples/bpf/test_cgrp2_sock.c')
-rw-r--r--samples/bpf/test_cgrp2_sock.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/samples/bpf/test_cgrp2_sock.c b/samples/bpf/test_cgrp2_sock.c
index 5a688837720c..e79594dd629b 100644
--- a/samples/bpf/test_cgrp2_sock.c
+++ b/samples/bpf/test_cgrp2_sock.c
@@ -46,8 +46,18 @@ static int prog_load(__u32 idx, __u32 mark, __u32 prio)
/* set mark on socket */
struct bpf_insn prog_mark[] = {
- BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+ /* get uid of process */
+ BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+ BPF_FUNC_get_current_uid_gid),
+ BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffffffff),
+
+ /* if uid is 0, use given mark, else use the uid as the mark */
+ BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
+ BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_MOV64_IMM(BPF_REG_3, mark),
+
+ /* set the mark on the new socket */
+ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, mark)),
BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, mark)),
};