OLD | NEW |
1 /*- | 1 /*- |
2 * Copyright 2003,2004 Colin Percival | 2 * Copyright 2003,2004 Colin Percival |
3 * All rights reserved | 3 * All rights reserved |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted providing that the following conditions | 6 * modification, are permitted providing that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 11 matching lines...) Expand all Loading... |
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * POSSIBILITY OF SUCH DAMAGE. | 24 * POSSIBILITY OF SUCH DAMAGE. |
25 * | 25 * |
26 * Changelog: | 26 * Changelog: |
27 * 2005-04-26 - Define the header as a C structure, add a CRC32 checksum to | 27 * 2005-04-26 - Define the header as a C structure, add a CRC32 checksum to |
28 * the header, and make all the types 32-bit. | 28 * the header, and make all the types 32-bit. |
29 * --Benjamin Smedberg <benjamin@smedbergs.us> | 29 * --Benjamin Smedberg <benjamin@smedbergs.us> |
30 * 2007-11-14 - Added CalculateCrc() and ApplyBinaryPatch() methods. | 30 * 2007-11-14 - Added CalculateCrc() and ApplyBinaryPatch() methods. |
31 * --Rahul Kuchhal | 31 * --Rahul Kuchhal |
| 32 * 2016-07-27 - Improve validation of diffs. |
| 33 * --Ricky Zhou |
32 */ | 34 */ |
33 | 35 |
34 #include "mbspatch.h" | 36 #include "mbspatch.h" |
35 | 37 |
36 #include <sys/stat.h> | 38 #include <sys/stat.h> |
| 39 #include <sys/types.h> |
37 #include <stdlib.h> | 40 #include <stdlib.h> |
38 #include <stdio.h> | 41 #include <stdio.h> |
39 #include <fcntl.h> | 42 #include <fcntl.h> |
40 #include <string.h> | 43 #include <string.h> |
41 #include <limits.h> | 44 #include <limits.h> |
42 | 45 |
43 #ifdef _WIN32 | 46 #ifdef _WIN32 |
44 # include <io.h> | 47 # include <io.h> |
45 # include <winsock2.h> | 48 # include <winsock2.h> |
46 #else | 49 #else |
(...skipping 18 matching lines...) Expand all Loading... |
65 | 68 |
66 header->slen = ntohl(header->slen); | 69 header->slen = ntohl(header->slen); |
67 header->scrc32 = ntohl(header->scrc32); | 70 header->scrc32 = ntohl(header->scrc32); |
68 header->dlen = ntohl(header->dlen); | 71 header->dlen = ntohl(header->dlen); |
69 header->cblen = ntohl(header->cblen); | 72 header->cblen = ntohl(header->cblen); |
70 header->difflen = ntohl(header->difflen); | 73 header->difflen = ntohl(header->difflen); |
71 header->extralen = ntohl(header->extralen); | 74 header->extralen = ntohl(header->extralen); |
72 | 75 |
73 struct stat hs; | 76 struct stat hs; |
74 s = fstat(fd, &hs); | 77 s = fstat(fd, &hs); |
75 if (s) | 78 if (s != 0) |
76 return READ_ERROR; | 79 return READ_ERROR; |
77 | 80 |
78 if (memcmp(header->tag, "MBDIFF10", 8) != 0) | 81 if (memcmp(header->tag, "MBDIFF10", 8) != 0) |
79 return UNEXPECTED_ERROR; | 82 return UNEXPECTED_ERROR; |
80 | 83 |
81 if (sizeof(MBSPatchHeader) + | 84 if (hs.st_size > INT_MAX) |
82 header->cblen + | 85 return UNEXPECTED_ERROR; |
83 header->difflen + | 86 |
84 header->extralen != int(hs.st_size)) | 87 size_t size = static_cast<size_t>(hs.st_size); |
| 88 if (size < sizeof(MBSPatchHeader)) |
| 89 return UNEXPECTED_ERROR; |
| 90 size -= sizeof(MBSPatchHeader); |
| 91 |
| 92 if (size < header->cblen) |
| 93 return UNEXPECTED_ERROR; |
| 94 size -= header->cblen; |
| 95 |
| 96 if (size < header->difflen) |
| 97 return UNEXPECTED_ERROR; |
| 98 size -= header->difflen; |
| 99 |
| 100 if (size < header->extralen) |
| 101 return UNEXPECTED_ERROR; |
| 102 size -= header->extralen; |
| 103 |
| 104 if (size != 0) |
85 return UNEXPECTED_ERROR; | 105 return UNEXPECTED_ERROR; |
86 | 106 |
87 return OK; | 107 return OK; |
88 } | 108 } |
89 | 109 |
90 int | 110 int |
91 MBS_ApplyPatch(const MBSPatchHeader *header, int patchfd, | 111 MBS_ApplyPatch(const MBSPatchHeader *header, int patchfd, |
92 unsigned char *fbuffer, int filefd) | 112 unsigned char *fbuffer, int filefd) |
93 { | 113 { |
| 114 unsigned char *fbufstart = fbuffer; |
94 unsigned char *fbufend = fbuffer + header->slen; | 115 unsigned char *fbufend = fbuffer + header->slen; |
95 | 116 |
96 unsigned char *buf = (unsigned char*) malloc(header->cblen + | 117 unsigned char *buf = (unsigned char*) malloc(header->cblen + |
97 header->difflen + | 118 header->difflen + |
98 header->extralen); | 119 header->extralen); |
99 if (!buf) | 120 if (!buf) |
100 return MEM_ERROR; | 121 return MEM_ERROR; |
101 | 122 |
102 int rv = OK; | 123 int rv = OK; |
103 | 124 |
104 int r = header->cblen + header->difflen + header->extralen; | 125 int r = header->cblen + header->difflen + header->extralen; |
105 unsigned char *wb = buf; | 126 unsigned char *wb = buf; |
106 while (r) { | 127 while (r) { |
107 int c = read(patchfd, wb, (r > SSIZE_MAX) ? SSIZE_MAX : r); | 128 int c = read(patchfd, wb, (r > SSIZE_MAX) ? SSIZE_MAX : r); |
108 if (c < 0) { | 129 if (c < 0) { |
109 rv = READ_ERROR; | 130 rv = READ_ERROR; |
110 goto end; | 131 goto end; |
111 } | 132 } |
112 | 133 |
113 r -= c; | 134 r -= c; |
| 135 wb += c; |
114 | 136 |
115 if (c == 0 && r) { | 137 if (c == 0 && r) { |
116 rv = UNEXPECTED_ERROR; | 138 rv = UNEXPECTED_ERROR; |
117 goto end; | 139 goto end; |
118 } | 140 } |
119 } | 141 } |
120 | 142 |
121 { | 143 { |
122 MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf; | 144 MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf; |
| 145 if (header->cblen % sizeof(MBSPatchTriple) != 0) { |
| 146 rv = UNEXPECTED_ERROR; |
| 147 goto end; |
| 148 } |
| 149 |
123 unsigned char *diffsrc = buf + header->cblen; | 150 unsigned char *diffsrc = buf + header->cblen; |
124 unsigned char *extrasrc = diffsrc + header->difflen; | 151 unsigned char *extrasrc = diffsrc + header->difflen; |
125 | 152 |
126 MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc; | 153 MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc; |
127 unsigned char *diffend = extrasrc; | 154 unsigned char *diffend = extrasrc; |
128 unsigned char *extraend = extrasrc + header->extralen; | 155 unsigned char *extraend = extrasrc + header->extralen; |
129 | 156 |
130 do { | 157 while (ctrlsrc < ctrlend) { |
131 ctrlsrc->x = ntohl(ctrlsrc->x); | 158 ctrlsrc->x = ntohl(ctrlsrc->x); |
132 ctrlsrc->y = ntohl(ctrlsrc->y); | 159 ctrlsrc->y = ntohl(ctrlsrc->y); |
133 ctrlsrc->z = ntohl(ctrlsrc->z); | 160 ctrlsrc->z = ntohl(ctrlsrc->z); |
134 | 161 |
135 #ifdef DEBUG_bsmedberg | 162 #ifdef DEBUG_bsmedberg |
136 printf("Applying block:\n" | 163 printf("Applying block:\n" |
137 " x: %u\n" | 164 " x: %u\n" |
138 " y: %u\n" | 165 " y: %u\n" |
139 " z: %i\n", | 166 " z: %i\n", |
140 ctrlsrc->x, | 167 ctrlsrc->x, |
141 ctrlsrc->y, | 168 ctrlsrc->y, |
142 ctrlsrc->z); | 169 ctrlsrc->z); |
143 #endif | 170 #endif |
144 | 171 |
145 /* Add x bytes from oldfile to x bytes from the diff block */ | 172 /* Add x bytes from oldfile to x bytes from the diff block */ |
146 | 173 |
147 if (fbuffer + ctrlsrc->x > fbufend || | 174 if (ctrlsrc->x > static_cast<size_t>(fbufend - fbuffer) || |
148 diffsrc + ctrlsrc->x > diffend) { | 175 ctrlsrc->x > static_cast<size_t>(diffend - diffsrc)) { |
149 rv = UNEXPECTED_ERROR; | 176 rv = UNEXPECTED_ERROR; |
150 goto end; | 177 goto end; |
151 } | 178 } |
152 for (unsigned int i = 0; i < ctrlsrc->x; ++i) { | 179 for (unsigned int i = 0; i < ctrlsrc->x; ++i) { |
153 diffsrc[i] += fbuffer[i]; | 180 diffsrc[i] += fbuffer[i]; |
154 } | 181 } |
155 if ((int) write(filefd, diffsrc, ctrlsrc->x) != ctrlsrc->x) { | 182 if ((int) write(filefd, diffsrc, ctrlsrc->x) != ctrlsrc->x) { |
156 rv = WRITE_ERROR; | 183 rv = WRITE_ERROR; |
157 goto end; | 184 goto end; |
158 } | 185 } |
159 fbuffer += ctrlsrc->x; | 186 fbuffer += ctrlsrc->x; |
160 diffsrc += ctrlsrc->x; | 187 diffsrc += ctrlsrc->x; |
161 | 188 |
162 /* Copy y bytes from the extra block */ | 189 /* Copy y bytes from the extra block */ |
163 | 190 |
164 if (extrasrc + ctrlsrc->y > extraend) { | 191 if (ctrlsrc->y > static_cast<size_t>(extraend - extrasrc)) { |
165 rv = UNEXPECTED_ERROR; | 192 rv = UNEXPECTED_ERROR; |
166 goto end; | 193 goto end; |
167 } | 194 } |
168 if ((int) write(filefd, extrasrc, ctrlsrc->y) != ctrlsrc->y) { | 195 if ((int) write(filefd, extrasrc, ctrlsrc->y) != ctrlsrc->y) { |
169 rv = WRITE_ERROR; | 196 rv = WRITE_ERROR; |
170 goto end; | 197 goto end; |
171 } | 198 } |
172 extrasrc += ctrlsrc->y; | 199 extrasrc += ctrlsrc->y; |
173 | 200 |
174 /* "seek" forwards in oldfile by z bytes */ | 201 /* "seek" forwards in oldfile by z bytes */ |
175 | 202 |
176 if (fbuffer + ctrlsrc->z > fbufend) { | 203 if (ctrlsrc->z < fbufstart - fbuffer || |
| 204 ctrlsrc->z > fbufend - fbuffer) { |
177 rv = UNEXPECTED_ERROR; | 205 rv = UNEXPECTED_ERROR; |
178 goto end; | 206 goto end; |
179 } | 207 } |
180 fbuffer += ctrlsrc->z; | 208 fbuffer += ctrlsrc->z; |
181 | 209 |
182 /* and on to the next control block */ | 210 /* and on to the next control block */ |
183 | 211 |
184 ++ctrlsrc; | 212 ++ctrlsrc; |
185 } while (ctrlsrc < ctrlend); | 213 } |
186 } | 214 } |
187 | 215 |
188 end: | 216 end: |
189 free(buf); | 217 free(buf); |
190 return rv; | 218 return rv; |
191 } | 219 } |
192 | 220 |
193 int CalculateCrc(const unsigned char *buf, int size) { | 221 int CalculateCrc(const unsigned char *buf, int size) { |
194 CrcGenerateTable(); | 222 CrcGenerateTable(); |
195 unsigned int crc = 0xffffffffL; | 223 unsigned int crc = 0xffffffffL; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 293 |
266 ret = MBS_ApplyPatch(&header, pfd, buf, nfd); | 294 ret = MBS_ApplyPatch(&header, pfd, buf, nfd); |
267 } while (0); | 295 } while (0); |
268 | 296 |
269 free(buf); | 297 free(buf); |
270 close(pfd); | 298 close(pfd); |
271 if (ofd >= 0) close(ofd); | 299 if (ofd >= 0) close(ofd); |
272 if (nfd >= 0) close(nfd); | 300 if (nfd >= 0) close(nfd); |
273 return ret; | 301 return ret; |
274 } | 302 } |
OLD | NEW |