From patchwork Tue Mar 14 22:58:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Zary X-Patchwork-Id: 69893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2035171wrd; Tue, 14 Mar 2023 16:22:17 -0700 (PDT) X-Google-Smtp-Source: AK7set/f9B8IeNJbIKF0xmi7n71tfgl8kzBW0DbPNWvkOwYKad1ykF/S4pSJHV4YMsU/4qGHdVJH X-Received: by 2002:a17:903:22c6:b0:19b:64bb:d546 with SMTP id y6-20020a17090322c600b0019b64bbd546mr556900plg.18.1678836137517; Tue, 14 Mar 2023 16:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678836137; cv=none; d=google.com; s=arc-20160816; b=OW4JJL3sjyTV+I/bCIKSsERUKTTI+Q9Yl8XwRh4Yzy8w2F/k1ylR3cQbsE+/JPamhf zG/1a0SiN+yFwI0ZUZB+sm22T5V+JVO+6negVGir+I0X/pK+OF7EWCWu7iwmnrDirv5H V2LyrjSdsfiQscSimjPJlWzE18jeK994QJgVTC9YzvqsU4EpE3TaW+0J0Oq1yVdglVkU ctMq+Fm7gtT2MBgG4L9u8IEJX+3zOZu6+4V8Ep+ineJj0p6s4q35WnU7GvO9JmvP4Sej S2CE6MKt72hMRn9vUkIGokRaFmABMAlkhNtHhLUtuMcL+Tc97BZEUJb5uOh+O5NZysMb 7aog== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OIipJzqABGEqArTa+CpDeRqd9G1GK/sxigWKDHfq5PE=; b=lvvi334niraAaCcREAEZqS0uMmSIFROlWYzZJ7HPkZfKBlg4qHInJyjYU//gW0U/kz MktacH7hAzhxzW1JR8gXK9CP2fe/78ulURtBvaLr5nJSTBJUOxmHaf1eorQITBSX9tDI dklhuoY3ALBB06309nZ//6BqP1E+9pkMdj64ykBgrqWD3MUMBU0OizlQ9jivCiXBv046 7AW+3NLBLtKjOGJ0duRItMA0WIbO7fbaiDs0YIjc0vcyV76mV5za+AxjJIA1xeoZJqho 1hsmEC/cBwz3vFvO/uju6m6TiqMf9tJ0+5KV5e/LPiqLBgOcsSyPOVSPkBEVeAjn5PJY GKow== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg22-20020a17090326d600b0019e3728f17csi3545128plb.209.2023.03.14.16.22.00; Tue, 14 Mar 2023 16:22:17 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229624AbjCNW7S (ORCPT + 99 others); Tue, 14 Mar 2023 18:59:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229545AbjCNW7Q (ORCPT ); Tue, 14 Mar 2023 18:59:16 -0400 Received: from hosting.gsystem.sk (hosting.gsystem.sk [212.5.213.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7EC0551CB4; Tue, 14 Mar 2023 15:58:52 -0700 (PDT) Received: from gsql.ggedos.sk (off-20.infotel.telecom.sk [212.5.213.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by hosting.gsystem.sk (Postfix) with ESMTPSA id E6A2A7A0081; Tue, 14 Mar 2023 23:58:11 +0100 (CET) From: Ondrej Zary To: Damien Le Moal Cc: Christoph Hellwig , Sergey Shtylyov , linux-ide@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] pata_parport: fix memory leaks Date: Tue, 14 Mar 2023 23:58:05 +0100 Message-Id: <20230314225805.9124-1-linux@zary.sk> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760387281789810464?= X-GMAIL-MSGID: =?utf-8?q?1760387281789810464?= When ida_alloc() fails, "pi" is not freed although the misleading comment says otherwise. Move the ida_alloc() call up so we really don't have to free "pi" in case of ida_alloc() failure. Also move ida_free() call from pi_remove_one() to pata_parport_dev_release(). It was dereferencing already freed dev pointer. Testing revealed leak even in non-failure case which was tracked down to missing put_device() call after bus_find_device_by_name(). As a result, pata_parport_dev_release() was never called. Reported-by: kernel test robot Reported-by: Dan Carpenter Link: https://lore.kernel.org/r/202303111822.IHNchbkp-lkp@intel.com/ Signed-off-by: Ondrej Zary --- drivers/ata/pata_parport/pata_parport.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/drivers/ata/pata_parport/pata_parport.c b/drivers/ata/pata_parport/pata_parport.c index 6165ee9aa7da..dc77b4c6fcef 100644 --- a/drivers/ata/pata_parport/pata_parport.c +++ b/drivers/ata/pata_parport/pata_parport.c @@ -452,6 +452,7 @@ static void pata_parport_dev_release(struct device *dev) { struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev); + ida_free(&pata_parport_bus_dev_ids, dev->id); kfree(pi); } @@ -503,23 +504,27 @@ static struct pi_adapter *pi_init_one(struct parport *parport, if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev)) return NULL; + id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL); + if (id < 0) + return NULL; + pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL); - if (!pi) + if (!pi) { + ida_free(&pata_parport_bus_dev_ids, id); return NULL; + } /* set up pi->dev before pi_probe_unit() so it can use dev_printk() */ pi->dev.parent = &pata_parport_bus; pi->dev.bus = &pata_parport_bus_type; pi->dev.driver = &pr->driver; pi->dev.release = pata_parport_dev_release; - id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL); - if (id < 0) - return NULL; /* pata_parport_dev_release will do kfree(pi) */ pi->dev.id = id; dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id); if (device_register(&pi->dev)) { put_device(&pi->dev); - goto out_ida_free; + /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */ + return NULL; } pi->proto = pr; @@ -534,8 +539,7 @@ static struct pi_adapter *pi_init_one(struct parport *parport, pi->port = parport->base; par_cb.private = pi; - pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, - pi->dev.id); + pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, id); if (!pi->pardev) goto out_module_put; @@ -570,8 +574,7 @@ static struct pi_adapter *pi_init_one(struct parport *parport, module_put(pi->proto->owner); out_unreg_dev: device_unregister(&pi->dev); -out_ida_free: - ida_free(&pata_parport_bus_dev_ids, pi->dev.id); + /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */ return NULL; } @@ -696,8 +699,7 @@ static void pi_remove_one(struct device *dev) pi_disconnect(pi); pi_release(pi); device_unregister(dev); - ida_free(&pata_parport_bus_dev_ids, dev->id); - /* pata_parport_dev_release will do kfree(pi) */ + /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */ } static ssize_t delete_device_store(struct bus_type *bus, const char *buf, @@ -713,6 +715,7 @@ static ssize_t delete_device_store(struct bus_type *bus, const char *buf, } pi_remove_one(dev); + put_device(dev); mutex_unlock(&pi_mutex); return count;