Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3922853pxb; Mon, 4 Oct 2021 12:44:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0MG5Dxh67zquOEd4YfbLFZHWiIeZ+RtXSbT2XlQmiWOH7Gc9v178VrUP3KYIyZiyvoSrQ X-Received: by 2002:a50:9d48:: with SMTP id j8mr20722437edk.165.1633376647331; Mon, 04 Oct 2021 12:44:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633376647; cv=none; d=google.com; s=arc-20160816; b=DA4qnEqswg4+LllU0dQUVQkyzgpCMZN6vmoUiv3HzHZ6d/XYCG0UWLuzYjYY1Y2mKi AgkytWbSDN/mPkdatYDN6NEy94+2qTtOOPEH/6py5ItzZ64di9RBPbr26xrWs+CoqbAa VEDc5E8wX75DcjLmA5Ntwewzo/43q3GPCXVXNJ70DiZHV6D3KpDc892Sg/EZ4+tsyuM6 d1uzKFleeBpI+VHD4lI5Sd/PuZVl6D2rG+cX/mgR3iUreMHGNURhWRoIedt2B7vVk+nl 8Gu5bwMYEh/Zt/qr6qhXzQuQAwllNcDozfYe0rFtdBsCzaLamYLiQVYCi0ieYSMm+ju2 rpuQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6+bmCHoNSZdiNh8LRhuTUuGdMLUaiS5C8pLIwYULRAM=; b=hyC9N7/55TXeoBOYbBBIWGHasg28VsgmrosE3XBLE9oLGJ1Ji8trv2mBsCfsyVB6HF kOci7/QViTjXj56W2JlRSYCh00RW29HgLxOV57ZWrXvrb9hGJZUsFpAcst1a4GMwBnWO KxvVKpDbEeQ8kP4Q3I3+vgoauHv56x6m7CD4CoGKarPlJc9YUrEsxLlandMqKrUBHdxS WDFP+00E3VOnynplZEjUfktz7J4uowxNZqWMU5pKLGloaMFCFzn4/3RuO40NtxD82G7k gcFt0UzVfjEAlxceirsi8+N9xM7ZWnh9DXvR/AFehj2JchQmoCW3zzagzZrR6ijaKhhC 67Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QgjBKJoL; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 13si20212774ejf.489.2021.10.04.12.43.43; Mon, 04 Oct 2021 12:44:07 -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=@linuxfoundation.org header.s=korg header.b=QgjBKJoL; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234452AbhJDNC4 (ORCPT + 99 others); Mon, 4 Oct 2021 09:02:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:32900 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234434AbhJDNBX (ORCPT ); Mon, 4 Oct 2021 09:01:23 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 45A9C619E5; Mon, 4 Oct 2021 12:58:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633352314; bh=Rs3Vcciiab+UuXssMClhbU/oEVDVBzV1cge3WXf6soM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QgjBKJoLMtirPvn37bRDpDd33fOwPWI7REPKGT3Kgpt5cVu/kif/oKX10iPB+P4xb idBDZpmPAkh4E0SYrxplS99azrTP99WchBnYahWlHIQLW/Dw524bYMZK949Cx3UMy9 KpMgRcr6KBbAfg+jQknxUEt9xAvo1CODA9pVo+Cc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Johannes Thumshirn Subject: [PATCH 4.14 14/75] mcb: fix error handling in mcb_alloc_bus() Date: Mon, 4 Oct 2021 14:51:49 +0200 Message-Id: <20211004125032.012617483@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125031.530773667@linuxfoundation.org> References: <20211004125031.530773667@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Carpenter commit 25a1433216489de4abc889910f744e952cb6dbae upstream. There are two bugs: 1) If ida_simple_get() fails then this code calls put_device(carrier) but we haven't yet called get_device(carrier) and probably that leads to a use after free. 2) After device_initialize() then we need to use put_device() to release the bus. This will free the internal resources tied to the device and call mcb_free_bus() which will free the rest. Fixes: 5d9e2ab9fea4 ("mcb: Implement bus->dev.release callback") Fixes: 18d288198099 ("mcb: Correctly initialize the bus's device") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter Signed-off-by: Johannes Thumshirn Link: https://lore.kernel.org/r/32e160cf6864ce77f9d62948338e24db9fd8ead9.1630931319.git.johannes.thumshirn@wdc.com Signed-off-by: Greg Kroah-Hartman --- drivers/mcb/mcb-core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- a/drivers/mcb/mcb-core.c +++ b/drivers/mcb/mcb-core.c @@ -280,8 +280,8 @@ struct mcb_bus *mcb_alloc_bus(struct dev bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL); if (bus_nr < 0) { - rc = bus_nr; - goto err_free; + kfree(bus); + return ERR_PTR(bus_nr); } bus->bus_nr = bus_nr; @@ -296,12 +296,12 @@ struct mcb_bus *mcb_alloc_bus(struct dev dev_set_name(&bus->dev, "mcb:%d", bus_nr); rc = device_add(&bus->dev); if (rc) - goto err_free; + goto err_put; return bus; -err_free: - put_device(carrier); - kfree(bus); + +err_put: + put_device(&bus->dev); return ERR_PTR(rc); } EXPORT_SYMBOL_GPL(mcb_alloc_bus);