Received: by 10.213.65.68 with SMTP id h4csp541699imn; Wed, 4 Apr 2018 03:03:14 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+uDn8fSp3xhdt29qqiGbxCT2fzZA8RQJGfS2278uYoEMw8X1WyIaLB/5xYbuFgqOtPghkt X-Received: by 10.99.130.199 with SMTP id w190mr11627965pgd.15.1522836194025; Wed, 04 Apr 2018 03:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522836193; cv=none; d=google.com; s=arc-20160816; b=oGj7+gwWh9b2nIOL9H74HP/LKaTAxadUeA9qxBWyKLCVJNAhKj7dampTLkFDJMgTRz XRRPL5u7o4OhZifR48n4hjEBemYdRdiNkBb7xgfYKjcbqKFAHsW/WJ4INT8BJsNJ519P ZGeVpXay1XGEaKoPciy2nOb7TqaXx2qc21fQyesjStY6K+HFWijPZkiz5Sy6C3012aQp qgHMBu6k2kPcM+f8bMuMQMvE3rOEaniQSz6EBGKIqHJn7qI6C6VAhNg53hBkzJJwwO2D 6Llpt99FItC5tGpPs7prkg2f0UaI7zXP0sdxDgbGigy5zpdsLzAeqfsY4yAN2uWQn/am EpDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=Dsf6aVHmmrHd6VBLmxLjrk1XKRTL+LBpACLBgvFdu1g=; b=r7s73EHgZCnzCHVLMVryFRDXPn5rfyK9LQCqL7Dg6g7YcJlrdL2+qPHn5bgro3WI8r nd28T5dVHar7K63PuSr0fXWJpPSutBSqi/7zvC8YAnGCdcFN0D9ekCVSLjlZkQ55Lmie KGhlmmBv/ho9Vkd/+5nj9nKczISIKHiw/eplMOKDhBbeY+WlhVeu7DJn5FDvwsJjVWO6 wZIzPIqpLOPnZEc+aJnSBwlhYQ2NyvkHB2wWl4r5RsLIdZl0VDwtGUeYLi6eDtG92jXk +h54fPimQDd3Nqoa4JEQlKuLXB4N+g5Xm+YxtuwOxhzCEFxtJIDUh76T63VdzzUryjAd JTgQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a3si2060074pgv.522.2018.04.04.03.02.59; Wed, 04 Apr 2018 03:03:13 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751295AbeDDKBu (ORCPT + 99 others); Wed, 4 Apr 2018 06:01:50 -0400 Received: from www62.your-server.de ([213.133.104.62]:50012 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750908AbeDDKBs (ORCPT ); Wed, 4 Apr 2018 06:01:48 -0400 Received: from [85.7.107.67] (helo=linux.home) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-SHA:256) (Exim 4.85_2) (envelope-from ) id 1f3fEg-0006E1-Dn; Wed, 04 Apr 2018 12:01:46 +0200 Subject: Re: PROBLEM: Using BPF_PROG_TEST_RUN with data_out != NULL is unsafe To: Lorenz Bauer , ast@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org References: From: Daniel Borkmann Message-ID: Date: Wed, 4 Apr 2018 12:01:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.99.3/24448/Wed Apr 4 06:20:23 2018) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/04/2018 11:04 AM, Lorenz Bauer wrote: > Hello, > > I’ve encountered an issue when using BPF_PROG_TEST_RUN and capturing the output. > The kernel copies data into user space without checking the length of > the destination buffer. > > In bpf_test_finish(), size is the amount of data in the XDP buffer / > skb after the program is run. This can be larger than data_size_in due > to bpf_xdp_adjust_head() and friends. > bpf_test_finish doesn’t clamp size to data_size_out, which is what I > was expecting. > > What is the correct way to use this interface? Yeah, so XDP has a headroom of XDP_PACKET_HEADROOM + NET_IP_ALIGN which in case of x86 is 256 bytes (NET_IP_ALIGN being 0 there). This means that attr.test.data_out buffer needs to be 256 bytes larger than attr.test.data_in in case the BPF prog calls bpf_xdp_adjust_head() or bpf_xdp_adjust_meta(). In case you point data_in and data_out to the same address, then the total buffer size therefore has to be attr.test.data_size_in + 256 in order to not overrun anything while not being aware of the BPF test program. The XDP_PACKET_HEADROOM is exposed to user space in linux/bpf.h.