all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: Emacs developers <emacs-devel@gnu.org>
Subject: Undefined behavior in OS X unexec detected by ASan
Date: Sat, 02 Apr 2016 17:26:17 +0000	[thread overview]
Message-ID: <CAArVCkR3sGvi1aNtS4QLT-5MMtXRA+oH5+QTa2PpaB7Ktav4Tg@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 237 bytes --]

unexmacosx.c reads process memory directly, which tends to work in practice
but is technically undefined behavior. I've attached a small patch that
uses vm_read instead. According to ASan with this patch there's no more UB
when dumping.

[-- Attachment #1.2: Type: text/html, Size: 267 bytes --]

[-- Attachment #2: 0001-Remove-undefined-behavior-in-OS-X-dumper.patch --]
[-- Type: application/octet-stream, Size: 1894 bytes --]

From cbf0d88b109254ae82fcc713302634d9817ba9c7 Mon Sep 17 00:00:00 2001
From: Philipp Stephani <phst@google.com>
Date: Thu, 31 Mar 2016 23:10:40 +0200
Subject: [PATCH] Remove undefined behavior in OS X dumper.

Found by Address Sanitizer.

* src/unexmacosx.c (unexec_write): Use Mach virtual memory API to
avoid undefined behavior when reading arbitrary memory.
---
 src/unexmacosx.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/unexmacosx.c b/src/unexmacosx.c
index 827eda5..bdacc8b 100644
--- a/src/unexmacosx.c
+++ b/src/unexmacosx.c
@@ -103,9 +103,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdio.h>
 #include <fcntl.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <mach/mach.h>
+#include <mach/vm_map.h>
 #include <mach-o/loader.h>
 #include <mach-o/reloc.h>
 #ifdef HAVE_MALLOC_MALLOC_H
@@ -217,10 +219,27 @@ unexec_read (void *dest, size_t n)
 static int
 unexec_write (off_t dest, const void *src, size_t count)
 {
+  task_t task = mach_task_self();
+  if (task == MACH_PORT_NULL || task == MACH_PORT_DEAD)
+    return false;
+
   if (lseek (outfd, dest, SEEK_SET) != dest)
     return 0;
 
-  return write (outfd, src, count) == count;
+  /* We use the Mach virtual memory API to read our process memory
+     because using src directly would be undefined behavior and fails
+     under Address Sanitizer.  */
+  bool success = false;
+  vm_offset_t data;
+  mach_msg_type_number_t data_count;
+  if (vm_read (task, (uintptr_t) src, count, &data, &data_count)
+      == KERN_SUCCESS)
+    {
+      success =
+        write (outfd, (const void *) (uintptr_t) data, data_count) == count;
+      vm_deallocate (task, data, data_count);
+    }
+  return success;
 }
 
 /* Write COUNT bytes of zeros to outfd starting at offset DEST.
-- 
2.7.4


             reply	other threads:[~2016-04-02 17:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-02 17:26 Philipp Stephani [this message]
2016-04-08  8:27 ` Undefined behavior in OS X unexec detected by ASan Eli Zaretskii
2016-04-08 16:01   ` John Wiegley
2016-05-15 19:36     ` Philipp Stephani
2016-05-15 19:54       ` Paul Eggert
2016-05-15 19:58         ` Philipp Stephani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAArVCkR3sGvi1aNtS4QLT-5MMtXRA+oH5+QTa2PpaB7Ktav4Tg@mail.gmail.com \
    --to=p.stephani2@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.