From 8ad56e8eda4a63e9a81c275e4eb26e9239237ad4 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 28 11月 2020 09:31:41 +0800 Subject: [PATCH] replace strncpy with a more safe alternative --- src/library/os/linux/execution_environment.cpp | 48 +++++++++++++++++++++--------------------------- 1 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp index 7df96e9..216df0c 100644 --- a/src/library/os/linux/execution_environment.cpp +++ b/src/library/os/linux/execution_environment.cpp @@ -1,16 +1,17 @@ /* - * virtualization.cpp + * execution_environment.cpp * * Created on: Dec 15, 2019 * Author: GC */ + #include <paths.h> #include <sys/stat.h> #include <unistd.h> #include <fstream> #include <iostream> #include <stdio.h> -#include <string.h> +#include <cstring> #include <dirent.h> #include <sys/utsname.h> @@ -18,7 +19,7 @@ #include "../cpu_info.hpp" #include "../execution_environment.hpp" #include "../../base/file_utils.hpp" -#include "../../base/StringUtils.h" +#include "../../base/string_utils.h" namespace license { namespace os { @@ -28,54 +29,47 @@ static CONTAINER_TYPE checkContainerProc() { // in docer /proc/self/cgroups contains the "docker" or "lxc" string // https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container - char path[MAX_PATH] = {0}; - char proc_path[MAX_PATH], pidStr[64]; - pid_t pid = getpid(); - sprintf(pidStr, "%d", pid); - strcpy(proc_path, "/proc/"); - strcat(proc_path, pidStr); - strcat(proc_path, "/cgroup"); FILE *fp; - char *line = NULL; + char *line = nullptr; size_t len = 0; ssize_t read; - CONTAINER_TYPE result = NONE; + CONTAINER_TYPE result = CONTAINER_TYPE::NONE; - fp = fopen(proc_path, "r"); - if (fp == NULL) { - return NONE; + fp = fopen("/proc/self/cgroup", "r"); + if (fp == nullptr) { + return CONTAINER_TYPE::NONE; } - while ((read = getline(&line, &len, fp)) != -1 && result == 0) { - // line[len]=0; - // printf("Retrieved line of length %zu:\n", read); - // printf("%s", line); + while ((read = getline(&line, &len, fp)) != -1 + && result == CONTAINER_TYPE::NONE) { if (strstr(line, "docker") != NULL) { - result = DOCKER; + result = CONTAINER_TYPE::DOCKER; } if (strstr(line, "lxc") != NULL) { - result = LXC; + result = CONTAINER_TYPE::LXC; } } + if (line) { + free(line); + } fclose(fp); - if (line) free(line); return result; } // 0=NO 1=Docker/2=Lxc static CONTAINER_TYPE checkSystemdContainer() { ifstream systemd_container("/var/run/systemd/container"); - CONTAINER_TYPE result = NONE; + CONTAINER_TYPE result = CONTAINER_TYPE::NONE; if (systemd_container.good()) { - result = DOCKER; + result = CONTAINER_TYPE::DOCKER; for (string line; getline(systemd_container, line);) { if (line.find("docker") != string::npos) { - result = DOCKER; + result = CONTAINER_TYPE::DOCKER; break; } else if (line.find("lxc") != string::npos) { - result = LXC; + result = CONTAINER_TYPE::LXC; break; } } @@ -85,7 +79,7 @@ static CONTAINER_TYPE get_container_type() { CONTAINER_TYPE result = checkContainerProc(); - if (result == NONE) { + if (result == CONTAINER_TYPE::NONE) { result = checkSystemdContainer(); } return result; -- Gitblit v1.9.1