From patchwork Mon Mar 25 21:55:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aditya Pakki X-Patchwork-Id: 10870083 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2996F1708 for ; Mon, 25 Mar 2019 21:55:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 08D1328E71 for ; Mon, 25 Mar 2019 21:55:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F102628EBD; Mon, 25 Mar 2019 21:55:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A840928E71 for ; Mon, 25 Mar 2019 21:55:33 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7EB5921A07094; Mon, 25 Mar 2019 14:55:33 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.84.196.205; helo=mta-p5.oit.umn.edu; envelope-from=pakki001@umn.edu; receiver=linux-nvdimm@lists.01.org Received: from mta-p5.oit.umn.edu (mta-p5.oit.umn.edu [134.84.196.205]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EF29321A07094 for ; Mon, 25 Mar 2019 14:55:31 -0700 (PDT) Received: from localhost (unknown [127.0.0.1]) by mta-p5.oit.umn.edu (Postfix) with ESMTP id 57A77B2A for ; Mon, 25 Mar 2019 21:55:31 +0000 (UTC) X-Virus-Scanned: amavisd-new at umn.edu Received: from mta-p5.oit.umn.edu ([127.0.0.1]) by localhost (mta-p5.oit.umn.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4dkEjWQz7b9Q for ; Mon, 25 Mar 2019 16:55:31 -0500 (CDT) Received: from mail-it1-f199.google.com (mail-it1-f199.google.com [209.85.166.199]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mta-p5.oit.umn.edu (Postfix) with ESMTPS id F0DDEB87 for ; Mon, 25 Mar 2019 16:55:30 -0500 (CDT) Received: by mail-it1-f199.google.com with SMTP id r136so9768607ith.3 for ; Mon, 25 Mar 2019 14:55:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umn.edu; s=google; h=from:to:cc:subject:date:message-id; bh=2vvXSIdAxQjf8zb5u5UoWxHhBu1vws6yCvjtMUWtbhw=; b=jK7dofdTbzt0PVPksd4nGjIDfty/wZQw62qHgniNwG7nFRxsyNl7S1fciiQNXZrLul 15T4mi3ssFF45KgcEdJlBUIxcm07FZTaoaANhf3B93YLgSKxZTctulLY6uQpIDkgbbkd De2B/AMDNU1LDyZi+Epb7O1PrSkryO5saUZLjcTFkPZdx3kZVDyFlnk3GrHII1DtTckd yuLPWlUovw6XDi2+usr88S5tpGpH+rkP9fwqHnDK1KbfL5qL/5y0V8hczrIPPWqGtgzH OeZcWjDCuKpQuLJgPQcAq2V9yxizkVFBgw1Nkl289+MKGEEbvv3CABYXdLUIs8+t7CH+ iW4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=2vvXSIdAxQjf8zb5u5UoWxHhBu1vws6yCvjtMUWtbhw=; b=UyITZswCeF/1KN1edkcuEyTalPcnFTOsalHA2WDbPkAlh8FXjiQTuXn04eMHe/g1ut y5qteKCdOLDj1gch0gSrupgTxZ5DAF06qEc43nzuc1NfM9+J2D4n0JlbVjBq5w6je8qv Z6JWi9J+uIgXXrDlrrxzlnLH8syosOtbLL8m08skHjmL11VYrvxgqaeBvK/GULqpHRbH Ewtt9w8pvpkzERBcwvaWWRHtWtyTC1Asu1GgQICkPeya1Z57YURntltSBnMqClZv7s5H 9Bxdh1RU7b3WHU3rmbzH8686ygzhv+6wq+Im4IDKm2MQtbpOCuHKdAYMU5HggO38K1sV YAmg== X-Gm-Message-State: APjAAAVN44HsA5Rso9ffA3md9Ty+mGwpNLvI1nt/mRA/Ple/QybqnhUB Yy/7zLUxJnB66J6mUSMRGHpvby/vcJGFlcTXVv70/LlYFKCEMA/C6z/wjWsdZhIae9kqOHOnp2Y /aVjlLJ2WrHQmkcAikY7xb2mG X-Received: by 2002:a24:35c9:: with SMTP id k192mr11440806ita.156.1553550930125; Mon, 25 Mar 2019 14:55:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzPtSwjk5EA9avAfKKFxeoRVTGKKoy3sPWM8w5JrkwwHVfkRP5R4rXHSsM1s+AGpMd2avMDA== X-Received: by 2002:a24:35c9:: with SMTP id k192mr11440794ita.156.1553550929849; Mon, 25 Mar 2019 14:55:29 -0700 (PDT) Received: from cs-u-syssec1.dtc.umn.edu (cs-u-syssec1.cs.umn.edu. [128.101.106.66]) by smtp.gmail.com with ESMTPSA id g10sm7492444itc.36.2019.03.25.14.55.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 14:55:29 -0700 (PDT) From: Aditya Pakki To: pakki001@umn.edu Subject: [PATCH v4] nvdimm: btt_devs: fix a NULL pointer dereference Date: Mon, 25 Mar 2019 16:55:27 -0500 Message-Id: <20190325215527.12574-1-pakki001@umn.edu> X-Mailer: git-send-email 2.17.1 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-nvdimm@lists.01.org, kjlu@umn.edu, linux-kernel@vger.kernel.org MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference. Signed-off-by: Aditya Pakki --- v3: Move kfree(nd_btt) to goto block. v2: Replace incorrect kfree with ida_simple_remove, suggested by Johannes Thumshirn v1: Free nd_btt->id in case of failure and avoid double free, suggested by Dan Williams --- drivers/nvdimm/btt_devs.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/nvdimm/btt_devs.c b/drivers/nvdimm/btt_devs.c index b72a303176c7..9486acc08402 100644 --- a/drivers/nvdimm/btt_devs.c +++ b/drivers/nvdimm/btt_devs.c @@ -198,14 +198,15 @@ static struct device *__nd_btt_create(struct nd_region *nd_region, return NULL; nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL); - if (nd_btt->id < 0) { - kfree(nd_btt); - return NULL; - } + if (nd_btt->id < 0) + goto out_nd_btt; nd_btt->lbasize = lbasize; - if (uuid) + if (uuid) { uuid = kmemdup(uuid, 16, GFP_KERNEL); + if (!uuid) + goto out_put_id; + } nd_btt->uuid = uuid; dev = &nd_btt->dev; dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id); @@ -220,6 +221,13 @@ static struct device *__nd_btt_create(struct nd_region *nd_region, return NULL; } return dev; + +out_put_id: + ida_simple_remove(&nd_region->btt_ida, nd_btt->id); + +out_nd_btt: + kfree(nd_btt); + return NULL; } struct device *nd_btt_create(struct nd_region *nd_region)