Skip to contentSkip to conversation

examples/vhost_blk: fix the TOCTOU

PATCH Superseded Jin Yu 3 participants 8 messages Nov 26, 2019 – Feb 13, 2020

lore
Share & tools
lore

Jin Yu

author
#0 lore raw

PATCH 1/1 examples/vhost_blk: fix the TOCTOU

Fix the time of check time of use warning in example code

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
examples/vhost_blk/vhost_blk.c +2 -7
index 3182a488b..bcb4ebb0b 100644
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 
 	if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
 		fprintf(stderr, "socket %s already exists\n", dev_pathname);
@@ -1040,8 +1036,7 @@ signal_handler(__rte_unused int signum)
 {
 	struct vhost_blk_ctrlr *ctrlr;
 
-	if (access(dev_pathname, F_OK) == 0)
-		unlink(dev_pathname);
+	unlink(dev_pathname);
 
 	if (g_should_stop != -1) {
 		g_should_stop = 1;
Signature · 1 line
2.17.2
#1 lore raw
Quoted 16 lines · Jin Yu

Fix the time of check time of use warning in example code

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
examples/vhost_blk/vhost_blk.c +0 -0
index 3182a488b..bcb4ebb0b 100644

examples/vhost_blk/vhost_blk.c vhost_blk_ctrlr_construct(const char *ctrlr_name) · line 997

Quoted 10 lines
examples/vhost_blk/vhost_blk.c +1 -5
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 

The original code did an exit if the delete failed, do you intend there to be a behaviour change here? You can probably get the same behaviour if you check the errno on an unlink failure, e.g. ENOENT means file doesn't exist.

If not having the app exit on unlink failure is reasonable behaviour then ignore this comment.

Regards,
/Bruce

author
#2 lore raw
Quoted 43 lines

-----Original Message-----
From: Bruce Richardson <[email protected]>
Sent: Tuesday, November 26, 2019 6:26 PM
To: Yu, Jin <[email protected]>
Cc: Maxime Coquelin <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: fix the TOCTOU

Quoted 18 lines · Jin Yu

Fix the time of check time of use warning in example code

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")
Cc: [email protected]

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/examples/vhost_blk/vhost_blk.c index 3182a488b..bcb4ebb0b 100644

examples/vhost_blk/vhost_blk.c +0 -0
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}

examples/vhost_blk/vhost_blk.c vhost_blk_ctrlr_construct(const char *ctrlr_name) · line 998

Quoted 10 lines
examples/vhost_blk/vhost_blk.c +1 -5
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path,
 ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 

The original code did an exit if the delete failed, do you intend there to be a behaviour change here? You can probably get the same behaviour if you check the errno on an unlink failure, e.g. ENOENT means file doesn't exist.

Quoted 2 lines

If not having the app exit on unlink failure is reasonable behaviour then ignore this comment.

I considered it. I think it's ok to ignore the errno of unlink failure. This code just want to remove the file. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too so I think it's ok to ignore too.

Quoted 3 lines

Regards,
/Bruce

#3 lore raw
Quoted 43 lines · Jin
Quoted 43 lines

-----Original Message-----
From: Bruce Richardson <[email protected]>
Sent: Tuesday, November 26, 2019 6:26 PM
To: Yu, Jin <[email protected]>
Cc: Maxime Coquelin <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: fix the TOCTOU

Quoted 18 lines · Jin Yu

Fix the time of check time of use warning in example code

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")
Cc: [email protected]

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/examples/vhost_blk/vhost_blk.c index 3182a488b..bcb4ebb0b 100644

examples/vhost_blk/vhost_blk.c +0 -0
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}

examples/vhost_blk/vhost_blk.c vhost_blk_ctrlr_construct(const char *ctrlr_name) · line 998

Quoted 10 lines
examples/vhost_blk/vhost_blk.c +1 -5
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path,
 ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 

The original code did an exit if the delete failed, do you intend there to be a behaviour change here? You can probably get the same behaviour if you check the errno on an unlink failure, e.g. ENOENT means file doesn't exist.

Quoted 8 lines
Quoted 2 lines

If not having the app exit on unlink failure is reasonable behaviour then ignore this comment.

