From: Christian Brauner Date: Sat, 2 Sep 2017 00:26:28 +0000 (+0200) Subject: lxc-user-nic: fix adding database entries X-Git-Tag: lxc-2.0.9~48^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9597e061fc86375a402a9d82b3dc586dec5a8003;p=thirdparty%2Flxc.git lxc-user-nic: fix adding database entries The code before inserted \0-bytes after every new line which made the db basically unusable. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c index 9164e44dc..39c7daabf 100644 --- a/src/lxc/lxc_user_nic.c +++ b/src/lxc/lxc_user_nic.c @@ -678,7 +678,7 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, char *intype, char *br, int allowed, char **cnic) { int ret; - off_t len, slen; + size_t slen; char *newline, *nicname, *owner; struct stat sb; struct alloted_s *n; @@ -693,31 +693,32 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, owner = names->name; - if (fstat(fd, &sb) < 0) { + ret = fstat(fd, &sb); + if (ret < 0) { usernic_error("Failed to fstat: %s\n", strerror(errno)); return NULL; } - len = sb.st_size; - if (len > 0) { - buf = - mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (sb.st_size > 0) { + buf = lxc_strmmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { - usernic_error("Failed to establish shared memory mapping: %s\n", - strerror(errno)); + usernic_error("Failed to establish shared memory " + "mapping: %s\n", strerror(errno)); return NULL; } owner = NULL; for (n = names; n != NULL; n = n->next) { - count = count_entries(buf, len, n->name, intype, br); - + count = count_entries(buf, sb.st_size, n->name, intype, br); if (count >= n->allowed) continue; owner = n->name; break; } + + lxc_strmunmap(buf, sb.st_size); } if (owner == NULL) @@ -729,41 +730,67 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, return NULL; } - /* owner ' ' intype ' ' br ' ' *nicname + '\n' + '\0' */ - slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 5; - newline = alloca(slen); + /* strlen(owner) + * + + * " " + * + + * strlen(intype) + * + + * " " + * + + * strlen(br) + * + + * " " + * + + * strlen(nicname) + * + + * \n + * + + * \0 + */ + slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4; + newline = malloc(slen + 1); if (!newline) { free(nicname); + free(newline); usernic_error("Failed allocate memory: %s\n", strerror(errno)); return NULL; } - ret = snprintf(newline, slen, "%s %s %s %s\n", owner, intype, br, nicname); - if (ret < 0 || ret >= slen) { + ret = snprintf(newline, slen + 1, "%s %s %s %s\n", owner, intype, br, nicname); + if (ret < 0 || (size_t)ret >= (slen + 1)) { if (lxc_netdev_delete_by_name(nicname) != 0) usernic_error("Error unlinking %s\n", nicname); free(nicname); + free(newline); return NULL; } - if (len) - munmap(buf, len); - if (ftruncate(fd, len + slen)) - usernic_error("Failed to set new file size: %s\n", - strerror(errno)); + /* Note that the file needs to be truncated to the size **without** the + * \0 byte! Files are not \0-terminated! + */ + ret = ftruncate(fd, sb.st_size + slen); + if (ret < 0) + usernic_error("Failed to truncate file: %s\n", strerror(errno)); - buf = mmap(NULL, len + slen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + buf = lxc_strmmap(NULL, sb.st_size + slen, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { usernic_error("Failed to establish shared memory mapping: %s\n", strerror(errno)); if (lxc_netdev_delete_by_name(nicname) != 0) usernic_error("Error unlinking %s\n", nicname); free(nicname); + free(newline); return NULL; } - strcpy(buf + len, newline); - munmap(buf, len + slen); + /* Note that the memory needs to be moved in the buffer **without** the + * \0 byte! Files are not \0-terminated! + */ + memmove(buf + sb.st_size, newline, slen); + free(newline); + lxc_strmunmap(buf, sb.st_size + slen); return nicname; }