From patchwork Mon Jan 12 16:12:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 427797 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 26093140192 for ; Tue, 13 Jan 2015 03:15:01 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753506AbbALQMv (ORCPT ); Mon, 12 Jan 2015 11:12:51 -0500 Received: from mail-la0-f48.google.com ([209.85.215.48]:45994 "EHLO mail-la0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753451AbbALQMr (ORCPT ); Mon, 12 Jan 2015 11:12:47 -0500 Received: by mail-la0-f48.google.com with SMTP id gf13so24769222lab.7; Mon, 12 Jan 2015 08:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=G0jPtsUeO+jT2lnk9pjqCPTuUK+j+wx2HxCZfK8HRyc=; b=X1ny2f8FXKngJEyTzZdQehlBlXs0sE0nHxemlgLDUbob1GBo40ZmNHlhu02Lt/FWmD bfjO0IfFHYCFElkTwgss0NRfrEx7idmRyOWlOcWkLsDgj95FXhepsKiJA72UbVMNVOzA 23g2LcYAx9McWugCqi513LRrzfY5ApIJ3Zc7xLBYAmZQEXhwK/25fDSQPSie+pTQuCQW RS35nX/iM9nWlv3MHqoB0xfxu0JQMn3drjmD4iLNi5r1ZrDXn1rcGflHJhLvBoJ1BYvY 80fv3sMHSpfNxL8XIsdfF4iohLOTEEPCWs3avetM6ZVs70J+ZRoD5k48HQ/l598pgEiZ 3ONQ== X-Received: by 10.152.45.65 with SMTP id k1mr37904875lam.14.1421079166213; Mon, 12 Jan 2015 08:12:46 -0800 (PST) Received: from xi.terra (s83-177-171-8.cust.tele2.se. [83.177.171.8]) by mx.google.com with ESMTPSA id q17sm102029lal.2.2015.01.12.08.12.43 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Jan 2015 08:12:43 -0800 (PST) Received: from johan by xi.terra with local (Exim 4.84) (envelope-from ) id 1YAhbo-0000au-RZ; Mon, 12 Jan 2015 17:12:52 +0100 From: Johan Hovold To: Linus Walleij Cc: Alexandre Courbot , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold Subject: [PATCH 2/6] gpio: fix gpio-chip list corruption Date: Mon, 12 Jan 2015 17:12:25 +0100 Message-Id: <1421079149-2236-3-git-send-email-johan@kernel.org> X-Mailer: git-send-email 2.0.5 In-Reply-To: <1421079149-2236-1-git-send-email-johan@kernel.org> References: <1421079149-2236-1-git-send-email-johan@kernel.org> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Fix potential corruption of gpio-chip list due to failure to remove the chip from the list before returning in gpiochip_add error path. The chip could be long gone when the global list is next traversed, something which could lead to a null-pointer dereference. In the best case (chip not deallocated) we are just leaking the gpio range. Fixes: 14e85c0e69d5 ("gpio: remove gpio_descs global array") Signed-off-by: Johan Hovold --- drivers/gpio/gpiolib.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 89c59f5f1924..ac5944b4e4d8 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -248,7 +248,8 @@ int gpiochip_add(struct gpio_chip *chip) base = gpiochip_find_base(chip->ngpio); if (base < 0) { status = base; - goto unlock; + spin_unlock_irqrestore(&gpio_lock, flags); + goto err_free_descs; } chip->base = base; } @@ -288,11 +289,8 @@ int gpiochip_add(struct gpio_chip *chip) acpi_gpiochip_add(chip); status = gpiochip_export(chip); - if (status) { - acpi_gpiochip_remove(chip); - of_gpiochip_remove(chip); - goto fail; - } + if (status) + goto err_remove_chip; pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, chip->base, chip->base + chip->ngpio - 1, @@ -300,9 +298,14 @@ int gpiochip_add(struct gpio_chip *chip) return 0; -unlock: +err_remove_chip: + acpi_gpiochip_remove(chip); + of_gpiochip_remove(chip); + spin_lock_irqsave(&gpio_lock, flags); + list_del(&chip->list); spin_unlock_irqrestore(&gpio_lock, flags); fail: +err_free_descs: kfree(descs); chip->desc = NULL;