Skip to contentSkip to conversation

arm: mach-snapdragon: warn on root compatible string truncation

PATCH Superseded Vivek Ray 2 participants 6 messages Jun 10 – Oct 1

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to Vivek Ray

Hi Vivek,

Quoted 8 lines · Vivek Ray

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a boot failure with no indication of the root cause.

Add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

Thanks for the patch! Is there a particular board you ran into this issue with?

If the buffer is too small for some boards then we should definitely increase it, could you bump it up to 128?

Quoted 20 lines
---
 arch/arm/mach-snapdragon/board.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-snapdragon/board.c +1 -0
index 829a0109ac7..9324030ae4d 100644
@@ -391,6 +391,7 @@ static void configure_env(void)
 	const char *first_compat, *last_compat;
 	char *tmp;
 	char buf[32] = { 0 };
+	int len = 0;
 	/*
 	 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
 	 * The vendor is skipped when it's a Qualcomm reference board, or the

arch/arm/mach-snapdragon/board.c static void configure_env(void) · line 423

Quoted 7 lines
arch/arm/mach-snapdragon/board.c +3 -1
@@ -417,7 +418,10 @@ static void configure_env(void)
 		return;
 	}
 
-	strlcpy(buf, first_compat, sizeof(buf) - 1);
+	/* A safety check to avoid silent failure due to name truncation */
+	len = strlcpy(buf, first_compat, sizeof(buf) - 1);
+	if (len >= sizeof(buf) - 1)

I think there's an off by 1 here, this should just be > not >=, and the same below.

Kind regards,

Quoted 17 lines
arch/arm/mach-snapdragon/board.c +6 -1
+		log_warning("compatible '%s' got truncated, fdtfile name too long\n", first_compat);
 	tmp = buf;
 
 	/* The Qualcomm reference boards (RBx, HDK, etc)  */
@@ -468,7 +472,11 @@ static void configure_env(void)
 
 		/* Copy the last compat (e.g. "qcom,sdm845") into buf */
 		memset(buf, 0, sizeof(buf));
-		strlcpy(buf, last_compat, sizeof(buf) - 1);
+
+		/* check for name truncation */
+		len = strlcpy(buf, last_compat, sizeof(buf) - 1);
+		if (len >= sizeof(buf) - 1)
+			log_warning("compatible '%s' got truncated, fdtfile name too long\n", last_compat);
 		tmp = buf;
 
 		/* strsep() is destructive, it replaces the comma with a \0 */
Signature · 1 line
// Casey (she/her)

Vivek Ray

author
#0 lore raw

PATCH 1/1 arm: mach-snapdragon: warn on root compatible string truncation

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a boot failure with no indication of the root cause.

Add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

---
 arch/arm/mach-snapdragon/board.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm/mach-snapdragon/board.c +10 -2
index 829a0109ac7..9324030ae4d 100644
@@ -391,6 +391,7 @@ static void configure_env(void)
 	const char *first_compat, *last_compat;
 	char *tmp;
 	char buf[32] = { 0 };
+	int len = 0;
 	/*
 	 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
 	 * The vendor is skipped when it's a Qualcomm reference board, or the
@@ -417,7 +418,10 @@ static void configure_env(void)
 		return;
 	}
 
-	strlcpy(buf, first_compat, sizeof(buf) - 1);
+	/* A safety check to avoid silent failure due to name truncation */
+	len = strlcpy(buf, first_compat, sizeof(buf) - 1);
+	if (len >= sizeof(buf) - 1)
+		log_warning("compatible '%s' got truncated, fdtfile name too long\n", first_compat);
 	tmp = buf;
 
 	/* The Qualcomm reference boards (RBx, HDK, etc)  */
@@ -468,7 +472,11 @@ static void configure_env(void)
 
 		/* Copy the last compat (e.g. "qcom,sdm845") into buf */
 		memset(buf, 0, sizeof(buf));
-		strlcpy(buf, last_compat, sizeof(buf) - 1);
+
+		/* check for name truncation */
+		len = strlcpy(buf, last_compat, sizeof(buf) - 1);
+		if (len >= sizeof(buf) - 1)
+			log_warning("compatible '%s' got truncated, fdtfile name too long\n", last_compat);
 		tmp = buf;
 
 		/* strsep() is destructive, it replaces the comma with a \0 */
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 1/1 arm: mach-snapdragon: increase compat buffer size and warn on root compatible string truncation

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a silent boot failure with no indication of the root cause.

