You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FileManager: avoid a TOCTOU issue in computing CWD (#1035)
On Windows, we could potentially return a `nil` for the current working
directory in the rare case that the current working directory was
changed during the computation:
```swift
let dwLength: DWORD = GetCurrentDirectoryW(0, nil) // 1
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
if GetCurrentDirectoryW(dwLength, $0.baseAddress) == dwLength - 1 { // 2
return String(decodingCString: $0.baseAddress!, as: UTF16.self)
}
return nil // 3
}
```
Consider the case where at step 1, we receive $n$. We then are
interrupted, the CWD changed. We then perform step 2, where we receive
$m$ (st $m != n$). We would then proceed to point 3, where we return
`nil`. Avoid this TOCTOU issue by repeating this operation to a fixed
point.
Because we are guaranteed a current directory on Windows (unless the
initial query for the buffer size fails), we will eventually succeed. In
order to avoid a DoS attack vector, limit the attempt to quiescence to a
fixed number.
0 commit comments