Skip to contentSkip to conversation

gfs2: fix overread in the strlcpy of init_names

PATCH In review Dongliang Mu 3 participants 4 messages Jun 28, 2022 – Jun 29, 2022 GFS2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gfs2: fix overread in the strlcpy of init_names

From: Dongliang Mu <[email protected]>

In init_names, strlcpy will overread the src string as the src string is less than GFS2_FSNAME_LEN(256).

Fix this by modifying strlcpy back to snprintf, reverting the commit 00377d8e3842.

Fixes 00377d8e3842 ("[GFS2] Prefer strlcpy() over snprintf()")
---
 fs/gfs2/ops_fstype.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/gfs2/ops_fstype.c +2 -2
index c9b423c874a3..ee29b50d39b9 100644
@@ -383,8 +383,8 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
 	if (!table[0])
 		table = sdp->sd_vfs->s_id;
 
-	strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
-	strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
+	snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
+	snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
 
 	table = sdp->sd_table_name;
 	while ((table = strchr(table, '/')))
Signature · 1 line
2.35.1
#1 lore raw

Dongliang Mu,

Quoted 7 lines · Dongliang Mu

From: Dongliang Mu <[email protected]>

In init_names, strlcpy will overread the src string as the src string is less than GFS2_FSNAME_LEN(256).

Fix this by modifying strlcpy back to snprintf, reverting the commit 00377d8e3842.

... if the source string isn't NULL-terminated. But in that case, the code will still do the same thing with this patch. In other words, this doesn't fix anything. So let's check for NULL termination instead.

Thanks,
Andreas

Quoted 25 lines
Fixes 00377d8e3842 ("[GFS2] Prefer strlcpy() over snprintf()")
---
 fs/gfs2/ops_fstype.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/gfs2/ops_fstype.c +2 -3
index c9b423c874a3..ee29b50d39b9 100644
@@ -383,8 +383,8 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
        if (!table[0])
                table = sdp->sd_vfs->s_id;
 
-       strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
-       strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
+       snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
+       snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
 
        table = sdp->sd_table_name;
        while ((table = strchr(table, '/')))
--

2.35.1

#2 lore raw
Quoted 9 lines · Andreas Gruenbacher

Dongliang Mu,

Quoted 5 lines · Dongliang Mu

From: Dongliang Mu <[email protected]>

In init_names, strlcpy will overread the src string as the src string is less than GFS2_FSNAME_LEN(256).

Quoted 7 lines
Quoted 2 lines

Fix this by modifying strlcpy back to snprintf, reverting the commit 00377d8e3842.

... if the source string isn't NULL-terminated. But in that case, the code will still do the same thing with this patch. In other words, this doesn't fix anything. So let's check for NULL termination instead.

Partially yes. Even if the source string is NULL-terminated, strlcpy will invoke memcpy to overread the adjacent memory of source string as the specified length is longer than source string.

Quoted 30 lines

Thanks,
Andreas

Quoted 25 lines
Fixes 00377d8e3842 ("[GFS2] Prefer strlcpy() over snprintf()")
---
 fs/gfs2/ops_fstype.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/gfs2/ops_fstype.c +2 -3
index c9b423c874a3..ee29b50d39b9 100644
@@ -383,8 +383,8 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
        if (!table[0])
                table = sdp->sd_vfs->s_id;
 
-       strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
-       strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
+       snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
+       snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
 
        table = sdp->sd_table_name;
        while ((table = strchr(table, '/')))
--

2.35.1

#3 lore raw
Quoted 14 lines · Dongliang Mu
Quoted 12 lines · Andreas Gruenbacher

Dongliang Mu,

Quoted 7 lines · Dongliang Mu

From: Dongliang Mu <[email protected]>

In init_names, strlcpy will overread the src string as the src string is less than GFS2_FSNAME_LEN(256).

Fix this by modifying strlcpy back to snprintf, reverting the commit 00377d8e3842.

Quoted 8 lines
Quoted 4 lines

... if the source string isn't NULL-terminated. But in that case, the code will still do the same thing with this patch. In other words, this doesn't fix anything. So let's check for NULL termination instead.

Partially yes. Even if the source string is NULL-terminated, strlcpy will invoke memcpy to overread the adjacent memory of source string as the specified length is longer than source string.

The above statement is incorrect after I double check the implementation of strlcpy.

The correct fix should be NULL-termination check of src string.

Quoted 31 lines
Quoted 31 lines

Thanks,
Andreas

Quoted 25 lines
Fixes 00377d8e3842 ("[GFS2] Prefer strlcpy() over snprintf()")
---
 fs/gfs2/ops_fstype.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/gfs2/ops_fstype.c +2 -3
index c9b423c874a3..ee29b50d39b9 100644
@@ -383,8 +383,8 @@ static int init_names(struct gfs2_sbd *sdp, int silent)
        if (!table[0])
                table = sdp->sd_vfs->s_id;
 
-       strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
-       strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
+       snprintf(sdp->sd_proto_name, GFS2_FSNAME_LEN, "%s", proto);
+       snprintf(sdp->sd_table_name, GFS2_FSNAME_LEN, "%s", table);
 
        table = sdp->sd_table_name;
        while ((table = strchr(table, '/')))
--

2.35.1