Skip to contentSkip to conversation

ibstat.c: fix buffer-not-null-terminated

PATCH Applied Hal Rosenstock 2 participants 6 messages Jun 12, 2013 5 messages not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 2/6 ibstat.c: fix buffer-not-null-terminated

From: Dan Ben Yosef <[email protected]>

Buffer may not have a null terminator if the source string's length is equal to the buffer size.

---
 src/ibstat.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/ibstat.c +2 -1
index 665bb0a..acb9e8e 100644
@@ -279,6 +279,7 @@ int main(int argc, char *argv[])
 	int dev_port = -1;
 	int n, i;
 
+	memset(names, 0, sizeof(names[0][0] * UMAD_MAX_DEVICES * UMAD_CA_NAME_LEN));
 	const struct ibdiag_opt opts[] = {
 		{"list_of_cas", 'l', 0, NULL, "list all IB devices"},
 		{"short", 's', 0, NULL, "short output"},
@@ -314,7 +315,7 @@ int main(int argc, char *argv[])
 		if (i >= n)
 			IBPANIC("'%s' IB device can't be found", argv[0]);
 
-		strncpy(names[i], argv[0], sizeof names[i]);
+		strncpy(names[i], argv[0], sizeof names[i]-1);
 		n = 1;
 	}
 
Signature · 6 lines
1.7.8.2

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 51B87DD4.9040005-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org

#2 lore raw
Quoted 4 lines · On Wed, 12 Jun 2013 09:55:32 -0400 Hal Rosenstock…

From: Dan Ben Yosef <[email protected]>

Buffer may not have a null terminator if the source string's length is equal to the buffer size.

Good catch, but I think this is an issue in umad_get_cas_names!

Quoted 12 lines
---
 src/ibstat.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/ibstat.c +0 -0
index 665bb0a..acb9e8e 100644

src/ibstat.c int main(int argc, char *argv[]) · line 282

Quoted 4 lines
src/ibstat.c +1 -0
@@ -279,6 +279,7 @@ int main(int argc, char *argv[])
 	int dev_port = -1;
 	int n, i;
 
+	memset(names, 0, sizeof(names[0][0] * UMAD_MAX_DEVICES * UMAD_CA_NAME_LEN));

I don't think you need this as it will not fix umad_get_cas_names.

src/ibstat.c int main(int argc, char *argv[]) · line 318

Quoted 9 lines
src/ibstat.c +1 -1
 	const struct ibdiag_opt opts[] = {
 		{"list_of_cas", 'l', 0, NULL, "list all IB devices"},
 		{"short", 's', 0, NULL, "short output"},
@@ -314,7 +315,7 @@ int main(int argc, char *argv[])
 		if (i >= n)
 			IBPANIC("'%s' IB device can't be found", argv[0]);
 
-		strncpy(names[i], argv[0], sizeof names[i]);
+		strncpy(names[i], argv[0], sizeof names[i]-1);

This is actually dead code. IBPANIC exits, if your linking to libibmad. Do you have a different IBPANIC which does not exit? Do you have a use case which hits this bug?

[root@iqa-136 sbin]# ./ibstat myverylongca_name_0123456789_0123456789
ibpanic: [9262] main: 'myverylongca_name_0123456789_0123456789' IB device can't be found: Success [root@iqa-136 sbin]# ./ibstat notfoundca
ibpanic: [9273] main: 'notfoundca' IB device can't be found: Success

Ira

Quoted 6 lines
src/ibstat.c +0 -0
 		n = 1;
 	}
 
Signature · 1 line
1.7.8.2
Signature · 5 lines
Ira Weiny <[email protected]>
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20130612141948.c4760f2856d2e4435be2b0a7-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#4 lore raw
Quoted 25 lines · Ira Weiny
Quoted 4 lines · On Wed, 12 Jun 2013 09:55:32 -0400 Hal Rosenstock…

From: Dan Ben Yosef <[email protected]>

Buffer may not have a null terminator if the source string's length is equal to the buffer size.

Good catch, but I think this is an issue in umad_get_cas_names!

Quoted 15 lines
---
 src/ibstat.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/ibstat.c +0 -0
