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

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: Added align to 4 tests 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 const size_t size = matrix->readFromMemory(fReader.peek(), fReader.available ());
122 this->validate(SkAlign4(size) == size); 122 this->validate((SkAlign4(size) != size) || (0 == size));
123 if (!fError) { 123 if (!fError) {
124 (void)this->skip(size); 124 (void)this->skip(size);
125 } 125 }
126 } 126 }
127 127
128 void SkValidatingReadBuffer::readIRect(SkIRect* rect) { 128 void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
129 const void* ptr = this->skip(sizeof(SkIRect)); 129 const void* ptr = this->skip(sizeof(SkIRect));
130 if (!fError) { 130 if (!fError) {
131 memcpy(rect, ptr, sizeof(SkIRect)); 131 memcpy(rect, ptr, sizeof(SkIRect));
132 } 132 }
133 } 133 }
134 134
135 void SkValidatingReadBuffer::readRect(SkRect* rect) { 135 void SkValidatingReadBuffer::readRect(SkRect* rect) {
136 const void* ptr = this->skip(sizeof(SkRect)); 136 const void* ptr = this->skip(sizeof(SkRect));
137 if (!fError) { 137 if (!fError) {
138 memcpy(rect, ptr, sizeof(SkRect)); 138 memcpy(rect, ptr, sizeof(SkRect));
139 } 139 }
140 } 140 }
141 141
142 void SkValidatingReadBuffer::readRegion(SkRegion* region) { 142 void SkValidatingReadBuffer::readRegion(SkRegion* region) {
143 const size_t size = region->readFromMemory(fReader.peek()); 143 const size_t size = region->readFromMemory(fReader.peek(), fReader.available ());
144 this->validate(SkAlign4(size) == size); 144 this->validate((SkAlign4(size) != size) || (0 == size));
145 if (!fError) { 145 if (!fError) {
146 (void)this->skip(size); 146 (void)this->skip(size);
147 } 147 }
148 } 148 }
149 149
150 void SkValidatingReadBuffer::readPath(SkPath* path) { 150 void SkValidatingReadBuffer::readPath(SkPath* path) {
151 const size_t size = path->readFromMemory(fReader.peek()); 151 const size_t size = path->readFromMemory(fReader.peek(), fReader.available() );
152 this->validate(SkAlign4(size) == size); 152 this->validate((SkAlign4(size) != size) || (0 == size));
153 if (!fError) { 153 if (!fError) {
154 (void)this->skip(size); 154 (void)this->skip(size);
155 } 155 }
156 } 156 }
157 157
158 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) { 158 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) {
159 const uint32_t count = this->getArrayCount(); 159 const uint32_t count = this->getArrayCount();
160 this->validate(size == count); 160 this->validate(size == count);
161 (void)this->skip(sizeof(uint32_t)); // Skip array count 161 (void)this->skip(sizeof(uint32_t)); // Skip array count
162 const size_t byteLength = count * elementSize; 162 const size_t byteLength = count * elementSize;
(...skipping 19 matching lines...) Expand all
182 182
183 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) { 183 bool SkValidatingReadBuffer::readPointArray(SkPoint* points, size_t size) {
184 return readArray(points, size, sizeof(SkPoint)); 184 return readArray(points, size, sizeof(SkPoint));
185 } 185 }
186 186
187 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) { 187 bool SkValidatingReadBuffer::readScalarArray(SkScalar* values, size_t size) {
188 return readArray(values, size, sizeof(SkScalar)); 188 return readArray(values, size, sizeof(SkScalar));
189 } 189 }
190 190
191 uint32_t SkValidatingReadBuffer::getArrayCount() { 191 uint32_t SkValidatingReadBuffer::getArrayCount() {
192 const size_t inc = sizeof(uint32_t);
193 fError = fError || !IsPtrAlign4(fReader.peek()) || !fReader.isAvailable(inc) ;
192 return *(uint32_t*)fReader.peek(); 194 return *(uint32_t*)fReader.peek();
193 } 195 }
194 196
195 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) { 197 void SkValidatingReadBuffer::readBitmap(SkBitmap* bitmap) {
196 const int width = this->readInt(); 198 const int width = this->readInt();
197 const int height = this->readInt(); 199 const int height = this->readInt();
198 const size_t length = this->readUInt(); 200 const size_t length = this->readUInt();
199 // A size of zero means the SkBitmap was simply flattened. 201 // A size of zero means the SkBitmap was simply flattened.
200 this->validate(length == 0); 202 this->validate(length == 0);
201 if (fError) { 203 if (fError) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 delete obj; 246 delete obj;
245 obj = NULL; 247 obj = NULL;
246 } 248 }
247 } else { 249 } else {
248 // we must skip the remaining data 250 // we must skip the remaining data
249 this->skip(sizeRecorded); 251 this->skip(sizeRecorded);
250 SkASSERT(false); 252 SkASSERT(false);
251 } 253 }
252 return obj; 254 return obj;
253 } 255 }
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