From patchwork Tue Apr 28 18:25:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 226818 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 230D2C83004 for ; Tue, 28 Apr 2020 18:49:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EC5E820730 for ; Tue, 28 Apr 2020 18:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588099769; bh=lmDtm9jdllLum61esOiwhBFGzn+l1a9Y0Ph5OnavGXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=0lczGYqoqVeto5SHs7FAPQVgHj5ho0ErgLTBaWmOpJFhCtu0miw7U7BS1vzDQiYuJ FTiiuxu8emqu5C03vPiVXpfc54mmRxXFpjrhSHxt9PPxjgnMv/b83nnCT4sj9XWhcf o7ZWJwNB/xMAyqN3Tnb0d9o7UXP0GgsvcV3qQzpI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730759AbgD1SkM (ORCPT ); Tue, 28 Apr 2020 14:40:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:59206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730892AbgD1SkK (ORCPT ); Tue, 28 Apr 2020 14:40:10 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6CE4F20730; Tue, 28 Apr 2020 18:40:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588099209; bh=lmDtm9jdllLum61esOiwhBFGzn+l1a9Y0Ph5OnavGXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UwihRN2u+xoxtB1v1XokDXEwT68s+HAJP/lSYPgHscNTNF3X2++NPmNrfmRd7y395 b6m+qthKOd60sq2gDOydpcrxVXoz2f0VnOgWZvnXAXMfc8OnLc0TsAluFcLgRq0FJP BDcA2yXJccZn86FUUrLGNgZVAgPp+Sc2HfZJi/DQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiyu Yang , Xin Tan , Ian Abbott Subject: [PATCH 4.19 114/131] staging: comedi: Fix comedi_device refcnt leak in comedi_open Date: Tue, 28 Apr 2020 20:25:26 +0200 Message-Id: <20200428182239.580994000@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182224.822179290@linuxfoundation.org> References: <20200428182224.822179290@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Xiyu Yang commit 332e0e17ad49e084b7db670ef43b5eb59abd9e34 upstream. comedi_open() invokes comedi_dev_get_from_minor(), which returns a reference of the COMEDI device to "dev" with increased refcount. When comedi_open() returns, "dev" becomes invalid, so the refcount should be decreased to keep refcount balanced. The reference counting issue happens in one exception handling path of comedi_open(). When "cfp" allocation is failed, the refcnt increased by comedi_dev_get_from_minor() is not decreased, causing a refcnt leak. Fix this issue by calling comedi_dev_put() on this error path when "cfp" allocation is failed. Fixes: 20f083c07565 ("staging: comedi: prepare support for per-file read and write subdevices") Signed-off-by: Xiyu Yang Cc: stable Signed-off-by: Xin Tan Signed-off-by: Ian Abbott Link: https://lore.kernel.org/r/1587361459-83622-1-git-send-email-xiyuyang19@fudan.edu.cn Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/staging/comedi/comedi_fops.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) --- a/drivers/staging/comedi/comedi_fops.c +++ b/drivers/staging/comedi/comedi_fops.c @@ -2594,8 +2594,10 @@ static int comedi_open(struct inode *ino } cfp = kzalloc(sizeof(*cfp), GFP_KERNEL); - if (!cfp) + if (!cfp) { + comedi_dev_put(dev); return -ENOMEM; + } cfp->dev = dev;