From patchwork Wed Aug 13 02:30:37 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: stepanm@codeaurora.org X-Patchwork-Id: 4715871 Return-Path: X-Original-To: patchwork-linux-arm-msm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BED129F377 for ; Wed, 13 Aug 2014 02:31:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DF06C201BC for ; Wed, 13 Aug 2014 02:31:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 15AA2201BA for ; Wed, 13 Aug 2014 02:31:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752827AbaHMCap (ORCPT ); Tue, 12 Aug 2014 22:30:45 -0400 Received: from smtp.codeaurora.org ([198.145.11.231]:56976 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752465AbaHMCap (ORCPT ); Tue, 12 Aug 2014 22:30:45 -0400 Received: from smtp.codeaurora.org (localhost [127.0.0.1]) by smtp.codeaurora.org (Postfix) with ESMTP id 8B2BE13FDF0; Wed, 13 Aug 2014 02:30:44 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 486) id 7C48513FF44; Wed, 13 Aug 2014 02:30:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from stepanm-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: stepanm@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id D51F013FDF0; Wed, 13 Aug 2014 02:30:43 +0000 (UTC) From: Stepan Moskovchenko To: grant.likely@linaro.org, robh+dt@kernel.org, devicetree@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, stepanm@codeaurora.org Subject: [PATCH v2] of: Deep-copy names of platform devices Date: Tue, 12 Aug 2014 19:30:37 -0700 Message-Id: <1407897037-6037-1-git-send-email-stepanm@codeaurora.org> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <53EAC37C.4020203@codeaurora.org> References: <53EAC37C.4020203@codeaurora.org> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we parse the device tree and allocate platform devices, the 'name' of the newly-created platform_device is set to point to the 'name' field of the 'struct device' embedded within the platform_device. This is dangerous, because the name of the 'struct device' is dynamically allocated. Drivers may call dev_set_name() on the device, which will free and reallocate the name of the device, leaving the 'name' of the platform_device pointing to the now-freed memory. Furthermore, if the dev_set_name() call is made from a driver's probe() function and a subsequent request results in probe deferral, the dangling 'name' reference may lead to the device being re-probed using the wrong driver. To mitigate these scenarios, we use kstrdup to perform a deep copy of the device name when assigning the name of the platform_device, so that the platform_device name is unaffected by any calls to dev_set_name() that might made by drivers to rename the embedded 'struct device'. Signed-off-by: Stepan Moskovchenko --- * v2 - swap cleanup order drivers/of/device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation -- To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/of/device.c b/drivers/of/device.c index f685e55..e9beae6 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -54,7 +54,7 @@ int of_device_add(struct platform_device *ofdev) /* name and id have to be set so that the platform bus doesn't get * confused on matching */ - ofdev->name = dev_name(&ofdev->dev); + ofdev->name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL); ofdev->id = -1; /* device_add will assume that this device is on the same node as @@ -75,6 +75,7 @@ EXPORT_SYMBOL(of_device_register); void of_device_unregister(struct platform_device *ofdev) { + kfree(ofdev->name); device_unregister(&ofdev->dev); } EXPORT_SYMBOL(of_device_unregister);