index 665bb0a..acb9e8e 100644
@@ -279,6 +279,7 @@ int main(int argc, char *argv[])
 	int dev_port = -1;
 	int n, i;
 
Quoted 3 lines

src/ibstat.c int main(int argc, char *argv[]) · line 282

Quoted 1 line
src/ibstat.c +1 -0
+	memset(names, 0, sizeof(names[0][0] * UMAD_MAX_DEVICES * UMAD_CA_NAME_LEN));

I don't think you need this as it will not fix umad_get_cas_names.

The proper memset is already in the tree.

Quoted 12 lines

src/ibstat.c int main(int argc, char *argv[]) · line 318

Quoted 9 lines
src/ibstat.c +1 -1
 	const struct ibdiag_opt opts[] = {
 		{"list_of_cas", 'l', 0, NULL, "list all IB devices"},
 		{"short", 's', 0, NULL, "short output"},
@@ -314,7 +315,7 @@ int main(int argc, char *argv[])
 		if (i >= n)
 			IBPANIC("'%s' IB device can't be found", argv[0]);
 
-		strncpy(names[i], argv[0], sizeof names[i]);
+		strncpy(names[i], argv[0], sizeof names[i]-1);

This is actually dead code. IBPANIC exits, if your linking to libibmad. Do you have a different IBPANIC which does not exit? Do you have a use case which hits this bug?

Quoted 4 lines

[root@iqa-136 sbin]# ./ibstat myverylongca_name_0123456789_0123456789
ibpanic: [9262] main: 'myverylongca_name_0123456789_0123456789' IB device can't be found: Success [root@iqa-136 sbin]# ./ibstat notfoundca
ibpanic: [9273] main: 'notfoundca' IB device can't be found: Success

I think the change is to quiet a Coverity detected error but Dan is the definitive source for this change.

-- Hal

Quoted 10 lines

Ira

Quoted 6 lines
src/ibstat.c +0 -0
 		n = 1;
 	}
 
Signature · 1 line
1.7.8.2

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 51B8F03C.8010004-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org

#6 lore raw
Quoted 26 lines · Hal Rosenstock
Quoted 25 lines · Ira Weiny
Quoted 4 lines · On Wed, 12 Jun 2013 09:55:32 -0400 Hal Rosenstock…

From: Dan Ben Yosef <[email protected]>

Buffer may not have a null terminator if the source string's length is equal to the buffer size.

Good catch, but I think this is an issue in umad_get_cas_names!

Quoted 15 lines
---
 src/ibstat.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/ibstat.c +0 -0
index 665bb0a..acb9e8e 100644
@@ -279,6 +279,7 @@ int main(int argc, char *argv[])
 	int dev_port = -1;
 	int n, i;
 
Quoted 5 lines
Quoted 3 lines

src/ibstat.c int main(int argc, char *argv[]) · line 282

Quoted 1 line
src/ibstat.c +1 -0
+	memset(names, 0, sizeof(names[0][0] * UMAD_MAX_DEVICES * UMAD_CA_NAME_LEN));

I don't think you need this as it will not fix umad_get_cas_names.

The proper memset is already in the tree.

It isn't in the tree; I was looking at modified source...

Quoted 17 lines
Quoted 12 lines

src/ibstat.c int main(int argc, char *argv[]) · line 318

Quoted 9 lines
src/ibstat.c +1 -1
 	const struct ibdiag_opt opts[] = {
 		{"list_of_cas", 'l', 0, NULL, "list all IB devices"},
 		{"short", 's', 0, NULL, "short output"},
@@ -314,7 +315,7 @@ int main(int argc, char *argv[])
 		if (i >= n)
 			IBPANIC("'%s' IB device can't be found", argv[0]);
 
-		strncpy(names[i], argv[0], sizeof names[i]);
+		strncpy(names[i], argv[0], sizeof names[i]-1);

This is actually dead code. IBPANIC exits, if your linking to libibmad. Do you have a different IBPANIC which does not exit? Do you have a use case which hits this bug?

Quoted 4 lines

[root@iqa-136 sbin]# ./ibstat myverylongca_name_0123456789_0123456789
ibpanic: [9262] main: 'myverylongca_name_0123456789_0123456789' IB device can't be found: Success [root@iqa-136 sbin]# ./ibstat notfoundca
ibpanic: [9273] main: 'notfoundca' IB device can't be found: Success

Quoted 16 lines

I think the change is to quiet a Coverity detected error but Dan is the definitive source for this change.

-- Hal

Quoted 10 lines

Ira

Quoted 6 lines
src/ibstat.c +0 -0
 		n = 1;
 	}
 
