Message ID | 20230308153200.682248-2-miquel.raynal@bootlin.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp403423wrd; Wed, 8 Mar 2023 07:34:34 -0800 (PST) X-Google-Smtp-Source: AK7set9MrUYNOHiwA0b1mZL+WGAkaybDtU1igHTfSEUPxC3yqY5AGQERiZsK3WjeqJBFXMN5vOZ0 X-Received: by 2002:a17:902:c389:b0:19e:7880:a5a with SMTP id g9-20020a170902c38900b0019e78800a5amr16457804plg.39.1678289674425; Wed, 08 Mar 2023 07:34:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678289674; cv=none; d=google.com; s=arc-20160816; b=Js/+drd4mwfvw2gUQtOv7HPFzwbRMBWLqrSgxrfrJzO8o0dFny1zHMnYV08IVjrXDh 435nx1k5dw4ssJTfc7DrLAhWjDm88/bQcLWf96Hvs++JZMMQ1yN4SHO5jJ1316ymGFgE EBn3mEYgoZTISkUhbIAOH+0KlENO886zAoCR6YCPL6M5omMmRKv0epgNZmePMbqp649R 7Lk+gXr8E3Yp6+FuaegKfNv9B1llkMznlAnpAQ1B2avIwmwfl3EknzicvElxId7HdaJk hJHN97jaQY3X3wW/j4qyp/UbyU59dfpRuNXo3OnXicLRyxUJKtqBxlRQKmyPWJghXz3u vSAQ== 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=IJ3o8unBTMIe9q90/GtJ5ny60FdItpVL7wHmQ3YSkUw=; b=CODOWyWc5Yt0aup0tL8oeXwYTUSsRAmlf49jFLZfHHkP8WYuzcsJpwZVsbIJRtGUi/ qVFF9rphNoPQXRbOthShcpkZSN2Y3t5WoKL+wY5VMR6iJPnO1AHTx0lNMUL4LVKEXv7X Ni0CPDpa+9gp+5N3tmTTiv9R9aQ0YbIhk0/qTNvflsk6cJKomxj5eZN76WZH0RcDugxp vDPc7d7KkWwXKUd40WUlaJC1LGQomZJiHSCYeda50bPtEO6BcTXAV9Z5Lm8IUvHVmL/K yny2lHv+cyJNx6wyF+uIqDBZK+xr9Htzlee1fV0cLiY14zgtQpM9cx0fRBK6z6IhAi/g O37A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=CJFcNjdd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h19-20020a63df53000000b004fb647a765esi14591003pgj.329.2023.03.08.07.34.18; Wed, 08 Mar 2023 07:34:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=CJFcNjdd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231975AbjCHPcN (ORCPT <rfc822;toshivichauhan@gmail.com> + 99 others); Wed, 8 Mar 2023 10:32:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231816AbjCHPcI (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 8 Mar 2023 10:32:08 -0500 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2A5B8C514; Wed, 8 Mar 2023 07:32:06 -0800 (PST) Received: (Authenticated sender: miquel.raynal@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 9C810FF80F; Wed, 8 Mar 2023 15:32:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1678289525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IJ3o8unBTMIe9q90/GtJ5ny60FdItpVL7wHmQ3YSkUw=; b=CJFcNjdd9fcOXVkHGwRZGNuoq0mkCfr5tUfdHT1Pa1l85hzCFkDM2s5KFO7RIKnuk7LhI8 sRJqdVpg8k3Zz8hwucefY4gQTbjof3zCKTM+m9I8AnXN7UoRzpGMhfkej83IQ9apvsy+vr hBBDAi5nlLRO8eaTxB/EAykV1zvGhJ9231v7hkONWBhvYsz2xC0RpZN5ovtcvLsoGhMrN7 4hRALJR3alssliRMa17xCMgXk9Fu/HgMdkBWZwAgD7jRClxxOmzrdybgfED7R8JVww3lRK kJunlnjLlRd+Wi5ZGAgzF770Fa1sWhqrb3Lc2p5U63d8UlCWxKaNrhAqRGLyaQ== From: Miquel Raynal <miquel.raynal@bootlin.com> To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>, <linux-kernel@vger.kernel.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Michael Walle <michael@walle.cc>, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= <rafal@milecki.pl>, Robert Marko <robert.marko@sartura.hr>, Luka Perkov <luka.perkov@sartura.hr>, Thomas Petazzoni <thomas.petazzoni@bootlin.com>, Rob Herring <robh+dt@kernel.org>, Frank Rowand <frowand.list@gmail.com>, devicetree@vger.kernel.org, Miquel Raynal <miquel.raynal@bootlin.com>, Stephen Boyd <sboyd@kernel.org>, Peter Chen <peter.chen@kernel.org>, Rob Herring <robh@kernel.org> Subject: [PATCH v3 01/20] of: Fix modalias string generation Date: Wed, 8 Mar 2023 16:31:41 +0100 Message-Id: <20230308153200.682248-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230308153200.682248-1-miquel.raynal@bootlin.com> References: <20230308153200.682248-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759814273664042309?= X-GMAIL-MSGID: =?utf-8?q?1759814273664042309?= |
Series |
[v3,01/20] of: Fix modalias string generation
|
|
Commit Message
Miquel Raynal
March 8, 2023, 3:31 p.m. UTC
The helper generating an OF based modalias (of_device_get_modalias()) works fine, but due to the use of snprintf() internally it needs a buffer one byte longer than what should be needed just for the entire string (excluding the '\0'). Most users of this helper are sysfs hooks providing the modalias string to users. They all provide a PAGE_SIZE buffer which is way above the number of bytes required to fit the modalias string and hence do not suffer from this issue. There is another user though, of_device_request_module(), which is only called by drivers/usb/common/ulpi.c. This request module function is faulty, but maybe because in most cases there is an alternative, ULPI driver users have not noticed it. In this function, of_device_get_modalias() is called twice. The first time without buffer just to get the number of bytes required by the modalias string (excluding the null byte), and a second time, after buffer allocation, to fill the buffer. The allocation asks for an additional byte, in order to store the trailing '\0'. However, the buffer *length* provided to of_device_get_modalias() excludes this extra byte. The internal use of snprintf() with a length that is exactly the number of bytes to be written has the effect of using the last available byte to store a '\0', which then smashes the last character of the modalias string. Provide the actual size of the buffer to of_device_get_modalias() to fix this issue. Note: the "str[size - 1] = '\0';" line is not really needed as snprintf will anyway end the string with a null byte, but there is a possibility that this function might be called on a struct device_node without compatible, in this case snprintf() would not be executed. So we keep it just to avoid possible unbounded strings. Cc: Stephen Boyd <sboyd@kernel.org> Cc: Peter Chen <peter.chen@kernel.org> Fixes: 9c829c097f2f ("of: device: Support loading a module with OF based modalias") Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Reviewed-by: Rob Herring <robh@kernel.org> --- drivers/of/device.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/of/device.c b/drivers/of/device.c index 955bfb3d1a83..c91bb5899256 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -297,12 +297,15 @@ int of_device_request_module(struct device *dev) if (size < 0) return size; - str = kmalloc(size + 1, GFP_KERNEL); + /* Reserve an additional byte for the trailing '\0' */ + size++; + + str = kmalloc(size, GFP_KERNEL); if (!str) return -ENOMEM; of_device_get_modalias(dev, str, size); - str[size] = '\0'; + str[size - 1] = '\0'; ret = request_module(str); kfree(str);