Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ldcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ ldcache_init(struct ldcache *ctx, struct error *err, const char *path)
*ctx = (struct ldcache){err, path, NULL, NULL, 0};
}

static bool
ldcache_ptr_valid(const struct ldcache *ctx, const void *ptr)
{
return ((const char *)ptr >= (const char *)ctx->addr &&
(const char *)ptr < (const char *)ctx->addr + ctx->size);
}

int
ldcache_open(struct ldcache *ctx)
{
Expand All @@ -77,6 +84,8 @@ ldcache_open(struct ldcache *ctx)
ctx->ptr = h5->libs + h5->nlibs;
padding = (-(uintptr_t)ctx->ptr) & (__alignof__(struct header_libc6) - 1);
ctx->ptr = (char *)ctx->ptr + padding; /* align on header_libc6 boundary */
if (!ldcache_ptr_valid(ctx, ctx->ptr))
goto fail;
}

h6 = (struct header_libc6 *)ctx->ptr;
Expand All @@ -85,6 +94,8 @@ ldcache_open(struct ldcache *ctx)
if (strncmp(h6->magic, MAGIC_LIBC6, MAGIC_LIBC6_LEN) ||
strncmp(h6->version, MAGIC_VERSION, MAGIC_VERSION_LEN))
goto fail;
if (!ldcache_ptr_valid(ctx, h6->libs + h6->nlibs))
goto fail;

return (0);

Expand Down Expand Up @@ -122,6 +133,11 @@ ldcache_resolve(struct ldcache *ctx, uint32_t arch, const char *root, const char
char *key = (char *)ctx->ptr + h->libs[i].key;
char *value = (char *)ctx->ptr + h->libs[i].value;

if (!ldcache_ptr_valid(ctx, key) || !ldcache_ptr_valid(ctx, value)) {
error_setx(ctx->err, "corrupted ldcache entry: %s", ctx->path);
return (-1);
}

if (!(flags & LD_ELF) || (flags & LD_ARCH_MASK) != arch)
continue;

Expand Down
Loading