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: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 7ab1ba02
...@@ -678,7 +678,7 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -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) char *intype, char *br, int allowed, char **cnic)
{ {
int ret; int ret;
off_t len, slen; size_t slen;
char *newline, *nicname, *owner; char *newline, *nicname, *owner;
struct stat sb; struct stat sb;
struct alloted_s *n; struct alloted_s *n;
...@@ -693,31 +693,32 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -693,31 +693,32 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
owner = names->name; owner = names->name;
if (fstat(fd, &sb) < 0) { ret = fstat(fd, &sb);
if (ret < 0) {
usernic_error("Failed to fstat: %s\n", strerror(errno)); usernic_error("Failed to fstat: %s\n", strerror(errno));
return NULL; return NULL;
} }
len = sb.st_size; if (sb.st_size > 0) {
if (len > 0) { buf = lxc_strmmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
buf = MAP_SHARED, fd, 0);
mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
usernic_error("Failed to establish shared memory mapping: %s\n", usernic_error("Failed to establish shared memory "
strerror(errno)); "mapping: %s\n", strerror(errno));
return NULL; return NULL;
} }
owner = NULL; owner = NULL;
for (n = names; n != NULL; n = n->next) { 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) if (count >= n->allowed)
continue; continue;
owner = n->name; owner = n->name;
break; break;
} }
lxc_strmunmap(buf, sb.st_size);
} }
if (owner == NULL) if (owner == NULL)
...@@ -729,41 +730,67 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid, ...@@ -729,41 +730,67 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
return NULL; return NULL;
} }
/* owner ' ' intype ' ' br ' ' *nicname + '\n' + '\0' */ /* strlen(owner)
slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 5; * +
newline = alloca(slen); * " "
* +
* strlen(intype)
* +
* " "
* +
* strlen(br)
* +
* " "
* +
* strlen(nicname)
* +
* \n
* +
* \0
*/
slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4;
newline = malloc(slen + 1);
if (!newline) { if (!newline) {
free(nicname); free(nicname);
free(newline);
usernic_error("Failed allocate memory: %s\n", strerror(errno)); usernic_error("Failed allocate memory: %s\n", strerror(errno));
return NULL; return NULL;
} }
ret = snprintf(newline, slen, "%s %s %s %s\n", owner, intype, br, nicname); ret = snprintf(newline, slen + 1, "%s %s %s %s\n", owner, intype, br, nicname);
if (ret < 0 || ret >= slen) { if (ret < 0 || (size_t)ret >= (slen + 1)) {
if (lxc_netdev_delete_by_name(nicname) != 0) if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname); usernic_error("Error unlinking %s\n", nicname);
free(nicname); free(nicname);
free(newline);
return NULL; return NULL;
} }
if (len)
munmap(buf, len);
if (ftruncate(fd, len + slen)) /* Note that the file needs to be truncated to the size **without** the
usernic_error("Failed to set new file size: %s\n", * \0 byte! Files are not \0-terminated!
strerror(errno)); */
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) { if (buf == MAP_FAILED) {
usernic_error("Failed to establish shared memory mapping: %s\n", usernic_error("Failed to establish shared memory mapping: %s\n",
strerror(errno)); strerror(errno));
if (lxc_netdev_delete_by_name(nicname) != 0) if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname); usernic_error("Error unlinking %s\n", nicname);
free(nicname); free(nicname);
free(newline);
return NULL; return NULL;
} }
strcpy(buf + len, newline); /* Note that the memory needs to be moved in the buffer **without** the
munmap(buf, len + slen); * \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; return nicname;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment