Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: src/core/SkValidatingReadBuffer.cpp

Issue 41253002: Checking structure sizes before reading them from memory to avoid overflowing the buffer's stream. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Adding validation before memory allocation in SkRegion::readFromMemory Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkErrorInternals.h" 9 #include "SkErrorInternals.h"
10 #include "SkValidatingReadBuffer.h" 10 #include "SkValidatingReadBuffer.h"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 111 }
112 return data; 112 return data;
113 } 113 }
114 114
115 void SkValidatingReadBuffer::readPoint(SkPoint* point) { 115 void SkValidatingReadBuffer::readPoint(SkPoint* point) {
116 point->fX = fReader.readScalar(); 116 point->fX = fReader.readScalar();
117 point->fY = fReader.readScalar(); 117 point->fY = fReader.readScalar();
118 } 118 }
119 119
120 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) { 120 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
121 const size_t size = matrix->readFromMemory(fReader.peek()); 121 size_t size = 0;
122 this->validate(SkAlign4(size) == size); 122 if (!fError) {
123 size = matrix->readFromMemory(fReader.peek(), fReader.available());
124 this->validate((SkAlign4(size) != size) || (0 == size));
125 }
123 if (!fError) { 126 if (!fError) {
124 (void)this->skip(size); 127 (void)this->skip(size);
125 } 128 }
126 } 129 }
127 130
128 void SkValidatingReadBuffer::readIRect(SkIRect* rect) { 131 void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
129 const void* ptr = this->skip(sizeof(SkIRect)); 132 const void* ptr = this->skip(sizeof(SkIRect));
130 if (!fError) { 133 if (!fError) {
131 memcpy(rect, ptr, sizeof(SkIRect)); 134 memcpy(rect, ptr, sizeof(SkIRect));
132 } 135 }
133 } 136 }
134 137
135 void SkValidatingReadBuffer::readRect(SkRect* rect) { 138 void SkValidatingReadBuffer::readRect(SkRect* rect) {
136 const void* ptr = this->skip(sizeof(SkRect)); 139 const void* ptr = this->skip(sizeof(SkRect));
137 if (!fError) { 140 if (!fError) {
138 memcpy(rect, ptr, sizeof(SkRect)); 141 memcpy(rect, ptr, sizeof(SkRect));
139 } 142 }
140 } 143 }
141 144
142 void SkValidatingReadBuffer::readRegion(SkRegion* region) { 145 void SkValidatingReadBuffer::readRegion(SkRegion* region) {
143 const size_t size = region->readFromMemory(fReader.peek()); 146 size_t size = 0;
144 this->validate(SkAlign4(size) == size); 147 if (!fError) {
148 size = region->readFromMemory(fReader.peek(), fReader.available());
149 this->validate((SkAlign4(size) != size) || (0 == size));
150 }
145 if (!fError) { 151 if (!fError) {
146 (void)this->skip(size); 152 (void)this->skip(size);
147 } 153 }
148 } 154 }
149 155
150 void SkValidatingReadBuffer::readPath(SkPath* path) { 156 void SkValidatingReadBuffer::readPath(SkPath* path) {
151 const size_t size = path->readFromMemory(fReader.peek()); 157 size_t size = 0;
152 this->validate(SkAlign4(size) == size); 158 if (!fError) {
159 size = path->readFromMemory(fReader.peek(), fReader.available());
160 this->validate((SkAlign4(size) != size) || (0 == size));
161 }
153 if (!fError) { 162 if (!fError) {
154 (void)this->skip(size); 163 (void)this->skip(size);
155 } 164 }
156 } 165 }
157 166
158 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) { 167 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) {
159 const uint32_t count = this->getArrayCount(); 168 const uint32_t count = this->getArrayCount();
160 this->validate(size == count); 169 this->validate(size == count);
161 (void)this->skip(sizeof(uint32_t)); // Skip array count 170 (void)this->skip(sizeof(uint32_t)); // Skip array count
162 const size_t byteLength = count * elementSize; 171 const size_t byteLength = count * elementSize;
(...skipping 19 matching lines...) Expand all
182 191
183 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) { 192 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
184 return readArray(points, size, sizeof(SkPoint)); 193 return readArray(points, size, sizeof(SkPoint));
185 } 194 }
186 195
187 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) { 196 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
188 return readArray(values, size, sizeof(SkScalar)); 197 return readArray(values, size, sizeof(SkScalar));
189 } 198 }
190 199
191 uint32_t SkValidatingReadBuffer::getArrayCount() { 200 uint32_t SkValidatingReadBuffer::getArrayCount() {
201 const size_t inc = sizeof(uint32_t);
202 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc) ;
192 return *(uint32_t*)fReader.peek(); 203 return *(uint32_t*)fReader.peek();
193 } 204 }
194 205
195 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) { 206 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) {
196 const int width = this->readInt(); 207 const int width = this->readInt();
197 const int height = this->readInt(); 208 const int height = this->readInt();
198 const size_t length = this->readUInt(); 209 const size_t length = this->readUInt();
199 // A size of zero means the SkBitmap was simply flattened. 210 // A size of zero means the SkBitmap was simply flattened.
200 this->validate(length == 0); 211 this->validate(length == 0);
201 if (fError) { 212 if (fError) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 delete obj; 255 delete obj;
245 obj = NULL; 256 obj = NULL;
246 } 257 }
247 } else { 258 } else {
248 // we must skip the remaining data 259 // we must skip the remaining data
249 this->skip(sizeRecorded); 260 this->skip(sizeRecorded);
250 SkASSERT(false); 261 SkASSERT(false);
251 } 262 }
252 return obj; 263 return obj;
253 } 264 }
OLDNEW
« no previous file with comments | « src/core/SkRegion.cpp ('k') | tests/MatrixTest.cpp » ('j') | tests/SerializationTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698