From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id D4F3448ACD; Mon, 10 Nov 2025 16:31:49 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 73ACA4065C; Mon, 10 Nov 2025 16:31:36 +0100 (CET) Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by mails.dpdk.org (Postfix) with ESMTP id 1077540650 for ; Mon, 10 Nov 2025 16:31:35 +0100 (CET) Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTPS id 4d4tt05jzyzJ468y; Mon, 10 Nov 2025 23:31:04 +0800 (CST) Received: from frapema500003.china.huawei.com (unknown [7.182.19.114]) by mail.maildlp.com (Postfix) with ESMTPS id 4462914033F; Mon, 10 Nov 2025 23:31:34 +0800 (CST) Received: from localhost.localdomain (10.220.239.45) by frapema500003.china.huawei.com (7.182.19.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 10 Nov 2025 16:31:33 +0100 From: Marat Khalili To: Konstantin Ananyev CC: Stephen Hemminger , Subject: [PATCH 3/3] bpf: make add/subtract one program validate Date: Mon, 10 Nov 2025 15:30:46 +0000 Message-ID: <20251110153046.63518-4-marat.khalili@huawei.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251110153046.63518-1-marat.khalili@huawei.com> References: <20251110153046.63518-1-marat.khalili@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.220.239.45] X-ClientProxiedBy: frapema500002.china.huawei.com (7.182.19.148) To frapema500003.china.huawei.com (7.182.19.114) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add tests loading simple BPF programs adding or subtracting one to its argument and fix triggered signed integer overflow undefined behaviours: lib/bpf/bpf_validate.c:324:24: runtime error: signed integer overflow: 1 + 9223372036854775807 cannot be represented in type 'long int' lib/bpf/bpf_validate.c:352:24: runtime error: signed integer overflow: -9223372036854775808 - 1 cannot be represented in type 'long int' As a minimal possible fix perform operation on unsigned integers where overflow is well-defined, which was probably the original intent. Signed-off-by: Marat Khalili --- app/test/test_bpf_simple.c | 58 ++++++++++++++++++++++++++++++++++++++ lib/bpf/bpf_validate.c | 8 +++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/app/test/test_bpf_simple.c b/app/test/test_bpf_simple.c index 576a6ed029..d4c5bbdc6e 100644 --- a/app/test/test_bpf_simple.c +++ b/app/test/test_bpf_simple.c @@ -129,3 +129,61 @@ test_simple_minimal_working(void) REGISTER_FAST_TEST(bpf_simple_minimal_working_autotest, true, true, test_simple_minimal_working); + +/* + * Try and load valid BPF program adding one to the argument. + */ +static int +test_simple_add_one(void) +{ + static const struct ebpf_insn ins[] = { + { + /* Set return value to one. */ + .code = (EBPF_ALU64 | EBPF_MOV | BPF_K), + .dst_reg = EBPF_REG_0, + .imm = 1, + }, + { + /* Add program argument to the return value. */ + .code = (EBPF_ALU64 | BPF_ADD | BPF_X), + .src_reg = EBPF_REG_1, + .dst_reg = EBPF_REG_0, + }, + { + .code = (BPF_JMP | EBPF_EXIT), + }, + }; + return simple_bpf_load_test(RTE_DIM(ins), ins, 0); +} + +REGISTER_FAST_TEST(bpf_simple_add_one_autotest, true, true, + test_simple_add_one); + +/* + * Try and load valid BPF program subtracting one from the argument. + */ +static int +test_simple_subtract_one(void) +{ + static const struct ebpf_insn ins[] = { + { + /* Subtract one from the program argument. */ + .code = (EBPF_ALU64 | BPF_SUB | BPF_K), + .dst_reg = EBPF_REG_1, + .imm = 1, + }, + { + /* Set return value to the result. */ + .code = (EBPF_ALU64 | EBPF_MOV | BPF_X), + .src_reg = EBPF_REG_1, + .dst_reg = EBPF_REG_0, + }, + { + .code = (BPF_JMP | EBPF_EXIT), + }, + }; + return simple_bpf_load_test(RTE_DIM(ins), ins, 0); +} + +REGISTER_FAST_TEST(bpf_simple_subtract_one_autotest, true, true, + test_simple_subtract_one); diff --git a/lib/bpf/bpf_validate.c b/lib/bpf/bpf_validate.c index 23444b3eaa..47ad6fef0f 100644 --- a/lib/bpf/bpf_validate.c +++ b/lib/bpf/bpf_validate.c @@ -243,8 +243,8 @@ eval_add(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk) rv.u.min = (rd->u.min + rs->u.min) & msk; rv.u.max = (rd->u.max + rs->u.max) & msk; - rv.s.min = (rd->s.min + rs->s.min) & msk; - rv.s.max = (rd->s.max + rs->s.max) & msk; + rv.s.min = ((uint64_t)rd->s.min + (uint64_t)rs->s.min) & msk; + rv.s.max = ((uint64_t)rd->s.max + (uint64_t)rs->s.max) & msk; /* * if at least one of the operands is not constant, @@ -272,8 +272,8 @@ eval_sub(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk) rv.u.min = (rd->u.min - rs->u.max) & msk; rv.u.max = (rd->u.max - rs->u.min) & msk; - rv.s.min = (rd->s.min - rs->s.max) & msk; - rv.s.max = (rd->s.max - rs->s.min) & msk; + rv.s.min = ((uint64_t)rd->s.min - (uint64_t)rs->s.max) & msk; + rv.s.max = ((uint64_t)rd->s.max - (uint64_t)rs->s.min) & msk; /* * if at least one of the operands is not constant, -- 2.43.0