Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3231358imc; Wed, 13 Mar 2019 12:13:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3a0JDE/jiLVxBNPs4RVgiCmm9+PqhXNi6FveWIocSVRcEX5VLl9Jnz9Q91HrpnCKjn1Ux X-Received: by 2002:a17:902:be0e:: with SMTP id r14mr22606318pls.129.1552504423493; Wed, 13 Mar 2019 12:13:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552504423; cv=none; d=google.com; s=arc-20160816; b=yXUfqjtJNBMbolQPMt2onDq0Jr++b0OIj/gVDeWUKq9kTIEDfWT1qTNgRXhD8Q0kBX ANDFub68pq4dvwEqjO2njznYPdRY2+JNaGlfOQDxfXgY4dHI7HN15amQYyjSQUIbcW51 hoWsLlmrLlD40/0+EpU+8dz6tbYYZf6m7IL/2/H/HWiAM77LP2M9gaU7+iFi5JFwssUX O/Dhq1pmV2UTAfb4u5mqmxQIlEGaQfGu/19ZXNwEVoGdQM4WuhR9HdOfwwETQCii0AeF qiiCuQHY0OiZhL4GcRp1wV4qeTXU3CaTX15wCUHkN5V2R5WbIw7a23jNexk4RluLmtbO lCJg== 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=3LM798j6NHg3fjiz0H6oE/RQEViUl90Zu69p/S3WLGA=; b=izGQcsG+vQjyrSFY+n8v3wFysXjMkudGpDH8bjDzkhlQsmTGkQGwWf5Gp7h6Eld6zw //uba47f//GeVbfT3V8mDEhHz1LUZ0TddLCO1uBDfg7t9Me1sj5ipo7SWBbLS8LLfaJc hgCbrFKFkwiLO+QNfOYoG/LC+TkGHYRnryErGhytrdhyflmCMLHQicRV+6Og9VXdaUcw MXHcUEDsijXU3UVT3GRl5FywKbYiyT9WZ8ZUeH37h9NWluKE3cB4kQR4DgvhS0L1Zv0i r3iWltEj8oC7zWt8N3n8Mc5/Tx1uLk+G1vRxNxhDo9gRx0JNAcDKFZ8fqcHuoaur+1F4 1K2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GfQBuXU2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f8si10430051pgo.80.2019.03.13.12.13.27; Wed, 13 Mar 2019 12:13:43 -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=@kernel.org header.s=default header.b=GfQBuXU2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727736AbfCMTMb (ORCPT + 99 others); Wed, 13 Mar 2019 15:12:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:43034 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727177AbfCMTM2 (ORCPT ); Wed, 13 Mar 2019 15:12:28 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A99DF217F5; Wed, 13 Mar 2019 19:12:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552504347; bh=TY+zVZ/4klUEczISMX9gft6iMeeZS9G6MXrHEdH5kSg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GfQBuXU2s0v4bkYhLta0hEVWQTyEdUdkGmzFEow47GtofnpcNYYeYSB+X+6uhyzkA TINWA9I2Cq336D46wqeoPgSFloKGEkSYORYs90FOFaWLRCuKmxz1To5AUNfMNKSGp6 kEjOh+/ksEHkiH1oVD42bI81bZDLyCYkZBoSUrxc= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Alban Crequy , Craig Gallek , Daniel Borkmann , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH AUTOSEL 4.20 51/60] bpf, lpm: fix lookup bug in map_delete_elem Date: Wed, 13 Mar 2019 15:10:12 -0400 Message-Id: <20190313191021.158171-51-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190313191021.158171-1-sashal@kernel.org> References: <20190313191021.158171-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alban Crequy [ Upstream commit 7c0cdf0b3940f63d9777c3fcf250a2f83859ca54 ] trie_delete_elem() was deleting an entry even though it was not matching if the prefixlen was correct. This patch adds a check on matchlen. Reproducer: $ sudo bpftool map create /sys/fs/bpf/mylpm type lpm_trie key 8 value 1 entries 128 name mylpm flags 1 $ sudo bpftool map update pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 aa bb cc dd value hex 01 $ sudo bpftool map dump pinned /sys/fs/bpf/mylpm key: 10 00 00 00 aa bb cc dd value: 01 Found 1 element $ sudo bpftool map delete pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 ff ff ff ff $ echo $? 0 $ sudo bpftool map dump pinned /sys/fs/bpf/mylpm Found 0 elements A similar reproducer is added in the selftests. Without the patch: $ sudo ./tools/testing/selftests/bpf/test_lpm_map test_lpm_map: test_lpm_map.c:485: test_lpm_delete: Assertion `bpf_map_delete_elem(map_fd, key) == -1 && errno == ENOENT' failed. Aborted With the patch: test_lpm_map runs without errors. Fixes: e454cf595853 ("bpf: Implement map_delete_elem for BPF_MAP_TYPE_LPM_TRIE") Cc: Craig Gallek Signed-off-by: Alban Crequy Acked-by: Craig Gallek Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- kernel/bpf/lpm_trie.c | 1 + tools/testing/selftests/bpf/test_lpm_map.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index 9058317ba9de..4f3138e6ecb2 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -432,6 +432,7 @@ static int trie_delete_elem(struct bpf_map *map, void *_key) } if (!node || node->prefixlen != key->prefixlen || + node->prefixlen != matchlen || (node->flags & LPM_TREE_NODE_FLAG_IM)) { ret = -ENOENT; goto out; diff --git a/tools/testing/selftests/bpf/test_lpm_map.c b/tools/testing/selftests/bpf/test_lpm_map.c index 147e34cfceb7..02d7c871862a 100644 --- a/tools/testing/selftests/bpf/test_lpm_map.c +++ b/tools/testing/selftests/bpf/test_lpm_map.c @@ -474,6 +474,16 @@ static void test_lpm_delete(void) assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 && errno == ENOENT); + key->prefixlen = 30; // unused prefix so far + inet_pton(AF_INET, "192.255.0.0", key->data); + assert(bpf_map_delete_elem(map_fd, key) == -1 && + errno == ENOENT); + + key->prefixlen = 16; // same prefix as the root node + inet_pton(AF_INET, "192.255.0.0", key->data); + assert(bpf_map_delete_elem(map_fd, key) == -1 && + errno == ENOENT); + /* assert initial lookup */ key->prefixlen = 32; inet_pton(AF_INET, "192.168.0.1", key->data); -- 2.19.1