phyp: Fix too small buffer allocation in phypAttachDevice

sizeof(domain->name) is the wrong thing. Instead of using strdup here
rewrite escape_specialcharacters to allocate the buffer itself.

Add a contains_specialcharacters to be used in phypOpen, as phypOpen is
not interested in the escaped version.
This commit is contained in:
Matthias Bolte 2011-04-09 11:59:10 +02:00
parent e13e1f4f5d
commit 28e938a9ec

View File

@ -894,54 +894,61 @@ phypUUIDTable_Free(uuid_tablePtr uuid_table)
VIR_FREE(uuid_table); VIR_FREE(uuid_table);
} }
static int #define SPECIALCHARACTER_CASES \
escape_specialcharacters(char *src, char *dst, size_t dstlen) case '&': case ';': case '`': case '@': case '"': case '|': case '*': \
case '?': case '~': case '<': case '>': case '^': case '(': case ')': \
case '[': case ']': case '{': case '}': case '$': case '%': case '#': \
case '\\': case '\n': case '\r': case '\t':
static bool
contains_specialcharacters(const char *src)
{ {
size_t len = strlen(src); size_t len = strlen(src);
char temp_buffer[len]; size_t i = 0;
unsigned int i = 0, j = 0;
if (len == 0) if (len == 0)
return -1; return false;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
switch (src[i]) { switch (src[i]) {
case '&': SPECIALCHARACTER_CASES
case ';': return true;
case '`': default:
case '@': continue;
case '"':
case '|':
case '*':
case '?':
case '~':
case '<':
case '>':
case '^':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '$':
case '%':
case '#':
case '\\':
case '\n':
case '\r':
case '\t':
continue;
default:
temp_buffer[j] = src[i];
j++;
} }
} }
temp_buffer[j] = '\0';
if (virStrcpy(dst, temp_buffer, dstlen) == NULL) return false;
return -1; }
return 0; static char *
escape_specialcharacters(const char *src)
{
size_t len = strlen(src);
size_t i = 0, j = 0;
char *dst;
if (len == 0)
return NULL;
if (VIR_ALLOC_N(dst, len + 1) < 0) {
virReportOOMError();
return NULL;
}
for (i = 0; i < len; i++) {
switch (src[i]) {
SPECIALCHARACTER_CASES
continue;
default:
dst[j] = src[i];
j++;
}
}
dst[j] = '\0';
return dst;
} }
static LIBSSH2_SESSION * static LIBSSH2_SESSION *
@ -1124,8 +1131,6 @@ phypOpen(virConnectPtr conn,
{ {
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
ConnectionData *connection_data = NULL; ConnectionData *connection_data = NULL;
char *string = NULL;
size_t len = 0;
int internal_socket; int internal_socket;
uuid_tablePtr uuid_table = NULL; uuid_tablePtr uuid_table = NULL;
phyp_driverPtr phyp_driver = NULL; phyp_driverPtr phyp_driver = NULL;
@ -1160,13 +1165,6 @@ phypOpen(virConnectPtr conn,
} }
if (conn->uri->path) { if (conn->uri->path) {
len = strlen(conn->uri->path) + 1;
if (VIR_ALLOC_N(string, len) < 0) {
virReportOOMError();
goto failure;
}
/* need to shift one byte in order to remove the first "/" of URI component */ /* need to shift one byte in order to remove the first "/" of URI component */
if (conn->uri->path[0] == '/') if (conn->uri->path[0] == '/')
managed_system = strdup(conn->uri->path + 1); managed_system = strdup(conn->uri->path + 1);
@ -1186,7 +1184,7 @@ phypOpen(virConnectPtr conn,
if (char_ptr) if (char_ptr)
*char_ptr = '\0'; *char_ptr = '\0';
if (escape_specialcharacters(conn->uri->path, string, len) == -1) { if (contains_specialcharacters(conn->uri->path)) {
PHYP_ERROR(VIR_ERR_INTERNAL_ERROR, PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
"%s", "%s",
_("Error parsing 'path'. Invalid characters.")); _("Error parsing 'path'. Invalid characters."));
@ -1245,7 +1243,6 @@ phypOpen(virConnectPtr conn,
} }
VIR_FREE(connection_data); VIR_FREE(connection_data);
VIR_FREE(string);
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
} }
@ -1950,14 +1947,9 @@ phypAttachDevice(virDomainPtr domain, const char *xml)
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *domain_name = NULL; char *domain_name = NULL;
if (VIR_ALLOC_N(domain_name, sizeof(domain->name)) < 0) { domain_name = escape_specialcharacters(domain->name);
virReportOOMError();
goto cleanup;
}
if (escape_specialcharacters if (domain_name == NULL) {
(domain->name, domain_name, strlen(domain->name)) == -1) {
virReportOOMError();
goto cleanup; goto cleanup;
} }