Bump buf[] to 128 bytes and dt_path[] to 256 bytes to accommodate longer compatible strings. Also, add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

---

Changes in V2:
- Bump buf[] from 32 to 128 bytes and dt_path[] from 64 to 256 (Casey Connolly)
- Fix line size check, use > not >= (Casey Connolly)

---
 arch/arm/mach-snapdragon/board.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/arm/mach-snapdragon/board.c +14 -4
index 829a0109ac7..d9390d9b5d3 100644
@@ -390,13 +390,14 @@ static void configure_env(void)
 {
 	const char *first_compat, *last_compat;
 	char *tmp;
-	char buf[32] = { 0 };
+	char buf[128] = { 0 };
+	int len = 0;
 	/*
 	 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
 	 * The vendor is skipped when it's a Qualcomm reference board, or the
 	 * db845c.
 	 */
-	char dt_path[64] = { 0 };
+	char dt_path[256] = { 0 };
 	int compat_count, ret;
 	ofnode root;
 
@@ -417,7 +418,11 @@ static void configure_env(void)
 		return;
 	}
 
-	strlcpy(buf, first_compat, sizeof(buf) - 1);
+	/* A safety check to avoid silent failure due to name truncation */
+	len = strlcpy(buf, first_compat, sizeof(buf) - 1);
+	if (len > sizeof(buf) - 1)
+		log_warning("compatible '%s' got truncated, fdtfile name too long\n",
+			    first_compat);
 	tmp = buf;
 
 	/* The Qualcomm reference boards (RBx, HDK, etc)  */
@@ -468,7 +473,12 @@ static void configure_env(void)
 
 		/* Copy the last compat (e.g. "qcom,sdm845") into buf */
 		memset(buf, 0, sizeof(buf));
-		strlcpy(buf, last_compat, sizeof(buf) - 1);
+
+		/* check for name truncation */
+		len = strlcpy(buf, last_compat, sizeof(buf) - 1);
+		if (len > sizeof(buf) - 1)
+			log_warning("compatible '%s' got truncated, fdtfile name too long\n",
+				    last_compat);
 		tmp = buf;
 
 		/* strsep() is destructive, it replaces the comma with a \0 */
Signature · 1 line
2.34.1
author
#3 lore raw

Re: [PATCH v2] arm: mach-snapdragon: increase compat buffer size and warn on root compatible string truncation

Hi Casey, Neil,

Just a friendly ping regarding this patch.

The v2 addresses the review comments from Casey:

- Increased the compatible string buffer size
- Fixed the truncation check

This was motivated by an issue I encountered on a QCS6490-based board where a longer compatible string caused the generated fdtfile name to be truncated, leading to a boot failure.

I wanted to check whether there are any remaining concerns or if further changes are needed from my side.

Thanks for your time.

Regards,
Vivek Ray

Quoted 81 lines · Vivek Ray

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a silent boot failure with no indication of the root cause.

Bump buf[] to 128 bytes and dt_path[] to 256 bytes to accommodate longer compatible strings. Also, add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

---

Changes in V2:
- Bump buf[] from 32 to 128 bytes and dt_path[] from 64 to 256 (Casey Connolly)
- Fix line size check, use > not >= (Casey Connolly)

---
 arch/arm/mach-snapdragon/board.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/arm/mach-snapdragon/board.c +2 -1
index 829a0109ac7..d9390d9b5d3 100644
@@ -390,13 +390,14 @@ static void configure_env(void)
 {
        const char *first_compat, *last_compat;
        char *tmp;
-       char buf[32] = { 0 };
+       char buf[128] = { 0 };
+       int len = 0;
        /*
         * Most DTB filenames follow the scheme:

qcom/<soc>-[vendor]-<board>.dtb
* The vendor is skipped when it's a Qualcomm reference board, or
the
* db845c.
*/

arch/arm/mach-snapdragon/board.c +6 -2
-       char dt_path[64] = { 0 };
+       char dt_path[256] = { 0 };
        int compat_count, ret;
        ofnode root;
@@ -417,7 +418,11 @@ static void configure_env(void)
                return;
        }
 
-       strlcpy(buf, first_compat, sizeof(buf) - 1);
+       /* A safety check to avoid silent failure due to name truncation */
+       len = strlcpy(buf, first_compat, sizeof(buf) - 1);
+       if (len > sizeof(buf) - 1)
+               log_warning("compatible '%s' got truncated, fdtfile name
 too long\n",
+                           first_compat);
        tmp = buf;
 
        /* The Qualcomm reference boards (RBx, HDK, etc)  */
