Received: by 10.192.165.148 with SMTP id m20csp1499289imm; Fri, 27 Apr 2018 22:30:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrCLfGnPpjl0JjmhX820JYThsYHxYgXFpI9AuWomPKf9HlYpGActLwQ7f+0GBd/iDH97GlO X-Received: by 10.98.210.134 with SMTP id c128mr4693911pfg.240.1524893435836; Fri, 27 Apr 2018 22:30:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524893435; cv=none; d=google.com; s=arc-20160816; b=lZjqFVcbpfymZpnyqK8EhagGeakeLbdq/sB6UDtJYUj3l4O66ZNJt8cv10px+uzz3s oejD5g+u8XisC8w1G+6zSsCqHzrGnTNKaE++9mbohX4MzdiY5XWWugBRTeyFWGcoGAkL +nCsz9Oz/jf0rvVbuogIIwCUz5+lc/jkOe1pTQ1/HsNrjzevb+TXeCSXMvL8dOhj5PdA gKxJYKQcw+znOEZmI1+1Mym47fJ6VUp/BYghxdoFo0/cfsdIQM/4Y0AuUh8t3/6MwBRo Fx3WyM3a2aHijs6La9bT5W9Rf7JxwSFZsYA9YJf2xz5YQGfMfFohJiSgHjdSXJgpw85/ IsHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=EZVEWGHIgDd+6qNaanPHn3ZZC2dXahqbLoXqn/bJzIA=; b=p1bbGTF0gpmvs4WhvUdhQv5pvP6L1WYGdsFelJlsscXIutxyH8rIty9G731uLFJMTZ GokT/T+7NsyCvxqGVktsvOrU4n0RXvhS8bOz1ry4GYWcO2noEMyYyipRNbAR37FGc1uZ WdH3VendZdMjCe3lA69cUQtnXfjhAAtzvKQyxgWOXLF+X6e5WE0PK7NOP/yZ02ycWNMF E9v0XmdFZnRv9K8+VegKe7unPGt7VGbl47ghwLD/zRVpd/oqj7fVidBd8QGke7VUajTS NRqS4rA4iQtipaxOOL+3UJmQihuyqilq7VmRQX6bB90uRZTpjL/zkTF8SKVEUW9LzMES Tq/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TEWOHyzG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d10si2689210pfn.84.2018.04.27.22.30.20; Fri, 27 Apr 2018 22:30:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TEWOHyzG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752585AbeD1F3P (ORCPT + 99 others); Sat, 28 Apr 2018 01:29:15 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:42914 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751303AbeD1F3N (ORCPT ); Sat, 28 Apr 2018 01:29:13 -0400 Received: by mail-pg0-f65.google.com with SMTP id p9-v6so56242pgc.9; Fri, 27 Apr 2018 22:29:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=EZVEWGHIgDd+6qNaanPHn3ZZC2dXahqbLoXqn/bJzIA=; b=TEWOHyzGBJ3nQDtDW2lrrm8f4nfRRTlx8oF/vu41tYZxdx6g0HtiEbVJWqFGPDRVl6 SN1KPY8+3Rgz3BtM0DuPWzqXUB8vdHwY8CMivz1gViOrnSqNvfs+A7k+r4lyFT9Mpe8M owIwNjZaAJV/V5F2FtcmVD46ympp0zLsqdCtWQMR2HxbdWRZpeiDQGBJb9g0Dh4FT3ii 15JhTnYyy5KJ+llERJTKYa7t67kbaUs0ffuM1zoNZKB77cQNxvbvoQVs4aFUfZ0Zvd47 YHQalJ+UGOvtjjKhKj6zfGL+tuGRr8ib6ypH4xc+bNP/UY/WtJM/jZW/rD42NWWCpi09 RAxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to:user-agent; bh=EZVEWGHIgDd+6qNaanPHn3ZZC2dXahqbLoXqn/bJzIA=; b=PygSPK4IhZtR0GyffI7H01rP1z+skl0zY5kYik0zvOrmzfYrtp9VPOZL168+7c5Ddh tbJlgEz3rRDXLRxIpnEFEJwzh7nLmknZjz+0Muis8XQ+xaOU2+uV88xZO6ZGU1NHHH/d 9K5L29AWbhvjks80MkdhaEjua75cJU6HcInNypArfSFOEWwjIday6WR+bogWM6ggEBCR 5QQQwAYkGe/eI/620rz0ame4/+WuFjsisFtsrZvytYw/3pQlFoaSHW47ohrXmNfwYuRq hbVzFq2EwkAQ3F0Bp0En6FPTq54jnUJEpcNhm8+TXWWPV267g0hg00uP98+3OP3pytJk yqSA== X-Gm-Message-State: ALQs6tCbP9CSFqahogfZTZN4mOD0l4PvE1eMBDCStIcRg74UOAuM4Y1t qoy8gV1gwqOEhZNgsi+ZYh5WBXiw X-Received: by 2002:a17:902:8d8b:: with SMTP id v11-v6mr4957003plo.9.1524893352595; Fri, 27 Apr 2018 22:29:12 -0700 (PDT) Received: from udknight.localhost ([183.90.36.235]) by smtp.gmail.com with ESMTPSA id e80sm6620917pfb.41.2018.04.27.22.29.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Apr 2018 22:29:11 -0700 (PDT) Received: from udknight.localhost (localhost [127.0.0.1]) by udknight.localhost (8.14.9/8.14.9) with ESMTP id w3S5THqa002594; Sat, 28 Apr 2018 13:29:18 +0800 Received: (from root@localhost) by udknight.localhost (8.14.9/8.14.9/Submit) id w3S5THfI002591; Sat, 28 Apr 2018 13:29:17 +0800 Date: Sat, 28 Apr 2018 13:29:17 +0800 From: Wang YanQing To: Daniel Borkmann Cc: Alexei Starovoitov , ast@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] bpf: fix misaligned access for BPF_PROG_TYPE_PERF_EVENT program type on x86_32 platform Message-ID: <20180428052917.GA1579@udknight> Mail-Followup-To: Wang YanQing , Daniel Borkmann , Alexei Starovoitov , ast@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org References: <20180426095749.GA29207@udknight> <20180427224854.2g7ximim7nwkgdpd@ast-mbp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.7.1 (2016-10-04) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Apr 28, 2018 at 01:33:15AM +0200, Daniel Borkmann wrote: > On 04/28/2018 12:48 AM, Alexei Starovoitov wrote: > > On Thu, Apr 26, 2018 at 05:57:49PM +0800, Wang YanQing wrote: > >> All the testcases for BPF_PROG_TYPE_PERF_EVENT program type in > >> test_verifier(kselftest) report below errors on x86_32: > >> " > >> 172/p unpriv: spill/fill of different pointers ldx FAIL > >> Unexpected error message! > >> 0: (bf) r6 = r10 > >> 1: (07) r6 += -8 > >> 2: (15) if r1 == 0x0 goto pc+3 > >> R1=ctx(id=0,off=0,imm=0) R6=fp-8,call_-1 R10=fp0,call_-1 > >> 3: (bf) r2 = r10 > >> 4: (07) r2 += -76 > >> 5: (7b) *(u64 *)(r6 +0) = r2 > >> 6: (55) if r1 != 0x0 goto pc+1 > >> R1=ctx(id=0,off=0,imm=0) R2=fp-76,call_-1 R6=fp-8,call_-1 R10=fp0,call_-1 fp-8=fp > >> 7: (7b) *(u64 *)(r6 +0) = r1 > >> 8: (79) r1 = *(u64 *)(r6 +0) > >> 9: (79) r1 = *(u64 *)(r1 +68) > >> invalid bpf_context access off=68 size=8 > >> > >> 378/p check bpf_perf_event_data->sample_period byte load permitted FAIL > >> Failed to load prog 'Permission denied'! > >> 0: (b7) r0 = 0 > >> 1: (71) r0 = *(u8 *)(r1 +68) > >> invalid bpf_context access off=68 size=1 > >> > >> 379/p check bpf_perf_event_data->sample_period half load permitted FAIL > >> Failed to load prog 'Permission denied'! > >> 0: (b7) r0 = 0 > >> 1: (69) r0 = *(u16 *)(r1 +68) > >> invalid bpf_context access off=68 size=2 > >> > >> 380/p check bpf_perf_event_data->sample_period word load permitted FAIL > >> Failed to load prog 'Permission denied'! > >> 0: (b7) r0 = 0 > >> 1: (61) r0 = *(u32 *)(r1 +68) > >> invalid bpf_context access off=68 size=4 > >> > >> 381/p check bpf_perf_event_data->sample_period dword load permitted FAIL > >> Failed to load prog 'Permission denied'! > >> 0: (b7) r0 = 0 > >> 1: (79) r0 = *(u64 *)(r1 +68) > >> invalid bpf_context access off=68 size=8 > >> " > >> > >> This patch fix it, the fix isn't only necessary for x86_32, it will fix the > >> same problem for other platforms too, if their size of bpf_user_pt_regs_t > >> can't divide exactly into 8. > >> > >> Signed-off-by: Wang YanQing > >> --- > >> Hi all! > >> After mainline accept this patch, then we need to submit a sync patch > >> to update the tools/include/uapi/linux/bpf_perf_event.h. > >> > >> Thanks. > >> > >> include/uapi/linux/bpf_perf_event.h | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/include/uapi/linux/bpf_perf_event.h b/include/uapi/linux/bpf_perf_event.h > >> index eb1b9d2..ff4c092 100644 > >> --- a/include/uapi/linux/bpf_perf_event.h > >> +++ b/include/uapi/linux/bpf_perf_event.h > >> @@ -12,7 +12,7 @@ > >> > >> struct bpf_perf_event_data { > >> bpf_user_pt_regs_t regs; > >> - __u64 sample_period; > >> + __u64 sample_period __attribute__((aligned(8))); > > > > I don't think this necessary. > > imo it's a bug in pe_prog_is_valid_access > > that should have allowed 8-byte access to 4-byte aligned sample_period. > > The access rewritten by pe_prog_convert_ctx_access anyway, > > no alignment issues as far as I can see. > > Right, good point. Wang, could you give the below a test run: > > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index 56ba0f2..95b9142 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -833,8 +833,14 @@ static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type > return false; > if (type != BPF_READ) > return false; > - if (off % size != 0) > - return false; > + if (off % size != 0) { > + if (sizeof(long) != 4) > + return false; > + if (size != 8) > + return false; > + if (off % size != 4) > + return false; > + } > > switch (off) { > case bpf_ctx_range(struct bpf_perf_event_data, sample_period): Hi all! I have tested this patch, but test_verifier reports the same errors for the five testcases. The reason is they all failed to pass the test of bpf_ctx_narrow_access_ok. Thanks.