Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3534290pxb; Mon, 4 Apr 2022 20:15:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDYp6Wgtxmo5izh37RiB+1ocniPWtVXk4/Uo70shXOMddFGErXU+A2JePsTFqG/RbM7E+9 X-Received: by 2002:a05:6a00:140c:b0:4e1:530c:edc0 with SMTP id l12-20020a056a00140c00b004e1530cedc0mr1453362pfu.18.1649128537327; Mon, 04 Apr 2022 20:15:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649128537; cv=none; d=google.com; s=arc-20160816; b=djdlnWZbdgvb1DMgACeG/ZqhwDkh3Vv9U841HX9VmxDaF+vkmMViGAV8m6LtgeZ3yN XNlO77RWvfCl6wgPAxsxxQ1rLrzhnUNDR09frQLLVUbnIzACXiy70w9CDLAcmSfH2X5N WVbLlU/ExtBJHMqgoCNNLMu4zzrpvMu6VXwJYqMxwlzW8+3aw68pbL0pBjj3cu3tkAKa GG6iVLRKVl0cokPAXS58qyvzOM+KH32jtqTPThVfJZxa9Fx8d8TD6n0hrWcQRrBIE82F d1S5BAu1Q98PxNZz/9BpuPE+kowS5+aqhhNaFEbKBj9992Md2Ai0eKHQqDgK5vOTszWw 83GQ== 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 :dkim-signature; bh=LdhFBHV1E5DK+gCWkNBuac3jC3tSm5auGCtCYomeMgE=; b=dPLJZ0Xl4qf0YxitlojZfCHvTdcsjo5D+9h8Nrk5e+LPImLX82SkKdHcw8QRj84va1 2fxwzlFrZW5KvOUdZqCc4hMTs3lcoJ8GqaNZPhK7sa25nkNfp9dLXuH2d4fJStU+thRX 7J8FZIKCHc6KGzNtQt/txZC9Opv48rdAcF0v/60i+0CCIqtl4+CRqjvho2bQzC+VdhiL NniONnImZR33J0+AuawEVWydcaODERajirb6tSUooKd+Pl1C0mJTvXUuv/lKuPnI1Hxw YFF4zWqQ47NZU28iRNLkLQ9JyGd+jAETVCMzSxsKPbkk6CWvy3kYmd9uO/zGhMeN4F3P e1og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ay7ezGDj; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z16-20020a170903019000b00153b2d164dasi12869469plg.226.2022.04.04.20.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 20:15:37 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ay7ezGDj; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1B9871AF7C0; Mon, 4 Apr 2022 18:15:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359826AbiDCSkQ (ORCPT + 99 others); Sun, 3 Apr 2022 14:40:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359786AbiDCSkL (ORCPT ); Sun, 3 Apr 2022 14:40:11 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 102FD3981F for ; Sun, 3 Apr 2022 11:38:16 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id m30so11436766wrb.1 for ; Sun, 03 Apr 2022 11:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LdhFBHV1E5DK+gCWkNBuac3jC3tSm5auGCtCYomeMgE=; b=Ay7ezGDjFrkwTXwEiqZMYMEiiu9xsXMYguy5XcBGdXYSYW9XSjlqQj59+ybEG4K1X+ iJ0AVr0/Nv4dU5RoKtptasf5mosjOgs9GAfYAFL2O7OexlrrPMN9fBEQ4g3O+eXM5EYt uzBOSgtSNwkpack605DKKtFaaHwohpFpmuxACGxKB2yq8rGC/HbN7zC+BBkJq+UDnNXw WPwMcWUpK7W4AZPm1TighySqVOFEgShYIAKAViWp6K//7k7oblPn8QYP5EIMqG+Vx+KS dP3iP10ds8AYr91NFZIubhda8Y2Wy7OSlKecSdNtJwRBBxEDUylyknzz6Va5E68OJejL AQDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LdhFBHV1E5DK+gCWkNBuac3jC3tSm5auGCtCYomeMgE=; b=x19/I/NKq0BWXHuX4hbaWAD9ZY3cM3RWL96Mm6HBfj/tMSvtIrPXCLKs3j7Ns910ZD 94QQMDO3iYF0s5g7QsHkO+QNyXojG9WwarqXbo10LK0bbQUNKTfB/p8ng+7qD89znQD5 06bx4acoRyhg3Y+ZqIJh97L/3Mku2f5lwBEpQGSdmf5UCfJ0gapUtxIN16S74dhICBta 7Qy2I4iwjVrkB6ZrwabJnAfcxkYPkHhCV8XdCEs0CafdxT70U3vsjHJCRBrKVTjweTvA iWn4u5OnuFT+VEs8PjX4Qg7TVochdCUCuxb9xDCVTX3nLAkACUchjFsXRh5WcC2jDqA9 Mveg== X-Gm-Message-State: AOAM530ZfriuD5hzGIJKrhyS0aMyZq/8YjnZqGLWDnDAzWKo2CpVAeeo jPPtl4hHR+EhYyQrX2mwCXW5tQ== X-Received: by 2002:adf:e3c2:0:b0:206:fca:993a with SMTP id k2-20020adfe3c2000000b002060fca993amr2092987wrm.159.1649011094331; Sun, 03 Apr 2022 11:38:14 -0700 (PDT) Received: from localhost.localdomain (xdsl-188-155-201-27.adslplus.ch. [188.155.201.27]) by smtp.gmail.com with ESMTPSA id l28-20020a05600c1d1c00b0038e72a95ec4sm593851wms.13.2022.04.03.11.38.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Apr 2022 11:38:13 -0700 (PDT) From: Krzysztof Kozlowski To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Stuart Yoder , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Bjorn Helgaas , Bjorn Andersson , Mathieu Poirier , Vineeth Vijayan , Peter Oberparleiter , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Andy Gross , linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, NXP Linux Team , linux-arm-kernel@lists.infradead.org, linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org, linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org, linux-arm-msm@vger.kernel.org, alsa-devel@alsa-project.org, linux-spi@vger.kernel.org, virtualization@lists.linux-foundation.org, Linus Torvalds , Rasmus Villemoes , Andy Shevchenko , Krzysztof Kozlowski Subject: [PATCH v6 01/12] driver: platform: Add helper for safer setting of driver_override Date: Sun, 3 Apr 2022 20:37:47 +0200 Message-Id: <20220403183758.192236-2-krzysztof.kozlowski@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220403183758.192236-1-krzysztof.kozlowski@linaro.org> References: <20220403183758.192236-1-krzysztof.kozlowski@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Several core drivers and buses expect that driver_override is a dynamically allocated memory thus later they can kfree() it. However such assumption is not documented, there were in the past and there are already users setting it to a string literal. This leads to kfree() of static memory during device release (e.g. in error paths or during unbind): kernel BUG at ../mm/slub.c:3960! Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM ... (kfree) from [] (platform_device_release+0x88/0xb4) (platform_device_release) from [] (device_release+0x2c/0x90) (device_release) from [] (kobject_put+0xec/0x20c) (kobject_put) from [] (exynos5_clk_probe+0x154/0x18c) (exynos5_clk_probe) from [] (platform_drv_probe+0x6c/0xa4) (platform_drv_probe) from [] (really_probe+0x280/0x414) (really_probe) from [] (driver_probe_device+0x78/0x1c4) (driver_probe_device) from [] (bus_for_each_drv+0x74/0xb8) (bus_for_each_drv) from [] (__device_attach+0xd4/0x16c) (__device_attach) from [] (bus_probe_device+0x88/0x90) (bus_probe_device) from [] (device_add+0x3dc/0x62c) (device_add) from [] (of_platform_device_create_pdata+0x94/0xbc) (of_platform_device_create_pdata) from [] (of_platform_bus_create+0x1a8/0x4fc) (of_platform_bus_create) from [] (of_platform_bus_create+0x20c/0x4fc) (of_platform_bus_create) from [] (of_platform_populate+0x84/0x118) (of_platform_populate) from [] (of_platform_default_populate_init+0xa0/0xb8) (of_platform_default_populate_init) from [] (do_one_initcall+0x8c/0x404) Provide a helper which clearly documents the usage of driver_override. This will allow later to reuse the helper and reduce the amount of duplicated code. Convert the platform driver to use a new helper and make the driver_override field const char (it is not modified by the core). Signed-off-by: Krzysztof Kozlowski --- drivers/base/driver.c | 65 +++++++++++++++++++++++++++++++++ drivers/base/platform.c | 28 ++------------ include/linux/device/driver.h | 2 + include/linux/platform_device.h | 6 ++- 4 files changed, 76 insertions(+), 25 deletions(-) diff --git a/drivers/base/driver.c b/drivers/base/driver.c index 8c0d33e182fd..a6bfe69a8ecc 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -30,6 +30,71 @@ static struct device *next_device(struct klist_iter *i) return dev; } +/** + * driver_set_override() - Helper to set or clear driver override. + * @dev: Device to change + * @override: Address of string to change (e.g. &device->driver_override); + * The contents will be freed and hold newly allocated override. + * @s: NUL-terminated string, new driver name to force a match, pass empty + * string to clear it + * @len: length of @s + * + * Helper to set or clear driver override in a device, intended for the cases + * when the driver_override field is allocated by driver/bus code. + * + * Returns: 0 on success or a negative error code on failure. + */ +int driver_set_override(struct device *dev, const char **override, + const char *s, size_t len) +{ + const char *new, *old; + char *cp; + + if (!override || !s) + return -EINVAL; + + /* + * The stored value will be used in sysfs show callback (sysfs_emit()), + * which has a length limit of PAGE_SIZE and adds a trailing newline. + * Thus we can store one character less to avoid truncation during sysfs + * show. + */ + if (len >= (PAGE_SIZE - 1)) + return -EINVAL; + + if (!len) { + device_lock(dev); + old = *override; + *override = NULL; + device_unlock(dev); + goto out_free; + } + + cp = strnchr(s, len, '\n'); + if (cp) + len = cp - s; + + new = kstrndup(s, len, GFP_KERNEL); + if (!new) + return -ENOMEM; + + device_lock(dev); + old = *override; + if (cp != s) { + *override = new; + } else { + kfree(new); + *override = NULL; + } + device_unlock(dev); + +out_free: + kfree(old); + + return 0; +} +EXPORT_SYMBOL_GPL(driver_set_override); + /** * driver_for_each_device - Iterator for devices bound to a driver. * @drv: Driver we're iterating. diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 8cc272fd5c99..b684157b7f2f 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -1275,31 +1275,11 @@ static ssize_t driver_override_store(struct device *dev, const char *buf, size_t count) { struct platform_device *pdev = to_platform_device(dev); - char *driver_override, *old, *cp; - - /* We need to keep extra room for a newline */ - if (count >= (PAGE_SIZE - 1)) - return -EINVAL; - - driver_override = kstrndup(buf, count, GFP_KERNEL); - if (!driver_override) - return -ENOMEM; - - cp = strchr(driver_override, '\n'); - if (cp) - *cp = '\0'; - - device_lock(dev); - old = pdev->driver_override; - if (strlen(driver_override)) { - pdev->driver_override = driver_override; - } else { - kfree(driver_override); - pdev->driver_override = NULL; - } - device_unlock(dev); + int ret; - kfree(old); + ret = driver_set_override(dev, &pdev->driver_override, buf, count); + if (ret) + return ret; return count; } diff --git a/include/linux/device/driver.h b/include/linux/device/driver.h index 15e7c5e15d62..700453017e1c 100644 --- a/include/linux/device/driver.h +++ b/include/linux/device/driver.h @@ -151,6 +151,8 @@ extern int __must_check driver_create_file(struct device_driver *driver, extern void driver_remove_file(struct device_driver *driver, const struct driver_attribute *attr); +int driver_set_override(struct device *dev, const char **override, + const char *s, size_t len); extern int __must_check driver_for_each_device(struct device_driver *drv, struct device *start, void *data, diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h index 7c96f169d274..582d83ed9a91 100644 --- a/include/linux/platform_device.h +++ b/include/linux/platform_device.h @@ -31,7 +31,11 @@ struct platform_device { struct resource *resource; const struct platform_device_id *id_entry; - char *driver_override; /* Driver name to force a match */ + /* + * Driver name to force a match. Do not set directly, because core + * frees it. Use driver_set_override() to set or clear it. + */ + const char *driver_override; /* MFD cell pointer */ struct mfd_cell *mfd_cell; -- 2.32.0