linux-arm-msm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy()
@ 2022-06-28 13:43 Douglas Anderson
  2022-06-28 13:44 ` Krzysztof Kozlowski
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Douglas Anderson @ 2022-06-28 13:43 UTC (permalink / raw)
  To: Bjorn Andersson
  Cc: Douglas Anderson, Matthias Kaehlcke, Krzysztof Kozlowski,
	Andy Gross, Konrad Dybcio, Stephen Boyd, linux-arm-msm,
	linux-kernel

Commit ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with
strscpy_pad()") breaks booting on my sc7280-herobrine-herobrine
device. From printouts I see that at bootup the function is called
with an id of "lnbclka2" which is 8 bytes big.

Previously all 8 bytes of this string were copied to the
destination. Now only 7 bytes will be copied since strscpy_pad() saves
a byte for '\0' termination.

We don't need the '\0' termination in the destination. Let's go back
to strncpy(). According to the warning:
  If a caller is using non-NUL-terminated strings, strncpy() can still
  be used, but destinations should be marked with the __nonstring
  attribute to avoid future compiler warnings.
...so we'll do that.

While we're at it, let's change the query array to use
"sizeof(ent->id)" so it can't possibly go out of sync with our later
copy.

Fixes: ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with strscpy_pad()")
Signed-off-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---

Changes in v3:
- Add comment that query isn't necessarily '\0' terminated.

Changes in v2:
- Size array with "sizeof(ent->id)"

 drivers/soc/qcom/cmd-db.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/soc/qcom/cmd-db.c b/drivers/soc/qcom/cmd-db.c
index c5137c25d819..629a7188b576 100644
--- a/drivers/soc/qcom/cmd-db.c
+++ b/drivers/soc/qcom/cmd-db.c
@@ -141,14 +141,18 @@ static int cmd_db_get_header(const char *id, const struct entry_header **eh,
 	const struct rsc_hdr *rsc_hdr;
 	const struct entry_header *ent;
 	int ret, i, j;
-	u8 query[8];
+	u8 query[sizeof(ent->id)] __nonstring;
 
 	ret = cmd_db_ready();
 	if (ret)
 		return ret;
 
-	/* Pad out query string to same length as in DB */
-	strscpy_pad(query, id, sizeof(query));
+	/*
+	 * Pad out query string to same length as in DB. NOTE: the output
+	 * query string is not necessarily '\0' terminated if it bumps up
+	 * against the max size. That's OK and expected.
+	 */
+	strncpy(query, id, sizeof(query));
 
 	for (i = 0; i < MAX_SLV_ID; i++) {
 		rsc_hdr = &cmd_db_header->header[i];
-- 
2.37.0.rc0.161.g10f37bed90-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy()
  2022-06-28 13:43 [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy() Douglas Anderson
@ 2022-06-28 13:44 ` Krzysztof Kozlowski
  2022-06-28 20:18 ` (subset) " Bjorn Andersson
  2022-06-29  7:31 ` Stephen Boyd
  2 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2022-06-28 13:44 UTC (permalink / raw)
  To: Douglas Anderson, Bjorn Andersson
  Cc: Matthias Kaehlcke, Andy Gross, Konrad Dybcio, Stephen Boyd,
	linux-arm-msm, linux-kernel

On 28/06/2022 15:43, Douglas Anderson wrote:
> Commit ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with
> strscpy_pad()") breaks booting on my sc7280-herobrine-herobrine
> device. From printouts I see that at bootup the function is called
> with an id of "lnbclka2" which is 8 bytes big.
> 
> Previously all 8 bytes of this string were copied to the
> destination. Now only 7 bytes will be copied since strscpy_pad() saves
> a byte for '\0' termination.
> 
> We don't need the '\0' termination in the destination. Let's go back
> to strncpy(). According to the warning:
>   If a caller is using non-NUL-terminated strings, strncpy() can still
>   be used, but destinations should be marked with the __nonstring
>   attribute to avoid future compiler warnings.
> ...so we'll do that.
> 
> While we're at it, let's change the query array to use
> "sizeof(ent->id)" so it can't possibly go out of sync with our later
> copy.
> 
> Fixes: ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with strscpy_pad()")
> Signed-off-by: Douglas Anderson <dianders@chromium.org>
> Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
> 
> Changes in v3:
> - Add comment that query isn't necessarily '\0' terminated.
> 

