tar: Don't assume that name, linkname and prefix are null-terminated

This commit is contained in:
sin 2015-04-21 15:05:51 +01:00
parent f1261b57d9
commit fde9e29d05

18
tar.c
View File

@ -203,7 +203,8 @@ unarchive(char *fname, ssize_t l, char b[BLKSIZ])
break; break;
case HARDLINK: case HARDLINK:
case SYMLINK: case SYMLINK:
estrlcpy(lname, h->link, sizeof(lname)); snprintf(lname, sizeof(lname), "%.*s", (int)sizeof(h->link),
h->link);
if (((h->type == HARDLINK) ? link : symlink)(lname, fname) < 0) if (((h->type == HARDLINK) ? link : symlink)(lname, fname) < 0)
eprintf("%s %s -> %s:", eprintf("%s %s -> %s:",
(h->type == HARDLINK) ? "link" : "symlink", (h->type == HARDLINK) ? "link" : "symlink",
@ -314,20 +315,21 @@ sanitize(struct header *h)
static void static void
xt(int (*fn)(char *, ssize_t, char[BLKSIZ])) xt(int (*fn)(char *, ssize_t, char[BLKSIZ]))
{ {
char b[BLKSIZ], fname[256 + 1], *p;
struct header *h; struct header *h;
long size; long size;
char b[BLKSIZ], fname[256 + 1], *p; int n;
h = (void *)b; h = (void *)b;
while (fread(b, BLKSIZ, 1, tarfile) == 1 && *(h->name)) { while (fread(b, BLKSIZ, 1, tarfile) == 1 && *(h->name)) {
sanitize(h); sanitize(h);
fname[0] = '\0'; n = 0;
if (*(h->prefix)) { if (h->prefix[0])
estrlcat(fname, h->prefix, sizeof(fname)); n = snprintf(fname, sizeof(fname), "%.*s/",
estrlcat(fname, "/", sizeof(fname)); (int)sizeof(h->prefix), h->prefix);
} snprintf(fname + n, sizeof(fname) - n, "%.*s",
estrlcat(fname, h->name, sizeof(fname)); (int)sizeof(h->name), h->name);
if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0') if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0')
eprintf("strtol %s: invalid number\n", h->size); eprintf("strtol %s: invalid number\n", h->size);