Received: by 10.192.165.148 with SMTP id m20csp2770200imm; Mon, 7 May 2018 00:23:34 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpPt/7rBt43q85evXicxBk79z62NGF63NSy+LaNMgkEJ31F98MzxSjtdw7cwnr3nFzpn+TF X-Received: by 2002:a63:a503:: with SMTP id n3-v6mr28947047pgf.19.1525677814809; Mon, 07 May 2018 00:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525677814; cv=none; d=google.com; s=arc-20160816; b=VgQiREGt8OgqOfc8U4azcJDkyEsp0nxnmIFqsxLVQUbhHEDIlBzT1a6CyxrY0K6vrH nURT3FrLybF4pR8GszyS06i6fYP5aoI0S6faciPZFVZP5W8EjppWHA3dGKkDDNHr7lIV /zZ6Moc6qcfIQPjN7OGQiRHXnSt9QcITdJPIi8LC7uC5DE5bD+NRlKBjDoM53R/Hj+o1 CDQ5ZuztjEGmh8b11lhT0nirM5nBKokS1pl34vC/LkYxK9yAN6jrqp5kjWzzxpj5be7l c1Vex16yScR/WUB9rq7mu+KoUPfREmrFDX7NslE9CtawTZR6B2N9W5Og5w+4WSMaD9Si ZO/A== 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:to:from:date:dkim-signature :arc-authentication-results; bh=6XD8VIeWTqhEF/oYfxOyQasJe9SSXIRrpIPe5RqZnSI=; b=hytvdVfMDVdQWy7T0Bi48T2S4y50+FylaHK8aniLtgs20y/pWZh1bUNBiBb9krDoB9 XysNsreJG0kmQ+MOOMo03H4a6UkLvB72MnQbjx2a4h5xYHUOYiNftMySGGBaojdCyjLd Xq2viI23BwvPuF7QBLClpWXk+kV5RGA2ll3jO1lgCr7vTZHr/Hihii8bJXNmhX0UGmXH Z8p3KEtH92+hdTJDBu+5NxguWPqN9MLg8OUQPTBLe+HkNhfeyTB2cUa91R7dAKlafAAG +qvwyyU+k6k8mlS12Pq00vksL9UtAkfx8mGwAAAvmIX1ekZFif7xIXw22nRVShADpRah LS4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OkGkpHAC; 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 t190-v6si17599067pgb.623.2018.05.07.00.23.20; Mon, 07 May 2018 00:23:34 -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=OkGkpHAC; 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 S1751975AbeEGHXF (ORCPT + 99 others); Mon, 7 May 2018 03:23:05 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:41379 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751848AbeEGHW7 (ORCPT ); Mon, 7 May 2018 03:22:59 -0400 Received: by mail-pf0-f196.google.com with SMTP id v63so22135469pfk.8; Mon, 07 May 2018 00:22:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=6XD8VIeWTqhEF/oYfxOyQasJe9SSXIRrpIPe5RqZnSI=; b=OkGkpHACghOzc/lc+4j0lwolxutm6AKYOHjbdD2RccCjjHdU+7lKUl9RAiF72vDmHi QLe4PHJ3gMhyVpMZdNASD05eX9bwTNIhd+bw4yTTF02Mw3j0ir8amf31SSaCNRqKWBy0 sgiT7e2FV03zwwe+Kju5XeYr1Dn/PMNAEN1NOE75q+f3bYWyzLbTd5PoYTZl1fWm21L6 06Tn2ChzxQabINgw5oLXLzcOpw5V6KnV0orrhOafKvJmAWGIltbZkp2AGQGWU7WYQ8nY ia1oAmvANuUIQGAyo7J99yYkWhWutVyTwKgKLKxeBuYFQYGDn4FeCtvl9byVAI7cPHHD OkMw== 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:subject:message-id:mail-followup-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=6XD8VIeWTqhEF/oYfxOyQasJe9SSXIRrpIPe5RqZnSI=; b=ojJuyLksGmvOr4LJTWqt9VpiFM02nzQFIdcK46lhjBsZsTXCZfZd8gFlAqes9Q7x4G CsypbVXfc8HoDIHvgfiEjfFeOzSWhhAQIZ5ZM5ZmcIMaH0it1sz0QIHhTZ3g5skGYwwZ XrOuP8DNZJkb4ESd84rlRmf0yWXMEErCF79y0nMT8y3lDQ6t54xTUoD7PCyayelMp17k s451aciM8Qfjbiwh18Hu7vNuCpvz8S6/J2+VzpCZ64GpGBy4pumkX97csiHjJdIAcnut kvPYi7bOAv3CoMLhqZEab7UONsQtA9evw0J6Lp7JPopzvXAIG1qeT3y+BvM7i4/abolF a2cQ== X-Gm-Message-State: ALQs6tBRq7gXS7S8Lty+nXh/phnoF9xmTusinkPzvVxHJqRiakjnBA+A dqNoxocLWB1dcllbt9/YHtg= X-Received: by 2002:a17:902:684c:: with SMTP id f12-v6mr37415404pln.139.1525677778855; Mon, 07 May 2018 00:22:58 -0700 (PDT) Received: from udknight.localhost ([183.90.36.235]) by smtp.gmail.com with ESMTPSA id n67sm45820178pfh.188.2018.05.07.00.22.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 00:22:58 -0700 (PDT) Received: from udknight.localhost (localhost [127.0.0.1]) by udknight.localhost (8.14.9/8.14.9) with ESMTP id w477NBKx011286; Mon, 7 May 2018 15:23:11 +0800 Received: (from root@localhost) by udknight.localhost (8.14.9/8.14.9/Submit) id w477N55g011284; Mon, 7 May 2018 15:23:05 +0800 Date: Mon, 7 May 2018 15:23:05 +0800 From: Wang YanQing To: Daniel Borkmann , 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: <20180507072305.GA11275@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> <20180428052917.GA1579@udknight> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180428052917.GA1579@udknight> 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:29:17PM +0800, Wang YanQing wrote: > 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. Hi! Daniel Borkmann. Do you have any plan to fix bpf_ctx_narrow_access_ok for these problems? Thanks.