Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp4045896ybd; Tue, 25 Jun 2019 13:04:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqyWFH1pgBNk0dnwdGACCnME49HL21z0LcIloVlWEeVkeoI1rkXqVo90rmqOj2awbBNXTMjg X-Received: by 2002:a17:90a:a09:: with SMTP id o9mr659034pjo.95.1561493088938; Tue, 25 Jun 2019 13:04:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561493088; cv=none; d=google.com; s=arc-20160816; b=SmUOE6fVOn4uLOmXgLueHIBf8qigQOzpO78AN29WfvveJLXotRtrlL7h40jSR8PcpW dotD2OZ2TB2wAz0+IDBzwQVfxGwGduXzBFZ3bzz3NF4h7uVLed/WXNPQ4/fEeEz6Tvdh S10vowRAJdqiPVdhPDzdOt+D3UJt7R0vo1/G8c+NUThHK21yILC0hlxlUwLoGyFgixDE b0UGG9a7JSzbghJEqK654NH6hPdPeoZG+tv9pzI5Jh2UR6HQF0Kwz6Z2VeeDp+eLaERr HcVV6FxPmW907/cHWyuYtkZ5lqxPlRADu4VpxwcUsE9e4T8CGc6Tj9FYjyrFSlYUHuXo ELvg== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zL0HrEGZcpopHqyYMX9VRwzNkgb23WmEI/HU5IMtTxA=; b=NXtbK9cWyQtgts43V4sB+3VPsIIXmXMK3AG9IiddGMbkydl9lDQvP5nhGc06xbkRvg 3YTdyTReNsQOqI4sQ9z65qgjDAcVQKSLVA42mVP22HmJlf5HP0vGwWMPkawRPZp+lIBh ne2OGLdGvmKYl7rleAnPdVrJhJM6Fmk8RkZ/DU6QmCXnigXIYqbjwK4tZEw90wZXkQT7 ji2LETDCBP0yByGbkoXt/jfvZGYjO1unObi0fIRVxP9yrHuNQPxFVVaVplNwBUEyPa73 dI9DyJORIuX+1jtiHO5VR6asuUPN/Vzx8F5JJfP7veTLNkRmKJlW7CnNRJQ9Agv5D1fj A9HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kinvolk.io header.s=google header.b=BVEkDRbH; 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 n1si1000964plk.388.2019.06.25.13.04.33; Tue, 25 Jun 2019 13:04:48 -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=@kinvolk.io header.s=google header.b=BVEkDRbH; 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 S1729657AbfFYTnA (ORCPT + 99 others); Tue, 25 Jun 2019 15:43:00 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:34077 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729561AbfFYTm5 (ORCPT ); Tue, 25 Jun 2019 15:42:57 -0400 Received: by mail-wr1-f66.google.com with SMTP id k11so19257244wrl.1 for ; Tue, 25 Jun 2019 12:42:55 -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=zL0HrEGZcpopHqyYMX9VRwzNkgb23WmEI/HU5IMtTxA=; b=BVEkDRbH+o3abT8tZJB6lbPIuysqa31NjxF8psYZadQqZoC3npMhwO3pkjA47Tw4Z8 NF+zzCL8b8AmQrOt0o9xgWglRDk1vKqjauhGXgVmzpLdWVB+dJD5WSvf1PMneJGx5rTt /vwmmE0bUo6tbLaP/IZc6HPlpiVFaNCxLrjfI= 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=zL0HrEGZcpopHqyYMX9VRwzNkgb23WmEI/HU5IMtTxA=; b=J3HSinqa7JcNXiPnnfDdM4D+IhFMI9XZNQ/WQfBWSDpHKRnJ5aprAa1Lt5wwBE71EQ oSTTFJhmHuhR2pC4Ll2pj+cgCtrIqe8e9xpV1Kk0DfLjqX08cwBQ0w8mU2gRL4wYLS9M 5aIqqL27vsquU/k+2VjHX4wfIyT/clWclvEluHceLU6pjANtTC4tVOu6zl2ogNA2dfmR 2b9hwi4tlPV4nBzmQaYn370HONynQ/nZyG5Q7Ns++2Yl/IBUBCg3UZ2j8PHr9+en1Bzp 0q7fxwb/LyQV41tbD5NVcj8TVuBfeRVy1TDFqMC+NNQaxG2kbC4HWGMgTyy1bxPWlLwe xq9A== X-Gm-Message-State: APjAAAV3acchv1IVS4Q9AsGyCJBDTAW+fNP4cQklMA0AfBtpKI1Rabh0 1ze+VXXUG36F2lIx49etZa7tl0p31WnGPg== X-Received: by 2002:adf:a345:: with SMTP id d5mr36952398wrb.234.1561491775190; Tue, 25 Jun 2019 12:42:55 -0700 (PDT) Received: from localhost.localdomain (ip5f5aedb6.dynamic.kabel-deutschland.de. [95.90.237.182]) by smtp.gmail.com with ESMTPSA id q193sm84991wme.8.2019.06.25.12.42.54 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 25 Jun 2019 12:42:54 -0700 (PDT) From: Krzesimir Nowak To: netdev@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 , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Krzesimir Nowak Subject: [bpf-next v2 10/10] selftests/bpf: Test correctness of narrow 32bit read on 64bit field Date: Tue, 25 Jun 2019 21:42:15 +0200 Message-Id: <20190625194215.14927-11-krzesimir@kinvolk.io> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190625194215.14927-1-krzesimir@kinvolk.io> References: <20190625194215.14927-1-krzesimir@kinvolk.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c index 6fa962014b64..444c1ea1e326 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 @@ -341,6 +342,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..14d222f37081 100644 --- a/tools/testing/selftests/bpf/verifier/var_off.c +++ b/tools/testing/selftests/bpf/verifier/var_off.c @@ -246,3 +246,23 @@ .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, +}, -- 2.20.1