Commit 26ba141
committed
darwin: fix use after free during reenumeration
When one process has two contexts to libusb, it may trigger a reenumeration
while the other context accesses `*dpriv->device`. Adding a mutex to access
`device` can be costly because it is used in every function. Instead, we
maintain an invariant: the `darwin_cached_device` must be valid at all
times. In particular, this means the fields `device` and `service` must not
be stale or we will have a use after free triggerable through a race
condition.
Previously, in `darwin_devices_detached` during reenumeration, it was
possible for `old_device->device` to be freed. We now defer this free to
`darwin_get_cached_device` after a new `device` is written. Note that the
order is important because we do not have locks, so we must free the old
device after writing the new one. The same point applies to `service` which
we take care to free after the new `service` is written.
A caveat here is that even if we do not crash it is still possible for one
context to be using an outdated `device` and `service` leading to an error
return. This should be acceptable because it would be a similar situation
to the device being detached during an API call.1 parent e3af735 commit 26ba141
1 file changed
+15
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
408 | 408 | | |
409 | 409 | | |
410 | 410 | | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | 411 | | |
419 | 412 | | |
420 | 413 | | |
| |||
1014 | 1007 | | |
1015 | 1008 | | |
1016 | 1009 | | |
1017 | | - | |
| 1010 | + | |
| 1011 | + | |
1018 | 1012 | | |
1019 | 1013 | | |
1020 | 1014 | | |
| |||
1042 | 1036 | | |
1043 | 1037 | | |
1044 | 1038 | | |
| 1039 | + | |
| 1040 | + | |
1045 | 1041 | | |
1046 | 1042 | | |
1047 | 1043 | | |
| |||
1084 | 1080 | | |
1085 | 1081 | | |
1086 | 1082 | | |
1087 | | - | |
1088 | | - | |
1089 | | - | |
1090 | 1083 | | |
1091 | 1084 | | |
1092 | 1085 | | |
| |||
1100 | 1093 | | |
1101 | 1094 | | |
1102 | 1095 | | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
1103 | 1107 | | |
1104 | 1108 | | |
1105 | 1109 | | |
| |||
0 commit comments