From patchwork Sun Sep 20 05:01:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 1367778 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=fcqW9OSa; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BvFmf0HZsz9sRf for ; Sun, 20 Sep 2020 15:02:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726929AbgITFCD (ORCPT ); Sun, 20 Sep 2020 01:02:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726760AbgITFCC (ORCPT ); Sun, 20 Sep 2020 01:02:02 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5933C061755; Sat, 19 Sep 2020 22:02:02 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id q4so5443879pjh.5; Sat, 19 Sep 2020 22:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0E0b+X1jkrlTu6z8mSI8a6HrAt56K4WH6UtjHG85cYo=; b=fcqW9OSaVGejCbM4BwKgGBI6hnrFhTfw0oJtj/Fibd0cjyNYPeFLbu3PUU3C08UgW6 igmQtLTejOw+tQkiI7yQzLx8ZqhXcmddLXi1O4FBZmUjKPWyE/bbHYLsP38W4o8k7waw nwajJGJ5PtUOYopz9XCvQIDUVaHRoGpkpsIHd7P3EtYnYhmWhHubTHslhhj5RLc5dv7l LZuYLoXcw2/Na2aTarnclHSCRF05eXSX6iaYdS3Lv68L3/k/3n4/sdtdMYW+1V0KsO3U G10gRDvleW0S7anBOLvBhItFXbkr0DamhKBIw2jAEjPR6pZrcRT1BCoYKnKMpBFEkcQd mqTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0E0b+X1jkrlTu6z8mSI8a6HrAt56K4WH6UtjHG85cYo=; b=UOIPlIsdVpMQ/vm0PepHSC0araZQh2QtERCDkC+GXw5MiabdkDTh/ak4CfkMjP5oW3 hI1qCJr0TYgnfnTqUqbSELKObBMA8ixDVK7gblgESkgg5emPCK7EX8jhppMTAc/2jJQ9 FT90fw3uvJwdBrApMuet+2Pkv9kS8MDQPDXLxI0RcUfnRnr8aNrjZ7NPkSovSzl3EVHz ffcCZ2/33BSdvq5fODw6yzOwsmf9bIdWBXVPPtEwva3NwWHu3BlwieU2qN6DTeFm+3GT HkcvvGcejaNlgahY/KIJAWVDgnKbciqmkPJ/meXvFLVcohaXqf9KVz7au0NVgboVEtE+ 9IhA== X-Gm-Message-State: AOAM533AoD6CP0hbtkFZdemHHKoyvHqdsrm5OBmyqrCQgksJxCHiY3UV fHKgjeD59nK8bjfiNpqosVDs1jMSODo6Ng38 X-Google-Smtp-Source: ABdhPJySyOgkQf0gYlRuUe5x87i7ZKU+PAmGWdVK0QLgKwbERmRE8AdcSPaYf3dNBQVV4nb1p/wS2A== X-Received: by 2002:a17:90b:889:: with SMTP id bj9mr19264070pjb.73.1600578122382; Sat, 19 Sep 2020 22:02:02 -0700 (PDT) Received: from localhost.localdomain ([2001:470:e92d:10:d88d:3b4f:9cac:cf18]) by smtp.gmail.com with ESMTPSA id w19sm8432556pfq.60.2020.09.19.22.02.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Sep 2020 22:02:01 -0700 (PDT) From: Tony Ambardar X-Google-Original-From: Tony Ambardar To: Alexei Starovoitov , Daniel Borkmann Cc: Tony Ambardar , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-arch@vger.kernel.org, Arnd Bergmann Subject: [PATCH bpf v1 1/3] bpf: fix sysfs export of empty BTF section Date: Sat, 19 Sep 2020 22:01:33 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org If BTF data is missing or removed from the ELF section it is still exported via sysfs as a zero-length file: root@OpenWrt:/# ls -l /sys/kernel/btf/vmlinux -r--r--r-- 1 root root 0 Jul 18 02:59 /sys/kernel/btf/vmlinux Moreover, reads from this file succeed and leak kernel data: root@OpenWrt:/# hexdump -C /sys/kernel/btf/vmlinux|head -10 000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 000cc0 00 00 00 00 00 00 00 00 00 00 00 00 80 83 b0 80 |................| 000cd0 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000ce0 00 00 00 00 00 00 00 00 00 00 00 00 57 ac 6e 9d |............W.n.| 000cf0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 002650 00 00 00 00 00 00 00 10 00 00 00 01 00 00 00 01 |................| 002660 80 82 9a c4 80 85 97 80 81 a9 51 68 00 00 00 02 |..........Qh....| 002670 80 25 44 dc 80 85 97 80 81 a9 50 24 81 ab c4 60 |.%D.......P$...`| This situation was first observed with kernel 5.4.x, cross-compiled for a MIPS target system. Fix by adding a sanity-check for export of zero-length data sections. Fixes: 341dfcf8d78e ("btf: expose BTF info through sysfs") Signed-off-by: Tony Ambardar Acked-by: John Fastabend Acked-by: Andrii Nakryiko --- kernel/bpf/sysfs_btf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c index 3b495773de5a..11b3380887fa 100644 --- a/kernel/bpf/sysfs_btf.c +++ b/kernel/bpf/sysfs_btf.c @@ -30,15 +30,15 @@ static struct kobject *btf_kobj; static int __init btf_vmlinux_init(void) { - if (!__start_BTF) + bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF; + + if (!__start_BTF || bin_attr_btf_vmlinux.size == 0) return 0; btf_kobj = kobject_create_and_add("btf", kernel_kobj); if (!btf_kobj) return -ENOMEM; - bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF; - return sysfs_create_bin_file(btf_kobj, &bin_attr_btf_vmlinux); } From patchwork Sun Sep 20 05:01:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 1367779 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=RtxpiOKS; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BvFmf4R3nz9sRR for ; Sun, 20 Sep 2020 15:02:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726944AbgITFCJ (ORCPT ); Sun, 20 Sep 2020 01:02:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726760AbgITFCI (ORCPT ); Sun, 20 Sep 2020 01:02:08 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64946C061755; Sat, 19 Sep 2020 22:02:08 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id b17so5219723pji.1; Sat, 19 Sep 2020 22:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VVuA7I40ahfUSFUhF/rh76ojr9Knrl+A3vaZY0vD+mA=; b=RtxpiOKSLr1NSy3ffy2+RruORNC2xkcNG3fz5n9HxaLj0Mb5ih7x7TRziM1EYyPuPZ yp1pNewgL99sSf5PDa4dNpIjnheAYLpa8F9xRnYmYyCU2efLXSTZpCqX74mxjkOeCXnZ HQCdSWp/Am3SK5EoIA1LlEoVK7ofClZp4NgofTRYy52JlLTE6534vX+hi5WH45Lc4QBs Hqfezf9TzgX0GIskuG72ouM08jtsw2UsydZz3mXrcQwT3kN/IB6CKWIm+k6LDuz2XMUQ O1GYJa9vZTK/gEp4QHl/PUIj7UCpWY2vNlR+x9t8wqOlxo6utGKyqRMu++KGsq2iiymn or/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VVuA7I40ahfUSFUhF/rh76ojr9Knrl+A3vaZY0vD+mA=; b=FNyx5UmJlG1yIStSjF+ddgilmlOibtveNbtETn70ZhK42o5DPE3qqsINYT39HPTM4b rD2xrYaLVolkA42D/yKqRj6/NmR2eFCLg9WHV2RiTfyzIYy8dXFiAOa3tWUnPWInwXZi cTUxHsByxH7d2uZ43VQKR/a8Tc3OlfIHnBGVHtnPqoT1QAIEPISCn+N0jSjzctwseMky +VcXe2AqqdSKUFz5zKzusru+2aZ53qnhjhwR9VqZTGzXiV5KXNV2r+w7nbufqI20Qv25 rmQzCKlvF3OY6uRvd6H96G5WPQ86hQyLLjpPAXqnEVneB7yvGmFaEGt1tR65VdGIF49j jyqg== X-Gm-Message-State: AOAM532/MXJqX7JiEvWNIz3WMn+MyBk15TGL7DyJEbrByd2193BkQuBU l9IEBZUhgTEJ99AXbXlNhBA= X-Google-Smtp-Source: ABdhPJw5qgPMIAPvD2E0wCa/1tGvhtBbZnlZsoCPVIAeOynYj3mgr10coUuwlKAJsvf7M3syWO2i3w== X-Received: by 2002:a17:90a:ab8f:: with SMTP id n15mr19257434pjq.139.1600578128006; Sat, 19 Sep 2020 22:02:08 -0700 (PDT) Received: from localhost.localdomain ([2001:470:e92d:10:d88d:3b4f:9cac:cf18]) by smtp.gmail.com with ESMTPSA id w19sm8432556pfq.60.2020.09.19.22.02.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Sep 2020 22:02:07 -0700 (PDT) From: Tony Ambardar X-Google-Original-From: Tony Ambardar To: Alexei Starovoitov , Daniel Borkmann Cc: Tony Ambardar , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-arch@vger.kernel.org, Arnd Bergmann Subject: [PATCH bpf v1 2/3] bpf: prevent .BTF section elimination Date: Sat, 19 Sep 2020 22:01:34 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Systems with memory or disk constraints often reduce the kernel footprint by configuring LD_DEAD_CODE_DATA_ELIMINATION. However, this can result in removal of any BTF information. Use the KEEP() macro to preserve the BTF data as done with other important sections, while still allowing for smaller kernels. Fixes: 90ceddcb4950 ("bpf: Support llvm-objcopy for vmlinux BTF") Signed-off-by: Tony Ambardar Acked-by: John Fastabend --- include/asm-generic/vmlinux.lds.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 5430febd34be..7636bc71c71f 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -661,7 +661,7 @@ #define BTF \ .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \ __start_BTF = .; \ - *(.BTF) \ + KEEP(*(.BTF)) \ __stop_BTF = .; \ } \ . = ALIGN(4); \ From patchwork Sun Sep 20 05:01:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 1367780 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=pwu62dsu; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BvFmp3RG0z9sRR for ; Sun, 20 Sep 2020 15:02:18 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726953AbgITFCQ (ORCPT ); Sun, 20 Sep 2020 01:02:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726760AbgITFCP (ORCPT ); Sun, 20 Sep 2020 01:02:15 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42F1DC061755; Sat, 19 Sep 2020 22:02:15 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id u9so5182889plk.4; Sat, 19 Sep 2020 22:02:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zf+F5l4BOBKHcTchW4Xrun/P792neDZIw8SXQYChyx8=; b=pwu62dsujY21zRrqVdCN4xjg1zMPVfJ6bpTeI2jGKrUyaJP9XUf/V73AqJmZHpxXn8 mbVW/xNIY4LRtJ9sizRS9aQhvukpMAIz4DGeK1aRK2mWyyxaPDGUMalxL351AIQTzOD7 Nwz+gUgt7h1JJgxOymrrzJLypTz13T0bBJ47O3F/FNNoYweray4pnTqbMy8J2xlbzl0S +bDbh1YNIMdGv4dcffnhn/KDboLQ0pk7F0E7eJ8T8eAtRWlKbkC9cT5wb/WfBFC3lbR4 WRpR/PQiDU5XjrCBed1T4iRYBeFBAuvYjOy+jAxxHqOgZ2BJ1bL6jO8zXj4eXyyVnbcW 0ijw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zf+F5l4BOBKHcTchW4Xrun/P792neDZIw8SXQYChyx8=; b=HB0IYxxNObpOx96mR/IU9KuvWmLPWJY+w6oEG7eTKMwDCT5ahKHYdMc5b+UqiPNnzJ uLr1mgLyD5E/SIWSb/geYtWwVpJVt8b02J7Co8pCWsog214Dob2Fchuf/OCnn7AowWdt uTc9IIwYEHb9Tm/bQWYbwBsJYX8BAp7GMfD9x4WFV3oARcYCUxC1EA3eR9k5OyXR/5mr LreD2tDHhiGE924zNGXh3pA1rCKx00uNhkLXZBcBwNA+WvO9e/h5I+g8C5IoiM7frnP7 ihq2H3zMfBSNqTuStQfKGSWIEbG2n79OUNQXZAwavdzPAIDLQOkJM5fnM0tA3Kb3tdDs TW3g== X-Gm-Message-State: AOAM530hXzYuCUe8yqWtjPvPtd9/yLslYZF6lrPILdPLgN2Kxs8UsOuX jt16DXIEUEEfZBbIhG7ugoU= X-Google-Smtp-Source: ABdhPJyKvHtlZDtNBQWDrwzNX2viJkG/PDEGBhae2HyhmHWRadjwR2S9VQW+lzDLIQmvA2yAVwdPRw== X-Received: by 2002:a17:90a:fb52:: with SMTP id iq18mr19562285pjb.207.1600578134799; Sat, 19 Sep 2020 22:02:14 -0700 (PDT) Received: from localhost.localdomain ([2001:470:e92d:10:d88d:3b4f:9cac:cf18]) by smtp.gmail.com with ESMTPSA id w19sm8432556pfq.60.2020.09.19.22.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Sep 2020 22:02:14 -0700 (PDT) From: Tony Ambardar X-Google-Original-From: Tony Ambardar To: Alexei Starovoitov , Daniel Borkmann Cc: Tony Ambardar , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-arch@vger.kernel.org, Arnd Bergmann Subject: [PATCH bpf v1 3/3] libbpf: fix native endian assumption when parsing BTF Date: Sat, 19 Sep 2020 22:01:35 -0700 Message-Id: <90f81508ecc57bc0da318e0fe0f45cfe49b17ea7.1600417359.git.Tony.Ambardar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Code in btf__parse_raw() fails to detect raw BTF of non-native endianness and assumes it must be ELF data, which then fails to parse as ELF and yields a misleading error message: root:/# bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: failed to get EHDR from /sys/kernel/btf/vmlinux For example, this could occur after cross-compiling a BTF-enabled kernel for a target with non-native endianness, which is currently unsupported. Check for correct endianness and emit a clearer error message: root:/# bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: non-native BTF endianness is not supported Fixes: 94a1fedd63ed ("libbpf: Add btf__parse_raw() and generic btf__parse() APIs") Signed-off-by: Tony Ambardar Acked-by: John Fastabend --- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 7dfca7016aaa..6bdbc389b493 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -659,6 +659,12 @@ struct btf *btf__parse_raw(const char *path) err = -EIO; goto err_out; } + if (magic == __bswap_16(BTF_MAGIC)) { + /* non-native endian raw BTF */ + pr_warn("non-native BTF endianness is not supported\n"); + err = -LIBBPF_ERRNO__ENDIAN; + goto err_out; + } if (magic != BTF_MAGIC) { /* definitely not a raw BTF */ err = -EPROTO;