Received: by 2002:a05:7412:8598:b0:f9:33c2:5753 with SMTP id n24csp508969rdh; Tue, 19 Dec 2023 05:52:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJdnv+iH/htX7uI9Fszbaxo4RZ1hO99QLQvMU8PQv1s1IbSO5ypY8AOq252pcWstttM4ks X-Received: by 2002:a05:622a:16:b0:425:a7e3:f55d with SMTP id x22-20020a05622a001600b00425a7e3f55dmr25519031qtw.3.1702993924502; Tue, 19 Dec 2023 05:52:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702993924; cv=none; d=google.com; s=arc-20160816; b=ouU6GnBsFr5GajouxRHZwoDBy989SouJWuZ4L+k9UO1LzSbCuF76qe9FlDOhR1O4WO EX1Rrig6V4ezXPKuu1UU7k2icSdviL0cfZmtC4ZU5yjimb/QdzF2kFEm/EIiw1w/Fyxk VWM+drc5GN22uXTAf7VXI0NdLao0Lh3QR9X1BZqt9+HcuMUzlpg428d5lkWvZDTMGvQ1 IWMZmNCm0d3ISx5xkZJXQvrZlctHaUmn5pMcXIPLVxdKwd05R7ZN5V3/YgQsprJ6XZJD r7OHLNmYred+G7hs+FPkjqrYd4Tv3lbgH9NSwZEZnHgHFi1YUiIPu0feMn0YnIk5G3L9 byOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=WYz67IsFZfue1NP3JExLN1mOy5YJ/ePcLatZPz8TkS8=; fh=ee8NkWBU80C8M/eAg0DZ2jcsykhEHHQG1vcxPr+vQmg=; b=YivKRGQeW84KSIQgaTXqeB75eRyenOqE/beGWgfkxs0dEo31VEuMgt1fETD27s4m1K ddJ/TDVqBKgTgmtKzO2MlOBwpq8pwrN+kzOVlywKs5HH/D1EMQyjHbhxlXLROfTe10NZ GwTeT0Oe2I2b5s7XSR8phuaOIocoQ2p4HFe1AIgoataSX7+eMMdgunq4rYOBehkwaQfk LROzZRYUPTtqi/oA5ibjjFqZyFfPtTY3YDsAH8dCOOPD/4SgPCfJqlx/cxT+GBKVOswV WGK4RmeTEvVeyXV0MVsC5KnWM69doJqVMOn9MSzMUwz0XsCj/zjqTbX2jmDBCXbEBZX3 bMqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=J23keRt2; spf=pass (google.com: domain of linux-kernel+bounces-5285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5285-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id cb5-20020a05622a1f8500b0042548782f0bsi28084692qtb.522.2023.12.19.05.52.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 05:52:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=J23keRt2; spf=pass (google.com: domain of linux-kernel+bounces-5285-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5285-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3613A1C23E80 for ; Tue, 19 Dec 2023 13:52:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4442D1C2AE; Tue, 19 Dec 2023 13:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J23keRt2" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36D731C2B2; Tue, 19 Dec 2023 13:50:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f196.google.com with SMTP id d2e1a72fcca58-6d93d15db24so248426b3a.0; Tue, 19 Dec 2023 05:50:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702993856; x=1703598656; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WYz67IsFZfue1NP3JExLN1mOy5YJ/ePcLatZPz8TkS8=; b=J23keRt2Kn+EGTs2WIr+QcttBiZqKx1gltE9odV+0frRXL4OlM12MMA6HRg1irXE+H ElopFSa0lGx1NHRnVFYOdvhJxUWe9iMIbzu2s4AKxkUr40qt04GfHD/sxi2hN22tI8m+ 8eb95I9hxdmqr+Pc192CLxR1XN5F/T/i8uav64cilhLG2sT1DLR2LxzLWarUQl661t7k +M9AtfDmvS0k51mxTCtY9fwNdPMGmBJDYfmTk3EB7gDiv8xhu8fP++Cjkm69n6/tS6kZ JhgFQkrMDw+joI1zFpuyKIGJvrlnwVwC+6cV/SKysw9o/SD7vl4eqF83xX5PUf166SU+ J8hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702993856; x=1703598656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WYz67IsFZfue1NP3JExLN1mOy5YJ/ePcLatZPz8TkS8=; b=dyBN7TNRYKgwRIsTKId1B08YPc5mCpNEaqq8AJuMsOWoa83g73j/hX1tz1hy0OwKPY csS4VAtShnafN8FswS9BWwvZjj6pUo0RdiVG1fpK5+x08c4hopgxbtye5O8gUE2mQN6M c3tkRMvIEyKodnNcy4T98aLCznMQ48Jt/h02+XRvBqTgxUlZNeQPKAXzeVJT0VE4Pbks 5YuGcQthFpIBkeSBwgr8D+NvJVZIlIwaWmUT3SdFN97o3Q78OHIsDBONw2uUnDCXmqO4 q6xvqF7Yloa0M5B7tBVahOmTWwPGlcwNfs7WTewf1hjaFJGwi3CWDbCPIHlYiQCcIPxx Uttw== X-Gm-Message-State: AOJu0Yz4kEDpACoTUlixQml5CjHgUyTBhnA3uil5j/YRDHA4+qZ/MU0x KkhJUDuwTohGGgpWbEC1VaI= X-Received: by 2002:a05:6a20:42a6:b0:18f:97c:8a1c with SMTP id o38-20020a056a2042a600b0018f097c8a1cmr25153568pzj.71.1702993856543; Tue, 19 Dec 2023 05:50:56 -0800 (PST) Received: from localhost.localdomain ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d17-20020a056a00245100b006cddecbf432sm20695264pfj.96.2023.12.19.05.50.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 05:50:56 -0800 (PST) From: Menglong Dong To: andrii@kernel.org, eddyz87@gmail.com, yonghong.song@linux.dev, alexei.starovoitov@gmail.com Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, song@kernel.org, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, shuah@kernel.org, menglong8.dong@gmail.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v5 4/4] selftests/bpf: add testcase to verifier_bounds.c for BPF_JNE Date: Tue, 19 Dec 2023 21:48:00 +0800 Message-Id: <20231219134800.1550388-5-menglong8.dong@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231219134800.1550388-1-menglong8.dong@gmail.com> References: <20231219134800.1550388-1-menglong8.dong@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add testcase for the logic that the verifier tracks the BPF_JNE for regs. The assembly function "reg_not_equal_const()" and "reg_equal_const" that we add is exactly converted from the following case: u32 a = bpf_get_prandom_u32(); u64 b = 0; a %= 8; /* the "a > 0" here will be optimized to "a != 0" */ if (a > 0) { /* now the range of a should be [1, 7] */ bpf_skb_store_bytes(skb, 0, &b, a, 0); } Signed-off-by: Menglong Dong --- v5: - add some comments to the function that we add - add reg_not_equal_const() --- .../selftests/bpf/progs/verifier_bounds.c | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_bounds.c b/tools/testing/selftests/bpf/progs/verifier_bounds.c index ec430b71730b..960998f16306 100644 --- a/tools/testing/selftests/bpf/progs/verifier_bounds.c +++ b/tools/testing/selftests/bpf/progs/verifier_bounds.c @@ -1075,4 +1075,66 @@ l0_%=: r0 = 0; \ : __clobber_all); } +SEC("tc") +__description("bounds check with JMP_NE for reg edge") +__success __retval(0) +__naked void reg_not_equal_const(void) +{ + asm volatile (" \ + r6 = r1; \ + r1 = 0; \ + *(u64*)(r10 - 8) = r1; \ + call %[bpf_get_prandom_u32]; \ + r4 = r0; \ + r4 &= 7; \ + if r4 != 0 goto l0_%=; \ + r0 = 0; \ + exit; \ +l0_%=: r1 = r6; \ + r2 = 0; \ + r3 = r10; \ + r3 += -8; \ + r5 = 0; \ + /* The 4th argument of bpf_skb_store_bytes is defined as \ + * ARG_CONST_SIZE, so 0 is not allowed. The 'r4 != 0' \ + * is providing us this exclusion of zero from initial \ + * [0, 7] range. \ + */ \ + call %[bpf_skb_store_bytes]; \ + r0 = 0; \ + exit; \ +" : + : __imm(bpf_get_prandom_u32), + __imm(bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("bounds check with JMP_EQ for reg edge") +__success __retval(0) +__naked void reg_equal_const(void) +{ + asm volatile (" \ + r6 = r1; \ + r1 = 0; \ + *(u64*)(r10 - 8) = r1; \ + call %[bpf_get_prandom_u32]; \ + r4 = r0; \ + r4 &= 7; \ + if r4 == 0 goto l0_%=; \ + r1 = r6; \ + r2 = 0; \ + r3 = r10; \ + r3 += -8; \ + r5 = 0; \ + /* Just the same as what we do in reg_not_equal_const() */ \ + call %[bpf_skb_store_bytes]; \ +l0_%=: r0 = 0; \ + exit; \ +" : + : __imm(bpf_get_prandom_u32), + __imm(bpf_skb_store_bytes) + : __clobber_all); +} + char _license[] SEC("license") = "GPL"; -- 2.39.2