From 9cf4e745efaafd921011a9f923a9a9881bfe3fd1 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 22 Jul 2021 21:23:30 +0800 Subject: [PATCH 1/2] init: fix code style Signed-off-by: sun_fan --- services/log/init_log.c | 7 ++- services/src/init_cmds.c | 66 ++++++++++++++++++++++------- services/src/init_reboot.c | 30 +++++++++++-- services/src/init_service_manager.c | 6 ++- 4 files changed, 87 insertions(+), 22 deletions(-) diff --git a/services/log/init_log.c b/services/log/init_log.c index dbadec4bd..0e5891005 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -52,6 +52,9 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...) if (logLevel < g_hiLogLevel) { return; } + if (tag == NULL) { + return; + } va_list list; va_start(list, fmt); char tmpFmt[MAX_FORMAT_SIZE]; @@ -70,7 +73,9 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l if (logLevel < g_logLevel) { return; } - + if (tag == NULL) { + return; + } time_t second = time(0); struct tm *t = localtime(&second); if (t == NULL) { diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index b3ffe4788..b56026ba7 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -187,7 +187,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) token = strstr(p, delim); if (token == NULL) { // No whitespaces // Make surce there is enough memory to store parameter value - allocSize = (size_t)(cmdLength + MAX_PARAM_VALUE_LEN); + allocSize = (size_t)(cmdLength + MAX_PARAM_VALUE_LEN + 1); ctx->argv[ctx->argc] = calloc(sizeof(char), allocSize); INIT_CHECK_ONLY_RETURN(ctx->argv[ctx->argc] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[ctx->argc], allocSize) == 0, @@ -204,7 +204,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) break; } *token = '\0'; // replace it with '\0'; - allocSize = (size_t)((p - token) + MAX_PARAM_VALUE_LEN); + allocSize = (size_t)((token - p) + MAX_PARAM_VALUE_LEN + 1); ctx->argv[index] = calloc(sizeof(char), allocSize); INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0, @@ -222,7 +222,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) if (p < tmpCmd + cmdLength) { // no more white space or encounter max argument count size_t restSize = tmpCmd + cmdLength - p; - allocSize = restSize + MAX_PARAM_VALUE_LEN; + allocSize = restSize + MAX_PARAM_VALUE_LEN + 1; ctx->argv[index] = calloc(sizeof(char), allocSize); INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0, @@ -308,18 +308,29 @@ static void DoCopy(const char* cmdContent) int rtLen = 0; int argsCount = 2; char buf[MAX_COPY_BUF_SIZE] = {0}; + char *realPath1 = NULL; + char *realPath2 = NULL; mode_t mode = 0; struct stat fileStat = {0}; struct CmdArgs *ctx = GetCmd(cmdContent, " ", argsCount); - if (ctx == NULL || ctx->argv == NULL || ctx->argv[0] == NULL || ctx->argc != DEFAULT_COPY_ARGS_CNT) { + if (ctx == NULL || ctx->argv == NULL || ctx->argv[0] == NULL || ctx->argv[1] == NULL || + ctx->argc != DEFAULT_COPY_ARGS_CNT) { INIT_LOGE("DoCopy failed."); goto out; } - srcFd = open(ctx->argv[0], O_RDONLY); + realPath1 = realpath(ctx->argv[0], NULL); + if (realPath1 == NULL) { + goto out; + } + realPath2 = realpath(ctx->argv[1], NULL); + if (realPath2 == NULL) { + goto out; + } + srcFd = open(realPath1, O_RDONLY); INIT_ERROR_CHECK(srcFd >= 0, goto out, "copy open %s fail %d! ", ctx->argv[0], errno); INIT_ERROR_CHECK(stat(ctx->argv[0], &fileStat) == 0, goto out, "stat fail "); mode = fileStat.st_mode; - dstFd = open(ctx->argv[1], O_WRONLY | O_TRUNC | O_CREAT, mode); + dstFd = open(realPath2, O_WRONLY | O_TRUNC | O_CREAT, mode); INIT_ERROR_CHECK(dstFd >= 0, goto out, "copy open %s fail %d! ", ctx->argv[1], errno); while ((rdLen = read(srcFd, buf, sizeof(buf) - 1)) > 0) { rtLen = write(dstFd, buf, rdLen); @@ -337,6 +348,14 @@ out: close(dstFd); dstFd = -1; } + if (realPath1) { + free(realPath1); + realPath1 = NULL; + } + if (realPath2) { + free(realPath2); + realPath2 = NULL; + } return; } @@ -611,19 +630,15 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt if (fileName == NULL) { return; } - char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char)); + char *realPath = realpath(fileName, NULL); if (realPath == NULL) { return; } - realPath = realpath(fileName, realPath); - if (realPath == NULL) { - free(realPath); - return; - } int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { INIT_LOGE("failed to open %s: %d", realPath, errno); free(realPath); + realPath = NULL; return; } int rc = syscall(__NR_finit_module, fd, options, flags); @@ -634,6 +649,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt close(fd); } free(realPath); + realPath = NULL; return; } @@ -740,9 +756,15 @@ static void DoLoadCfg(const char *path) INIT_LOGE("CheckCfg path is NULL."); return; } - fp = fopen(path, "r"); + char *realPath = realpath(path, NULL); + if (realPath == NULL) { + return; + } + fp = fopen(realPath, "r"); if (fp == NULL) { INIT_LOGE("open cfg error = %d", errno); + free(realPath); + realPath = NULL; return; } @@ -750,6 +772,8 @@ static void DoLoadCfg(const char *path) if (cmdLine == NULL) { INIT_LOGE("malloc cmdline error"); fclose(fp); + free(realPath); + realPath = NULL; return; } @@ -767,7 +791,8 @@ static void DoLoadCfg(const char *path) DoCmd(cmdLine); (void)memset_s(buf, sizeof(char) * LOADCFG_BUF_SIZE, 0, sizeof(char) * LOADCFG_BUF_SIZE); } - + free(realPath); + realPath = NULL; free(cmdLine); fclose(fp); } @@ -781,19 +806,28 @@ static void DoWrite(const char *cmdContent) INIT_LOGE("DoWrite: invalid arguments"); goto out; } - - int fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU | S_IRGRP | S_IROTH); + char *realPath = realpath(ctx->argv[0], NULL); + if (realPath == NULL) { + goto out; + } + int fd = open(realPath, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU | S_IRGRP | S_IROTH); if (fd == -1) { INIT_LOGE("DoWrite: open %s failed: %d", ctx->argv[0], errno); + free(realPath); + realPath = NULL; goto out; } size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1])); if (ret < 0) { INIT_LOGE("DoWrite: write to file %s failed: %d", ctx->argv[0], errno); + free(realPath); + realPath = NULL; close(fd); goto out; } + free(realPath); + realPath = NULL; close(fd); out: FreeCmd(&ctx); diff --git a/services/src/init_reboot.c b/services/src/init_reboot.c index 38ea5fb73..b059fa43f 100644 --- a/services/src/init_reboot.c +++ b/services/src/init_reboot.c @@ -43,19 +43,28 @@ static bool RBMiscWriteUpdaterMessage(const char *path, struct RBMiscUpdateMessa INIT_LOGE("path or boot is NULL."); return false; } - FILE* fp = fopen(path, "rb+"); + char *realPath = realpath(path, NULL); + if (realPath == NULL) { + return false; + } + FILE* fp = fopen(realPath, "rb+"); if (fp == NULL) { INIT_LOGE("open %s failed", path); + free(realPath); + realPath = NULL; return false; } size_t ret = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp); if (ret < 0) { INIT_LOGE("write to misc failed"); + free(realPath); + realPath = NULL; fclose(fp); return false; } - + free(realPath); + realPath = NULL; fclose(fp); return true; } @@ -66,18 +75,28 @@ static bool RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessag INIT_LOGE("path or boot is NULL."); return false; } - FILE* fp = fopen(path, "rb"); + char *realPath = realpath(path, NULL); + if (realPath == NULL) { + return false; + } + FILE* fp = fopen(realPath, "rb"); if (fp == NULL) { INIT_LOGE("open %s failed", path); + free(realPath); + realPath = NULL; return false; } size_t ret = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp); if (ret <= 0) { INIT_LOGE("read to misc failed"); + free(realPath); + realPath = NULL; fclose(fp); return false; } + free(realPath); + realPath = NULL; fclose(fp); return true; } @@ -166,7 +185,10 @@ void DoReboot(const char *value) return; } const int commandSize = 12; - snprintf(msg.command, MAX_COMMAND_SIZE, "%s", "boot_updater"); + if (snprintf_s(msg.command, MAX_COMMAND_SIZE, MAX_COMMAND_SIZE - 1, "%s", "boot_updater") == -1) { + INIT_LOGE("DoReboot updater: RBMiscWriteUpdaterMessage error"); + return; + } msg.command[commandSize] = 0; if (strlen(valueData) > strlen("updater:") && strncmp(valueData, "updater:", strlen("updater:")) == 0) { diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 53b14e99e..9cc181be3 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -185,6 +185,9 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ) } char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i)); + if (fieldStr == NULL) { + return SERVICE_FAILURE; + } size_t strLen = strlen(fieldStr); curServ->writepidFiles[i] = (char *)malloc(sizeof(char) * strLen + 1); if (curServ->writepidFiles[i] == NULL) { @@ -246,7 +249,7 @@ static int GetGidArray(const cJSON *curArrItem, Service *curServ) // gid { int gIDCnt = 0; cJSON *filedJ = GetArrItem(curArrItem, &gIDCnt, GID_STR_IN_CFG); // "gid" must have 1 item. - if ((gIDCnt <= 0) && (filedJ == NULL)) { // not a array, but maybe a item? + if ((gIDCnt <= 0) || (filedJ == NULL)) { // not a array, but maybe a item? return GetGidOneItem(curArrItem, curServ); } @@ -578,6 +581,7 @@ static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ) curServ->onRestart->cmdLine = (CmdLine *)calloc(cmdCnt, sizeof(CmdLine)); if (curServ->onRestart->cmdLine == NULL) { free(curServ->onRestart); + curServ->onRestart = NULL; return SERVICE_FAILURE; } curServ->onRestart->cmdNum = cmdCnt; -- Gitee From bb1cd7d1f73849d3ec55d4dab2e50454dd90d233 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 22 Jul 2021 21:48:19 +0800 Subject: [PATCH 2/2] init: add group Signed-off-by: sun_fan --- services/BUILD.gn | 6 +++ services/etc/group | 115 ++++++++++++++++++++++++++++++++++++++++++ services/etc/passwd | 1 - services/src/uevent.c | 58 +++++++++++++++++++-- 4 files changed, 176 insertions(+), 4 deletions(-) create mode 100755 services/etc/group diff --git a/services/BUILD.gn b/services/BUILD.gn index d1a1f0bdc..a317ab1b7 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -169,6 +169,11 @@ if (defined(ohos_lite)) { part_name = "init" } + ohos_prebuilt_etc("group") { + source = "//base/startup/init_lite/services/etc/group" + part_name = "init" + } + ohos_prebuilt_etc("init.Hi3516DV300.cfg") { source = "//device/hisilicon/hi3516dv300/build/rootfs/init.Hi3516DV300.cfg" part_name = "init" @@ -197,6 +202,7 @@ if (defined(ohos_lite)) { ":init.usb.cfg", ":init.usb.configfs.cfg", ":passwd", + ":group", ] } } diff --git a/services/etc/group b/services/etc/group new file mode 100755 index 000000000..47bc75cf0 --- /dev/null +++ b/services/etc/group @@ -0,0 +1,115 @@ +root:x:0 +daemon:x:1 +bin:x:2 +system:x:1000 +radio:x:1001 +bluetooth:x:1002 +graphics:x:1003 +input:x:1004 +audio:x:1005 +camera:x:1006 +log:x:1007 +compass:x:1008 +mount:x:1009 +wifi:x:1010 +adb:x:1011 +install:x:1012 +media:x:1013 +dhcp:x:1014 +sdcard_rw:x:1015 +vpn:x:1016 +keystore:x:1017 +usb:x:1018 +drm:x:1019 +mdnsr:x:1020 +gps:x:1021 +unused1:x:1022 +media_rw:x:1023 +mtp:x:1024 +unused2:x:1025 +drmrpc:x:1026 +nfc:x:1027 +sdcard_r:x:1028 +clat:x:1029 +loop_radio:x:1030 +media_drm:x:1031 +package_info:x:1032 +sdcard_pics:x:1033 +sdcard_av:x:1034 +sdcard_all:x:1035 +logd:x:1036 +shared_relro:x:1037 +dbus:x:1038 +tlsdate:x:1039 +media_ex:x:1040 +audioserver:x:1041 +metrics_coll:x:1042 +metricsd:x:1043 +webserv:x:1044 +debuggerd:x:1045 +media_codec:x:1046 +cameraserver:x:1047 +firewall:x:1048 +trunks:x:1049 +nvram:x:1050 +dns:x:1051 +dns_tether:x:1052 +webview_zygote:x:1053 +vehicle_network:x:1054 +media_audio:x:1055 +media_video:x:1056 +media_image:x:1057 +tombstoned:x:1058 +media_obb:x:1059 +ese:x:1060 +ota_update:x:1061 +automotive_evs:x:1062 +lowpan:x:1063 +hsm:x:1064 +reserved_disk:x:1065 +statsd:x:1066 +incidentd:x:1067 +secure_element:x:1068 +lmkd:x:1069 +llkd:x:1070 +iorapd:x:1071 +gpu_service:x:1072 +network_stack:x:1073 +gsid:x:1074 +shell:x:2000 +cache:x:2001 +diag:x:2002 +oem_reserved_start:x:2900 +oem_reserved_end:x:2999 +net_bt_admin:x:3001 +net_bt:x:3002 +inet:x:3003 +net_raw:x:3004 +net_admin:x:3005 +net_bw_stats:x:3006 +net_bw_acct:x:3007 +readproc:x:3009 +wakelock:x:3010 +uhid:x:3011 +oem_reserved_2_start:x:5000 +oem_reserved_2_end:x:5999 +everybody:x:9997 +misc:x:9998 +nobody:x:9999 +app:x:10000 +app_start:x:10000 +app_end:x:19999 +cache_gid_start:x:20000 +cache_gid_end:x:29999 +ext_gid_start:x:30000 +ext_gid_end:x:39999 +ext_cache_gid_start:x:40000 +ext_cache_gid_end:x:49999 +shared_gid_start:x:50000 +shared_gid_end:x:59999 +overflowuid:x:65534 +isolated_start:x:90000 +isolated_end:x:99999 +user:x:100000 +user_offset:x:100000 + diff --git a/services/etc/passwd b/services/etc/passwd index bad5f0ff9..f84c1e21e 100755 --- a/services/etc/passwd +++ b/services/etc/passwd @@ -112,4 +112,3 @@ isolated_start:x:90000:90000:::/bin/false isolated_end:x:99999:99999:::/bin/false user:x:100000:100000:::/bin/false user_offset:x:100000:100000:::/bin/false - diff --git a/services/src/uevent.c b/services/src/uevent.c index 103952fb9..b9f422d0f 100644 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -33,7 +33,7 @@ #include "securec.h" #define LINK_NUMBER 4 -#define DEFAULT_DIR_MODE 0755 +#define DEFAULT_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) #define DEV_DRM 3 #define DEV_ONCRPC 6 #define DEV_ADSP 4 @@ -67,7 +67,7 @@ #define DEFAULT_MODE 0000 #define DEVICE_SKIP 5 #define HANDLE_DEVICE_USB 3 -#define DEFAULT_NO_AUTHORITY_MODE 0600 +#define DEVICE_DEFAULT_MODE (S_IRUSR | S_IWUSR | S_IRGRP) int g_ueventFD = -1; @@ -387,6 +387,58 @@ struct DevPermissionMapper { struct DevPermissionMapper DEV_MAPPER[] = { {"/dev/binder", 0666, 0, 0}, + {"/dev/input/event0", 0660, 0, 1004}, + {"/dev/input/event1", 0660, 0, 1004}, + {"/dev/input/mice", 0660, 0, 1004}, + {"/dev/input/mouse0", 0660, 0, 1004}, + {"/dev/snd/timer", 0660, 1000, 1005}, + {"/dev/zero", 0666, 0, 0}, + {"/dev/full", 0666, 0, 0}, + {"/dev/ptmx", 0666, 0, 0}, + {"/dev/tty", 0666, 0, 0}, + {"/dev/random", 0666, 0, 0}, + {"/dev/urandom", 0666, 0, 0}, + {"/dev/ashmem", 0666, 0, 0}, + {"/dev/pmsg0", 0222, 0, 0}, + {"/dev/jpeg", 0666, 1000, 1003}, + {"/dev/vinput", 0660, 1000, 1004}, + {"/dev/mmz_userdev", 0644, 1000, 1005}, + {"/dev/graphics/fb0", 0660, 1000, 1003}, + {"/dev/mem", 0660, 1000, 1005}, + {"/dev/ion", 0666, 1000, 1000}, + {"/dev/btusb0", 0660, 1002, 1002}, + {"/dev/uhid", 0660, 1002, 1002}, + {"/dev/tc_ns_client", 0660, 1000, 1005}, + {"/dev/rtk_btusb", 0660, 1002, 0}, + {"/dev/sil9293", 0660, 1000, 1005}, + {"/dev/stpbt", 0660, 1002, 1001}, + {"/dev/avs", 0660, 1000, 1005}, + {"/dev/gdc", 0660, 1000, 1005}, + {"/dev/hdmi", 0660, 1000, 1005}, + {"/dev/hi_mipi", 0660, 1000, 1005}, + {"/dev/hi_mipi_tx", 0660, 1000, 1005}, + {"/dev/hi_tde", 0644, 1000, 1003}, + {"/dev/isp_dev", 0660, 1000, 1006}, + {"/dev/match", 0660, 1000, 1005}, + {"/dev/photo", 0660, 1000, 1005}, + {"/dev/rect", 0660, 1000, 1005}, + {"/dev/rgn", 0660, 1000, 1005}, + {"/dev/sys", 0660, 1000, 1005}, + {"/dev/vb", 0666, 1000, 1005}, + {"/dev/vdec", 0666, 1000, 1005}, + {"/dev/venc", 0666, 1000, 1005}, + {"/dev/vi", 0660, 1000, 1005}, + {"/dev/vo", 0660, 1000, 1005}, + {"/dev/vpss", 0660, 1000, 1005}, + {"/dev/i2c-0", 0660, 1000, 1006}, + {"/dev/i2c-1", 0660, 1000, 1006}, + {"/dev/i2c-2", 0660, 1000, 1006}, + {"/dev/i2c-3", 0660, 1000, 1006}, + {"/dev/i2c-4", 0660, 1000, 1006}, + {"/dev/i2c-5", 0660, 1000, 1006}, + {"/dev/i2c-6", 0660, 1000, 1006}, + {"/dev/i2c-7", 0660, 1000, 1006}, + {"/dev/vgs", 0666, 1000, 1005}, {"/dev/dri/card0", 0666, 0, 1003}, {"/dev/dri/card0-DSI-1", 0666, 0, 1003}, {"/dev/dri/card0-HDMI-A-1", 0666, 0, 1003}, @@ -415,7 +467,7 @@ static void MakeDevice(const char *devPath, const char *path, int block, int maj /* Only for super user */ gid_t gid = 0; dev_t dev; - mode_t mode = DEFAULT_NO_AUTHORITY_MODE; + mode_t mode = DEVICE_DEFAULT_MODE; mode |= (block ? S_IFBLK : S_IFCHR); dev = makedev(major, minor); setegid(gid); -- Gitee