From e15229f05506ecdac1348878ded1ccbf1b484f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 17 Apr 2023 07:16:33 +0000 Subject: [PATCH] bpfloader: use non-overwriting rename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is for better error reporting. The target should never exist, unless somehow someone managed to cause naming collision... See: https://manpages.debian.org/testing/manpages-dev/renameat2.2.en.html which mentions support was added for bpffs in Linux 4.9 Bug: 236707886 Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: Ic69ff777bbd2e77a4605477c3196a234f04d3bde --- libbpf_android/Loader.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index ae2a60f..a8ef1a0 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -17,6 +17,7 @@ #define LOG_TAG "LibBpfLoader" #include +#include #include #include #include @@ -901,7 +902,8 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err)); return -err; } - ret = rename(createLoc.c_str(), mapPinLoc.c_str()); + ret = renameat2(AT_FDCWD, createLoc.c_str(), + AT_FDCWD, mapPinLoc.c_str(), RENAME_NOREPLACE); if (ret) { int err = errno; ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), mapPinLoc.c_str(), ret, @@ -1153,7 +1155,8 @@ static int loadCodeSections(const char* elfPath, vector& cs, const ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err)); return -err; } - ret = rename(createLoc.c_str(), progPinLoc.c_str()); + ret = renameat2(AT_FDCWD, createLoc.c_str(), + AT_FDCWD, progPinLoc.c_str(), RENAME_NOREPLACE); if (ret) { int err = errno; ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), progPinLoc.c_str(), ret,