diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index dd63ca085940d076b9d2ce6ae8bb584cfb028ee2..f2d8e3e5346684b84d8e1c23b4e05b6331899e2a 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -14,6 +14,7 @@ */ #include "init_cmds.h" + #include #include #include @@ -28,6 +29,7 @@ #include #include #endif + #include "init_service_manager.h" #include "securec.h" @@ -326,7 +328,6 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt { int fd = -1; char options[OPTIONS_SIZE] = {0}; - if (flags == 0) { // '-f' option if (restPtr != NULL && secondPtr != NULL) { // Reset arugments, combine then all. if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) { @@ -339,9 +340,14 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt } } else { // Only restPtr is option if (restPtr != NULL) { - strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)); + if (strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)) != 0) { + goto out; + } } } + if (!fileName) { + goto out; + } fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { printf("[Init] failed to open %s: %d\n", fileName, errno); @@ -352,7 +358,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt printf("[Init] finit_module for %s failed: %d\n", fileName, errno); } out: - if (fd > 0) { + if (fd >= 0) { close(fd); } return; @@ -379,12 +385,14 @@ static void DoInsmod(const char *cmdContent) if (memcpy_s(line, count, cmdContent, count) != EOK) { printf("[Init] memcpy failed\n"); + free(line); return; } line[count] = '\0'; do { if ((p = strtok_r(line, " ", &restPtr)) == NULL) { printf("[Init] debug, cannot get filename\n"); + free(line); return; } fileName = p; diff --git a/services/src/uevent.c b/services/src/uevent.c index 54c023011c7df4c42c20904fa7f4ecd1609be891..3594d19ed256122dbf95c2c7f6ae30435dbe36ae 100755 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -152,7 +152,7 @@ static void RetriggerUevent() Trigger("/sys/block"); Trigger("/sys/devices"); int fd = open(g_trigger, O_WRONLY | O_CREAT | O_CLOEXEC, DEFAULT_MODE); - if (fd > 0) { + if (fd >= 0) { close(fd); } printf("Re-trigger uevent done\n"); @@ -220,7 +220,7 @@ ssize_t ReadUevent(int fd, void *buf, size_t len) if (uid != 0) { goto out; } - + if (addr.nl_groups == 0 || addr.nl_pid != 0) { /* ignoring non-kernel or unicast netlink message */ goto out; @@ -344,7 +344,8 @@ static char **ParsePlatformBlockDevice(const struct Uevent *uevent) if (!links) { return NULL; } - if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath), "/dev/block/%s/%s", type, device) == -1) { + if (snprintf_s(linkPath, sizeof(linkPath), sizeof(linkPath) - 1, "/dev/block/%s/%s", type, device) == -1) { + free(links); return NULL; } if (uevent->partitionName) { @@ -514,7 +515,7 @@ static void HandleBlockDevice(struct Uevent *event) if (strlen(name) > MAX_DEVICE_LEN) { // too long return; } - if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath), "%s/%s", base, name) == -1) { + if (snprintf_s(devpath, sizeof(devpath), sizeof(devpath) - 1, "%s/%s", base, name) == -1) { return; } MakeDir(base, DEFAULT_DIR_MODE); @@ -537,10 +538,14 @@ static void AddPlatformDevice(const char *path) } printf("adding platform device %s (%s)\n", name, path); struct PlatformNode *bus = calloc(1, sizeof(struct PlatformNode)); + if (!bus) { + return; + } bus->path = strdup(path); bus->pathLen = pathLen; bus->name = bus->path + (name - path); ListAddTail(&g_platformNames, &bus->list); + return; } static void RemovePlatformDevice(const char *path) @@ -661,7 +666,7 @@ static int HandleUsbDevice(const struct Uevent *event, char *devpath, int len) * see drivers/base/core.c */ char *p = devpath; - if (snprintf_s(devpath, len, len, "/dev/%s", event->deviceName) == -1) { + if (snprintf_s(devpath, len, len - 1, "/dev/%s", event->deviceName) == -1) { return -1; } /* skip leading /dev/ */ @@ -702,13 +707,21 @@ static void HandleDeviceEvent(struct Uevent *event, char *devpath, int len, cons char **links = NULL; links = GetCharacterDeviceSymlinks(event); if (!devpath[0]) { - if (snprintf_s(devpath, len, len, "%s%s", base, name) == -1) { + if (snprintf_s(devpath, len, len - 1, "%s%s", base, name) == -1) { printf("[Init] snprintf_s err \n"); - return; + goto err; } } HandleDevice(event, devpath, 0, links); return; +err: + if (links) { + for (int i = 0; links[i]; i++) { + free(links[i]); + } + free(links); + } + return; } static void HandleGenericDevice(struct Uevent *event) {