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 46594A0C43; Wed, 16 Jun 2021 20:18:41 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1862C40683; Wed, 16 Jun 2021 20:18:41 +0200 (CEST) Received: from mail-ua1-f97.google.com (mail-ua1-f97.google.com [209.85.222.97]) by mails.dpdk.org (Postfix) with ESMTP id 8AAC94067A for ; Wed, 16 Jun 2021 20:18:39 +0200 (CEST) Received: by mail-ua1-f97.google.com with SMTP id c17so1110862uao.1 for ; Wed, 16 Jun 2021 11:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o0KB/JZckSxzFjg+6Q/cfM+NHTuNzPFroGtQBWHW4OU=; b=hrb32zMjxAemblRDghnSHVuGc6xlZxrrTncUOadpNDgMehZT7DKiiyNeykuXdS7B+h /kM901hsQfezeix6zFrOERoO6xREaUs7AQ3u8ZNbLbHM7dd6KdGo8Zvfk7xPLpjOysBv 6f01EB9AwayRsD13odU9n8qXjJF3h+BtbkZlA= 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=o0KB/JZckSxzFjg+6Q/cfM+NHTuNzPFroGtQBWHW4OU=; b=j5zdCZN2f5FYa3jNzsbUk7rz/Bf/DzG/2mpp2dgdzZ/Y3++PP+TFV50LXOLEponG3g nB7Nq7nKvct8XmpjAZk7U6GnxQc+q8TUhuOeYBoGPdLb7OWtfx4ZEpMLVhtrfyN+ZIbQ Tm8dgcyx0c1ulvTELFSFyGcMLAyZmYx0+yNLZnKWH94cV8kuc4zwGb+e9mcVKTlxOM67 tGzQDQbrPJwEM8/5QGTsNFvKSJi/SodGsl7WX6D4W7rTzUePtU6ewMEwu4ME1cZbLSeU fvak27YY6Z7jR65HUGFpr+q1P2kEtaDj50/JmJNWd+mJai/OKlqcWErkjfqUiiOVKG7b t2EA== X-Gm-Message-State: AOAM5300sBZvinZKj4cunHuD0umDIHbcmGZQYjkFwJmXuM8ItLXLGVwI zXglIg7Vi56NjTLLW9DJdsrky4wBXKr9DziNsfQ61GjjyLXf09EUxK9UoO1CPxBLQwX3EqA6qx3 61Z13vOnMhOip3rLibtK6NWiDlyHqX6MIQswHsvuoSz1X8mZOGHm4TBipkVPV2z+xAbu4dSHjcg == X-Google-Smtp-Source: ABdhPJzox8honlAmLwqBs9F/aNVVOS1wimKmroGn+eCx2te+IpWiAdvH87mUSFktsOnKmIZr5Dsvz8VT9C7t X-Received: by 2002:ab0:4a6:: with SMTP id 35mr1091831uaw.76.1623867518862; Wed, 16 Jun 2021 11:18:38 -0700 (PDT) Received: from postal.iol.unh.edu (postal.iol.unh.edu. [132.177.123.84]) by smtp-relay.gmail.com with ESMTPS id r74sm1129792vkf.14.2021.06.16.11.18.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jun 2021 11:18:38 -0700 (PDT) X-Relaying-Domain: iol.unh.edu Received: from iol.unh.edu (unknown [IPv6:2606:4100:3880:1220:5e2f:685e:142b:54c3]) by postal.iol.unh.edu (Postfix) with ESMTP id 3360B605246B; Wed, 16 Jun 2021 14:18:38 -0400 (EDT) From: ohilyard@iol.unh.edu To: vladimir.medvedkin@intel.com Cc: dev@dpdk.org, stephen@networkplumber.org, david.marchand@redhat.com, Owen Hilyard Date: Wed, 16 Jun 2021 14:18:32 -0400 Message-Id: <20210616181833.356159-1-ohilyard@iol.unh.edu> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210616160730.348523-1-ohilyard@iol.unh.edu> References: <20210616160730.348523-1-ohilyard@iol.unh.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2] lib/rte_rib6: fix stack buffer overflow 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 Sender: "dev" From: Owen Hilyard ASAN found a stack buffer overflow in lib/rib/rte_rib6.c:get_dir. The fix for the stack buffer overflow was to make sure depth was always < 128, since when depth = 128 it caused the index into the ip address to be 16, which read off the end of the array. While trying to solve the buffer overflow, I noticed that a few changes could be made to remove the for loop entirely. Fixes: f7e861e21c ("rib: support IPv6") Signed-off-by: Owen Hilyard --- lib/rib/rte_rib6.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c index f6c55ee45..a4daf12ca 100644 --- a/lib/rib/rte_rib6.c +++ b/lib/rib/rte_rib6.c @@ -79,20 +79,31 @@ is_covered(const uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE], static inline int get_dir(const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth) { - int i = 0; - uint8_t p_depth, msk; - - for (p_depth = depth; p_depth >= 8; p_depth -= 8) - i++; - - msk = 1 << (7 - p_depth); - return (ip[i] & msk) != 0; + uint8_t index, msk; + + /* depth & 127 clamps depth to values that will not + * read off the end of ip. + * depth is the number of bits deep into ip to traverse, and + * is incremented in blocks of 8 (1 byte). This means the last + * 3 bits are irrelevant to what the index of ip should be. + */ + index = (depth & 127) >> 3; + + /* + * msk is the bitmask used to extract the bit used to decide the + * direction of the next step of the binary search. + */ + msk = 1 << (7 - (depth & 7)); + + return (ip[index] & msk) != 0; } static inline struct rte_rib6_node * get_nxt_node(struct rte_rib6_node *node, const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]) { + if (node->depth == 128) + return NULL; return (get_dir(ip, node->depth)) ? node->right : node->left; } -- 2.30.2