lib/initend.c: xbps_init(): safer string handling

Signed-off-by: Cameron Nemo <camerontnorman@gmail.com>
Closes: #58 [via git-merge-pr]
This commit is contained in:
Cameron Nemo 2019-02-22 22:15:56 -08:00 committed by Duncaen
parent 71b076908d
commit 5aa1adca90

View File

@ -400,6 +400,7 @@ xbps_init(struct xbps_handle *xhp)
char cwd[PATH_MAX-1], sysconfdir[XBPS_MAXPATH+sizeof(XBPS_SYSDEFCONF_PATH)], *buf; char cwd[PATH_MAX-1], sysconfdir[XBPS_MAXPATH+sizeof(XBPS_SYSDEFCONF_PATH)], *buf;
const char *repodir, *native_arch; const char *repodir, *native_arch;
int rv; int rv;
size_t size;
assert(xhp != NULL); assert(xhp != NULL);
@ -413,26 +414,42 @@ xbps_init(struct xbps_handle *xhp)
xhp->rootdir[1] = '\0'; xhp->rootdir[1] = '\0';
} else if (xhp->rootdir[0] != '/') { } else if (xhp->rootdir[0] != '/') {
buf = strdup(xhp->rootdir); buf = strdup(xhp->rootdir);
snprintf(xhp->rootdir, sizeof(xhp->rootdir), "%s/%s", cwd, buf); if (!buf)
return ENOMEM;
size = sizeof(xhp->rootdir);
rv = snprintf(xhp->rootdir, size, "%s/%s", cwd, buf);
free(buf); free(buf);
if (rv < 0 || (size_t)rv >= size)
return 1;
} }
xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER); xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER);
/* set confdir */ /* set confdir */
if (xhp->confdir[0] == '\0') { if (xhp->confdir[0] == '\0') {
snprintf(xhp->confdir, sizeof(xhp->confdir), size = sizeof(xhp->confdir);
rv = snprintf(xhp->confdir, size,
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", "%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
XBPS_SYSCONF_PATH); XBPS_SYSCONF_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->confdir[0] != '/') { } else if (xhp->confdir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->confdir); buf = strdup(xhp->confdir);
snprintf(xhp->confdir, sizeof(xhp->confdir), if (!buf)
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); return ENOMEM;
size = sizeof(xhp->confdir);
rv = snprintf(xhp->confdir, size, "%s/%s",
strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
free(buf); free(buf);
if (rv < 0 || (size_t)rv >= size)
return 1;
} }
/* set sysconfdir */ /* set sysconfdir */
snprintf(sysconfdir, sizeof(sysconfdir), size = sizeof(sysconfdir);
rv = snprintf(sysconfdir, size,
"%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", "%s%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
XBPS_SYSDEFCONF_PATH); XBPS_SYSDEFCONF_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
xhp->target_arch = getenv("XBPS_TARGET_ARCH"); xhp->target_arch = getenv("XBPS_TARGET_ARCH");
if ((native_arch = getenv("XBPS_ARCH")) != NULL) { if ((native_arch = getenv("XBPS_ARCH")) != NULL) {
@ -451,27 +468,43 @@ xbps_init(struct xbps_handle *xhp)
/* Set cachedir */ /* Set cachedir */
if (xhp->cachedir[0] == '\0') { if (xhp->cachedir[0] == '\0') {
snprintf(xhp->cachedir, sizeof(xhp->cachedir), size = sizeof(xhp->cachedir);
rv = snprintf(xhp->cachedir, size,
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
XBPS_CACHE_PATH); XBPS_CACHE_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->cachedir[0] != '/') { } else if (xhp->cachedir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->cachedir); buf = strdup(xhp->cachedir);
snprintf(xhp->cachedir, sizeof(xhp->cachedir), if (!buf)
return ENOMEM;
size = sizeof(xhp->cachedir);
rv = snprintf(xhp->cachedir, size,
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
free(buf); free(buf);
if (rv < 0 || (size_t)rv >= size)
return 1;
} }
/* Set metadir */ /* Set metadir */
if (xhp->metadir[0] == '\0') { if (xhp->metadir[0] == '\0') {
snprintf(xhp->metadir, sizeof(xhp->metadir), size = sizeof(xhp->metadir);
rv = snprintf(xhp->metadir, size,
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "",
XBPS_META_PATH); XBPS_META_PATH);
if (rv < 0 || (size_t)rv >= size)
return 1;
} else if (xhp->metadir[0] != '/') { } else if (xhp->metadir[0] != '/') {
/* relative path */ /* relative path */
buf = strdup(xhp->metadir); buf = strdup(xhp->metadir);
snprintf(xhp->metadir, sizeof(xhp->metadir), if (!buf)
return ENOMEM;
size = sizeof(xhp->metadir);
rv = snprintf(xhp->metadir, size,
"%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf);
free(buf); free(buf);
if (rv < 0 || (size_t)rv >= size)
return 1;
} }
xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir); xbps_dbg_printf(xhp, "rootdir=%s\n", xhp->rootdir);