修改监视意见,验证uid与devicedebug消息字段扩展

Signed-off-by: 王达 <wangda20@huawei.com>
This commit is contained in:
王达 2024-10-08 21:37:24 +08:00
parent 7cd275bfa2
commit 7c3be6f6d4
6 changed files with 134 additions and 33 deletions

View File

@ -29,7 +29,10 @@ if (!defined(ohos_lite)) {
"${appspawn_path}/interfaces/innerkits/client:appspawn_client",
"${appspawn_path}/util:libappspawn_util",
]
external_deps = [ "hilog:libhilog" ]
external_deps = [
"cJSON:cjson",
"hilog:libhilog",
]
install_enable = true
subsystem_name = "${subsystem_name}"
part_name = "${part_name}"

View File

@ -51,10 +51,10 @@ enum {
#define DEVICEDEBUG_ERRNO_OPERATOR_ARGV_MISS 0x12
// 0x13 非开发者模式
#define DEVICEDEBUG_ERRNO_NOT_IN_DEVELOPER_MODE 0x13
// 0x14 创建json对象失败
#define DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED 0x14
// 0x16 参数错误
#define DEVICEDEBUG_ERRNO_PARAM_INVALID 0x16
// 0x17 内存不足
#define DEVICEDEBUG_ERRNO_NOMEM 0x17
#define DEVICEDEBUG_LOGI(args, ...) \
HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)

View File