Signature · 1 line
1.7.8.2

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 13 lines

-----Original Message-----
From: Hal Rosenstock [mailto:[email protected]]
Subject: Re: [PATCH infiniband-diags 2/6] ibstat.c: fix buffer-not-null- terminated

Quoted 7 lines · Hal Rosenstock
Quoted 6 lines · Ira Weiny
Quoted 3 lines · On Wed, 12 Jun 2013 09:55:32 -0400 Hal Rosenstock…

From: Dan Ben Yosef <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	memset(names, 0, sizeof(names[0][0] * UMAD_MAX_DEVICES *
+UMAD_CA_NAME_LEN));

I don't think you need this as it will not fix umad_get_cas_names.

The proper memset is already in the tree.

It isn't in the tree; I was looking at modified source...

Sounds like that should be pushed upstream! ;-)

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +0 -0
 	const struct ibdiag_opt opts[] = {
 		{"list_of_cas", 'l', 0, NULL, "list all IB devices"},
 		{"short", 's', 0, NULL, "short output"}, @@ -314,7 +315,7 @@

int

Quoted 8 lines
Quoted 8 lines

Review

Quoted 6 lines

main(int argc, char *argv[])

(quoted diff) +1 -1
 		if (i >= n)
 			IBPANIC("'%s' IB device can't be found", argv[0]);
 
-		strncpy(names[i], argv[0], sizeof names[i]);
+		strncpy(names[i], argv[0], sizeof names[i]-1);

This is actually dead code. IBPANIC exits, if your linking to libibmad. Do

Quoted 12 lines

you have a different IBPANIC which does not exit? Do you have a use case which hits this bug?

Quoted 2 lines
Quoted 2 lines

[root@iqa-136 sbin]# ./ibstat

myverylongca_name_0123456789_0123456789

Quoted 7 lines
Quoted 4 lines

ibpanic: [9262] main: 'myverylongca_name_0123456789_0123456789' IB device can't be found: Success
[root@iqa-136 sbin]# ./ibstat notfoundca
ibpanic: [9273] main: 'notfoundca' IB device can't be found: Success

I think the change is to quiet a Coverity detected error but Dan is the definitive source for this change.

I think we should remove the dead code and that should fix Coverity. I thought it detected dead code. I wonder if I missed something?

Ira

Quoted 8 lines
Quoted 8 lines

-- Hal

Quoted 5 lines

Ira

Quoted 3 lines
(quoted diff) +0 -0
 		n = 1;
 	}
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

--
1.7.8.2

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 2807E5FD2F6FDA4886F6618EAC48510E0207E124-8k97q/[email protected]

#10 lore raw
Quoted 11 lines

-----Original Message-----
From: [email protected] [mailto:linux-rdma-
Subject: RE: [PATCH infiniband-diags 2/6] ibstat.c: fix buffer-not-null- terminated

Quoted 3 lines
Quoted 3 lines

I think the change is to quiet a Coverity detected error but Dan is the definitive source for this change.

I think we should remove the dead code and that should fix Coverity. I thought it detected dead code. I wonder if I missed something?

That is not dead code. I misread where the break jumps to.

However, the code is redundant and there is a bug.[*]

16:16:51 > ibstat -l
mlx4_0
mlx4_1
16:17:57 > ibstat -l mlx4_1
mlx4_0

The intent was to copy argv[0] into names[0] and set n=1. But that is not happening...

Ira

[*] not that the above is really a valid use case.

Quoted 18 lines

Ira

Quoted 8 lines
Quoted 8 lines

-- Hal

Quoted 5 lines

Ira

Quoted 3 lines
(quoted diff) +0 -0
 		n = 1;
 	}
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

--
1.7.8.2

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html