Match-id-cdbea457685cb750a3355aebbfa814a9d3bd5cfd

This commit is contained in:
BianTanggui
2022-09-05 16:44:57 +08:00
parent 6081df41b7
commit 39686c4f2d
12 changed files with 148 additions and 111 deletions

View File

@@ -15,9 +15,9 @@
#define DEVMM_SVM "devmm_svm" #define DEVMM_SVM "devmm_svm"
#define HISI_HDC "hisi_hdc" #define HISI_HDC "hisi_hdc"
#define DEFAULT_DIR_MODE 0755 #define DEFAULT_DIR_MODE 0755
#define DEFAULT_LOG_MODE 0640 #define DEFAULT_LOG_MODE 0600
#define DUMP_LOG_MODE 0440 #define DUMP_LOG_MODE 0400
#define DEFAULT_LOGDIR_MODE 0750 #define DEFAULT_LOGDIR_MODE 0700
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define MAX_DEVICE_NR 1024 #define MAX_DEVICE_NR 1024
#define MAX_MOUNT_NR 512 #define MAX_MOUNT_NR 512
@@ -65,7 +65,7 @@ struct MountList {
struct ParsedConfig { struct ParsedConfig {
char rootfs[BUF_SIZE]; char rootfs[BUF_SIZE];
unsigned int devices[MAX_DEVICE_NR]; size_t devices[MAX_DEVICE_NR];
size_t devicesNr; size_t devicesNr;
char containerNsPath[BUF_SIZE]; char containerNsPath[BUF_SIZE];
char cgroupPath[BUF_SIZE]; char cgroupPath[BUF_SIZE];

View File

@@ -83,6 +83,7 @@ bool CheckSubStr(char **pLine, const char *subsys)
return strstr(substr, subsys) != NULL; return strstr(substr, subsys) != NULL;
} }
typedef char *(*ParseFileLine)(char *, const char *); typedef char *(*ParseFileLine)(char *, const char *);
static bool GetFileInfo(const char* resolvedPath, char* buffer, const int bufferSize, const ParseFileLine fn) static bool GetFileInfo(const char* resolvedPath, char* buffer, const int bufferSize, const ParseFileLine fn)
{ {
@@ -274,7 +275,7 @@ int SetupDriverCgroup(FILE *cgroupAllow)
return 0; return 0;
} }
int GetCgroupPath(int pid, char *effPath, size_t maxSize) int GetCgroupPath(const long pid, char *effPath, size_t maxSize)
{ {
if (effPath == NULL) { if (effPath == NULL) {
(void)fprintf(stderr, "effPath pointer is null!\n"); (void)fprintf(stderr, "effPath pointer is null!\n");

View File

@@ -7,7 +7,7 @@
#include "basic.h" #include "basic.h"
int GetCgroupPath(int pid, char *effPath, size_t maxSize); int GetCgroupPath(const long pid, char *effPath, size_t maxSize);
int SetupCgroup(const struct ParsedConfig *config); int SetupCgroup(const struct ParsedConfig *config);
#endif #endif

View File

@@ -29,6 +29,9 @@ int GetCurrentLocalTime(char* buffer, int length)
} }
time_t timep = time(NULL); time_t timep = time(NULL);
if (timep == (time_t)-1) {
return -1;
}
struct tm result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; struct tm result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
struct tm *timeinfo = localtime_r(&timep, &result); struct tm *timeinfo = localtime_r(&timep, &result);
if (timeinfo == NULL) { if (timeinfo == NULL) {
@@ -96,12 +99,8 @@ long GetLogSize(const char* filename)
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) { if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
return -1; return -1;
} }
struct stat fileStat; if (!CheckExistsFile(path, strlen(path), 0, false)) {
if ((stat(path, &fileStat) == 0) && (S_ISREG(fileStat.st_mode) != 0)) { return -1;
const size_t maxFileSzieMb = 50; // max 50MB
if (!CheckExternalFile(path, strlen(path), maxFileSzieMb, true)) {
return -1;
}
} }
return GetLogSizeProcess(path); return GetLogSizeProcess(path);
} }
@@ -115,12 +114,19 @@ int LogLoop(const char* filename)
int ret; int ret;
char* loopPath = LOG_PATH_DIR"docker-runtime-log.log.1"; char* loopPath = LOG_PATH_DIR"docker-runtime-log.log.1";
int exist;
exist = access(loopPath, 0); if (!CheckExistsFile(loopPath, strlen(loopPath), 0, false)) {
if (exist == 0) { return -1;
}
if (!CheckExistsFile(filename, strlen(filename), 0, false)) {
return -1;
}
if (access(loopPath, 0) == 0) {
unlink(loopPath); unlink(loopPath);
} }
rename(filename, loopPath); if (rename(filename, loopPath) == -1) {
return -1;
}
if (chmod(loopPath, DUMP_LOG_MODE) != 0) { if (chmod(loopPath, DUMP_LOG_MODE) != 0) {
return -1; return -1;
} }
@@ -182,12 +188,8 @@ static void LogFileProcess(const char* filename, const long maxSize, const char*
if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) { if (strlen(filename) > PATH_MAX || realpath(filename, path) == NULL) {
return; return;
} }
struct stat fileStat; if (!CheckExistsFile(path, strlen(path), 0, false)) {
if ((stat(path, &fileStat) == 0) && (S_ISREG(fileStat.st_mode) != 0)) { return;
const size_t maxFileSzieMb = 50; // max 50MB
if (!CheckExternalFile(path, strlen(path), maxFileSzieMb, true)) {
return;
}
} }
WriteLogInfo(path, PATH_MAX + 1, buffer, bufferSize); WriteLogInfo(path, PATH_MAX + 1, buffer, bufferSize);
} }
@@ -199,30 +201,32 @@ void WriteLogFile(const char* filename, long maxSize, const char* buffer, unsign
return; return;
} }
if (filename != NULL && buffer != NULL) { LogFileProcess(filename, maxSize, buffer, bufferSize);
LogFileProcess(filename, maxSize, buffer, bufferSize);
}
} }
static void DivertAndWrite(const char *logPath, const char *msg, const int level) static void DivertAndWrite(const char *logPath, const char *msg, const int level)
{ {
int ret; int ret;
char* buffer = malloc(LOG_LENGTH); size_t destMax = LOG_LENGTH;
if (destMax <= 0) {
return;
}
char* buffer = (char*)malloc(destMax * sizeof(char));
if (buffer == NULL) { if (buffer == NULL) {
return; return;
} }
switch (level) { switch (level) {
case LEVEL_DEBUG: case LEVEL_DEBUG:
ret = sprintf_s(buffer, LOG_LENGTH, "[Debug]%s\n", msg); ret = sprintf_s(buffer, destMax, "[Debug]%s\n", msg);
break; break;
case LEVEL_ERROR: case LEVEL_ERROR:
ret = sprintf_s(buffer, LOG_LENGTH, "[Error]%s\n", msg); ret = sprintf_s(buffer, destMax, "[Error]%s\n", msg);
break; break;
case LEVEL_WARN: case LEVEL_WARN:
ret = sprintf_s(buffer, LOG_LENGTH, "[Warn]%s\n", msg); ret = sprintf_s(buffer, destMax, "[Warn]%s\n", msg);
break; break;
default: default:
ret = sprintf_s(buffer, LOG_LENGTH, "[Info]%s\n", msg); ret = sprintf_s(buffer, destMax, "[Info]%s\n", msg);
} }
if (ret < 0) { if (ret < 0) {
free(buffer); free(buffer);

View File

@@ -28,7 +28,7 @@
struct CmdArgs { struct CmdArgs {
char devices[BUF_SIZE]; char devices[BUF_SIZE];
char rootfs[BUF_SIZE]; char rootfs[BUF_SIZE];
int pid; long pid;
char options[BUF_SIZE]; char options[BUF_SIZE];
struct MountList files; struct MountList files;
struct MountList dirs; struct MountList dirs;
@@ -224,7 +224,7 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
return false; return false;
} }
if (args->files.count == MAX_MOUNT_NR) { if (args->files.count >= MAX_MOUNT_NR) {
char* str = FormatLogMessage("too many files to mount, max number is %u", MAX_MOUNT_NR); char* str = FormatLogMessage("too many files to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR, SCREEN_YES); Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str); free(str);
@@ -239,7 +239,6 @@ static bool MountFileCmdArgParser(struct CmdArgs *args, const char *arg)
free(str); free(str);
return false; return false;
} }
const size_t maxFileSzieMb = 50; // max 50MB const size_t maxFileSzieMb = 50; // max 50MB
if (!CheckFileLegality(dst, strlen(dst), maxFileSzieMb)) { if (!CheckFileLegality(dst, strlen(dst), maxFileSzieMb)) {
char* str = FormatLogMessage("failed to check files: %s", dst); char* str = FormatLogMessage("failed to check files: %s", dst);
@@ -258,7 +257,7 @@ static bool MountDirCmdArgParser(struct CmdArgs *args, const char *arg)
return false; return false;
} }
if (args->dirs.count == MAX_MOUNT_NR) { if (args->dirs.count >= MAX_MOUNT_NR) {
char* str = FormatLogMessage("too many directories to mount, max number is %u", MAX_MOUNT_NR); char* str = FormatLogMessage("too many directories to mount, max number is %u", MAX_MOUNT_NR);
Logger(str, LEVEL_ERROR, SCREEN_YES); Logger(str, LEVEL_ERROR, SCREEN_YES);
free(str); free(str);
@@ -335,7 +334,7 @@ static inline bool IsCmdArgsValid(const struct CmdArgs *args)
return (strlen(args->devices) > 0) && (strlen(args->rootfs) > 0) && (args->pid > 0); return (strlen(args->devices) > 0) && (strlen(args->rootfs) > 0) && (args->pid > 0);
} }
static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *devices) static int ParseDeviceIDs(size_t *idList, size_t *idListSize, char *devices)
{ {
if (idList == NULL || idListSize == NULL || devices == NULL) { if (idList == NULL || idListSize == NULL || devices == NULL) {
Logger("idList, idListSize, devices pointer is null!", LEVEL_ERROR, SCREEN_YES); Logger("idList, idListSize, devices pointer is null!", LEVEL_ERROR, SCREEN_YES);
@@ -348,7 +347,7 @@ static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *device
size_t idx = 0; size_t idx = 0;
token = strtok_s(devices, sep, &context); token = strtok_s(devices, sep, &context);
while (token != NULL) { while (token != NULL && idx < *idListSize) {
if (idx >= *idListSize) { if (idx >= *idListSize) {
char* str = FormatLogMessage("too many devices(%u), support %u devices maximally", idx, *idListSize); char* str = FormatLogMessage("too many devices(%u), support %u devices maximally", idx, *idListSize);
Logger(str, LEVEL_ERROR, SCREEN_YES); Logger(str, LEVEL_ERROR, SCREEN_YES);

View File

@@ -13,7 +13,7 @@
#include "utils.h" #include "utils.h"
#include "logger.h" #include "logger.h"
int GetNsPath(const int pid, const char *nsType, char *buf, const size_t bufSize) int GetNsPath(const long pid, const char *nsType, char *buf, const size_t bufSize)
{ {
if ((nsType == NULL) || (buf == NULL)) { if ((nsType == NULL) || (buf == NULL)) {
return -1; return -1;

View File

@@ -7,7 +7,7 @@
#include <sys/types.h> #include <sys/types.h>
int GetNsPath(int pid, const char *nsType, char *buf, const size_t bufSize); int GetNsPath(const long pid, const char *nsType, char *buf, const size_t bufSize);
int GetSelfNsPath(const char *nsType, char *buf, const size_t bufSize); int GetSelfNsPath(const char *nsType, char *buf, const size_t bufSize);
int EnterNsByFd(int fd, int nsType); int EnterNsByFd(int fd, int nsType);
int EnterNsByPath(const char *path, int nsType); int EnterNsByPath(const char *path, int nsType);

View File

@@ -35,6 +35,10 @@ void ParseRuntimeOptions(const char *options)
static const char *seperator = ","; static const char *seperator = ",";
char *runtimeOptions = strdup(options); char *runtimeOptions = strdup(options);
if (runtimeOptions == NULL) {
(void)fprintf(stderr, "strdup failed!\n");
return;
}
char *context = NULL; char *context = NULL;
char *token = NULL; char *token = NULL;
@@ -42,7 +46,7 @@ void ParseRuntimeOptions(const char *options)
token != NULL; token != NULL;
token = strtok_s(NULL, seperator, &context)) { token = strtok_s(NULL, seperator, &context)) {
for (int i = 0; g_optionNameFlagTable[i].name != NULL; i++) { for (int i = 0; g_optionNameFlagTable[i].name != NULL; i++) {
if (!strcmp((const char *)token, g_optionNameFlagTable[i].name)) { if (strcmp((const char *)token, g_optionNameFlagTable[i].name) == 0) {
*g_optionNameFlagTable[i].flag = true; *g_optionNameFlagTable[i].flag = true;
} }
} }

View File

@@ -19,8 +19,10 @@
static bool checkSrcFile(const char *src) static bool checkSrcFile(const char *src)
{ {
struct stat fileStat; struct stat fileStat;
if ((stat(src, &fileStat) == 0) && if (stat(src, &fileStat) != 0) {
((S_ISREG(fileStat.st_mode) != 0) || (S_ISDIR(fileStat.st_mode) != 0))) { // 只校验文件和目录 return -1; // 待挂载源文件不存在
}
if ((S_ISREG(fileStat.st_mode) != 0) || (S_ISDIR(fileStat.st_mode) != 0)) { // 只校验文件和目录
const size_t maxFileSzieMb = 10 * 1024; // max 10 G const size_t maxFileSzieMb = 10 * 1024; // max 10 G
if (!CheckExternalFile(src, strlen(src), maxFileSzieMb, false)) { if (!CheckExternalFile(src, strlen(src), maxFileSzieMb, false)) {
char* str = FormatLogMessage("failed to mount src: %s.", src); char* str = FormatLogMessage("failed to mount src: %s.", src);
@@ -187,7 +189,7 @@ int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDe
return 0; return 0;
} }
int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) int DoDeviceMounting(const char *rootfs, const char *device_name, const size_t ids[], size_t idsNr)
{ {
if (rootfs == NULL || device_name == NULL || ids == NULL) { if (rootfs == NULL || device_name == NULL || ids == NULL) {
Logger("rootfs, device_name pointer is null!", LEVEL_ERROR, SCREEN_YES); Logger("rootfs, device_name pointer is null!", LEVEL_ERROR, SCREEN_YES);

View File

@@ -17,6 +17,8 @@
#include "securec.h" #include "securec.h"
#include "logger.h" #include "logger.h"
static bool g_checkWgroup = true;
char *FormatLogMessage(char *format, ...) char *FormatLogMessage(char *format, ...)
{ {
if (format == NULL) { if (format == NULL) {
@@ -28,11 +30,12 @@ char *FormatLogMessage(char *format, ...)
// 获取格式化后字符串的长度 // 获取格式化后字符串的长度
va_start(list, format); va_start(list, format);
char buff[1024] = {0}; char buff[1024] = {0};
int size = vsnprintf_s(buff, sizeof(buff), sizeof(buff) - 1, format, list); int ret = vsnprintf_s(buff, sizeof(buff), sizeof(buff) - 1, format, list);
va_end(list); va_end(list);
if (size <= 0) { if (ret <= 0) {
return NULL; return NULL;
} }
size_t size = (size_t)ret;
size++; size++;
// 复位va_list, 将格式化字符串写入到buf // 复位va_list, 将格式化字符串写入到buf
va_start(list, format); va_start(list, format);
@@ -40,7 +43,7 @@ char *FormatLogMessage(char *format, ...)
if (buf == NULL) { if (buf == NULL) {
return NULL; return NULL;
} }
int ret = vsnprintf_s(buf, size, size - 1, format, list); ret = vsnprintf_s(buf, size, size - 1, format, list);
va_end(list); va_end(list);
if (ret <= 0) { if (ret <= 0) {
free(buf); free(buf);
@@ -54,12 +57,12 @@ int IsStrEqual(const char *s1, const char *s2)
return (strcmp(s1, s2) == 0); return (strcmp(s1, s2) == 0);
} }
int StrHasPrefix(const char *str, const char *prefix) bool StrHasPrefix(const char *str, const char *prefix)
{ {
return (!strncmp(str, prefix, strlen(prefix))); return (strncmp(str, prefix, strlen(prefix)) == 0);
} }
int MkDir(const char *dir, int mode) static int MkDir(const char *dir, mode_t mode)
{ {
if (dir == NULL) { if (dir == NULL) {
(void)fprintf(stderr, "dir pointer is null!\n"); (void)fprintf(stderr, "dir pointer is null!\n");
@@ -195,11 +198,11 @@ static bool CheckFileOwner(const struct stat fileStat, const bool checkOwner)
return true; return true;
} }
static bool CheckParentDir(const char* resolvedPath, const size_t resolvedPathLen, static bool CheckParentDir(const char* filePath, const size_t filePathLen,
struct stat fileStat, const bool checkOwner) struct stat fileStat, const bool checkOwner)
{ {
char buf[PATH_MAX] = {0}; char buf[PATH_MAX] = {0};
if (strncpy_s(buf, sizeof(buf), resolvedPath, resolvedPathLen) != EOK) { if (strncpy_s(buf, sizeof(buf), filePath, filePathLen) != EOK) {
return false; return false;
} }
for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) { for (int iLoop = 0; iLoop < PATH_MAX; iLoop++) {
@@ -209,32 +212,42 @@ static bool CheckParentDir(const char* resolvedPath, const size_t resolvedPathLe
if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写 if ((fileStat.st_mode & S_IWOTH) != 0) { // 操作文件对other用户可写
return ShowExceptionInfo("Please check the write permission!"); return ShowExceptionInfo("Please check the write permission!");
} }
if (g_checkWgroup && ((fileStat.st_mode & S_IWGRP) != 0)) { // 除日志文件外对group可写
return ShowExceptionInfo("Please check the write permission!");
}
if (S_ISLNK(fileStat.st_mode) != 0) { // 存在软链接
return ShowExceptionInfo("resolvedPath is symbolic link!");
}
if ((strcmp(buf, "/") == 0) || (strstr(buf, "/") == NULL)) { if ((strcmp(buf, "/") == 0) || (strstr(buf, "/") == NULL)) {
break; break;
} }
if (strcmp(dirname(buf), ".") == 0) { if (strcmp(dirname(buf), ".") == 0) {
break; break;
} }
if (stat(buf, &fileStat) != 0) { if (lstat(buf, &fileStat) != 0) {
return false; return false;
} }
} }
return true; return true;
} }
static bool CheckLegality(const char* resolvedPath, const size_t resolvedPathLen, static bool CheckLegality(const char* filePath, const size_t filePathLen,
const unsigned long long maxFileSzieMb, const bool checkOwner) const unsigned long long maxFileSzieMb, const bool checkOwner)
{ {
const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024; const unsigned long long maxFileSzieB = maxFileSzieMb * 1024 * 1024;
struct stat fileStat; char buf[PATH_MAX] = {0};
if ((stat(resolvedPath, &fileStat) != 0) || if (strncpy_s(buf, sizeof(buf), filePath, filePathLen) != EOK) {
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) { return false;
return ShowExceptionInfo("resolvedPath does not exist or is not a file!");
} }
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限 struct stat fileStat;
if ((lstat(buf, &fileStat) != 0) ||
((S_ISREG(fileStat.st_mode) == 0) && (S_ISDIR(fileStat.st_mode) == 0))) {
return ShowExceptionInfo("filePath does not exist or is not a file/dir!");
}
if ((maxFileSzieMb > 0) && (fileStat.st_size >= maxFileSzieB)) { // 文件大小超限,日志文件不校验大小,由轮滚机制保护
return ShowExceptionInfo("fileSize out of bounds!"); return ShowExceptionInfo("fileSize out of bounds!");
} }
return CheckParentDir(resolvedPath, resolvedPathLen, fileStat, checkOwner); return CheckParentDir(filePath, filePathLen, fileStat, checkOwner);
} }
bool IsValidChar(const char c) bool IsValidChar(const char c)
@@ -261,14 +274,26 @@ bool CheckExternalFile(const char* filePath, const size_t filePathLen,
return ShowExceptionInfo("filePath has an illegal character!"); return ShowExceptionInfo("filePath has an illegal character!");
} }
} }
char resolvedPath[PATH_MAX] = {0}; return CheckLegality(filePath, filePathLen, maxFileSzieMb, checkOwner);
if (realpath(filePath, resolvedPath) == NULL && errno != ENOENT) { }
return ShowExceptionInfo("realpath failed!");
bool CheckExistsFile(const char* filePath, const size_t filePathLen,
const size_t maxFileSzieMb, const bool checkWgroup)
{
struct stat fileStat;
if (lstat(filePath, &fileStat) != 0) {
return true; // 文件不存在
} }
if (strcmp(resolvedPath, filePath) != 0) { // 存在软链接 if (S_ISREG(fileStat.st_mode) == 0) { // 不是文件
return ShowExceptionInfo("filePath has a soft link!"); return false;
} }
return CheckLegality(resolvedPath, strlen(resolvedPath), maxFileSzieMb, checkOwner); g_checkWgroup = checkWgroup;
if (!CheckExternalFile(filePath, filePathLen, maxFileSzieMb, true)) {
g_checkWgroup = true;
return false;
}
g_checkWgroup = true;
return true;
} }
static bool CheckFileSubset(const char* filePath, const size_t filePathLen, static bool CheckFileSubset(const char* filePath, const size_t filePathLen,
@@ -284,23 +309,19 @@ static bool CheckFileSubset(const char* filePath, const size_t filePathLen,
return ShowExceptionInfo("filePath has an illegal character!"); return ShowExceptionInfo("filePath has an illegal character!");
} }
} }
char resolvedPath[PATH_MAX] = {0};
if (realpath(filePath, resolvedPath) == NULL && errno != ENOENT) {
return ShowExceptionInfo("realpath failed!");
}
if (strcmp(resolvedPath, filePath) != 0) { // 存在软链接
return ShowExceptionInfo("filePath has a soft link!");
}
struct stat fileStat; struct stat fileStat;
if (stat(filePath, &fileStat) != 0) { if (lstat(filePath, &fileStat) != 0) {
return ShowExceptionInfo("filePath does not exist or is not a file!"); return ShowExceptionInfo("filePath does not exist!");
}
if (S_ISLNK(fileStat.st_mode) != 0) { // 存在软链接
return ShowExceptionInfo("filePath is symbolic link!");
} }
if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限 if (fileStat.st_size >= maxFileSzieB) { // 文件大小超限
return ShowExceptionInfo("fileSize out of bounds!"); return ShowExceptionInfo("fileSize out of bounds!");
} }
return true; return true;
} }
bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen) bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen)
{ {
DIR *dir = NULL; DIR *dir = NULL;
@@ -324,7 +345,7 @@ bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen)
} }
if (ptr->d_type == DT_REG) { // 文件 if (ptr->d_type == DT_REG) { // 文件
const size_t maxFileSzieMb = 10; // max 10 MB const size_t maxFileSzieMb = 10; // max 10 MB
if (!CheckFileSubset(base, strlen(base), maxFileSzieMb)) { if (!(base, strlen(base), maxFileSzieMb)) {
return false; return false;
} }
} else if (ptr->d_type == DT_LNK) { // 软链接 } else if (ptr->d_type == DT_LNK) { // 软链接

View File

@@ -15,8 +15,7 @@
char *FormatLogMessage(char *format, ...); char *FormatLogMessage(char *format, ...);
int IsStrEqual(const char *s1, const char *s2); int IsStrEqual(const char *s1, const char *s2);
int StrHasPrefix(const char *str, const char *prefix); bool StrHasPrefix(const char *str, const char *prefix);
int MkDir(const char *dir, int mode);
int VerifyPathInfo(const struct PathInfo* pathInfo); int VerifyPathInfo(const struct PathInfo* pathInfo);
int CheckDirExists(const char *dir); int CheckDirExists(const char *dir);
int GetParentPathStr(const char *path, char *parent, size_t bufSize); int GetParentPathStr(const char *path, char *parent, size_t bufSize);
@@ -26,4 +25,6 @@ bool IsValidChar(const char c);
bool CheckExternalFile(const char* filePath, const size_t filePathLen, bool CheckExternalFile(const char* filePath, const size_t filePathLen,
const size_t maxFileSzieMb, const bool checkOwner); const size_t maxFileSzieMb, const bool checkOwner);
bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen); bool GetFileSubsetAndCheck(const char *basePath, const size_t basePathLen);
bool CheckExistsFile(const char* filePath, const size_t filePathLen,
const size_t maxFileSzieMb, const bool checkWgroup);
#endif #endif

View File

@@ -11,10 +11,15 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "mockcpp/mockcpp.hpp" #include "mockcpp/mockcpp.hpp"
using namespace std; using namespace std;
using namespace testing; using namespace testing;
#ifndef GOOGLE_TEST
# define STATIC static
#else
# define STATIC
#endif
#define DAVINCI_MANAGER_PATH "/dev/davinci_manager" #define DAVINCI_MANAGER_PATH "/dev/davinci_manager"
#define DEVICE_NAME "davinci" #define DEVICE_NAME "davinci"
#define BUF_SIZE 1024 #define BUF_SIZE 1024
@@ -30,15 +35,15 @@ extern "C" int stat(const char *file_name, struct stat *buf);
extern "C" int mount(const char *source, const char *target, extern "C" int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags, const void *data); const char *filesystemtype, unsigned long mountflags, const void *data);
extern "C" int Mount(const char *src, const char *dst); extern "C" int Mount(const char *src, const char *dst);
extern "C" int MkDir(const char *dir, int mode); STATIC int MkDir(const char *dir, mode_t mode);
extern "C" int rmdir(const char *pathname); extern "C" int rmdir(const char *pathname);
extern "C" int EnterNsByFd(int fd, int nsType); extern "C" int EnterNsByFd(int fd, int nsType);
extern "C" int StrHasPrefix(const char *str, const char *prefix); extern "C" bool StrHasPrefix(const char *str, const char *prefix);
extern "C" int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize); extern "C" int GetNsPath(const int pid, const char *nsType, char *buf, size_t bufSize);
extern "C" int GetSelfNsPath(const char *nsType, char *buf, size_t bufSize); extern "C" int GetSelfNsPath(const char *nsType, char *buf, size_t bufSize);
extern "C" int EnterNsByPath(const char *path, int nsType); extern "C" int EnterNsByPath(const char *path, int nsType);
extern "C" int MountDevice(const char *rootfs, const char *deviceName); extern "C" int MountDevice(const char *rootfs, const char *deviceName);
extern "C" int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr); extern "C" int DoDeviceMounting(const char *rootfs, const char *device_name, const size_t ids[], size_t idsNr);
extern "C" int CheckDirExists(char *dir, int len); extern "C" int CheckDirExists(char *dir, int len);
extern "C" int GetParentPathStr(const char *path, char *parent, size_t bufSize); extern "C" int GetParentPathStr(const char *path, char *parent, size_t bufSize);
extern "C" int MakeDirWithParent(const char *path, mode_t mode); extern "C" int MakeDirWithParent(const char *path, mode_t mode);
@@ -222,12 +227,12 @@ int Stub_EnterNsByPath_Failed(const char *path, int nsType)
return 0; return 0;
} }
int Stub_DoDeviceMounting_Success(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) int Stub_DoDeviceMounting_Success(const char *rootfs, const char *device_name, const size_t ids[], size_t idsNr)
{ {
return 0; return 0;
} }
int Stub_DoDeviceMounting_Failed(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) int Stub_DoDeviceMounting_Failed(const char *rootfs, const char *device_name, const size_t ids[], size_t idsNr)
{ {
return -1; return -1;
} }
@@ -461,7 +466,7 @@ TEST_F(Test_Fhho, StatusOneDoDeviceMounting)
{ {
MOCKER(MountDevice).stubs().will(invoke(Stub_MountDevice_Success)); MOCKER(MountDevice).stubs().will(invoke(Stub_MountDevice_Success));
char *rootfs = "/home"; char *rootfs = "/home";
unsigned int devicesList[2] = {1, 2}; size_t devicesList[2] = {1, 2};
size_t idNr = 2; size_t idNr = 2;
char *device_name = "davinci"; char *device_name = "davinci";
int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr); int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr);
@@ -473,7 +478,7 @@ TEST_F(Test_Fhho, StatusTwoDoDeviceMounting)
{ {
MOCKER(MountDevice).stubs().will(invoke(Stub_MountDevice_Failed)); MOCKER(MountDevice).stubs().will(invoke(Stub_MountDevice_Failed));
char *rootfs = "/home"; char *rootfs = "/home";
unsigned int devicesList[2] = {1, 2}; size_t devicesList[2] = {1, 2};
size_t idNr = 2; size_t idNr = 2;
char *device_name = "davinci"; char *device_name = "davinci";
int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr); int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr);
@@ -630,17 +635,6 @@ TEST_F(Test_Fhho, MakeMountPoints1)
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);
} }
TEST_F(Test_Fhho, MkDirtestsuccess)
{
// The test create directory contains the parent directory
mode_t mode = 0755;
char *dir = "/home";
int ret = MkDir(dir, mode);
EXPECT_EQ(-1, ret);
}
TEST_F(Test_Fhho, LogLoopSuccess) TEST_F(Test_Fhho, LogLoopSuccess)
{ {
// The test create directory contains the parent directory // The test create directory contains the parent directory
@@ -659,6 +653,16 @@ TEST_F(Test_Fhho, StatusTwoMakeDirWithParent)
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
} }
#ifdef GOOGLE_TEST
TEST_F(Test_Fhho, MkDirtestsuccess)
{
// The test create directory contains the parent directory
mode_t mode = 0755;
char *dir = "/home";
int ret = MkDir(dir, mode);
EXPECT_EQ(-1, ret);
}
TEST_F(Test_Fhho, StatusThreeMakeDirWithParent) TEST_F(Test_Fhho, StatusThreeMakeDirWithParent)
{ {
char *pathData = "/path/abc/abcd"; char *pathData = "/path/abc/abcd";
@@ -673,6 +677,18 @@ TEST_F(Test_Fhho, StatusThreeMakeDirWithParent)
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
} }
TEST_F(Test_Fhho, StatusThreeMountDir)
{
MOCKER(CheckDirExists).stubs().will(invoke(Stub_CheckDirExists_Failed));
MOCKER(MkDir).stubs().will(invoke(stub_MkDir_failed));
char *rootfs = "/rootfs";
unsigned long reMountRwFlag = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC;
int ret = MountDir(rootfs, "/home", reMountRwFlag);
GlobalMockObject::verify();
EXPECT_EQ(-1, ret);
}
#endif
TEST_F(Test_Fhho, StatusOneMountDir) TEST_F(Test_Fhho, StatusOneMountDir)
{ {
MOCKER(stat).stubs().will(invoke(stub_stat_failed)); MOCKER(stat).stubs().will(invoke(stub_stat_failed));
@@ -694,17 +710,6 @@ TEST_F(Test_Fhho, StatusTwoMountDir)
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);
} }
TEST_F(Test_Fhho, StatusThreeMountDir)
{
MOCKER(CheckDirExists).stubs().will(invoke(Stub_CheckDirExists_Failed));
MOCKER(MkDir).stubs().will(invoke(stub_MkDir_failed));
char *rootfs = "/rootfs";
unsigned long reMountRwFlag = MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC;
int ret = MountDir(rootfs, "/home", reMountRwFlag);
GlobalMockObject::verify();
EXPECT_EQ(-1, ret);
}
TEST_F(Test_Fhho, StatusFourMountDir) TEST_F(Test_Fhho, StatusFourMountDir)
{ {
MOCKER(CheckDirExists).stubs().will(invoke(Stub_CheckDirExists_Failed)); MOCKER(CheckDirExists).stubs().will(invoke(Stub_CheckDirExists_Failed));
@@ -788,7 +793,7 @@ TEST_F(Test_Fhho, StatusOneSetupDeviceCgroup)
MOCKER(stat).stubs().will(invoke(stub_stat_failed)); MOCKER(stat).stubs().will(invoke(stub_stat_failed));
int ret = SetupDeviceCgroup(cgroupAllow, cgroupPath); int ret = SetupDeviceCgroup(cgroupAllow, cgroupPath);
if (cgroupAllow != NULL) { if (cgroupAllow != NULL) {
fclose(cgroupAllow); (void)fclose(cgroupAllow);
} }
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);
} }
@@ -803,7 +808,7 @@ TEST_F(Test_Fhho, StatusTwoSetupDeviceCgroup)
MOCKER(stat).stubs().will(invoke(stub_stat_success)); MOCKER(stat).stubs().will(invoke(stub_stat_success));
int ret = SetupDeviceCgroup(cgroupAllow, cgroupPath); int ret = SetupDeviceCgroup(cgroupAllow, cgroupPath);
if (cgroupAllow != NULL) { if (cgroupAllow != NULL) {
fclose(cgroupAllow); (void)fclose(cgroupAllow);
} }
GlobalMockObject::verify(); GlobalMockObject::verify();
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);
@@ -817,7 +822,7 @@ TEST_F(Test_Fhho, StatusOneSetupDriverCgroup)
MOCKER(SetupDeviceCgroup).stubs().will(invoke(Stub_SetupDeviceCgroup_Success)); MOCKER(SetupDeviceCgroup).stubs().will(invoke(Stub_SetupDeviceCgroup_Success));
int ret = SetupDriverCgroup(cgroupAllow); int ret = SetupDriverCgroup(cgroupAllow);
if (cgroupAllow != NULL) { if (cgroupAllow != NULL) {
fclose(cgroupAllow); (void)fclose(cgroupAllow);
} }
GlobalMockObject::verify(); GlobalMockObject::verify();
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
@@ -831,7 +836,7 @@ TEST_F(Test_Fhho, StatusTwoSetupDriverCgroup)
MOCKER(SetupDeviceCgroup).stubs().will(invoke(Stub_SetupDeviceCgroup_Failed)); MOCKER(SetupDeviceCgroup).stubs().will(invoke(Stub_SetupDeviceCgroup_Failed));
int ret = SetupDriverCgroup(cgroupAllow); int ret = SetupDriverCgroup(cgroupAllow);
if (cgroupAllow != NULL) { if (cgroupAllow != NULL) {
fclose(cgroupAllow); (void)fclose(cgroupAllow);
} }
GlobalMockObject::verify(); GlobalMockObject::verify();
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);