summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_poison.c
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2013-03-28 16:41:39 +0000
committertedu <tedu@openbsd.org>2013-03-28 16:41:39 +0000
commitef2441bcf93b02ad7a090ebae81ef35f7ac914cc (patch)
tree04021fc3ce263eff9602f1f0c98265747a5cc1dc /sys/kern/subr_poison.c
parentremove excesss includes (diff)
downloadwireguard-openbsd-ef2441bcf93b02ad7a090ebae81ef35f7ac914cc.tar.xz
wireguard-openbsd-ef2441bcf93b02ad7a090ebae81ef35f7ac914cc.zip
separate memory poisoning code to a new file and make it usable kernel wide
ok deraadt
Diffstat (limited to 'sys/kern/subr_poison.c')
-rw-r--r--sys/kern/subr_poison.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/sys/kern/subr_poison.c b/sys/kern/subr_poison.c
new file mode 100644
index 00000000000..f3bf077ff6a
--- /dev/null
+++ b/sys/kern/subr_poison.c
@@ -0,0 +1,64 @@
+/* $OpenBSD */
+/*
+ * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/systm.h>
+
+/*
+ * The POISON is used as known text to copy into free objects so
+ * that modifications after frees can be detected.
+ */
+#ifdef DEADBEEF0
+#define POISON ((unsigned) DEADBEEF0)
+#else
+#define POISON ((unsigned) 0xdeadbeef)
+#endif
+#define POISON_SIZE 32
+
+void
+poison_mem(void *v, size_t len)
+{
+ uint32_t *ip = v;
+ size_t i;
+
+ if (len > POISON_SIZE)
+ len = POISON_SIZE;
+ len = len / sizeof(*ip);
+ for (i = 0; i < len; i++)
+ ip[i] = POISON;
+}
+
+int
+poison_check(void *v, size_t len, size_t *pidx, int *pval)
+{
+
+ uint32_t *ip = v;
+ size_t i;
+
+ if (len > POISON_SIZE)
+ len = POISON_SIZE;
+ len = len / sizeof(*ip);
+ for (i = 0; i < len; i++) {
+ if (ip[i] != POISON) {
+ *pidx = i;
+ *pval = ip[i];
+ return 1;
+ }
+ }
+ return 0;
+}
+