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 6E363A0547; Mon, 21 Jun 2021 15:28:47 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5A43D41199; Mon, 21 Jun 2021 15:28:47 +0200 (CEST) Received: from mail-oi1-f227.google.com (mail-oi1-f227.google.com [209.85.167.227]) by mails.dpdk.org (Postfix) with ESMTP id CC0A440040 for ; Mon, 21 Jun 2021 15:28:45 +0200 (CEST) Received: by mail-oi1-f227.google.com with SMTP id m137so19893284oig.6 for ; Mon, 21 Jun 2021 06:28:45 -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=alF1gt8BCNesvoTNZomHSxylOaMdkgYvTCWlhtaKopw=; b=Dt/Y1JERhnQUjEWt6IrMD2WoQ9yLbpcLiADaD7XDmvIxevIgcrcvBopd0hgU+0PsFU jd3R9oJZrA9mTWeZwGSli6Js3xjZ1q6kttAzo8yLsZ9EVdG02T2Xzpb7u0mXPfh2B+Z6 L1MT58uIfU27Xx2QLYbM5n9CXr2FNwziI2OVI= 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=alF1gt8BCNesvoTNZomHSxylOaMdkgYvTCWlhtaKopw=; b=Jv9KzzQxAvWEz1PGF2mSyYgY6JTXMcIm2oNLpDCvIRd/a5Fvosv3NaOx+CgaihskP8 2sXKx3axtOBTdUg5RLVKzPuLZzYUQqVVomp4Zu0vsR2my7CjckAMCFgNi05Za7bs7Bvg 5m1d4d5B+ccx++BrkPbCOLgM5qy0XcgRvX19BUWOdQ5+7gJFjmiU32Xd1iz0Bto2pAF6 VVf8KYvcC6xxd1s4wPurCIdXy4O9lbAfDlDgXxO9MXGu9GDNp70/wBjCG2FZ4XGDibk/ LnUJSU7nxMbykp8o/HGRTi1LxAQtrpFDAIyVji9wu39XYUR8i9mDtAaNZ6zl4YZVDFHn qMSg== X-Gm-Message-State: AOAM531x8DCSsbKZS+tHI7ROCKY6ecKpG8BwFv1ZGF8mALGDQWQf3YIZ CNpdBHBU3cKwS7jy9OdSoP9ZluAftP7WF1pHIUSOv/NIkyPQTiaBaOVxM9c9LSyOF7adA7FSN3R jagmToMGf6pZwu76h6JZMK9Zp/NCmaAEWTAgVC82mcJa4ZRg3hFbEuEtSG+y+swSFZLY81bJwYA == X-Google-Smtp-Source: ABdhPJyFIRVLQN5US91M9xVsiIkg8aKjVjJaCYZZj7YiAXwtA6h/PZpQCvhsV+CXNKaKI811jtCwPt93XtOp X-Received: by 2002:a05:6808:2107:: with SMTP id r7mr17435998oiw.64.1624282124910; Mon, 21 Jun 2021 06:28:44 -0700 (PDT) Received: from postal.iol.unh.edu (postal.iol.unh.edu. [132.177.123.84]) by smtp-relay.gmail.com with ESMTPS id y3sm379606oot.7.2021.06.21.06.28.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Jun 2021 06:28:44 -0700 (PDT) X-Relaying-Domain: iol.unh.edu Received: from iol.unh.edu (unknown [IPv6:2606:4100:3880:1220:73b3:2227:c156:c558]) by postal.iol.unh.edu (Postfix) with ESMTP id 4D8DC6052490; Mon, 21 Jun 2021 09:28:44 -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: Mon, 21 Jun 2021 09:28:34 -0400 Message-Id: <20210621132834.21673-1-ohilyard@iol.unh.edu> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210616181833.356159-1-ohilyard@iol.unh.edu> References: <20210616181833.356159-1-ohilyard@iol.unh.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v3] 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 | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c index f6c55ee45..96424e9c9 100644 --- a/lib/rib/rte_rib6.c +++ b/lib/rib/rte_rib6.c @@ -79,20 +79,33 @@ 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 & (UINT8_MAX - 1)) / CHAR_BIT; + + /* + * 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 == RIB6_MAXDEPTH) + return NULL; + return (get_dir(ip, node->depth)) ? node->right : node->left; } -- 2.30.2