@ -21,6 +21,7 @@
#include "devicedebug_base.h"
#include "devicedebug_kill.h"
#include "cJSON.h"
#ifdef __cplusplus
extern "C" {
@ -38,7 +39,24 @@ APPSPAWN_STATIC void DeviceDebugShowKillHelp(void)
"\r\n kill -9 -12111 send a signal to a process\r\n");
}
APPSPAWN_STATIC int DeviceDebugKill(char *signal, char *pid)
APPSPAWN_STATIC char* DeviceDebugJsonStringGeneral(int pid, const char *op, cJSON *args)
{
cJSON *root = cJSON_CreateObject();
if (root == NULL) {
DEVICEDEBUG_LOGE("devicedebug json write create root object unsuccess");
return NULL;
}
cJSON_AddNumberToObject(root, "app", pid);
cJSON_AddStringToObject(root, "op", op);
cJSON_AddItemToObject(root, "args", args);
char *jsonString = cJSON_Print(root);
cJSON_Delete(root);
return jsonString;
}
APPSPAWN_STATIC int DeviceDebugKill(int pid, int signal)
{
AppSpawnClientHandle clientHandle;
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
@ -54,21 +72,29 @@ APPSPAWN_STATIC int DeviceDebugKill(char *signal, char *pid)
return ret;
}
ret = AppSpawnReqMsgAddStringInfo(reqHandle, "signal", signal);
if (ret != 0) {
DEVICEDEBUG_LOGE("devicedebug appspawn message add signal unsuccess, ret=%{public}d", ret);
return ret;
cJSON *args = cJSON_CreateObject();
if (args == NULL) {
DEVICEDEBUG_LOGE("devicedebug json write create args object unsuccess");
return DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED;
}
cJSON_AddNumberToObject(args, "signal", signal);
char *jsonString = DeviceDebugJsonStringGeneral(pid, "kill", args);
if (jsonString == NULL) {
return DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED;
}
ret = AppSpawnReqMsgAddStringInfo(reqHandle, "pid", pid);
ret = AppSpawnReqMsgAddExtInfo(reqHandle, "devicedebug", (uint8_t *)jsonString, strlen(jsonString) + 1);
if (ret != 0) {
DEVICEDEBUG_LOGE("devicedebug appspawn message add pid unsuccess, ret=%{public}d", ret);
DEVICEDEBUG_LOGE("devicedebug appspawn message add devicedebug[%{public}s] unsuccess, ret=%{public}d",
jsonString, ret);
free(args);
return ret;
}
AppSpawnResult result = {0};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
AppSpawnClientDestroy(clientHandle);
free(args);
if (ret != 0) {
DEVICEDEBUG_LOGE("devicedebug appspawn send msg unsuccess, ret=%{public}d", ret);
return ret;
@ -110,7 +136,7 @@ int DeviceDebugCmdKill(int argc, char *argv[])
int pid = atoi(argv[DEVICEDEBUG_KILL_CMD_PID_INDEX]);
DEVICEDEBUG_LOGI("devicedebug cmd kill start signal[%{public}d], pid[%{public}d]", signal, pid);
return DeviceDebugKill(argv[DEVICEDEBUG_KILL_CMD_SIGNAL_INDEX], argv[DEVICEDEBUG_KILL_CMD_PID_INDEX]);
return DeviceDebugKill(pid, signal);
}
#ifdef __cplusplus

View File

@ -66,6 +66,7 @@ ohos_executable("appspawn") {
}
external_deps = [
"cJSON:cjson",
"c_utils:utils",
"config_policy:configpolicy_util",
"hilog:libhilog",
@ -173,6 +174,7 @@ ohos_executable("cjappspawn") {
}
external_deps = [
"cJSON:cjson",
"c_utils:utils",
"config_policy:configpolicy_util",
"hilog:libhilog",
@ -239,6 +241,7 @@ ohos_executable("nativespawn") {
}
external_deps = [
"cJSON:cjson",
"c_utils:utils",
"config_policy:configpolicy_util",
"hilog:libhilog",

View File

@ -42,6 +42,7 @@
#include "parameter.h"
#include "appspawn_adapter.h"
#include "securec.h"
#include "cJSON.h"
#ifdef APPSPAWN_HISYSEVENT
#include "appspawn_hisysevent.h"
#endif
@ -356,7 +357,7 @@ static int OnConnection(const LoopHandle loopHandle, const TaskHandle server)
socklen_t credSize = sizeof(struct ucred);
if ((getsockopt(LE_GetSocketFd(stream), SOL_SOCKET, SO_PEERCRED, &cred, &credSize) < 0) ||
(cred.uid != DecodeUid("foundation") && cred.uid != DecodeUid("root")
&& cred.uid != DecodeUid("app_fwk_update"))) {
&& cred.uid != DecodeUid("app_fwk_update") && cred.uid != DecodeUid("shell"))) {
APPSPAWN_LOGE("Invalid uid %{public}d from client", cred.uid);
LE_CloseStreamTask(LE_GetDefaultLoop(), stream);
return -1;
@ -375,6 +376,32 @@ static int OnConnection(const LoopHandle loopHandle, const TaskHandle server)
return 0;
}
APPSPAWN_STATIC bool MsgDevicedebugCheck(TaskHandle stream, AppSpawnMsgNode *message)
{
struct ucred cred = {0, 0, 0};
socklen_t credSize = sizeof(cred);
if (getsockopt(LE_GetSocketFd(stream), SOL_SOCKET, SO_PEERCRED, &cred, &credSize) < 0) {
return false;
}
if (cred.uid != DecodeUid("shell")) {
return true;
}
if (!IsDeveloperModeOpen()) {
APPSPAWN_LOGE("appspawn devicedebug this is not develop mode on");
return false;
}
AppSpawnMsg *msg = &message->msgHeader;
if (msg->msgType != MSG_DEVICE_DEBUG) {
APPSPAWN_LOGE("appspawn devicedebug msg type is not devicedebug [%{public}d]", msg->msgType);
return false;
}
return true;
}
static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen)
{
AppSpawnConnection *connection = (AppSpawnConnection *)LE_GetUserData(taskHandle);
@ -406,6 +433,10 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer,
LE_StopTimer(LE_GetDefaultLoop(), connection->receiverCtx.timer);
connection->receiverCtx.timer = NULL;
}
APPSPAWN_CHECK_ONLY_EXPER(MsgDevicedebugCheck(connection->stream, message),
LE_CloseTask(LE_GetDefaultLoop(), taskHandle); return);
// decode msg
ret = DecodeAppSpawnMsg(message);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
@ -1391,25 +1422,11 @@ static void ProcessSpawnRestartMsg(AppSpawnConnection *connection, AppSpawnMsgNo
APPSPAWN_LOGE("Failed to execv, ret %{public}d, errno %{public}d", ret, errno);
}
static int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message)
APPSPAWN_STATIC int AppspawpnDevicedebugKill(int pid, cJSON *args)
{
APPSPAWN_CHECK_ONLY_EXPER(message != NULL, return -1);
uint32_t len = 0;
if (!IsDeveloperModeOpen()) {
APPSPAWN_LOGE("appspawn devicedebug this is not develop mode on");
return -1;
}
pid_t pid = atoi((char *)GetAppSpawnMsgExtInfo(message, "pid", &len));
if (pid == 0) {
APPSPAWN_LOGE("appspawn devicedebug get pid fail");
return -1;
}
int signal = atoi((char *)GetAppSpawnMsgExtInfo(message, "signal", &len) + 1);
if (signal == 0) {
APPSPAWN_LOGE("appspawn devicedebug get signal fail");
cJSON *signal = cJSON_GetObjectItem(args, "signal");
if (!cJSON_IsNumber(signal)) {
APPSPAWN_LOGE("appspawn devicedebug json get signal fail");
return -1;
}
@ -1425,9 +1442,9 @@ static int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message)
}
APPSPAWN_LOGI("appspawn devicedebug debugable=%{public}d, pid=%{public}d, signal=%{public}d",
appInfo->isDebuggable, pid, signal);
appInfo->isDebuggable, pid, signal->valueint);
if (kill(pid, signal) != 0) {
if (kill(pid, signal->valueint) != 0) {
APPSPAWN_LOGE("appspawn devicedebug unable to kill process, pid: %{public}d ret %{public}d", pid, errno);
return -1;
}
@ -1435,6 +1452,55 @@ static int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message)
return 0;
}
APPSPAWN_STATIC int AppspawnDevicedebugDeal(const char* op, int pid, cJSON *args)
{
if (strcmp(op, "kill") == 0) {
return AppspawpnDevicedebugKill(pid, args);
}
APPSPAWN_LOGE("appspawn devicedebug op:%{public}s invaild", op);
return -1;
}
APPSPAWN_STATIC int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message)
{
APPSPAWN_CHECK_ONLY_EXPER(message != NULL, return -1);
uint32_t len = 0;
const char* jsonString = (char *)GetAppSpawnMsgExtInfo(message, "devicedebug", &len);
if (jsonString == NULL || len == 0) {
APPSPAWN_LOGE("appspawn devicedebug get devicedebug fail");
return -1;
}
cJSON *json = cJSON_Parse(jsonString);
if (json == NULL) {
APPSPAWN_LOGE("appspawn devicedebug json parse fail");
return -1;
}
cJSON *app = cJSON_GetObjectItem(json, "app");
if (!cJSON_IsNumber(app)) {
APPSPAWN_LOGE("appspawn devicedebug json get app fail");
return -1;
}
cJSON *op = cJSON_GetObjectItem(json, "op");
if (!cJSON_IsString(op) || op->valuestring == NULL) {
APPSPAWN_LOGE("appspawn devicedebug json get op fail");
return -1;
}
cJSON *args = cJSON_GetObjectItem(json, "args");
if (!cJSON_IsObject(args)) {
APPSPAWN_LOGE("appspawn devicedebug json get args fail");
return -1;
}
return AppspawnDevicedebugDeal(op->valuestring, app->valueint, args);
}
static void ProcessRecvMsg(AppSpawnConnection *connection, AppSpawnMsgNode *message)
{
AppSpawnMsg *msg = &message->msgHeader;

View File

@ -49,7 +49,10 @@ if (!defined(ohos_lite)) {
"APPSPAWN_TEST",
]
external_deps = [ "hilog:libhilog" ]
external_deps = [
"cJSON:cjson",
"hilog:libhilog",
]
deps = [
"${appspawn_path}/interfaces/innerkits/client:appspawn_client",