arch/arm/mach-snapdragon/board.c +6 -1
@@ -468,7 +473,12 @@ static void configure_env(void)
 
                /* Copy the last compat (e.g. "qcom,sdm845") into buf */
                memset(buf, 0, sizeof(buf));
-               strlcpy(buf, last_compat, sizeof(buf) - 1);
+
+               /* check for name truncation */
+               len = strlcpy(buf, last_compat, sizeof(buf) - 1);
+               if (len > sizeof(buf) - 1)
+                       log_warning("compatible '%s' got truncated,
 fdtfile name too long\n",
+                                   last_compat);
                tmp = buf;
 
                /* strsep() is destructive, it replaces the comma with a

\0 */
--
2.34.1

Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] arm: mach-snapdragon: increase compat buffer size and warn on root compatible string truncation

In reply to Vivek Ray

Hi Vivek,

Quoted 11 lines · Vivek Ray

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a silent boot failure with no indication of the root cause.

Bump buf[] to 128 bytes and dt_path[] to 256 bytes to accommodate longer compatible strings. Also, add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

Thanks for the quick respin!

Quoted 59 lines

---

Changes in V2:
- Bump buf[] from 32 to 128 bytes and dt_path[] from 64 to 256 (Casey Connolly)
- Fix line size check, use > not >= (Casey Connolly)

---
 arch/arm/mach-snapdragon/board.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/arm/mach-snapdragon/board.c +14 -4
index 829a0109ac7..d9390d9b5d3 100644
@@ -390,13 +390,14 @@ static void configure_env(void)
 {
 	const char *first_compat, *last_compat;
 	char *tmp;
-	char buf[32] = { 0 };
+	char buf[128] = { 0 };
+	int len = 0;
 	/*
 	 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
 	 * The vendor is skipped when it's a Qualcomm reference board, or the
 	 * db845c.
 	 */
-	char dt_path[64] = { 0 };
+	char dt_path[256] = { 0 };
 	int compat_count, ret;
 	ofnode root;
 
@@ -417,7 +418,11 @@ static void configure_env(void)
 		return;
 	}
 
-	strlcpy(buf, first_compat, sizeof(buf) - 1);
+	/* A safety check to avoid silent failure due to name truncation */
+	len = strlcpy(buf, first_compat, sizeof(buf) - 1);
+	if (len > sizeof(buf) - 1)
+		log_warning("compatible '%s' got truncated, fdtfile name too long\n",
+			    first_compat);
 	tmp = buf;
 
 	/* The Qualcomm reference boards (RBx, HDK, etc)  */
@@ -468,7 +473,12 @@ static void configure_env(void)
 
 		/* Copy the last compat (e.g. "qcom,sdm845") into buf */
 		memset(buf, 0, sizeof(buf));
-		strlcpy(buf, last_compat, sizeof(buf) - 1);
+
+		/* check for name truncation */
+		len = strlcpy(buf, last_compat, sizeof(buf) - 1);
+		if (len > sizeof(buf) - 1)
+			log_warning("compatible '%s' got truncated, fdtfile name too long\n",
+				    last_compat);
 		tmp = buf;
 
 		/* strsep() is destructive, it replaces the comma with a \0 */
Signature · 1 line
// Casey (she/her)
#5 lore raw

Re: [PATCH v2] arm: mach-snapdragon: increase compat buffer size and warn on root compatible string truncation

In reply to Vivek Ray
Quoted 11 lines · Vivek Ray

The configure_env() function copies root compatible strings into a 32-byte buffer prior to parsing. If a compatible string exceeds this limit, strlcpy() silently truncates it, producing a malformed fdtfile path and a silent boot failure with no indication of the root cause.

Bump buf[] to 128 bytes and dt_path[] to 256 bytes to accommodate longer compatible strings. Also, add explicit truncation checks after both strlcpy() calls and emit a log_warning() with the offending compatible string to make such failures easier to diagnose.

[...]

Applied, thanks!

[1/1] arm: mach-snapdragon: increase compat buffer size and warn on root compatible string truncation

      https://source.denx.de/u-boot/custodians/u-boot-snapdragon/-/commit/17943d4d08e0

Best regards,

Signature · 1 line
Casey Connolly <[email protected]>