Thanks!

Best regards,
Krzysztof

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: (subset) [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy()
  2022-06-28 13:43 [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy() Douglas Anderson
  2022-06-28 13:44 ` Krzysztof Kozlowski
@ 2022-06-28 20:18 ` Bjorn Andersson
  2022-06-29  7:31 ` Stephen Boyd
  2 siblings, 0 replies; 4+ messages in thread
From: Bjorn Andersson @ 2022-06-28 20:18 UTC (permalink / raw)
  To: Douglas Anderson
  Cc: linux-kernel, linux-arm-msm, Andy Gross, Stephen Boyd,
	Matthias Kaehlcke, Krzysztof Kozlowski, Konrad Dybcio

On Tue, 28 Jun 2022 06:43:13 -0700, Douglas Anderson wrote:
> Commit ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with
> strscpy_pad()") breaks booting on my sc7280-herobrine-herobrine
> device. From printouts I see that at bootup the function is called
> with an id of "lnbclka2" which is 8 bytes big.
> 
> Previously all 8 bytes of this string were copied to the
> destination. Now only 7 bytes will be copied since strscpy_pad() saves
> a byte for '\0' termination.
> 
> [...]

Applied, thanks!

[1/1] soc: qcom: cmd-db: replace strscpy_pad() with strncpy()
      commit: fe72f9bce137055fb744d4f8a91baa234ec07baa

Best regards,
-- 
Bjorn Andersson <bjorn.andersson@linaro.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy()
  2022-06-28 13:43 [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy() Douglas Anderson
  2022-06-28 13:44 ` Krzysztof Kozlowski
  2022-06-28 20:18 ` (subset) " Bjorn Andersson
@ 2022-06-29  7:31 ` Stephen Boyd
  2 siblings, 0 replies; 4+ messages in thread
From: Stephen Boyd @ 2022-06-29  7:31 UTC (permalink / raw)
  To: Bjorn Andersson, Douglas Anderson
  Cc: Douglas Anderson, Matthias Kaehlcke, Krzysztof Kozlowski,
	Andy Gross, Konrad Dybcio, linux-arm-msm, linux-kernel

Quoting Douglas Anderson (2022-06-28 06:43:13)
> Commit ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with
> strscpy_pad()") breaks booting on my sc7280-herobrine-herobrine
> device. From printouts I see that at bootup the function is called
> with an id of "lnbclka2" which is 8 bytes big.
> 
> Previously all 8 bytes of this string were copied to the
> destination. Now only 7 bytes will be copied since strscpy_pad() saves
> a byte for '\0' termination.
> 
> We don't need the '\0' termination in the destination. Let's go back
> to strncpy(). According to the warning:
>   If a caller is using non-NUL-terminated strings, strncpy() can still
>   be used, but destinations should be marked with the __nonstring
>   attribute to avoid future compiler warnings.
> ...so we'll do that.
> 
> While we're at it, let's change the query array to use
> "sizeof(ent->id)" so it can't possibly go out of sync with our later
> copy.
> 
> Fixes: ac0126a01735 ("soc: qcom: cmd-db: replace strncpy() with strscpy_pad()")
> Signed-off-by: Douglas Anderson <dianders@chromium.org>
> Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---

Reviewed-by: Stephen Boyd <sboyd@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-29  7:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-28 13:43 [PATCH v3] soc: qcom: cmd-db: replace strscpy_pad() with strncpy() Douglas Anderson
2022-06-28 13:44 ` Krzysztof Kozlowski
2022-06-28 20:18 ` (subset) " Bjorn Andersson
2022-06-29  7:31 ` Stephen Boyd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).