netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] lib/bpf: Fix and simplify bpf_mnt_check_target()
@ 2021-02-22 17:43 Andrea Claudi
  0 siblings, 0 replies; only message in thread
From: Andrea Claudi @ 2021-02-22 17:43 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

As stated in commit ac3415f5c1b1 ("lib/fs: Fix and simplify make_path()"),
calling stat() before mkdir() is racey, because the entry might change in
between.

As the call to stat() seems to only check for target existence, we can
simply call mkdir() unconditionally and catch all errors but EEXIST.

Fixes: 95ae9a4870e7 ("bpf: fix mnt path when from env")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 lib/bpf_legacy.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c
index bc869c3f..8a03b9c2 100644
--- a/lib/bpf_legacy.c
+++ b/lib/bpf_legacy.c
@@ -510,20 +510,14 @@ static int bpf_mnt_fs(const char *target)
 
 static int bpf_mnt_check_target(const char *target)
 {
-	struct stat sb = {};
 	int ret;
 
-	ret = stat(target, &sb);
-	if (ret) {
-		ret = mkdir(target, S_IRWXU);
-		if (ret) {
-			fprintf(stderr, "mkdir %s failed: %s\n", target,
-				strerror(errno));
-			return ret;
-		}
-	}
+	ret = mkdir(target, S_IRWXU);
+	if (ret && errno != EEXIST)
+		fprintf(stderr, "mkdir %s failed: %s\n", target,
+			strerror(errno));
 
-	return 0;
+	return ret;
 }
 
 static int bpf_valid_mntpt(const char *mnt, unsigned long magic)
-- 
2.29.2


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-22 17:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22 17:43 [PATCH iproute2] lib/bpf: Fix and simplify bpf_mnt_check_target() Andrea Claudi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).