From 56b3befcd10f09b441979ee733cee5277f3328df Mon Sep 17 00:00:00 2001 From: zhangcui Date: Tue, 17 Jan 2023 08:56:49 +0000 Subject: [PATCH 1/7] update Signed-off-by: zhangcui --- src/symbols_file.cpp | 15 ++++++++++++--- .../common/native/symbols_file_test.cpp | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/symbols_file.cpp b/src/symbols_file.cpp index f082024..5b2e4df 100644 --- a/src/symbols_file.cpp +++ b/src/symbols_file.cpp @@ -949,10 +949,12 @@ public: printf("No vmlinux path is given, and kallsyms cannot be opened\n"); return false; } - - if (ReadFileToString(KPTR_RESTRICT).front() != '0') { + bool ret = false; + std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); + if (oldKptrRestrict.front() != '0') { printf("/proc/sys/kernel/kptr_restrict is NOT 0, will try set it to 0.\n"); - if (!WriteStringToFile(KPTR_RESTRICT, "0")) { + ret = WriteStringToFile(KPTR_RESTRICT, "0"); + if (!ret) { printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); } } @@ -962,6 +964,13 @@ public: return false; } + if (ret) { + ret = WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict); + if (!ret) { + printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); + } + } + if (symbols_.empty()) { printf("The symbol table addresses in /proc/kallsyms are all 0.\n" "Please check the value of /proc/sys/kernel/kptr_restrict, it " diff --git a/test/unittest/common/native/symbols_file_test.cpp b/test/unittest/common/native/symbols_file_test.cpp index 330a37a..bddb0f2 100644 --- a/test/unittest/common/native/symbols_file_test.cpp +++ b/test/unittest/common/native/symbols_file_test.cpp @@ -14,6 +14,7 @@ */ #include "symbols_file_test.h" +#include "utilities.h" #include #include @@ -274,14 +275,11 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1) // read from kernel runtime std::unique_ptr symbolsFile = SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_FILE); ScopeDebugLevel tempLogLevel(LEVEL_VERBOSE); + std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); ASSERT_EQ(symbolsFile->LoadSymbols(), true); const std::vector &symbols = symbolsFile->GetSymbols(); - if (KptrRestrict()) { - EXPECT_EQ(symbols.empty(), true); - } else { - EXPECT_EQ(symbols.empty(), false); - } + EXPECT_EQ(symbols.empty(), false); std::string modulesMap = ReadFileToString("/proc/modules"); int lines = std::count(modulesMap.begin(), modulesMap.end(), '\n'); @@ -309,6 +307,11 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1) EXPECT_EQ(TestLoadSymbols(SYMBOL_KERNEL_FILE, TEST_FILE_VMLINUX_STRIPPED_NOBUILDID), true); // will be load from runtime, still return true EXPECT_EQ(TestLoadSymbols(SYMBOL_KERNEL_FILE, TEST_FILE_VMLINUX_STRIPPED_BROKEN), true); + if (ReadFileToString(KPTR_RESTRICT) != oldKptrRestrict) { + if (!WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict)) { + printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); + } + } } /** @@ -371,6 +374,7 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1) CheckSymbols(symbols); } else { EXPECT_EQ(symbols->LoadSymbols(), true); + std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); if (!KptrRestrict()) { HLOGD("NOT KptrRestrict"); if (!symbols->GetSymbols().empty()) { @@ -382,6 +386,11 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1) HLOGD("KptrRestrict"); ASSERT_EQ(symbols->GetSymbols().empty(), true); } + if (ReadFileToString(KPTR_RESTRICT) != oldKptrRestrict) { + if (!WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict)) { + printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); + } + } } } From 65e4d1a18c4ad8758fa77c16cde85f6fbae34654 Mon Sep 17 00:00:00 2001 From: zhangcui Date: Wed, 18 Jan 2023 09:19:11 +0000 Subject: [PATCH 2/7] update Signed-off-by: zhangcui --- script/recv_binary_cache.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/script/recv_binary_cache.py b/script/recv_binary_cache.py index 9f91800..4d35e8a 100644 --- a/script/recv_binary_cache.py +++ b/script/recv_binary_cache.py @@ -193,9 +193,16 @@ class GetLibFiles(object): if os.path.isfile(file_path): os.remove(file_path) if self.hdc.switch_root(): + os.system('hdc shell cat /proc/sys/kernel/kptr_restrict > tmp.txt') + file_object = open('./tmp.txt','r') + text = file_object.read() + file_object.close() + os.remove('./tmp.txt') self.hdc.run_hdc_cmd(['shell', '"echo 0 >/proc/sys/kernel/kptr_restrict"']) self.hdc.run_hdc_cmd(['file recv', '/proc/kallsyms', file_path]) + self.hdc.run_hdc_cmd(['shell', + '"echo ' + text[0] + ' >/proc/sys/kernel/kptr_restrict"']) def main(): From fc2d07d30e10c0b6a7f6fc21b6ca5b910ba33387 Mon Sep 17 00:00:00 2001 From: zhangcui Date: Thu, 19 Jan 2023 02:05:44 +0000 Subject: [PATCH 3/7] update Signed-off-by: zhangcui --- script/recv_binary_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/recv_binary_cache.py b/script/recv_binary_cache.py index 4d35e8a..54d110c 100644 --- a/script/recv_binary_cache.py +++ b/script/recv_binary_cache.py @@ -193,8 +193,8 @@ class GetLibFiles(object): if os.path.isfile(file_path): os.remove(file_path) if self.hdc.switch_root(): - os.system('hdc shell cat /proc/sys/kernel/kptr_restrict > tmp.txt') - file_object = open('./tmp.txt','r') + os.system('hdc shell cat /proc/sys/kernel/kptr_restrict > ./tmp.txt') + file_object = open('./tmp.txt', 'r') text = file_object.read() file_object.close() os.remove('./tmp.txt') From 874fc9c3a0914fe0bf4477d1a8abbd9276630eae Mon Sep 17 00:00:00 2001 From: zhangcui Date: Thu, 19 Jan 2023 06:56:06 +0000 Subject: [PATCH 4/7] update Signed-off-by: zhangcui --- src/symbols_file.cpp | 14 +++++++------- test/unittest/common/native/symbols_file_test.cpp | 13 ------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/symbols_file.cpp b/src/symbols_file.cpp index 5b2e4df..d1c3b2c 100644 --- a/src/symbols_file.cpp +++ b/src/symbols_file.cpp @@ -949,12 +949,12 @@ public: printf("No vmlinux path is given, and kallsyms cannot be opened\n"); return false; } - bool ret = false; + bool hasChangeKptr = false; std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); if (oldKptrRestrict.front() != '0') { printf("/proc/sys/kernel/kptr_restrict is NOT 0, will try set it to 0.\n"); - ret = WriteStringToFile(KPTR_RESTRICT, "0"); - if (!ret) { + hasChangeKptr = WriteStringToFile(KPTR_RESTRICT, "0"); + if (!hasChangeKptr) { printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); } } @@ -964,10 +964,10 @@ public: return false; } - if (ret) { - ret = WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict); - if (!ret) { - printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); + if (hasChangeKptr) { + hasChangeKptr = WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict); + if (!hasChangeKptr) { + printf("recover /proc/sys/kernel/kptr_restrict fail.\n"); } } diff --git a/test/unittest/common/native/symbols_file_test.cpp b/test/unittest/common/native/symbols_file_test.cpp index bddb0f2..27a49d2 100644 --- a/test/unittest/common/native/symbols_file_test.cpp +++ b/test/unittest/common/native/symbols_file_test.cpp @@ -14,7 +14,6 @@ */ #include "symbols_file_test.h" -#include "utilities.h" #include #include @@ -275,7 +274,6 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1) // read from kernel runtime std::unique_ptr symbolsFile = SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_FILE); ScopeDebugLevel tempLogLevel(LEVEL_VERBOSE); - std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); ASSERT_EQ(symbolsFile->LoadSymbols(), true); const std::vector &symbols = symbolsFile->GetSymbols(); @@ -307,11 +305,6 @@ HWTEST_F(SymbolsFileTest, LoadKernelSymbols, TestSize.Level1) EXPECT_EQ(TestLoadSymbols(SYMBOL_KERNEL_FILE, TEST_FILE_VMLINUX_STRIPPED_NOBUILDID), true); // will be load from runtime, still return true EXPECT_EQ(TestLoadSymbols(SYMBOL_KERNEL_FILE, TEST_FILE_VMLINUX_STRIPPED_BROKEN), true); - if (ReadFileToString(KPTR_RESTRICT) != oldKptrRestrict) { - if (!WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict)) { - printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); - } - } } /** @@ -374,7 +367,6 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1) CheckSymbols(symbols); } else { EXPECT_EQ(symbols->LoadSymbols(), true); - std::string oldKptrRestrict = ReadFileToString(KPTR_RESTRICT); if (!KptrRestrict()) { HLOGD("NOT KptrRestrict"); if (!symbols->GetSymbols().empty()) { @@ -386,11 +378,6 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1) HLOGD("KptrRestrict"); ASSERT_EQ(symbols->GetSymbols().empty(), true); } - if (ReadFileToString(KPTR_RESTRICT) != oldKptrRestrict) { - if (!WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict)) { - printf("/proc/sys/kernel/kptr_restrict write failed and we can't not change it.\n"); - } - } } } From ef577f7bc5149277ef903945400ebefc948dc695 Mon Sep 17 00:00:00 2001 From: zhangcui Date: Thu, 19 Jan 2023 06:58:55 +0000 Subject: [PATCH 5/7] update Signed-off-by: zhangcui --- src/symbols_file.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/symbols_file.cpp b/src/symbols_file.cpp index d1c3b2c..52f39d5 100644 --- a/src/symbols_file.cpp +++ b/src/symbols_file.cpp @@ -965,8 +965,7 @@ public: } if (hasChangeKptr) { - hasChangeKptr = WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict); - if (!hasChangeKptr) { + if (!WriteStringToFile(KPTR_RESTRICT, oldKptrRestrict)) { printf("recover /proc/sys/kernel/kptr_restrict fail.\n"); } } From b40f63ab52165bfc531ed7aaa97ccddb5b047c0c Mon Sep 17 00:00:00 2001 From: zhangcui Date: Thu, 19 Jan 2023 07:17:26 +0000 Subject: [PATCH 6/7] update Signed-off-by: zhangcui --- script/recv_binary_cache.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/script/recv_binary_cache.py b/script/recv_binary_cache.py index 54d110c..4b2ec83 100644 --- a/script/recv_binary_cache.py +++ b/script/recv_binary_cache.py @@ -193,11 +193,7 @@ class GetLibFiles(object): if os.path.isfile(file_path): os.remove(file_path) if self.hdc.switch_root(): - os.system('hdc shell cat /proc/sys/kernel/kptr_restrict > ./tmp.txt') - file_object = open('./tmp.txt', 'r') - text = file_object.read() - file_object.close() - os.remove('./tmp.txt') + text = os.popen('hdc shell cat /proc/sys/kernel/kptr_restrict').read() self.hdc.run_hdc_cmd(['shell', '"echo 0 >/proc/sys/kernel/kptr_restrict"']) self.hdc.run_hdc_cmd(['file recv', '/proc/kallsyms', file_path]) From 112a551272ff231fcbd4fa2d77a99778aecfe61a Mon Sep 17 00:00:00 2001 From: zhangcui Date: Thu, 19 Jan 2023 07:25:17 +0000 Subject: [PATCH 7/7] update Signed-off-by: zhangcui --- script/recv_binary_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/recv_binary_cache.py b/script/recv_binary_cache.py index 4b2ec83..6e615d3 100644 --- a/script/recv_binary_cache.py +++ b/script/recv_binary_cache.py @@ -193,12 +193,12 @@ class GetLibFiles(object): if os.path.isfile(file_path): os.remove(file_path) if self.hdc.switch_root(): - text = os.popen('hdc shell cat /proc/sys/kernel/kptr_restrict').read() + oldKptrRestrict = os.popen('hdc shell cat /proc/sys/kernel/kptr_restrict').read() self.hdc.run_hdc_cmd(['shell', '"echo 0 >/proc/sys/kernel/kptr_restrict"']) self.hdc.run_hdc_cmd(['file recv', '/proc/kallsyms', file_path]) self.hdc.run_hdc_cmd(['shell', - '"echo ' + text[0] + ' >/proc/sys/kernel/kptr_restrict"']) + '"echo ' + oldKptrRestrict[0] + ' >/proc/sys/kernel/kptr_restrict"']) def main():