Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp1889309rdb; Sun, 4 Feb 2024 05:49:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzmOklRDEGn8BdueEodlLNDSwz5UflPbOFrncu7n5xHdLWhSNQ4Mnmc9Q+cIcKmNvdy67R X-Received: by 2002:a25:ae48:0:b0:dc2:82b0:1686 with SMTP id g8-20020a25ae48000000b00dc282b01686mr6123413ybe.33.1707054561805; Sun, 04 Feb 2024 05:49:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707054561; cv=pass; d=google.com; s=arc-20160816; b=d+q/JwBzIcEcUirg2wlZdoGSaupCmT239ZFiH+zkP90AJImoFDAtm3QvdO40VHnqvS slZNh/fe5zkSgHTsa7J6sjvoc+AENmuuC2I5IATjFRGA6zgs1vg9lCxuhxKD6af0p2EZ J1ON+Ck8yhZpZ8bV/Ysh1X8+VazXjUbA1oCcHoeG4ak5bGVvC7Jwsh4hKqzdBiRa2Tl6 WQBVnN8mFSwVg2Rn6wL/auV2BfKytpZa1jqo5x4inrWBfhM2aLpYlT0wHGDpSnY3Wqzf ONzlGHtEaj1zVB2w25ZExZQjOM0iLuAOnGJty6/J/Wcz+Av8xCkfVpVuSjOiyIj3tJ+E TPig== 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:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter; bh=TRbuuNKODNr0ZF9osYOg1TOt2Lw0Gxq1svVXRYqAlxE=; fh=xcHnRWzJDmJP6+ghV9IksZW1xHSw+Ttkqjv/ubVik1Q=; b=BCPwaK7k2W8DqV1JXDyJb5CIqIuFLwxvq+SBhdGdu4jP3jRImEim9528HWKtFdYKSB bveswnG1ILCepxqmIxfsnKrxSvrqRYH2Cyh6Q7lmhYCtWrUuS1plzDajhMMsnj/zLsL+ sHy1ZgArx+M6wsFUiKXNWOBUfhQYkCzH+REGRtCN8sRmEvVk/70iwC6S9Pde/ab8d1Cc Nvpd+UL1Py7C+rGpoGYa96Kiu0UNmi3ZaxSBoQKzUff8/w+noDgeuwRRnNy+3ol6JakE gtKAnkNACf3o+LHPDV3X9KueTzXMLa1118TIPWjSoJsFa27/YVy5iWN/R/s8pXOyy2eI 0ZNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=MH57cDeK; arc=pass (i=1 spf=pass spfdomain=ispras.ru dkim=pass dkdomain=ispras.ru dmarc=pass fromdomain=ispras.ru); spf=pass (google.com: domain of linux-kernel+bounces-51680-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51680-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru X-Forwarded-Encrypted: i=1; AJvYcCW0+nhdwOBa1Z4kTb8EPi65w+/CUJv7trl77AYc8akDoFVccATdYVM7q8wbXk35BXyAYsaFtRTAhJiUDHSwrJ2QaD7eMRqAZ78PyQAtcA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id n12-20020a056214008c00b0068c87856bbfsi5524289qvr.357.2024.02.04.05.49.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 05:49:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-51680-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=MH57cDeK; arc=pass (i=1 spf=pass spfdomain=ispras.ru dkim=pass dkdomain=ispras.ru dmarc=pass fromdomain=ispras.ru); spf=pass (google.com: domain of linux-kernel+bounces-51680-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51680-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8D1691C20E8A for ; Sun, 4 Feb 2024 13:49:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8969225A9; Sun, 4 Feb 2024 13:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b="MH57cDeK" Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41089224D5; Sun, 4 Feb 2024 13:49:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.149.199.84 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707054552; cv=none; b=ox8qRW0sU9aR+0v8s1S+R7U559YpLhexqPuk3dY4EpcjwE5Ca2PHQwuJwV1cABKc923LjwSH7bOSRPQaUJUZH6XkWU7U+lfqNSqh4tjui02Rf/4K+3cBJhh3qhzxQ1VPSNM+J3Oa9bCEeFen7B6cOlo04+g8FU1X5RaxztOhfiM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707054552; c=relaxed/simple; bh=PBAj8pYVayUNKv2kDcL/faNAsbTdiZimmpd85dfB2ZI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=MPyXq2lO+4CcYCL0dKC5PTBWq/VKrZ/J26pcRyXiz2VtBVr2H1kB6ZWvQqGSBkjogdZwTDKUl5TQ4KBGrshjJyZWWZMolc0af7ZLWC+czco4ien8t/pc5yQrHTPyNvpyGujX4qZ3GzvGqiqxZDMw4yxzJv297BbhSz+jtfO3WLg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru; spf=pass smtp.mailfrom=ispras.ru; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b=MH57cDeK; arc=none smtp.client-ip=83.149.199.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ispras.ru Received: from fpc.intra.ispras.ru (unknown [10.10.165.4]) by mail.ispras.ru (Postfix) with ESMTPSA id 5DE6C40F1DC7; Sun, 4 Feb 2024 13:49:06 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 5DE6C40F1DC7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1707054546; bh=TRbuuNKODNr0ZF9osYOg1TOt2Lw0Gxq1svVXRYqAlxE=; h=From:To:Cc:Subject:Date:From; b=MH57cDeKZyKtqOiF4JcYFV5LKdyE1Xj8W3l2U6ibIoBbwLR0r8StuM88Vg/CGKo60 I5Rrb/lnpkvbvnwOvlnpm54OApiu7w4CHwWr8Tk7nC7jtS5hyWPTjmqUrlgMEyJr1g 2/tPQDc+0doFPjNAgd3QYbKFXuVruhkEpKshEZX4= From: Fedor Pchelkin To: Peter Zijlstra , Ingo Molnar Cc: Fedor Pchelkin , Arnaldo Carvalho de Melo , x86@kernel.org, Alexander Antonov , Kan Liang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org Subject: [PATCH] perf/x86/uncore: avoid null-ptr-deref on error in pmu_alloc_topology Date: Sun, 4 Feb 2024 16:48:41 +0300 Message-Id: <20240204134841.80003-1-pchelkin@ispras.ru> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit If topology[die] array allocation fails then topology[die][idx] elements can't be accessed on error path. Checking this on the error path probably looks more readable than decrementing the counter in the allocation loop. Found by Linux Verification Center (linuxtesting.org). Fixes: 4d13be8ab5d4 ("perf/x86/intel/uncore: Generalize IIO topology support") Signed-off-by: Fedor Pchelkin --- arch/x86/events/intel/uncore_snbep.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/x86/events/intel/uncore_snbep.c b/arch/x86/events/intel/uncore_snbep.c index a96496bef678..7d4deb9126be 100644 --- a/arch/x86/events/intel/uncore_snbep.c +++ b/arch/x86/events/intel/uncore_snbep.c @@ -3831,9 +3831,11 @@ static int pmu_alloc_topology(struct intel_uncore_type *type, int topology_type) return 0; clear: for (; die >= 0; die--) { - for (idx = 0; idx < type->num_boxes; idx++) - kfree(topology[die][idx].untyped); - kfree(topology[die]); + if (topology[die]) { + for (idx = 0; idx < type->num_boxes; idx++) + kfree(topology[die][idx].untyped); + kfree(topology[die]); + } } kfree(topology); err: -- 2.39.2