Skip to content

Commit

Permalink
As noted by tb@ previous commit only removed an unused fucntion.
Browse files Browse the repository at this point in the history
So redo previous commit properly:
Use random value for canary bytes; ok tb@.
  • Loading branch information
omoerbeek committed Oct 9, 2020
1 parent 3cd79bd commit 8d0b0db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/libc/dlfcn/dlfcn_stubs.c
@@ -1,4 +1,4 @@
/* $OpenBSD: dlfcn_stubs.c,v 1.15 2019/06/02 01:03:01 guenther Exp $ */
/* $OpenBSD: dlfcn_stubs.c,v 1.16 2020/10/09 16:01:48 otto Exp $ */

/*
* Copyright (c) 1998 Per Fogelstrom, Opsycon AB
Expand Down Expand Up @@ -103,6 +103,7 @@ dladdr(const void *addr, struct dl_info *info)
printf("Wrong dl symbols!\n");
return -1;
}
DEF_WEAK(dladdr);

#if 0
/* Thread Local Storage argument structure */
Expand Down
4 changes: 2 additions & 2 deletions lib/libc/hidden/dlfcn.h
@@ -1,4 +1,4 @@
/* $OpenBSD: dlfcn.h,v 1.1 2019/06/02 01:03:01 guenther Exp $ */
/* $OpenBSD: dlfcn.h,v 1.2 2020/10/09 16:01:48 otto Exp $ */
/*
* Copyright (c) 2019 Philip Guenther <guenther@openbsd.org>
*
Expand All @@ -20,7 +20,7 @@

#include_next <dlfcn.h>

PROTO_DEPRECATED(dladdr);
PROTO_NORMAL(dladdr);
PROTO_DEPRECATED(dlclose);
PROTO_DEPRECATED(dlerror);
PROTO_DEPRECATED(dlopen);
Expand Down
13 changes: 9 additions & 4 deletions lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
/* $OpenBSD: malloc.c,v 1.264 2020/10/06 06:31:14 otto Exp $ */
/* $OpenBSD: malloc.c,v 1.265 2020/10/09 16:01:48 otto Exp $ */
/*
* Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net>
* Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
Expand Down Expand Up @@ -193,7 +193,7 @@ struct malloc_readonly {
int def_malloc_junk; /* junk fill? */
int malloc_realloc; /* always realloc? */
int malloc_xmalloc; /* xmalloc behaviour? */
int chunk_canaries; /* use canaries after chunks? */
u_int chunk_canaries; /* use canaries after chunks? */
int internal_funcs; /* use better recallocarray/freezero? */
u_int def_malloc_cache; /* free pages we cache */
size_t malloc_guard; /* use guard pages after allocations? */
Expand Down Expand Up @@ -468,6 +468,11 @@ omalloc_init(void)

while ((mopts.malloc_canary = arc4random()) == 0)
;
if (mopts.chunk_canaries)
do {
mopts.chunk_canaries = arc4random();
} while ((u_char)mopts.chunk_canaries == 0 ||
(u_char)mopts.chunk_canaries == SOME_FREEJUNK);
}

static void
Expand Down Expand Up @@ -918,7 +923,7 @@ fill_canary(char *ptr, size_t sz, size_t allocated)

if (check_sz > CHUNK_CHECK_LENGTH)
check_sz = CHUNK_CHECK_LENGTH;
memset(ptr + sz, SOME_JUNK, check_sz);
memset(ptr + sz, mopts.chunk_canaries, check_sz);
}

/*
Expand Down Expand Up @@ -1019,7 +1024,7 @@ validate_canary(struct dir_info *d, u_char *ptr, size_t sz, size_t allocated)
q = p + check_sz;

while (p < q) {
if (*p != SOME_JUNK) {
if (*p != (u_char)mopts.chunk_canaries && *p != SOME_JUNK) {
wrterror(d, "chunk canary corrupted %p %#tx@%#zx%s",
ptr, p - ptr, sz,
*p == SOME_FREEJUNK ? " (double free?)" : "");
Expand Down

0 comments on commit 8d0b0db

Please sign in to comment.