From: Alexei Starovoitov Subject: Re: [PATCH v2 net-next] bpf: enable verifier to add 0 to packet ptr Date: Fri, 3 Feb 2017 14:53:58 -0800 Message-ID: <20170203225355.GA26227@ast-mbp.thefacebook.com> References: <1486142565-2794-1-git-send-email-u9012063@gmail.com> <20170203205513.GA24959@ast-mbp.thefacebook.com> <5895043F.8080007@iogearbox.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: William Tu , Linux Kernel Network Developers , Mihai Budiu To: Daniel Borkmann Return-path: Received: from mail-pf0-f194.google.com ([209.85.192.194]:35708 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752268AbdBCWyC (ORCPT ); Fri, 3 Feb 2017 17:54:02 -0500 Received: by mail-pf0-f194.google.com with SMTP id f144so2398668pfa.2 for ; Fri, 03 Feb 2017 14:54:02 -0800 (PST) Content-Disposition: inline In-Reply-To: <5895043F.8080007@iogearbox.net> Sender: netdev-owner@vger.kernel.org List-ID: On Fri, Feb 03, 2017 at 11:29:19PM +0100, Daniel Borkmann wrote: > On 02/03/2017 10:10 PM, William Tu wrote: > >Hi Alexei, > > > >why it is bogus? on my system, it fails without the patch applied. > > > >--William > > > >On Fri, Feb 3, 2017 at 12:55 PM, Alexei Starovoitov > > wrote: > >>On Fri, Feb 03, 2017 at 09:22:45AM -0800, William Tu wrote: > >>>The patch fixes the case when adding a zero value to the packet > >>>pointer. The verifer reports the following error: > >>> [...] > >>> R0=imm0,min_value=0,max_value=0 > >>> R1=pkt(id=0,off=0,r=4) > >>> R2=pkt_end R3=fp-12 > >>> R4=imm4,min_value=4,max_value=4 > >>> R5=pkt(id=0,off=4,r=4) > >>> 269: (bf) r2 = r0 // r2 becomes imm0 > >>> 270: (77) r2 >>= 3 > >>> 271: (bf) r4 = r1 // r4 becomes pkt ptr > >>> 272: (0f) r4 += r2 // r4 += 0 > >>> addition of negative constant to packet pointer is not allowed > >>> > >>>Signed-off-by: William Tu > >>>Signed-off-by: Mihai Budiu > [...] > >>> { > >>>+ "direct packet access: test14 (pkt_ptr += 0, good access)", > >>>+ .insns = { > >>>+ BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, > >>>+ offsetof(struct __sk_buff, data)), > >>>+ BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1, > >>>+ offsetof(struct __sk_buff, data_end)), > >>>+ BPF_MOV64_REG(BPF_REG_0, BPF_REG_2), > >>>+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0), > >> > >>wait. the test is bogus. > >>please write the proper test for the feature > >>and check that it fails before the patch and passes afterwards. > > But still same code path that is executed in verifier as BPF_K and > CONST_IMM tracked reg both share the same path under add_imm label > in check_packet_ptr_add(), no? So it becomes r2=pkt(id=0,off=0,r=0); > r0 = r2; r0 += 0 here in this test. Probably okay as well, though > there could be risk that in future both don't share the same path > for some reason. I guess you were referring to either adding tests > for BPF_K /and/ CONST_IMM reg or just the latter, right? yes. Sorry I wasn't clear. imo the 'r0 += 0' is not something that verifier should recognize, since such nop insns shouldn't be generated by the compiler. It happened that the code path in verifier covers that case as well, but I think we really need to test 'rX += rY' case where rY is recognized as imm0, since that what the original use case was about.