Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1390770ybj; Thu, 7 May 2020 23:42:02 -0700 (PDT) X-Google-Smtp-Source: APiQypJidl1S3mDgnnaq9HrhTj4szW8/F8raJ2xBz4ena7YQZMXO9UxVrg5hpWUE9If0JFrie0wF X-Received: by 2002:aa7:c1ca:: with SMTP id d10mr951099edp.152.1588920122185; Thu, 07 May 2020 23:42:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588920122; cv=none; d=google.com; s=arc-20160816; b=swbWo/q9LIebO/5MioIuflZzeIy9FtiDomAhlezOJP+H7AcPnFmD9D0qPVDbqEOT/4 OosuU28qQnNVdpmUv670UeZQvTFHsb9XQ0mzFrRCgEDaoFLdcZ7/Ct1iOLbpxCSW0fUm 96FV/KSmlDKPQj4bupStSn26isfYITHAFWUZ8uTXaO2O7q23AyOfF5bwevaBb9lFfeN1 KreNRf89y1wedGeoVSx7xUYML/6GXlMKqlv32lCva85PLPcFgciRNFDl+unCFhebTIUA s/D3eAC1IiioPYbxld6IUg2QRZjUK7zGT4xkZ2QXwv34SsQR4rU87R94iB2KbkZYZbKw p7Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=JqKocGa5cimql1fp4DS+xPXwrVZ2Z2hJEVO/fiKh20k=; b=T64fuJEosHrIELJS3nQGwBLCc1i02E3FSvZcxXMQZ5+R0uMOZhMcuh2/J4sqxxXdwD 3lKCYrWilP3Xq84CAy5z14kK4VPH2vrLJNmXQBCCgE45sR7u2MeXrrjWrNp3vUU+UBHD +fTwOvY7O7qF60bd42YXxpBDJEBCzq6kNWluTtqwddysOhSmNt/DnZ60pQX+QTqeB2qi S+Z3Fy/ht3IiwRD3DnCSYOU2BuwU9UwSBL2/Li2XzxXbh9OtpPkYrUOu6M50Aw9p68rP vyGhoie74iPOS9n8pRxWRCUOLpqSuDbkXSWAEH+vL7hhAHLx6Dc0ZKA7FC+vhtSVOHQI 5Z1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LSasER3W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q7si448436edr.137.2020.05.07.23.41.39; Thu, 07 May 2020 23:42:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LSasER3W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727804AbgEHGkA (ORCPT + 99 others); Fri, 8 May 2020 02:40:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726873AbgEHGj7 (ORCPT ); Fri, 8 May 2020 02:39:59 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60F29C05BD09 for ; Thu, 7 May 2020 23:39:58 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z11so991063ybk.2 for ; Thu, 07 May 2020 23:39:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=JqKocGa5cimql1fp4DS+xPXwrVZ2Z2hJEVO/fiKh20k=; b=LSasER3WBqtfBg/bGE3PeVOf3C1Dim6JBLIEgeU3rioM9+MNBKorVZ4wLEoKOWeVEB WVpX2iRLeOoAaTH5FU79u0hIMkOIFUHESFKAB1oxCjCgjRvmMdaxTMqaBOwQHVR2u8SD YZHIpK0wPVJW+tg5pO++cjzhWrun5K+JyrZzWt5wXl2Y8cto4/QGzWXb3Z/TQ/PXtNRg qgUSdqGjHPbW9fg5ueq6bMEwFbpewcbkDB66XY25g0+h47IYWDzeNld91gofqSW4vQkh JGmHEsw+wh2w1youzj1UIYZYEk5FplV54amOTaCJkWNEEuH/6+HkYRwNtBmntewVUL2F WsrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=JqKocGa5cimql1fp4DS+xPXwrVZ2Z2hJEVO/fiKh20k=; b=sp89BUCDpK2wAXZoXq+ke7j9wBDiJoGyxgvNfljfcF7aIOubz2hrSYydXmqcBYm4bi qK1bj6THhqV5YTadpDGTqts/9crXrqjGlCSDQBXU0/N/YIz+NtlhXymuZ8xM1wM3p+UN kuGJH1SxjiOPR8w3IF8QwqfCTQ+mfc37js06uBV9qxqy/7pftJFJIH87ZcAROhqrLkpv O2PW3PLmvjbioU5QwoxBFKDxt1M9vDowVfBhPAKT6oSBMdn1vYlEVII0bF2G7NRyy10o gbz64VCvDPgMteCbAmTqlDg6D4G1X3dyllP21EqlnlAldnE6USVeR0tOVRr33o+65yH9 BoFQ== X-Gm-Message-State: AGi0PubIw/wDqigNWSUrNhyAGqKGePzBqn1ZmCjZG77GJpGMUhI7QWB0 6GVguQ+n+d4JaWOPI9Sdo2JtuT8QHjYj X-Received: by 2002:a25:dbd2:: with SMTP id g201mr2328607ybf.17.1588919997600; Thu, 07 May 2020 23:39:57 -0700 (PDT) Date: Thu, 7 May 2020 23:39:54 -0700 Message-Id: <20200508063954.256593-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH] libbpf hashmap: fix undefined behavior in hash_bits From: Ian Rogers To: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ian Rogers Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If bits is 0, the case when the map is empty, then the >> is the size of the register which is undefined behavior - on x86 it is the same as a shift by 0. Fix by handling the 0 case explicitly. Signed-off-by: Ian Rogers --- tools/lib/bpf/hashmap.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/lib/bpf/hashmap.h b/tools/lib/bpf/hashmap.h index d5ef212a55ba..781db653d16c 100644 --- a/tools/lib/bpf/hashmap.h +++ b/tools/lib/bpf/hashmap.h @@ -19,6 +19,8 @@ static inline size_t hash_bits(size_t h, int bits) { /* shuffle bits and return requested number of upper bits */ + if (bits == 0) + return 0; return (h * 11400714819323198485llu) >> (__WORDSIZE - bits); } -- 2.26.2.645.ge9eca65c58-goog