diff --git a/interfaces/innerkits/include/init_reboot.h b/interfaces/innerkits/include/init_reboot.h index 805f16187a754890734b8f33eb4a0064544203ca..8157e5759de8aedce904ea2d28b7e5e11124ef8e 100644 --- a/interfaces/innerkits/include/init_reboot.h +++ b/interfaces/innerkits/include/init_reboot.h @@ -29,8 +29,26 @@ extern "C" { #define DEVICE_CMD_FREEZE "freeze" #define DEVICE_CMD_RESTORE "restore" +/** + * @brief reboot system + * + * @param option format is: mode[:info] + * mode : command sub key, example: updater, shutdown ... + * info : extend info + * @return int + */ int DoReboot(const char *cmdContent); +/** + * @brief reboot system + * + * @param mode command sub key, example: updater, shutdown ... + * @param option extend info + * + * @return int + */ +int DoRebootExt(const char *mode, const char *option); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/libbegetutil.versionscript b/interfaces/innerkits/libbegetutil.versionscript index a7fe68590086dd16b34082c66cdca557f1d210b3..27e32a270d548b693a5e1eacc46cd6029bf7843f 100644 --- a/interfaces/innerkits/libbegetutil.versionscript +++ b/interfaces/innerkits/libbegetutil.versionscript @@ -48,6 +48,7 @@ CachedParameterGetChanged; CachedParameterDestroy; DoReboot; + DoRebootExt; GetControlSocket; OH_ListAddTail; OH_ListAddWithOrder; diff --git a/interfaces/innerkits/reboot/init_reboot_innerkits.c b/interfaces/innerkits/reboot/init_reboot_innerkits.c index 94e97c8f4c5a893c7bef06c21e4a91db8b73a92f..daf42b1ee536880388cce7d80def5a0cfc4666a4 100644 --- a/interfaces/innerkits/reboot/init_reboot_innerkits.c +++ b/interfaces/innerkits/reboot/init_reboot_innerkits.c @@ -30,23 +30,25 @@ #define DOREBOOT_PARAM "reboot.ut" #endif -static int DoReboot_(const char *option) +static int DoReboot_(const char *mode, const char *option) { char value[MAX_REBOOT_OPTION_SIZE]; int ret = 0; - if (option == NULL || strlen(option) == 0) { - ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP); - BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param"); - ret = SystemSetParameter(DOREBOOT_PARAM, "reboot"); - BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set reboot param"); - return 0; + if (mode != NULL) { + if ((option != NULL) && (strlen(option) >= 0)) { + ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s:%s", mode, option); + } else { + ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", mode); + } + } else { + if ((option != NULL) && (strlen(option) >= 0)) { + ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option); + } else { + ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "%s", "reboot"); + } } - int length = strlen(option); - BEGET_ERROR_CHECK(length <= MAX_REBOOT_OPTION_SIZE, return -1, - "Reboot option \" %s \" is too large, overflow", option); - ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option); - BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to copy boot option \" %s \"", option); - + BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to format boot mode "); + BEGET_LOGV("Reboot cmd %s", value); ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP); BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param"); ret = SystemSetParameter(DOREBOOT_PARAM, value); @@ -54,7 +56,7 @@ static int DoReboot_(const char *option) return 0; } -int DoReboot(const char *option) +static int ExecReboot_(const char *mode, const char *option) { // check if param set ok #ifndef STARTUP_INIT_TEST @@ -63,7 +65,7 @@ int DoReboot(const char *option) const int maxCount = 1; #endif int count = 0; - DoReboot_(option); + DoReboot_(mode, option); while (count < maxCount) { usleep(100 * 1000); // 100 * 1000 wait 100ms char result[10] = {0}; // 10 stop len @@ -74,8 +76,18 @@ int DoReboot(const char *option) return 0; } count++; - DoReboot_(option); + DoReboot_(mode, option); } BEGET_LOGE("Failed to reboot system"); return 0; } + +int DoReboot(const char *option) +{ + return ExecReboot_(NULL, option); +} + +int DoRebootExt(const char *mode, const char *option) +{ + return ExecReboot_(mode, option); +} \ No newline at end of file diff --git a/services/begetctl/init_cmd_reboot.c b/services/begetctl/init_cmd_reboot.c index 364d2dcc3b2ab6c90a311d44f762ca6dab125778..e1c3401707725e1b07a7e35df9c14f3e882a2681 100644 --- a/services/begetctl/init_cmd_reboot.c +++ b/services/begetctl/init_cmd_reboot.c @@ -49,7 +49,7 @@ MODULE_CONSTRUCTOR(void) { CmdInfo infos[] = { {"reboot", main_cmd, "reboot system", "reboot", ""}, - {"reboot", main_cmd, "shutdown system", "reboot shutdown", ""}, + {"reboot", main_cmd, "shutdown system", "reboot shutdown[:options]", ""}, {"reboot", main_cmd, "suspend system", "reboot suspend", ""}, {"reboot", main_cmd, "reboot and boot into updater", "reboot updater", ""}, {"reboot", main_cmd, "reboot and boot into updater", "reboot updater[:options]", ""}, diff --git a/services/modules/reboot/reboot.c b/services/modules/reboot/reboot.c index d6bd933895504a5523e1372cb4b9f5ae38817a99..b61dc96583f002e19461ad6f3a637bfcc56af09c 100644 --- a/services/modules/reboot/reboot.c +++ b/services/modules/reboot/reboot.c @@ -78,8 +78,22 @@ static int DoRebootShutdown(int id, const char *name, int argc, const char **arg UNUSED(name); UNUSED(argc); UNUSED(argv); + PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter"); // clear misc (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL); + const size_t len = strlen("reboot,"); + const char *cmd = strstr(argv[0], "reboot,"); + if (cmd != NULL && strlen(cmd) > len) { + PLUGIN_LOGI("DoRebootShutdown argv %s", cmd + len); + // by job to stop service and unmount + DoJobNow("reboot"); +#ifndef STARTUP_INIT_TEST + return syscall(__NR_reboot, + LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, cmd + len); +#else + return 0; +#endif + } return DoRoot_("reboot", RB_POWER_OFF); } @@ -139,8 +153,12 @@ static int DoRebootOther(int id, const char *name, int argc, const char **argv) const char *cmd = strstr(argv[0], "reboot,"); PLUGIN_CHECK(cmd != NULL, return -1, "Invalid parameter argc %s", argv[0]); PLUGIN_LOGI("DoRebootOther argv %s", argv[0]); +#ifndef STARTUP_INIT_TEST return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, cmd + strlen("reboot,")); +#else + return 0; +#endif } static void RebootAdpInit(void) diff --git a/services/modules/selinux/selinux_adp.c b/services/modules/selinux/selinux_adp.c index 618dcc19d423e6243c176c130dd23761f8e65b93..29fe30b4234de5ba13d6d8f8c94eb0a82d59889c 100755 --- a/services/modules/selinux/selinux_adp.c +++ b/services/modules/selinux/selinux_adp.c @@ -145,6 +145,9 @@ MODULE_CONSTRUCTOR(void) MODULE_DESTRUCTOR(void) { + if (getpid() != 1) { + return; + } PLUGIN_LOGI("Selinux adapter plug-in exit now ..."); SelinuxAdpExit(); } diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index ff9e6768b91d1f060615f62fe23fcc1371bcbae0..2b389a700be5b6d3d6ef65b82ba76600533d93ad 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -52,6 +52,30 @@ ohos_fuzztest("DoRebootFuzzTest") { defines = [ "STARTUP_INIT_TEST" ] } +ohos_fuzztest("DoRebootExtFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/DoRebootExt_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/interfaces/innerkits:libbegetutil", + "//third_party/bounds_checking_function:libsec_static", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "DoRebootExt_fuzzer/DoRebootExt_fuzzer.cpp" ] + defines = [ "STARTUP_INIT_TEST" ] +} + ohos_fuzztest("GetControlFileFuzzTest") { module_out_path = module_output_path fuzz_config_file = "//base/startup/init/test/fuzztest/getcontrolfile_fuzzer" diff --git a/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.cpp b/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1dee270046c0e44212f108611897293b2d2986f4 --- /dev/null +++ b/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DoRebootExt_fuzzer.h" +#include +#include "init_reboot.h" + +namespace OHOS { + bool FuzzDoRebootExt(const uint8_t* data, size_t size) + { + bool result = false; + std::string str(reinterpret_cast(data), size); + if (!DoRebootExt(str.c_str(), str.c_str())) { + result = true; + } + return result; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzDoRebootExt(data, size); + return 0; +} diff --git a/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.h b/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.h new file mode 100755 index 0000000000000000000000000000000000000000..aa9a500a11b67c1c8675ad426dd108f577a366ae --- /dev/null +++ b/test/fuzztest/DoRebootExt_fuzzer/DoRebootExt_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_FUZZTEST_DOREBOOTEXT_FUZZER_H +#define TEST_FUZZTEST_DOREBOOTEXT_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "DoRebootExt_fuzzer" +#endif diff --git a/test/fuzztest/DoRebootExt_fuzzer/corpus/init b/test/fuzztest/DoRebootExt_fuzzer/corpus/init new file mode 100755 index 0000000000000000000000000000000000000000..2b595da0c26af63ffb2d5f4132c086b2e5986fce --- /dev/null +++ b/test/fuzztest/DoRebootExt_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/DoRebootExt_fuzzer/project.xml b/test/fuzztest/DoRebootExt_fuzzer/project.xml new file mode 100755 index 0000000000000000000000000000000000000000..a03232f21591eac33f2e0b55e2902ee4ecf49296 --- /dev/null +++ b/test/fuzztest/DoRebootExt_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/unittest/init/init_reboot_unittest.cpp b/test/unittest/init/init_reboot_unittest.cpp index 09f966e5ca3579732c10643261bf387f66384a2b..b92ed8bf2f351fb4047d7526f298874fefcd5f4c 100644 --- a/test/unittest/init/init_reboot_unittest.cpp +++ b/test/unittest/init/init_reboot_unittest.cpp @@ -99,6 +99,28 @@ HWTEST_F(InitRebootUnitTest, TestAddRebootCmdNormal, TestSize.Level1) SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd"); SystemWriteParam("ohos.startup.powerctrl", "reboot,flashd:1000000"); } + +HWTEST_F(InitRebootUnitTest, TestRebootCmdExec, TestSize.Level1) +{ + PARAM_LOGE("TestRebootCmdExec"); + PluginExecCmdByName("reboot", "reboot"); + PluginExecCmdByName("reboot.shutdown", "reboot,shutdown"); + PluginExecCmdByName("reboot.shutdown", "reboot,shutdown:333333333333"); + PluginExecCmdByName("reboot.suspend", "reboot,suspend"); + PluginExecCmdByName("reboot.charge", "reboot,charge"); + PluginExecCmdByName("reboot.updater", "reboot,updater"); + PluginExecCmdByName("reboot.updater", "reboot,updater:2222222"); + PluginExecCmdByName("reboot.flashd", "reboot,flashd"); + PluginExecCmdByName("reboot.flashd", "reboot,flashd:1000000"); + PluginExecCmdByName("reboot.other", "reboot,other"); + PARAM_LOGE("TestRebootCmdExec end"); + int ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge"); + if (ret == 0) { + ret = GetBootModeFromMisc(); + EXPECT_EQ(ret, GROUP_CHARGE); + clearMisc(); + } +} #endif HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1) @@ -121,11 +143,7 @@ HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1) ret = DoReboot(DEVICE_CMD_FREEZE); EXPECT_EQ(ret, 0); - ret = UpdateMiscMessage("charge:wwwwwwwwwww", "charge", "charge:", "boot_charge"); - if (ret == 0) { - ret = GetBootModeFromMisc(); - EXPECT_EQ(ret, GROUP_CHARGE); - clearMisc(); - } + ret = DoRebootExt("shutdown", DEVICE_CMD_FREEZE); + EXPECT_EQ(ret, 0); } } // namespace init_ut