71c2a1
71c2a1
# HG changeset patch
71c2a1
# User Jed Davis <jld@mozilla.com>
71c2a1
# Date 1526943705 21600
71c2a1
# Node ID 6bb3adfa15c6877f7874429462dad88f8c978c4f
71c2a1
# Parent  4c71c8454879c841871ecf3afb7dbdc96bad97fc
71c2a1
Bug 1436242 - Avoid undefined behavior in IPC fd-passing code.  r=froydnj
71c2a1
71c2a1
MozReview-Commit-ID: 3szIPUssgF5
71c2a1
71c2a1
diff --git a/ipc/chromium/src/chrome/common/ipc_channel_posix.cc b/ipc/chromium/src/chrome/common/ipc_channel_posix.cc
71c2a1
--- a/ipc/chromium/src/chrome/common/ipc_channel_posix.cc
71c2a1
+++ b/ipc/chromium/src/chrome/common/ipc_channel_posix.cc
71c2a1
@@ -418,20 +418,37 @@ bool Channel::ChannelImpl::ProcessIncomi
71c2a1
     const int* fds;
71c2a1
     unsigned num_fds;
71c2a1
     unsigned fds_i = 0;  // the index of the first unused descriptor
71c2a1
 
71c2a1
     if (input_overflow_fds_.empty()) {
71c2a1
       fds = wire_fds;
71c2a1
       num_fds = num_wire_fds;
71c2a1
     } else {
71c2a1
-      const size_t prev_size = input_overflow_fds_.size();
71c2a1
-      input_overflow_fds_.resize(prev_size + num_wire_fds);
71c2a1
-      memcpy(&input_overflow_fds_[prev_size], wire_fds,
71c2a1
-             num_wire_fds * sizeof(int));
71c2a1
+      // This code may look like a no-op in the case where
71c2a1
+      // num_wire_fds == 0, but in fact:
71c2a1
+      //
71c2a1
+      // 1. wire_fds will be nullptr, so passing it to memcpy is
71c2a1
+      // undefined behavior according to the C standard, even though
71c2a1
+      // the memcpy length is 0.
71c2a1
+      //
71c2a1
+      // 2. prev_size will be an out-of-bounds index for
71c2a1
+      // input_overflow_fds_; this is undefined behavior according to
71c2a1
+      // the C++ standard, even though the element only has its
71c2a1
+      // pointer taken and isn't accessed (and the corresponding
71c2a1
+      // operation on a C array would be defined).
71c2a1
+      //
71c2a1
+      // UBSan makes #1 a fatal error, and assertions in libstdc++ do
71c2a1
+      // the same for #2 if enabled.
71c2a1
+      if (num_wire_fds > 0) {
71c2a1
+        const size_t prev_size = input_overflow_fds_.size();
71c2a1
+        input_overflow_fds_.resize(prev_size + num_wire_fds);
71c2a1
+        memcpy(&input_overflow_fds_[prev_size], wire_fds,
71c2a1
+               num_wire_fds * sizeof(int));
71c2a1
+      }
71c2a1
       fds = &input_overflow_fds_[0];
71c2a1
       num_fds = input_overflow_fds_.size();
71c2a1
     }
71c2a1
 
71c2a1
     // The data for the message we're currently reading consists of any data
71c2a1
     // stored in incoming_message_ followed by data in input_buf_ (followed by
71c2a1
     // other messages).
71c2a1
 
71c2a1