Skip to contentSkip to conversation

Fix strncpy() usage

PATCH In review Oleh Kravchenko 4 participants 15 messages Nov 16, 2021 – Dec 2, 2021 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix strncpy() usage

Manpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.

This patch also fixes a compile error with a newer version of GCC.

---
 mmc_cmds.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mmc_cmds.c +2 -2
index 73bd32a..016fe70 100644
@@ -1834,8 +1834,8 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
+		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], sizeof(lbuf) - 1);
+		lbuf[sizeof(lbuf) - 1] = 0;
 		printf("eMMC Firmware Version: %s\n", lbuf);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
Signature · 1 line
2.32.0
#1 lore raw

+ Avri, Bean

Quoted 20 lines · Oleh Kravchenko

Manpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.

This patch also fixes a compile error with a newer version of GCC.

---
 mmc_cmds.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mmc_cmds.c +0 -0
index 73bd32a..016fe70 100644

mmc_cmds.c int do_read_extcsd(int nargs, char **argv) · line 1837

Quoted 6 lines
mmc_cmds.c +1 -2
@@ -1834,8 +1834,8 @@ int do_read_extcsd(int nargs, char **argv)
        }
 
        if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
+               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], sizeof(lbuf) - 1);

This may look better, but is actually making it worse. lbuf is 10 bytes long, while the ext_csd is 8 bytes.

Quoted 7 lines
mmc_cmds.c +1 -1
+               lbuf[sizeof(lbuf) - 1] = 0;
                printf("eMMC Firmware Version: %s\n", lbuf);
                printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
--

2.32.0

Kind regards
Uffe

#2 lore raw

PATCH v2 1/1 Replace strncpy() usage by printf()

In reply to Oleh Kravchenko

Manpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.

This patch also fixes a compile error with a newer version of GCC.

---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mmc_cmds.c +1 -4
index 73bd32a..753fcd9 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
 	__u32 regl;
 	int fd, ret;
 	char *device;
-	char lbuf[10];
 	const char *str;
 
 	if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-		printf("eMMC Firmware Version: %s\n", lbuf);
