Commit c4cfe16
authored
Fix a race condition between certain Has properties and their collection property. (#843)
We found a rare race condition between `MethodDefinition.HasOverrides` and `MethodDefinition.Overrides`.
What can happen is
1) Thread 1 get's past the null check in `MethodDefinition.HasOverrides` and then is suspended.
2) Thread 2, calls `MethodDefinition.Overrides` and executes at least as far as the `metadata.RemoveOverrideMapping (method)` call in `AssemblyReader.ReadOverrides`
3) Thread 1 resumes on `return HasImage && Module.Read (this, (method, reader) => reader.HasOverrides (method));` It now proceeds to AssemblyReader.HasOverrides. No overrides are found and false is returned due to the overrides for that method having been removed from `MetadataSystem`
To recap, the two notable behaviors are triggering this are
a) The following check in `MethodDefinition.HasOverrides` happens outside of the lock.
```
if (overrides != null)
return overrides.Count > 0;
```
b) The call to `metadata.RemoveOverrideMapping` in `AssemblyReader.ReadOverrides` means that `AssemblyReader.ReadOverrides` and `AssemblyReader.HasOverrides` cannot be called again after the first call to `AssemblyReader.ReadOverrides`
I did not attempt to reproduce this vulnerability for every pair of properties that follows this pattern. However, I think it's safe to assume any pair of properties that follows this same pattern is vulnerable.
Using `ReadingMode.Deferred` also appears to be a required prerequisite to encounter this problem.
We had two thoughts on how to fix this
1) Repeat the collection null check after obtaining the module lock in `Module.Read` during `MethodDefinition.HasOverrides`
2) Remove the behavior of `AssemblyReader` removing data from the `MetadataSystem`.
I decided to go with Fix 2 because it was easy to find all of problematic property pairings by searching `MetadataSystem.cs` for `Remove`. I also feel that this behavior of modifying the metadata system is asking for problems and probably not worth the freed memory is provides.
If you'd prefer Fix 1 instead. Or both Fix 1 & Fix 2 let me know and I can change around the PR.1 parent 65a2912 commit c4cfe16
2 files changed
+0
-69
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
899 | 899 | | |
900 | 900 | | |
901 | 901 | | |
902 | | - | |
903 | | - | |
904 | 902 | | |
905 | 903 | | |
906 | 904 | | |
| |||
975 | 973 | | |
976 | 974 | | |
977 | 975 | | |
978 | | - | |
979 | 976 | | |
980 | 977 | | |
981 | 978 | | |
| |||
1242 | 1239 | | |
1243 | 1240 | | |
1244 | 1241 | | |
1245 | | - | |
1246 | | - | |
1247 | 1242 | | |
1248 | 1243 | | |
1249 | 1244 | | |
| |||
1466 | 1461 | | |
1467 | 1462 | | |
1468 | 1463 | | |
1469 | | - | |
1470 | | - | |
1471 | 1464 | | |
1472 | 1465 | | |
1473 | 1466 | | |
| |||
1536 | 1529 | | |
1537 | 1530 | | |
1538 | 1531 | | |
1539 | | - | |
1540 | | - | |
1541 | 1532 | | |
1542 | 1533 | | |
1543 | 1534 | | |
| |||
1912 | 1903 | | |
1913 | 1904 | | |
1914 | 1905 | | |
1915 | | - | |
1916 | | - | |
1917 | 1906 | | |
1918 | 1907 | | |
1919 | 1908 | | |
| |||
2029 | 2018 | | |
2030 | 2019 | | |
2031 | 2020 | | |
2032 | | - | |
2033 | | - | |
2034 | 2021 | | |
2035 | 2022 | | |
2036 | 2023 | | |
| |||
2083 | 2070 | | |
2084 | 2071 | | |
2085 | 2072 | | |
2086 | | - | |
2087 | | - | |
2088 | 2073 | | |
2089 | 2074 | | |
2090 | 2075 | | |
| |||
2521 | 2506 | | |
2522 | 2507 | | |
2523 | 2508 | | |
2524 | | - | |
2525 | | - | |
2526 | 2509 | | |
2527 | 2510 | | |
2528 | 2511 | | |
| |||
2676 | 2659 | | |
2677 | 2660 | | |
2678 | 2661 | | |
2679 | | - | |
2680 | | - | |
2681 | 2662 | | |
2682 | 2663 | | |
2683 | 2664 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | 246 | | |
252 | 247 | | |
253 | 248 | | |
| |||
258 | 253 | | |
259 | 254 | | |
260 | 255 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | 256 | | |
267 | 257 | | |
268 | 258 | | |
| |||
273 | 263 | | |
274 | 264 | | |
275 | 265 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | 266 | | |
282 | 267 | | |
283 | 268 | | |
| |||
288 | 273 | | |
289 | 274 | | |
290 | 275 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | 276 | | |
297 | 277 | | |
298 | 278 | | |
| |||
303 | 283 | | |
304 | 284 | | |
305 | 285 | | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | 286 | | |
312 | 287 | | |
313 | 288 | | |
314 | 289 | | |
315 | 290 | | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | 291 | | |
322 | 292 | | |
323 | 293 | | |
324 | 294 | | |
325 | 295 | | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | 296 | | |
332 | 297 | | |
333 | 298 | | |
334 | 299 | | |
335 | 300 | | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | 301 | | |
342 | 302 | | |
343 | 303 | | |
| |||
348 | 308 | | |
349 | 309 | | |
350 | 310 | | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | 311 | | |
357 | 312 | | |
358 | 313 | | |
| |||
363 | 318 | | |
364 | 319 | | |
365 | 320 | | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | 321 | | |
372 | 322 | | |
373 | 323 | | |
| |||
0 commit comments