Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp986121rwb; Sat, 13 Aug 2022 13:29:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR4QblAKYd43Xc2h2N4xWJWwClGc0lWdlMG1CH13O5lCkvwC64g9hvpFOe5oH7uQiK24YleI X-Received: by 2002:aa7:d292:0:b0:43d:7923:66cd with SMTP id w18-20020aa7d292000000b0043d792366cdmr8113550edq.403.1660422559077; Sat, 13 Aug 2022 13:29:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660422559; cv=none; d=google.com; s=arc-20160816; b=0KNSCHgTp79jSHOTz2zeVwtgJgWun3tF9RuKpqcJ4YqE9LdNzuqsPo6ZCe98/IrmtU besmNjeAfsJghR4vV+XtWPisKSoqVSYGFA1pDWEBnDDNo4gcZRqW9cQSGOevg9de81WF /zKYFFdw4myNFodeYDvhPJL7NuKlBoiboRmWUSR1hGSCrZBlzUKXeTsfHPoNakBKzpNg SSlzuWG9BiEPB1u0iGZTe9vKU6TQyU+O17vebnYByiGc7CXUS5G0ccZLn5LSHm/uyo7B nDeVkwLTf4O1LlmqTG/r/buDQViIZV0w0eGf5iHiJcmSwelZV1m5AtVpFsibNSU1y1Ct DKhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=9kjaAmWltXQmrwFotlSPSIHG4w3YsdS7FtntyWjtkzs=; b=Al5MfWqeYqD8eK9KappeijhY/RDzfbNUXbRkNm5NXy0OYY1Avu0GNc5O5Jj2GF2mIG h/8jVavhK0LHDhWpUzYit1DYkDCUdIzAsXKpT/LiIs40B6tEnlhXp2vOrXPtiDsvARbx FT8+0OJKuXC8vlio0NSTqyrTDmA/zLGauFpYO3EnDKB7vTLo3xUvGphja2YDmP3n0G4s 2U+s2xS+R7SVeNdok/GI+GGl+SMZ7DEDxgQnQzYRuqW45DVcadl/ar5oBeu/5RgmqlVD KcTJ2pgN1n6IJXvTWie3VjbJnM0b/j21yp5xpQW29ZumodVQmYrePLhBbNpKYBuIvV1w e1xg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id au16-20020a170907093000b007364913fc71si1520296ejc.384.2022.08.13.13.28.31; Sat, 13 Aug 2022 13:29:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237399AbiHMUJU (ORCPT + 99 others); Sat, 13 Aug 2022 16:09:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229787AbiHMUJT (ORCPT ); Sat, 13 Aug 2022 16:09:19 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA925140D3 for ; Sat, 13 Aug 2022 13:09:17 -0700 (PDT) Received: from localhost.localdomain (unknown [178.129.182.214]) by mail.ispras.ru (Postfix) with ESMTPSA id 707DF40737A6; Sat, 13 Aug 2022 20:09:11 +0000 (UTC) From: Rustam Subkhankulov To: Benson Leung Cc: Rustam Subkhankulov , Dmitry Torokhov , chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, Alexey Khoroshilov , ldv-project@linuxtesting.org Subject: [PATCH] platform/chrome: fix double-free in chromeos_laptop_prepare() Date: Sun, 14 Aug 2022 01:08:43 +0300 Message-Id: <20220813220843.2373004-1-subkhankulov@ispras.ru> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,RCVD_IN_SORBS_WEB, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If chromeos_laptop_prepare_i2c_peripherals() fails after allocating memory for 'cros_laptop->i2c_peripherals', this memory is freed at 'err_out' label and nonzero value is returned. Then chromeos_laptop_destroy() is called, resulting in double-free error. Found by Linux Verification Center (linuxtesting.org) with SVACE. Signed-off-by: Rustam Subkhankulov Fixes: 5020cd29d8bf ("platform/chrome: chromeos_laptop - supply properties for ACPI devices") --- drivers/platform/chrome/chromeos_laptop.c | 24 ++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/platform/chrome/chromeos_laptop.c b/drivers/platform/chrome/chromeos_laptop.c index 4e14b4d6635d..a2cdbfbaeae6 100644 --- a/drivers/platform/chrome/chromeos_laptop.c +++ b/drivers/platform/chrome/chromeos_laptop.c @@ -740,6 +740,7 @@ static int __init chromeos_laptop_prepare_i2c_peripherals(struct chromeos_laptop *cros_laptop, const struct chromeos_laptop *src) { + struct i2c_peripheral *i2c_peripherals; struct i2c_peripheral *i2c_dev; struct i2c_board_info *info; int i; @@ -748,17 +749,15 @@ chromeos_laptop_prepare_i2c_peripherals(struct chromeos_laptop *cros_laptop, if (!src->num_i2c_peripherals) return 0; - cros_laptop->i2c_peripherals = kmemdup(src->i2c_peripherals, - src->num_i2c_peripherals * - sizeof(*src->i2c_peripherals), - GFP_KERNEL); - if (!cros_laptop->i2c_peripherals) + i2c_peripherals = kmemdup(src->i2c_peripherals, + src->num_i2c_peripherals * + sizeof(*src->i2c_peripherals), + GFP_KERNEL); + if (!i2c_peripherals) return -ENOMEM; - cros_laptop->num_i2c_peripherals = src->num_i2c_peripherals; - - for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) { - i2c_dev = &cros_laptop->i2c_peripherals[i]; + for (i = 0; i < src->num_i2c_peripherals; i++) { + i2c_dev = &i2c_peripherals[i]; info = &i2c_dev->board_info; error = chromeos_laptop_setup_irq(i2c_dev); @@ -775,16 +774,19 @@ chromeos_laptop_prepare_i2c_peripherals(struct chromeos_laptop *cros_laptop, } } + cros_laptop->i2c_peripherals = i2c_peripherals; + cros_laptop->num_i2c_peripherals = src->num_i2c_peripherals; + return 0; err_out: while (--i >= 0) { - i2c_dev = &cros_laptop->i2c_peripherals[i]; + i2c_dev = &i2c_peripherals[i]; info = &i2c_dev->board_info; if (!IS_ERR_OR_NULL(info->fwnode)) fwnode_remove_software_node(info->fwnode); } - kfree(cros_laptop->i2c_peripherals); + kfree(i2c_peripherals); return error; } -- 2.34.1