+		printf("eMMC Firmware Version: %.*s\n", 8, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
 		printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
#3 lore raw

Re: [PATCH v2] Replace strncpy() usage by printf()

+ Avri

Quoted 7 lines · Oleh Kravchenko

Manpage of strncpy() says:
If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Put \0 to the end of the buffer to ensure that
the destination string is NULL-terminated.

Please update the commit message to reflect what the patch really does, as the above is no longer correct.

Quoted 4 lines

This patch also fixes a compile error with a newer version of GCC.

At next submission, please also add Avri Altman <[email protected]>, who co-maintains the mmc-utils.

Quoted 17 lines
---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mmc_cmds.c +0 -1
index 73bd32a..753fcd9 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
        __u32 regl;
        int fd, ret;
        char *device;
-       char lbuf[10];
        const char *str;
 
        if (nargs != 2) {

mmc_cmds.c int do_read_extcsd(int nargs, char **argv) · line 1836

Quoted 7 lines
mmc_cmds.c +1 -3
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
        }
 
        if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-               printf("eMMC Firmware Version: %s\n", lbuf);
+               printf("eMMC Firmware Version: %.*s\n", 8, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);

This looks good to me. However, I suggest you skip the dynamic length format. Just use %.8s.

Quoted 6 lines
mmc_cmds.c +0 -1
                printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
                printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--

2.32.0

Kind regards
Uffe

#4 lore raw

PATCH v3 1/1 Use printf() to extract and print fw version

In reply to Oleh Kravchenko

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mmc_cmds.c +1 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
 	__u32 regl;
 	int fd, ret;
 	char *device;
-	char lbuf[10];
 	const char *str;
 
 	if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-		printf("eMMC Firmware Version: %s\n", lbuf);
+		printf("eMMC Firmware Version: %.8s\n", (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
 		printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
Reviewed-by on v3 1/1
#5 lore raw

RE: [EXT] [PATCH v3] Use printf() to extract and print fw version

Micron Confidential

Quoted 7 lines

Cc: Oleh Kravchenko <[email protected]>
Subject: [EXT] [PATCH v3] Use printf() to extract and print fw version

CAUTION: EXTERNAL EMAIL. Do not click links or open attachments unless you recognize the sender and were expecting this message.

Quoted 5 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

Hi Oleh,

It is better to add the prefix "mmc-utils:" in the Commit subject to distinguish it from Linux kernel changes

Micron Confidential

#6 lore raw

Re: [EXT] [PATCH v3] Use printf() to extract and print fw version

29.11.21 17:05, Bean Huo (beanhuo) пише:

Quoted 9 lines

Micron Confidential

Quoted 7 lines

Cc: Oleh Kravchenko <[email protected]>
Subject: [EXT] [PATCH v3] Use printf() to extract and print fw version

CAUTION: EXTERNAL EMAIL. Do not click links or open attachments unless you recognize the sender and were expecting this message.

Quoted 8 lines
Quoted 5 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

Hi Oleh,

It is better to add the prefix "mmc-utils:" in the Commit subject to distinguish it from Linux kernel changes

Will do it.

Quoted 1 line

Thanks!

#7 lore raw

PATCH v3 1/1 mmc-utils: Use printf() to extract and print fw version

In reply to Oleh Kravchenko

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

Update commit message.

mmc_cmds.c +1 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
 	__u32 regl;
 	int fd, ret;
 	char *device;
-	char lbuf[10];
 	const char *str;
 
 	if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-		printf("eMMC Firmware Version: %s\n", lbuf);
+		printf("eMMC Firmware Version: %.8s\n", (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
 		printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
#8 lore raw

PATCH v4 1/1 mmc-utils: Use printf() to extract and print fw version

In reply to Oleh Kravchenko

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

PATCH v4
Update commit message.

mmc_cmds.c +1 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
 	__u32 regl;
 	int fd, ret;
 	char *device;
-	char lbuf[10];
 	const char *str;
 
 	if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-		printf("eMMC Firmware Version: %s\n", lbuf);
+		printf("eMMC Firmware Version: %.8s\n", (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
 		printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
#9 lore raw

RE: [PATCH v4] mmc-utils: Use printf() to extract and print fw version

Hi,

Quoted 4 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

You are reverting commit 0eea71e4f2 (mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.

You might also would like to take a look at the correspondence concerning a different approach to the same issue here - https://lore.kernel.org/lkml/[email protected]/

Thanks,
Avri

Quoted 38 lines
---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

PATCH v4
Update commit message.

mmc_cmds.c +2 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
        __u32 regl;
        int fd, ret;
        char *device;
-       char lbuf[10];
        const char *str;
 
        if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
        }
 
        if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-               printf("eMMC Firmware Version: %s\n", lbuf);
+               printf("eMMC Firmware Version: %.8s\n",
+ (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
                printf("eMMC Life Time Estimation A
 [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
                printf("eMMC Life Time Estimation B

[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--
2.32.0

#10 lore raw

Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version

Hello Avri!

30.11.21 09:42, Avri Altman пише:

Quoted 6 lines

Hi,

Quoted 3 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

You are reverting commit 0eea71e4f2 (mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.

I'm not reverting this commit.
It's similar but not.

Quoted 2 lines

You might also would like to take a look at the correspondence concerning a different approach to the same issue here - https://lore.kernel.org/lkml/[email protected]/

Thank you.
I didn't know that.

Quoted 41 lines

Thanks,
Avri

Quoted 37 lines
---
  mmc_cmds.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)

PATCH v4
Update commit message.

mmc_cmds.c +2 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
         __u32 regl;
         int fd, ret;
         char *device;
-       char lbuf[10];
         const char *str;
 
         if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
         }
 
         if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-               printf("eMMC Firmware Version: %s\n", lbuf);
+               printf("eMMC Firmware Version: %.8s\n",
+ (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
                 printf("eMMC Life Time Estimation A
 [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
                         ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
                 printf("eMMC Life Time Estimation B

[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--
2.32.0

#11 lore raw

Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version

Quoted 5 lines · Oleh Kravchenko

Hello Avri!

30.11.21 09:42, Avri Altman пише:

Quoted 1 line

Hi,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

You are reverting commit 0eea71e4f2 (mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.

I'm not reverting this commit.
It's similar but not.

Right, this time we limit the print to 8 chars, which avoids garbage from a non-NULL terminated string.

Perhaps we should add (similar to what we do for kernel commits):

Fixes 0eea71e4f22a ("mmc-utils: Fix for Firmware Version string printing")

[...]

Kind regards
Uffe

#12 lore raw

Re: [PATCH v4] mmc-utils: Use printf() to extract and print fw version

Hello Ulf,

30.11.21 14:14, Ulf Hansson пише:

Quoted 10 lines · Oleh Kravchenko
Quoted 9 lines

Hello Avri!

30.11.21 09:42, Avri Altman пише:

Quoted 6 lines

Hi,

Quoted 3 lines

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

You are reverting commit 0eea71e4f2 (mmc-utils: Fix for Firmware Version string printing). Please use git revert and add an explanation in your commit log.

Quoted 7 lines
Quoted 2 lines

I'm not reverting this commit.
It's similar but not.

Right, this time we limit the print to 8 chars, which avoids garbage from a non-NULL terminated string.

Perhaps we should add (similar to what we do for kernel commits):

Fixes 0eea71e4f22a ("mmc-utils: Fix for Firmware Version string printing")

Good point!

Quoted 5 lines

[...]

Kind regards
Uffe

#13 lore raw

PATCH v5 1/1 mmc-utils: Use printf() to extract and print fw version

In reply to Oleh Kravchenko

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

Fixes 0eea71e4f22a (mmc-utils: Fix for Firmware Version string printing)
---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

PATCH v5
Updating commit message.
Adding Fixes: 0eea71e4f22a

mmc_cmds.c +1 -4
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
 	__u32 regl;
 	int fd, ret;
 	char *device;
-	char lbuf[10];
 	const char *str;
 
 	if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
 	}
 
 	if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-		strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-		printf("eMMC Firmware Version: %s\n", lbuf);
+		printf("eMMC Firmware Version: %.8s\n", (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
 		printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
 		printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
Signature · 1 line
2.32.0
Applied
#14 lore raw

Re: [PATCH v5] mmc-utils: Use printf() to extract and print fw version

Quoted 8 lines · Oleh Kravchenko

This patch also fixes a compile error with a newer version of GCC:
error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 511 [-Werror=stringop-truncation]

Fixes 0eea71e4f22a (mmc-utils: Fix for Firmware Version string printing)

Applied to git.kernel.org/pub/scm/utils/mmc/mmc-utils.git master, thanks!

Kind regards
Uffe

Quoted 34 lines
---
 mmc_cmds.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

PATCH v5
Updating commit message.
Adding Fixes: 0eea71e4f22a

mmc_cmds.c +1 -5
index 73bd32a..e07ec94 100644
@@ -1392,7 +1392,6 @@ int do_read_extcsd(int nargs, char **argv)
        __u32 regl;
        int fd, ret;
        char *device;
-       char lbuf[10];
        const char *str;
 
        if (nargs != 2) {
@@ -1834,9 +1833,7 @@ int do_read_extcsd(int nargs, char **argv)
        }
 
        if (ext_csd_rev >= 7) {
-                memset(lbuf, 0, sizeof(lbuf));
-               strncpy(lbuf, (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION], 8);
-               printf("eMMC Firmware Version: %s\n", lbuf);
+               printf("eMMC Firmware Version: %.8s\n", (char*)&ext_csd[EXT_CSD_FIRMWARE_VERSION]);
                printf("eMMC Life Time Estimation A [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]: 0x%02x\n",
                        ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
                printf("eMMC Life Time Estimation B [EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]: 0x%02x\n",
--

2.32.0