From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7834C606C1 for ; Mon, 8 Jul 2019 16:32:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B713F21479 for ; Mon, 8 Jul 2019 16:32:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kinvolk.io header.i=@kinvolk.io header.b="SJBZLbfj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403940AbfGHQcE (ORCPT ); Mon, 8 Jul 2019 12:32:04 -0400 Received: from mail-wm1-f43.google.com ([209.85.128.43]:37392 "EHLO mail-wm1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403826AbfGHQcD (ORCPT ); Mon, 8 Jul 2019 12:32:03 -0400 Received: by mail-wm1-f43.google.com with SMTP id f17so171572wme.2 for ; Mon, 08 Jul 2019 09:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kinvolk.io; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sQGKbunRlrvdVKbp02vEGwvG8+xrq5xUVQ5fY/+t4TE=; b=SJBZLbfj73WvztsTVCyOaGzkao16f0cK4Qd3O7+93OdYSwxGMZrFSA+QNEXHIq4q52 QTqMGAMrkclA3q+GV20vV9rDe5T7Aq+vjBeSxnO4sxrsd425CRXW6cWA9wOzNeN23mBb DH4d4tYhyv4Fh9Zq0DD9xa42KhSbQp1I9es+s= 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=sQGKbunRlrvdVKbp02vEGwvG8+xrq5xUVQ5fY/+t4TE=; b=iC9RIa3l/E3pLiSW3YjCoFFvBVa1+BM1QdCiRjOexwHvA0KLLUmr1JVUe13Z4OIFdF 02aErevHa2FTVWQ3wtYXEO6BXajqgnBRPFZk3fnTae2jNXsupa0JLskTxIxvskhBXKTp MCYK+GTiG15WSs11Je+Q79zJsbKF0UxjN5QiOsHvT7lAdqi0Se3UwZok9EzYK6/f2q1s DMdMqFacbUKpPYZvkTEZpkfB4kU0ZAJL5gX6/hbgt0ZzIN566kj+uTKWLLkTckCWjVls sIVF7DyjwyXLd/OIsp0I5vNsLn9UjjG+c8hj8oVv1C5M5V6k78E5b46PUlWRsAPzxwK/ KT0g== X-Gm-Message-State: APjAAAU9l06tKnprvME5u6NKx7gi5RQlNs5PDzvzyVZs1+Rs6iKh1etQ HaTGAbmlga0pMLkk70ddKdZ71w== X-Google-Smtp-Source: APXvYqzZGSWnfDDVHTzxD8mRM5nAtI2EXCovxJ1DXmzY0w4uYyNkN/nErPCAqTn8joiAXOQfOGRdWg== X-Received: by 2002:a1c:cc0d:: with SMTP id h13mr17034505wmb.119.1562603521360; Mon, 08 Jul 2019 09:32:01 -0700 (PDT) Received: from localhost.localdomain (ip5f5aedbe.dynamic.kabel-deutschland.de. [95.90.237.190]) by smtp.gmail.com with ESMTPSA id e6sm18255086wrw.23.2019.07.08.09.32.00 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 08 Jul 2019 09:32:00 -0700 (PDT) From: Krzesimir Nowak To: linux-kernel@vger.kernel.org Cc: Alban Crequy , =?UTF-8?q?Iago=20L=C3=B3pez=20Galeiras?= , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Stanislav Fomichev , netdev@vger.kernel.org, bpf@vger.kernel.org, xdp-newbies@vger.kernel.org, Krzesimir Nowak Subject: [bpf-next v3 12/12] selftests/bpf: Test correctness of narrow 32bit read on 64bit field Date: Mon, 8 Jul 2019 18:31:21 +0200 Message-Id: <20190708163121.18477-13-krzesimir@kinvolk.io> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190708163121.18477-1-krzesimir@kinvolk.io> References: <20190708163121.18477-1-krzesimir@kinvolk.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Test the correctness of the 32bit narrow reads by reading both halves of the 64 bit field and doing a binary or on them to see if we get the original value. It succeeds as it should, but with the commit e2f7fc0ac695 ("bpf: fix undefined behavior in narrow load handling") reverted, the test fails with a following message: > $ sudo ./test_verifier > ... > #967/p 32bit loads of a 64bit field (both least and most significant words) FAIL retval -1985229329 != 0 > verification time 17 usec > stack depth 0 > processed 8 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 > ... > Summary: 1519 PASSED, 0 SKIPPED, 1 FAILED Signed-off-by: Krzesimir Nowak --- tools/testing/selftests/bpf/test_verifier.c | 19 +++++++++++++++++ .../testing/selftests/bpf/verifier/var_off.c | 21 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c index 484ea8842b06..2a20280a4a44 100644 --- a/tools/testing/selftests/bpf/test_verifier.c +++ b/tools/testing/selftests/bpf/test_verifier.c @@ -24,6 +24,7 @@ #include +#include #include #include #include @@ -343,6 +344,24 @@ static void bpf_fill_perf_event_test_run_check(struct bpf_test *self) self->fill_insns = NULL; } +static void bpf_fill_32bit_loads(struct bpf_test *self) +{ + compiletime_assert( + sizeof(struct bpf_perf_event_data) <= TEST_CTX_LEN, + "buffer for ctx is too short to fit struct bpf_perf_event_data"); + compiletime_assert( + sizeof(struct bpf_perf_event_value) <= TEST_DATA_LEN, + "buffer for data is too short to fit struct bpf_perf_event_value"); + + struct bpf_perf_event_data ctx = { + .sample_period = 0x0123456789abcdef, + }; + + memcpy(self->ctx, &ctx, sizeof(ctx)); + free(self->fill_insns); + self->fill_insns = NULL; +} + /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */ #define BPF_SK_LOOKUP(func) \ /* struct bpf_sock_tuple tuple = {} */ \ diff --git a/tools/testing/selftests/bpf/verifier/var_off.c b/tools/testing/selftests/bpf/verifier/var_off.c index 8504ac937809..3f8bee0a50ef 100644 --- a/tools/testing/selftests/bpf/verifier/var_off.c +++ b/tools/testing/selftests/bpf/verifier/var_off.c @@ -246,3 +246,24 @@ .result = ACCEPT, .prog_type = BPF_PROG_TYPE_LWT_IN, }, +{ + "32bit loads of a 64bit field (both least and most significant words)", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1, offsetof(struct bpf_perf_event_data, sample_period)), + BPF_LDX_MEM(BPF_W, BPF_REG_5, BPF_REG_1, offsetof(struct bpf_perf_event_data, sample_period) + 4), + BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct bpf_perf_event_data, sample_period)), + BPF_ALU64_IMM(BPF_LSH, BPF_REG_5, 32), + BPF_ALU64_REG(BPF_OR, BPF_REG_4, BPF_REG_5), + BPF_ALU64_REG(BPF_XOR, BPF_REG_4, BPF_REG_6), + BPF_MOV64_REG(BPF_REG_0, BPF_REG_4), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_PERF_EVENT, + .ctx = { 0, }, + .ctx_len = sizeof(struct bpf_perf_event_data), + .data = { 0, }, + .data_len = sizeof(struct bpf_perf_event_value), + .fill_helper = bpf_fill_32bit_loads, + .override_data_out_len = true, +}, -- 2.20.1