<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[201381] trunk/Source/WebKit2</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/201381">201381</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2016-05-25 06:18:26 -0700 (Wed, 25 May 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>[Unix] Potential buffer overrun of m_fileDescriptors in readBytesFromSocket of ConnectionUnix.cpp
https://bugs.webkit.org/show_bug.cgi?id=158058
Patch by Fujii Hironori <Hironori.Fujii@sony.com> on 2016-05-25
Reviewed by Carlos Garcia Campos.
Memcpy does not check the boundary of m_fileDescriptors in
readBytesFromSocket of ConnectionUnix.cpp. This is not a problem
in normal cases, but in the case when Web process is hijacked and
malicious IPC packets were sent. WTF::Vector already has two
members m_capacity and m_size. There is no need to have a
separate member m_fileDescriptorsSize to remember the number of
remaining data.
* Platform/IPC/Connection.h: Remove members m_readBufferSize and
m_fileDescriptorsSize.
* Platform/IPC/unix/ConnectionUnix.cpp:
(IPC::Connection::platformInitialize): Removed initialization of
m_readBufferSize and m_fileDescriptorsSize. Reserve initial
capacity for m_readBuffer and m_fileDescriptors.
(IPC::Connection::processMessage): Replace m_readBufferSize and
m_fileDescriptorsSize with m_readBuffer.size() and
m_fileDescriptors.size(). Use Vector::shrink() to reset the
number of remaining data in the buffers.
(IPC::readBytesFromSocket) : Change argument types to WTF::Vector
instead of pointers and sizes.
(IPC::Connection::readyReadHandler): Call new readBytesFromSocket</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2PlatformIPCConnectionh">trunk/Source/WebKit2/Platform/IPC/Connection.h</a></li>
<li><a href="#trunkSourceWebKit2PlatformIPCunixConnectionUnixcpp">trunk/Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (201380 => 201381)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2016-05-25 13:03:24 UTC (rev 201380)
+++ trunk/Source/WebKit2/ChangeLog        2016-05-25 13:18:26 UTC (rev 201381)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2016-05-25 Fujii Hironori <Hironori.Fujii@sony.com>
+
+ [Unix] Potential buffer overrun of m_fileDescriptors in readBytesFromSocket of ConnectionUnix.cpp
+ https://bugs.webkit.org/show_bug.cgi?id=158058
+
+ Reviewed by Carlos Garcia Campos.
+
+ Memcpy does not check the boundary of m_fileDescriptors in
+ readBytesFromSocket of ConnectionUnix.cpp. This is not a problem
+ in normal cases, but in the case when Web process is hijacked and
+ malicious IPC packets were sent. WTF::Vector already has two
+ members m_capacity and m_size. There is no need to have a
+ separate member m_fileDescriptorsSize to remember the number of
+ remaining data.
+
+ * Platform/IPC/Connection.h: Remove members m_readBufferSize and
+ m_fileDescriptorsSize.
+ * Platform/IPC/unix/ConnectionUnix.cpp:
+ (IPC::Connection::platformInitialize): Removed initialization of
+ m_readBufferSize and m_fileDescriptorsSize. Reserve initial
+ capacity for m_readBuffer and m_fileDescriptors.
+ (IPC::Connection::processMessage): Replace m_readBufferSize and
+ m_fileDescriptorsSize with m_readBuffer.size() and
+ m_fileDescriptors.size(). Use Vector::shrink() to reset the
+ number of remaining data in the buffers.
+ (IPC::readBytesFromSocket) : Change argument types to WTF::Vector
+ instead of pointers and sizes.
+ (IPC::Connection::readyReadHandler): Call new readBytesFromSocket
+
</ins><span class="cx"> 2016-05-25 Chris Dumez <cdumez@apple.com>
</span><span class="cx">
</span><span class="cx"> Update constructRevalidationRequest() to stop returning a unique_ptr<ResourceRequest>
</span></span></pre></div>
<a id="trunkSourceWebKit2PlatformIPCConnectionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Platform/IPC/Connection.h (201380 => 201381)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Platform/IPC/Connection.h        2016-05-25 13:03:24 UTC (rev 201380)
+++ trunk/Source/WebKit2/Platform/IPC/Connection.h        2016-05-25 13:18:26 UTC (rev 201381)
</span><span class="lines">@@ -325,9 +325,7 @@
</span><span class="cx"> bool processMessage();
</span><span class="cx">
</span><span class="cx"> Vector<uint8_t> m_readBuffer;
</span><del>- size_t m_readBufferSize;
</del><span class="cx"> Vector<int> m_fileDescriptors;
</span><del>- size_t m_fileDescriptorsSize;
</del><span class="cx"> int m_socketDescriptor;
</span><span class="cx"> #if PLATFORM(GTK)
</span><span class="cx"> GSocketMonitor m_socketMonitor;
</span></span></pre></div>
<a id="trunkSourceWebKit2PlatformIPCunixConnectionUnixcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp (201380 => 201381)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp        2016-05-25 13:03:24 UTC (rev 201380)
+++ trunk/Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp        2016-05-25 13:18:26 UTC (rev 201381)
</span><span class="lines">@@ -132,10 +132,8 @@
</span><span class="cx"> void Connection::platformInitialize(Identifier identifier)
</span><span class="cx"> {
</span><span class="cx"> m_socketDescriptor = identifier;
</span><del>- m_readBuffer.resize(messageMaxSize);
- m_readBufferSize = 0;
- m_fileDescriptors.resize(attachmentMaxAmount);
- m_fileDescriptorsSize = 0;
</del><ins>+ m_readBuffer.reserveInitialCapacity(messageMaxSize);
+ m_fileDescriptors.reserveInitialCapacity(attachmentMaxAmount);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void Connection::platformInvalidate()
</span><span class="lines">@@ -161,7 +159,7 @@
</span><span class="cx">
</span><span class="cx"> bool Connection::processMessage()
</span><span class="cx"> {
</span><del>- if (m_readBufferSize < sizeof(MessageInfo))
</del><ins>+ if (m_readBuffer.size() < sizeof(MessageInfo))
</ins><span class="cx"> return false;
</span><span class="cx">
</span><span class="cx"> uint8_t* messageData = m_readBuffer.data();
</span><span class="lines">@@ -170,7 +168,7 @@
</span><span class="cx"> messageData += sizeof(messageInfo);
</span><span class="cx">
</span><span class="cx"> size_t messageLength = sizeof(MessageInfo) + messageInfo.attachmentCount() * sizeof(AttachmentInfo) + (messageInfo.isMessageBodyIsOutOfLine() ? 0 : messageInfo.bodySize());
</span><del>- if (m_readBufferSize < messageLength)
</del><ins>+ if (m_readBuffer.size() < messageLength)
</ins><span class="cx"> return false;
</span><span class="cx">
</span><span class="cx"> size_t attachmentFileDescriptorCount = 0;
</span><span class="lines">@@ -251,25 +249,25 @@
</span><span class="cx">
</span><span class="cx"> processIncomingMessage(WTFMove(decoder));
</span><span class="cx">
</span><del>- if (m_readBufferSize > messageLength) {
- memmove(m_readBuffer.data(), m_readBuffer.data() + messageLength, m_readBufferSize - messageLength);
- m_readBufferSize -= messageLength;
</del><ins>+ if (m_readBuffer.size() > messageLength) {
+ memmove(m_readBuffer.data(), m_readBuffer.data() + messageLength, m_readBuffer.size() - messageLength);
+ m_readBuffer.shrink(m_readBuffer.size() - messageLength);
</ins><span class="cx"> } else
</span><del>- m_readBufferSize = 0;
</del><ins>+ m_readBuffer.shrink(0);
</ins><span class="cx">
</span><span class="cx"> if (attachmentFileDescriptorCount) {
</span><del>- if (m_fileDescriptorsSize > attachmentFileDescriptorCount) {
- memmove(m_fileDescriptors.data(), m_fileDescriptors.data() + attachmentFileDescriptorCount, (m_fileDescriptorsSize - attachmentFileDescriptorCount) * sizeof(int));
- m_fileDescriptorsSize -= attachmentFileDescriptorCount;
</del><ins>+ if (m_fileDescriptors.size() > attachmentFileDescriptorCount) {
+ memmove(m_fileDescriptors.data(), m_fileDescriptors.data() + attachmentFileDescriptorCount, (m_fileDescriptors.size() - attachmentFileDescriptorCount) * sizeof(int));
+ m_fileDescriptors.shrink(m_fileDescriptors.size() - attachmentFileDescriptorCount);
</ins><span class="cx"> } else
</span><del>- m_fileDescriptorsSize = 0;
</del><ins>+ m_fileDescriptors.shrink(0);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> return true;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static ssize_t readBytesFromSocket(int socketDescriptor, uint8_t* buffer, int count, int* fileDescriptors, size_t* fileDescriptorsCount)
</del><ins>+static ssize_t readBytesFromSocket(int socketDescriptor, Vector<uint8_t>& buffer, Vector<int>& fileDescriptors)
</ins><span class="cx"> {
</span><span class="cx"> struct msghdr message;
</span><span class="cx"> memset(&message, 0, sizeof(message));
</span><span class="lines">@@ -282,8 +280,10 @@
</span><span class="cx"> memset(attachmentDescriptorBuffer.get(), 0, sizeof(char) * message.msg_controllen);
</span><span class="cx"> message.msg_control = attachmentDescriptorBuffer.get();
</span><span class="cx">
</span><del>- iov[0].iov_base = buffer;
- iov[0].iov_len = count;
</del><ins>+ size_t previousBufferSize = buffer.size();
+ buffer.grow(buffer.capacity());
+ iov[0].iov_base = buffer.data() + previousBufferSize;
+ iov[0].iov_len = buffer.size() - previousBufferSize;
</ins><span class="cx">
</span><span class="cx"> message.msg_iov = iov;
</span><span class="cx"> message.msg_iovlen = 1;
</span><span class="lines">@@ -295,33 +295,31 @@
</span><span class="cx"> if (errno == EINTR)
</span><span class="cx"> continue;
</span><span class="cx">
</span><ins>+ buffer.shrink(previousBufferSize);
</ins><span class="cx"> return -1;
</span><span class="cx"> }
</span><span class="cx">
</span><del>- bool found = false;
</del><span class="cx"> struct cmsghdr* controlMessage;
</span><span class="cx"> for (controlMessage = CMSG_FIRSTHDR(&message); controlMessage; controlMessage = CMSG_NXTHDR(&message, controlMessage)) {
</span><span class="cx"> if (controlMessage->cmsg_level == SOL_SOCKET && controlMessage->cmsg_type == SCM_RIGHTS) {
</span><del>- *fileDescriptorsCount = (controlMessage->cmsg_len - CMSG_LEN(0)) / sizeof(int);
- memcpy(fileDescriptors, CMSG_DATA(controlMessage), sizeof(int) * *fileDescriptorsCount);
</del><ins>+ size_t previousFileDescriptorsSize = fileDescriptors.size();
+ size_t fileDescriptorsCount = (controlMessage->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+ fileDescriptors.grow(fileDescriptors.size() + fileDescriptorsCount);
+ memcpy(fileDescriptors.data() + previousFileDescriptorsSize, CMSG_DATA(controlMessage), sizeof(int) * fileDescriptorsCount);
</ins><span class="cx">
</span><del>- for (size_t i = 0; i < *fileDescriptorsCount; ++i) {
- while (fcntl(fileDescriptors[i], F_SETFD, FD_CLOEXEC) == -1) {
</del><ins>+ for (size_t i = 0; i < fileDescriptorsCount; ++i) {
+ while (fcntl(fileDescriptors[previousFileDescriptorsSize + i], F_SETFD, FD_CLOEXEC) == -1) {
</ins><span class="cx"> if (errno != EINTR) {
</span><span class="cx"> ASSERT_NOT_REACHED();
</span><span class="cx"> break;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx"> }
</span><del>-
- found = true;
</del><span class="cx"> break;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- if (!found)
- *fileDescriptorsCount = 0;
-
</del><ins>+ buffer.shrink(previousBufferSize + bytesRead);
</ins><span class="cx"> return bytesRead;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -331,10 +329,7 @@
</span><span class="cx"> void Connection::readyReadHandler()
</span><span class="cx"> {
</span><span class="cx"> while (true) {
</span><del>- size_t fileDescriptorsCount = 0;
- size_t bytesToRead = m_readBuffer.size() - m_readBufferSize;
- ssize_t bytesRead = readBytesFromSocket(m_socketDescriptor, m_readBuffer.data() + m_readBufferSize, bytesToRead,
- m_fileDescriptors.data() + m_fileDescriptorsSize, &fileDescriptorsCount);
</del><ins>+ ssize_t bytesRead = readBytesFromSocket(m_socketDescriptor, m_readBuffer, m_fileDescriptors);
</ins><span class="cx">
</span><span class="cx"> if (bytesRead < 0) {
</span><span class="cx"> // EINTR was already handled by readBytesFromSocket.
</span><span class="lines">@@ -348,9 +343,6 @@
</span><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx">
</span><del>- m_readBufferSize += bytesRead;
- m_fileDescriptorsSize += fileDescriptorsCount;
-
</del><span class="cx"> if (!bytesRead) {
</span><span class="cx"> connectionDidClose();
</span><span class="cx"> return;
</span></span></pre>
</div>
</div>
</body>
</html>