tools: acrnctl fix cmd buffer is truncated when vmname too long

'acrnctl start vmname' use a cmd buffer with a sizeof 128 bytes,
cmd[128] buffer will be truncated, when vmnane size is bigger than
29 bytes. In that situation, vmnane size still smaller than
MAX_VM_OS_NAME_LEN, so it should be fixed.
Macro PATH_LEN is useful to determine the cmd[] buffer length, so move
it from acrn_mngr.c into acrn_mngr.h

Tracked-On: #2851
Signed-off-by: Tao Yuhong <yuhong.tao@intel.com>
Reviewed-by: Yan Like <like.yan@intel.com>
Acked-by: Yin Fengwei <fengwei.yin@intel.com>
This commit is contained in:
yuhong.tao@intel.com 2019-03-11 20:56:02 +08:00 committed by wenlingz
parent b2f2d95246
commit 5a7be9b869
4 changed files with 17 additions and 18 deletions

View File

@ -40,7 +40,6 @@ static int check_dir(const char *path)
#define MNGR_SOCK_FMT "/run/acrn/mngr/%s.%d.socket" #define MNGR_SOCK_FMT "/run/acrn/mngr/%s.%d.socket"
#define MNGR_MAX_HANDLER 8 #define MNGR_MAX_HANDLER 8
#define MNGR_MAX_CLIENT 4 #define MNGR_MAX_CLIENT 4
#define PATH_LEN 128
#define CLIENT_BUF_LEN 4096 #define CLIENT_BUF_LEN 4096

View File

@ -10,6 +10,7 @@
#include <acrn_common.h> #include <acrn_common.h>
#define MNGR_MSG_MAGIC 0x67736d206d6d76 /* that is char[8] "mngr msg", on X86 */ #define MNGR_MSG_MAGIC 0x67736d206d6d76 /* that is char[8] "mngr msg", on X86 */
#define PATH_LEN 128
struct mngr_msg { struct mngr_msg {
unsigned long long magic; /* Make sure you get a mngr_msg */ unsigned long long magic; /* Make sure you get a mngr_msg */

View File

@ -127,7 +127,7 @@ static void _scan_alive_vm(void)
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
struct vmmngr_struct *vm; struct vmmngr_struct *vm;
char name[128]; char name[PATH_LEN];
int pid; int pid;
int ret; int ret;
@ -224,8 +224,8 @@ static void _scan_added_vm(void)
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
struct vmmngr_struct *vm; struct vmmngr_struct *vm;
char name[128]; char name[PATH_LEN];
char suffix[128]; char suffix[PATH_LEN];
int ret; int ret;
ret = check_dir(ACRN_CONF_PATH); ret = check_dir(ACRN_CONF_PATH);
@ -375,7 +375,7 @@ int list_vm()
int start_vm(const char *vmname) int start_vm(const char *vmname)
{ {
char cmd[128]; char cmd[PATH_LEN + sizeof(ACRN_CONF_PATH_ADD) * 2 + MAX_VM_OS_NAME_LEN * 2];
if (snprintf(cmd, sizeof(cmd), "bash %s/%s.sh $(cat %s/%s.args)", if (snprintf(cmd, sizeof(cmd), "bash %s/%s.sh $(cat %s/%s.args)",
ACRN_CONF_PATH_ADD, vmname, ACRN_CONF_PATH_ADD, vmname) >= sizeof(cmd)) { ACRN_CONF_PATH_ADD, vmname, ACRN_CONF_PATH_ADD, vmname) >= sizeof(cmd)) {

View File

@ -100,7 +100,7 @@ static int find_acrn_dm;
static int write_tmp_file(int fd, int n, char *word[]) static int write_tmp_file(int fd, int n, char *word[])
{ {
int len, ret, i = 0; int len, ret, i = 0;
char buf[128]; char buf[PATH_LEN];
if (!n) if (!n)
return 0; return 0;
@ -165,7 +165,6 @@ static inline int _get_vmname(const char *src, char *vmname, int max_len_vmname)
} }
#define MAX_FILE_SIZE (4096 * 5) #define MAX_FILE_SIZE (4096 * 5)
#define FILE_NAME_LENGTH 128
#define TMP_FILE_SUFFIX ".acrnctl" #define TMP_FILE_SUFFIX ".acrnctl"
@ -177,16 +176,16 @@ static int acrnctl_do_add(int argc, char *argv[])
char *word[MAX_WORD], *line; char *word[MAX_WORD], *line;
char *word_p = NULL, *line_p = NULL; char *word_p = NULL, *line_p = NULL;
int n_word; int n_word;
char fname[FILE_NAME_LENGTH + sizeof(TMP_FILE_SUFFIX)]; char fname[PATH_LEN + sizeof(TMP_FILE_SUFFIX)];
char cmd[128]; char cmd[PATH_LEN];
char args[128]; char args[PATH_LEN];
int p, i, len_cmd_out = 0, c_flag = 0; int p, i, len_cmd_out = 0, c_flag = 0;
char cmd_out[256]; char cmd_out[PATH_LEN * 2];
char vmname[128]; char vmname[PATH_LEN];
size_t len = sizeof(cmd_out); size_t len = sizeof(cmd_out);
if (strnlen(argv[1], FILE_NAME_LENGTH) == FILE_NAME_LENGTH) { if (strnlen(argv[1], PATH_LEN) == PATH_LEN) {
printf("File name too long (maximum len %d)\n", FILE_NAME_LENGTH); printf("File name too long (maximum len %d)\n", PATH_LEN);
return -1; return -1;
} }
@ -441,9 +440,9 @@ static int acrnctl_do_stop(int argc, char *argv[])
/* Function: Delete runC configuration */ /* Function: Delete runC configuration */
static inline int del_runC(char *argv) static inline int del_runC(char *argv)
{ {
char cmd[128]; char cmd[PATH_LEN];
char cmd_out[256]; char cmd_out[PATH_LEN * 2];
char runc_path[128]; char runc_path[PATH_LEN];
/* The configuration added by launch_uos script */ /* The configuration added by launch_uos script */
if (snprintf(runc_path, sizeof(runc_path), "%s/runc/%s", if (snprintf(runc_path, sizeof(runc_path), "%s/runc/%s",
@ -489,7 +488,7 @@ static int acrnctl_do_del(int argc, char *argv[])
{ {
struct vmmngr_struct *s; struct vmmngr_struct *s;
int i; int i;
char cmd[128]; char cmd[PATH_LEN];
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
s = vmmngr_find(argv[i]); s = vmmngr_find(argv[i]);