On Wed, Jul 29, 2020 at 9:25 AM John Fastabend <john.fastabend@xxxxxxxxx> wrote: > > Similar to patch ("bpf: sock_ops ctx access may stomp registers") if the > src_reg = dst_reg when reading the sk field of a sock_ops struct we > generate xlated code, > > 53: (61) r9 = *(u32 *)(r9 +28) > 54: (15) if r9 == 0x0 goto pc+3 > 56: (79) r9 = *(u64 *)(r9 +0) > > This stomps on the r9 reg to do the sk_fullsock check and then when > reading the skops->sk field instead of the sk pointer we get the > sk_fullsock. To fix use similar pattern noted in the previous fix > and use the temp field to save/restore a register used to do > sk_fullsock check. > > After the fix the generated xlated code reads, > > 52: (7b) *(u64 *)(r9 +32) = r8 > 53: (61) r8 = *(u32 *)(r9 +28) > 54: (15) if r9 == 0x0 goto pc+3 > 55: (79) r8 = *(u64 *)(r9 +32) > 56: (79) r9 = *(u64 *)(r9 +0) > 57: (05) goto pc+1 > 58: (79) r8 = *(u64 *)(r9 +32) > > Here r9 register was in-use so r8 is chosen as the temporary register. > In line 52 r8 is saved in temp variable and at line 54 restored in case > fullsock != 0. Finally we handle fullsock == 0 case by restoring at > line 58. > > This adds a new macro SOCK_OPS_GET_SK it is almost possible to merge > this with SOCK_OPS_GET_FIELD, but I found the extra branch logic a > bit more confusing than just adding a new macro despite a bit of > duplicating code. > > Fixes: 1314ef561102e ("bpf: export bpf_sock for BPF_PROG_TYPE_SOCK_OPS prog type") > Signed-off-by: John Fastabend <john.fastabend@xxxxxxxxx> Acked-by: Song Liu <songliubraving@xxxxxx> [...]