From a9afbf4fc5c72e0322536359950f94d9e668ae1b Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 31 Aug 2010 16:56:22 -0600 Subject: [PATCH] openvz: use virAsprintf to avoid large stacks * src/openvz/openvz_conf.c (openvzLocateConfFile): Alter signature. (openvzGetVPSUUID, openvzSetDefinedUUID) (openvzWriteVPSConfigParam, openvzReadVPSConfigParam) (openvzCopyDefaultConfig): Adjust callers. --- src/openvz/openvz_conf.c | 79 +++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index 356c7f00aa..ec11bbc3e9 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -55,7 +55,7 @@ static char *openvzLocateConfDir(void); static int openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len); -static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext); +static int openvzLocateConfFile(int vpsid, char **conffile, const char *ext); static int openvzAssignUUIDs(void); int @@ -615,12 +615,15 @@ error: int openvzWriteVPSConfigParam(int vpsid, const char *param, const char *value) { - char conf_file[PATH_MAX]; + char *conf_file; + int ret; - if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0) + if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) return -1; - return openvzWriteConfigParam(conf_file, param, value); + ret = openvzWriteConfigParam(conf_file, param, value); + VIR_FREE(conf_file); + return ret; } static int @@ -676,12 +679,15 @@ openvzReadConfigParam(const char * conf_file ,const char * param, char *value, i int openvzReadVPSConfigParam(int vpsid ,const char * param, char *value, int maxlen) { - char conf_file[PATH_MAX]; + char *conf_file; + int ret; - if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0) + if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) return -1; - return openvzReadConfigParam(conf_file, param, value, maxlen); + ret = openvzReadConfigParam(conf_file, param, value, maxlen); + VIR_FREE(conf_file); + return ret; } static int @@ -736,7 +742,7 @@ openvzCopyDefaultConfig(int vpsid) char * confdir = NULL; char * default_conf_file = NULL; char configfile_value[PATH_MAX]; - char conf_file[PATH_MAX]; + char *conf_file = NULL; int ret = -1; if (openvzReadConfigParam(VZ_CONF_FILE, "CONFIGFILE", configfile_value, @@ -747,12 +753,13 @@ openvzCopyDefaultConfig(int vpsid) if (confdir == NULL) goto cleanup; - if (virAsprintf(&default_conf_file, "%s/ve-%s.conf-sample", confdir, configfile_value) < 0) { + if (virAsprintf(&default_conf_file, "%s/ve-%s.conf-sample", confdir, + configfile_value) < 0) { virReportOOMError(); goto cleanup; } - if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0) + if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) goto cleanup; if (openvz_copyfile(default_conf_file, conf_file)<0) @@ -762,6 +769,7 @@ openvzCopyDefaultConfig(int vpsid) cleanup: VIR_FREE(confdir); VIR_FREE(default_conf_file); + VIR_FREE(conf_file); return ret; } @@ -770,7 +778,7 @@ cleanup: * 0 - OK */ static int -openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext) +openvzLocateConfFile(int vpsid, char **conffile, const char *ext) { char * confdir; int ret = 0; @@ -779,9 +787,11 @@ openvzLocateConfFile(int vpsid, char *conffile, int maxlen, const char *ext) if (confdir == NULL) return -1; - if (snprintf(conffile, maxlen, "%s/%d.%s", - confdir, vpsid, ext ? ext : "conf") >= maxlen) + if (virAsprintf(conffile, "%s/%d.%s", confdir, vpsid, + ext ? ext : "conf") < 0) { + virReportOOMError(); ret = -1; + } VIR_FREE(confdir); return ret; @@ -830,27 +840,25 @@ openvz_readline(int fd, char *ptr, int maxlen) static int openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len) { - char conf_file[PATH_MAX]; + char *conf_file; char line[1024]; char *saveptr = NULL; char *uuidbuf; char *iden; int fd, ret; - int retval = 0; + int retval = -1; - if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0) + if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) return -1; fd = open(conf_file, O_RDONLY); if (fd == -1) - return -1; + goto cleanup; while (1) { ret = openvz_readline(fd, line, sizeof(line)); - if (ret == -1) { - close(fd); - return -1; - } + if (ret == -1) + goto cleanup; if (ret == 0) { /* EoF, UUID was not found */ uuidstr[0] = 0; @@ -861,12 +869,19 @@ openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len) uuidbuf = strtok_r(NULL, "\n", &saveptr); if (iden != NULL && uuidbuf != NULL && STREQ(iden, "#UUID:")) { - if (virStrcpy(uuidstr, uuidbuf, len) == NULL) - retval = -1; + if (virStrcpy(uuidstr, uuidbuf, len) == NULL) { + openvzError(VIR_ERR_INTERNAL_ERROR, + _("invalid uuid %s"), uuidbuf); + goto cleanup; + } break; } } - close(fd); + retval = 0; +cleanup: + if (fd >= 0) + close(fd); + VIR_FREE(conf_file); return retval; } @@ -877,22 +892,23 @@ openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len) int openvzSetDefinedUUID(int vpsid, unsigned char *uuid) { - char conf_file[PATH_MAX]; + char *conf_file; char uuidstr[VIR_UUID_STRING_BUFLEN]; + int ret = -1; if (uuid == NULL) return -1; - if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX, "conf")<0) + if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) return -1; if (openvzGetVPSUUID(vpsid, uuidstr, sizeof(uuidstr))) - return -1; + goto cleanup; if (uuidstr[0] == 0) { FILE *fp = fopen(conf_file, "a"); /* append */ if (fp == NULL) - return -1; + goto cleanup; virUUIDFormat(uuid, uuidstr); @@ -900,10 +916,13 @@ openvzSetDefinedUUID(int vpsid, unsigned char *uuid) and be careful always to close the stream. */ if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0) + (fclose(fp) == EOF)) - return -1; + goto cleanup; } - return 0; + ret = 0; +cleanup: + VIR_FREE(conf_file); + return ret; } static int