Received: by 2002:ac0:a679:0:0:0:0:0 with SMTP id p54csp1520409imp; Fri, 22 Feb 2019 05:20:22 -0800 (PST) X-Google-Smtp-Source: AHgI3IZBPntaFrba5/g/aK67jrRe+KOVWIBZWzE40+q/VlSHMHZSuYQwAmBA9c2LjWf0YAiyX1Ax X-Received: by 2002:a62:6282:: with SMTP id w124mr4221656pfb.168.1550841622433; Fri, 22 Feb 2019 05:20:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550841622; cv=none; d=google.com; s=arc-20160816; b=voektiwWAc5n6bXQn+x0/bcv4jEcjxrKmovKX2tdJZ2hJk4VBD0E7YadLI5c+n6rXo kNz2cqmLZJIZ5IebOEWEydNolrCEfMBTKHkBQVC8O67wqCP1macpd1uMt8hwd/FwBMb9 EJtbqDIRci/QyyPYmngF95aAQta9Bfk3mbhaaMv9keWw+/EhNXASujQRk2u+Berb7tAm jPUVyURSlR2hyCjv7Uu9jNjm3YzBkThXWcEOkiqvA3A8a0innoTM7NhiSWLgMX4HnHwu NePLWrevth73OmWBSVzIR/d1dqqALd4UgeLaNb6DhLOeIOMNw12Nu/d0jK4f3y+qaHpI pWOQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=aHqTCPXdvug5lA59A68yzeo0AQkFEugXyQy7aWtvks0=; b=A8AEHe7Ss+/Z5kKDvngN+HJwoqCu/H0RclGVEUgC2SyvDXsdQDuCfc7grTDF7d8cZx VhXiYCkGhiWlkBhNT75l8WsgUo9oggop/A+E1fvIrq2tFOpxPaCQe2QYyRYYDvESIxQZ 1Y+harecw8n7PEfIneo2fM4HD0V0EjhamOSPpOFPKaY7wINDsTsc9BDfyyeos5aMu9ya lJrkUF+21wq7q35jgXJACYehZ4tElGKY/tmbIJ41y4EIlikN/cKZjIXCoCYymuv+DCK9 siHKW34Fjzx+CE1HYH0YgqMTC2Ydn24uyhPqAURVEVbyZF5WZuCkULL6vbv2ic2oNONY w5vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=IEH3hhEY; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t11si1297049pgv.251.2019.02.22.05.20.06; Fri, 22 Feb 2019 05:20:22 -0800 (PST) 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=fail header.i=@gmail.com header.s=20161025 header.b=IEH3hhEY; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726712AbfBVNTn (ORCPT + 99 others); Fri, 22 Feb 2019 08:19:43 -0500 Received: from mail-ed1-f65.google.com ([209.85.208.65]:37037 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725978AbfBVNTm (ORCPT ); Fri, 22 Feb 2019 08:19:42 -0500 Received: by mail-ed1-f65.google.com with SMTP id m12so1740668edv.4; Fri, 22 Feb 2019 05:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aHqTCPXdvug5lA59A68yzeo0AQkFEugXyQy7aWtvks0=; b=IEH3hhEYyjEB7PY0hJHit2gyESnYXrjJU4Bk+m9lM7UTCh2RjSuyruZUZCB0RP01JJ z4E2VpApEDiw4xicWzu8i6Nn9hu/gVJz2zdUpLDVYBjTUFnUZGWZkRZbjEplsVmRU2Ay Uz6BGClA5QesR7d/ppQHtXmALXMPLPM65yz+HubvvjOmehk9wfG9DVaSTrc4JnMrdw+1 wq+kRDu/n9J9/+BP/J3i/dUjhxkM7GdBk1kmexXUi7ohBm/7L3kzR50jFDkTOrsThLWX S6fcGazJhh08asQEIBCX/eRy7y/38umDB5vYwUfmegleQqguVaHFQy+RbKfR+0wqK5Ye kyhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=aHqTCPXdvug5lA59A68yzeo0AQkFEugXyQy7aWtvks0=; b=BlI8NWgzfS73vIC2NYDE5s2b/HLXgEIhAOwqHQwoUMeaS2TgsHu8USeslTDFOd8uEQ u5nyESmRt4WVQNWnk0F7LgZBJOwXr9fgU0LH6+sDyrugmLVozOaLkErAK35QfrCOAWen 1ZFHsheHB11FPx0GCaRGpjGbxdsj4LoDXlrQwvfxatcOTeY/ODe1KVelbiKM/3zv/sA5 D+GrmBISPNqMKyvxVolypoDgnvFJvWQtmzc7eN976CsMRloztSs2rCeAO/8LIETdG8cw EPiIvm0O3R/VsaY2MwHgVn7AG3LQocvxRTCL7e1zRodbYbncFa3U9I8yzSS8IskXBfc0 X5kw== X-Gm-Message-State: AHQUAuaYGl0WTX90mgPB65wH9TphYraPdCcJcMfirOPE8M7icshuG2pT gJGEI2NOpKw1DB9ivg5yiHE= X-Received: by 2002:a05:6402:158e:: with SMTP id c14mr3259322edv.218.1550841580272; Fri, 22 Feb 2019 05:19:40 -0800 (PST) Received: from neptune.fritz.box ([178.19.216.175]) by smtp.gmail.com with ESMTPSA id i24sm403288edq.0.2019.02.22.05.19.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Feb 2019 05:19:39 -0800 (PST) From: Alban Crequy X-Google-Original-From: Alban Crequy To: ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, alban@kinvolk.io, iago@kinvolk.io Subject: [PATCH bpf v2] bpf, lpm: fix lookup bug in map_delete_elem Date: Fri, 22 Feb 2019 14:19:08 +0100 Message-Id: <20190222131908.6207-1-alban@kinvolk.io> X-Mailer: git-send-email 2.20.1 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 From: Alban Crequy 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 --- Changes v1 to v2: - add selftest (review from Martin) - update commitmsg tags (review from Martin) - rebase on "bpf" tree and test again (review from Martin) --- 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 abf1002080df..93a5cbbde421 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -471,6 +471,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.20.1