Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2642383lqp; Mon, 25 Mar 2024 05:28:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWbOPzyhHZuLZsN7Tyf6jUcBpThimzjke6QoX1sCvzWcxrOnEcyb2+9uzElSQOCK+YxWO5XUeAOXTyRQhjQT5+3g0ggGZ303OFY/cYUWw== X-Google-Smtp-Source: AGHT+IH7WlLAB3bsP0upgg4W3EIlaeJSPSVrFnH2cidYq8XWP0xMih0B2BEfTB+Sux4vjjheEe77 X-Received: by 2002:a05:6a20:3d06:b0:1a3:c4c2:b016 with SMTP id y6-20020a056a203d0600b001a3c4c2b016mr4294641pzi.45.1711369728033; Mon, 25 Mar 2024 05:28:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711369728; cv=pass; d=google.com; s=arc-20160816; b=uhx6IHMUJ5m774f4JXiCplggPRBCMUaf+JpfoDQRNGrclApNE6x8bs8adWbZa2n198 k9YHXlY8gUGju2t/WJLwHRXMGdiSBVpFu/rI6EvP5fzUVhlpl3AFyHLt2275xebP3TAX GLELT0L/6Ivsy6nbRoSmnc6EZX/INcagKNleN8dqPSP2XarNVrgVtmqydi9jw1JfoKEy QvSZB6vWeU49CFxqnJEKEMtF0P8YAyLXo1Oga1zs5WWcRQcWxWIuzsf/TbTbEiv6lAIy EZQAzjBZDYifrzYgXnV/LFtIGDacB3w1ZVulqhG7twACoQI9GtxYn+LVINwRHaufvaqv f6fQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=SahR+55arwo2zEueH60H3JIMo4h/q7rxXpM8msaAKow=; fh=+eOY+onAjaYOZ1gH3klb7JwlG3e8ho6mbpaSD5D7dUA=; b=leu7pIIZmz8YRp3PQH5VkB7sj8Mb9fcE4SE3FaXKuILNDsB93810rRgmgSIjCyU50F IvI6btQu18S4BMDpriE+FORXmHdAdDVsYgQUB4MLV4BasvpMAOmuqRdphzxMS8lvw314 CUqGd5QcqlVDExPaAx1i0qmITeIrl8K18JrNIFWVft9CM2e9dtuiZMy5B0rNAdb7gtaS cmOErqK83heICz6rBLlUK4gz3FvBJYIQ+UZjrOn7micbRVqC2Zv7q7p7T+rzhM1jou4Y 6nUYaxgydyU3zl0nj6bszsfjDVYrpyy3yvP0S3orZgjGoUWOgC+hY8saUC8T4DXb8uSC rYoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ECprwjQg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116153-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id p23-20020a62ab17000000b006e73209df4bsi4958348pff.72.2024.03.25.05.28.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 05:28:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ECprwjQg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116153-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D15B5B2E2BC for ; Mon, 25 Mar 2024 10:16:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 700863B2678; Mon, 25 Mar 2024 03:32:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ECprwjQg" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1952417B4E4; Sun, 24 Mar 2024 23:41:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323717; cv=none; b=VUOOiH8cPBKGDlIfQtw31bRHDtJ5ScAr8AN4QjMnTldZZbE4DDd7QbGo9nUszt9DgStN1QL4dPsth39P/qsVjyIWqFih8N1bBhoWzOoXG4DiWTBoD952mSX5R3BlH0op/LLaPwrY3b5YWOk0oPmwLnH7/RJ0R/yQJKAb4UudDiY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323717; c=relaxed/simple; bh=UMvKY0wk5C7zCqIQwVCdsZgWyc39Rvq8mbRFlU1ZrN8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BdbILkoS6Mkx0yaNGuhrQK4yS9qH2UXZ4zCyO1jatHYTlF1XJ1nGrX5M0Y9cJwu7TDaYEuUsd8Y9dF/Vl1//5wl/rDR9RW3gPIvuOj0jO8KFZLTY6Cd8qzykmivH487jdTZMBEstbnpw5guongTFjltAEVnGF6XGD7jGvpifyrU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ECprwjQg; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 408F6C43399; Sun, 24 Mar 2024 23:41:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323717; bh=UMvKY0wk5C7zCqIQwVCdsZgWyc39Rvq8mbRFlU1ZrN8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ECprwjQgfmnO/dv51l6VoFOEQyGgE7OA9lA5ybAfaKwOJCathq3HW6odfxcBll/9O +RCO4EKpxSWbRDPA04K2XzVvHPb9W/C1HpjGWOMZRitGYgJGscFXV3UvPPAxMQjCy2 S1sCo57vviTGE2zxCqltA3O4zFVYesTZLMoEAU8gDp6otLWokV45u1vI48jLQ0Mcv7 bSZDfxLlkztEFBwTRDed9zEbeY39oNwXcKY2XBtEZvrvtXwL6DB8B7uW9v90rKUR8F C3wpNuYUiyv6UrVaow2LtfsCmlqGQhP0nPiXBk/fB12NUI6yj+jMC3wvAQaevc98/E G8cf102Dt4EpQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , syzbot+8cd36f6b65f3cafd400a@syzkaller.appspotmail.com, Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.10 090/238] bpf: Fix DEVMAP_HASH overflow check on 32-bit arches Date: Sun, 24 Mar 2024 19:37:58 -0400 Message-ID: <20240324234027.1354210-91-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324234027.1354210-1-sashal@kernel.org> References: <20240324234027.1354210-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Toke Høiland-Jørgensen [ Upstream commit 281d464a34f540de166cee74b723e97ac2515ec3 ] The devmap code allocates a number hash buckets equal to the next power of two of the max_entries value provided when creating the map. When rounding up to the next power of two, the 32-bit variable storing the number of buckets can overflow, and the code checks for overflow by checking if the truncated 32-bit value is equal to 0. However, on 32-bit arches the rounding up itself can overflow mid-way through, because it ends up doing a left-shift of 32 bits on an unsigned long value. If the size of an unsigned long is four bytes, this is undefined behaviour, so there is no guarantee that we'll end up with a nice and tidy 0-value at the end. Syzbot managed to turn this into a crash on arm32 by creating a DEVMAP_HASH with max_entries > 0x80000000 and then trying to update it. Fix this by moving the overflow check to before the rounding up operation. Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index") Link: https://lore.kernel.org/r/000000000000ed666a0611af6818@google.com Reported-and-tested-by: syzbot+8cd36f6b65f3cafd400a@syzkaller.appspotmail.com Signed-off-by: Toke Høiland-Jørgensen Message-ID: <20240307120340.99577-2-toke@redhat.com> Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/devmap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c index ca2cade2871b8..07b5edb2c70f5 100644 --- a/kernel/bpf/devmap.c +++ b/kernel/bpf/devmap.c @@ -129,13 +129,14 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) bpf_map_init_from_attr(&dtab->map, attr); if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) { - dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries); - - if (!dtab->n_buckets) /* Overflow check */ + /* hash table size must be power of 2; roundup_pow_of_two() can + * overflow into UB on 32-bit arches, so check that first + */ + if (dtab->map.max_entries > 1UL << 31) return -EINVAL; - } - if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) { + dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries); + dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets, dtab->map.numa_node); if (!dtab->dev_index_head) -- 2.43.0