I considered it. I think it's ok to ignore the errno of unlink failure. This code just want to remove the file. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too so I think it's ok to ignore too.

Quoted 1 line

Ok, thanks for clarifying.

#4 lore raw
In reply to Yu, Jin
Quoted 43 lines · Jin
Quoted 43 lines

-----Original Message-----
From: Bruce Richardson <[email protected]>
Sent: Tuesday, November 26, 2019 6:26 PM
To: Yu, Jin <[email protected]>
Cc: Maxime Coquelin <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: fix the TOCTOU

Quoted 18 lines · Jin Yu

Fix the time of check time of use warning in example code

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")
Cc: [email protected]

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/examples/vhost_blk/vhost_blk.c index 3182a488b..bcb4ebb0b 100644

examples/vhost_blk/vhost_blk.c +0 -0
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}

examples/vhost_blk/vhost_blk.c vhost_blk_ctrlr_construct(const char *ctrlr_name) · line 998

Quoted 10 lines
examples/vhost_blk/vhost_blk.c +1 -5
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path,
 ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 

The original code did an exit if the delete failed, do you intend there to be a behaviour change here? You can probably get the same behaviour if you check the errno on an unlink failure, e.g. ENOENT means file doesn't exist.

Quoted 7 lines
Quoted 2 lines

If not having the app exit on unlink failure is reasonable behaviour then ignore this comment.

I considered it. I think it's ok to ignore the errno of unlink failure. This code just want to remove the file. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too so I think it's ok to ignore too.

That's fine by me, but please could you mention it in the commit message?

Quoted 3 lines
Quoted 2 lines

Regards,
/Bruce

author
#5 lore raw

PATCH v2 1/1 examples/vhost_blk: fix the TOCTOU

In reply to Jin Yu

Fix the time of check time of use warning in example code. Ignore the errno of unlink failure. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too.

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")

---
V2 - complement the commit message.

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
examples/vhost_blk/vhost_blk.c +2 -7
index 3182a488b..bcb4ebb0b 100644
@@ -993,11 +993,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 
 	if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
 		fprintf(stderr, "socket %s already exists\n", dev_pathname);
@@ -1040,8 +1036,7 @@ signal_handler(__rte_unused int signum)
 {
 	struct vhost_blk_ctrlr *ctrlr;
 
-	if (access(dev_pathname, F_OK) == 0)
-		unlink(dev_pathname);
+	unlink(dev_pathname);
 
 	if (g_should_stop != -1) {
 		g_should_stop = 1;
Signature · 1 line
2.17.2
author
#6 lore raw

PATCH v2 1/1 examples/vhost_blk: fix the TOCTOU

Fix the time of check time of use warning in example code. Ignore the errno of unlink failure. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too.

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")

---
V2 - complement the commit message.

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
examples/vhost_blk/vhost_blk.c +2 -7
index e1036bf3a..74c82a900 100644
@@ -994,11 +994,7 @@ vhost_blk_ctrlr_construct(const char *ctrlr_name)
 	}
 	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
 
-	if (access(dev_pathname, F_OK) != -1) {
-		if (unlink(dev_pathname) != 0)
-			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
-				 dev_pathname);
-	}
+	unlink(dev_pathname);
 
 	if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
 		fprintf(stderr, "socket %s already exists\n", dev_pathname);
@@ -1041,8 +1037,7 @@ signal_handler(__rte_unused int signum)
 {
 	struct vhost_blk_ctrlr *ctrlr;
 
-	if (access(dev_pathname, F_OK) == 0)
-		unlink(dev_pathname);
+	unlink(dev_pathname);
 
 	if (g_should_stop != -1) {
 		g_should_stop = 1;
Signature · 1 line
2.17.2
#7 lore raw
In reply to Jin Yu
Quoted 10 lines · Jin Yu

Fix the time of check time of use warning in example code. Ignore the errno of unlink failure. There are two situations. The first one is that file doesn't exist the unlink fails and it's ok to ignore. The second one is that unlink fails to remove file but the next bind() would fail too.

Coverity issue: 350589 158663

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
Quoted 7 lines

---
V2 - complement the commit message.

---
 examples/vhost_blk/vhost_blk.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

Applied to dpdk-next-virtio/master

Thanks,
Maxime