|
| 1 | +# ShareX — `Rfc2898DeriveBytes` (PBKDF2 deriver) created on the upload path, never disposed |
| 2 | + |
| 3 | +**Found by mining** `ShareX/ShareX` @ `ed2a864` (the re-mine after the WinForms |
| 4 | +modeless-`Form` precision fix, #57 — see `docs/notes/real-world-mining.md` and |
| 5 | +`docs/notes/winforms-modeless-precision.md`). Once the dominant modeless-`Form` false |
| 6 | +positives were gone, the local-disposable findings dropped to a short, mostly-real |
| 7 | +list; this is the cleanest real bug in it. |
| 8 | + |
| 9 | +Location: `ShareX.UploadersLib/FileUploaders/Vault_ooo.cs:216`, the Vault.ooo file |
| 10 | +uploader's `DeriveCryptoData`. |
| 11 | + |
| 12 | +## The bug |
| 13 | + |
| 14 | +```csharp |
| 15 | +private static Vault_oooCryptoData DeriveCryptoData(byte[] key) |
| 16 | +{ |
| 17 | + byte[] salt = new byte[8]; |
| 18 | + RandomNumberGenerator rng = RandomNumberGenerator.Create(); // leak #2 (factory) |
| 19 | + rng.GetBytes(salt); |
| 20 | + |
| 21 | + Rfc2898DeriveBytes rfcDeriver = // leak #1 (new) — flagged |
| 22 | + new Rfc2898DeriveBytes(key, salt, PBKDF2_ITERATIONS, HashAlgorithmName.SHA256); |
| 23 | + |
| 24 | + return new Vault_oooCryptoData { Salt = salt, |
| 25 | + Key = rfcDeriver.GetBytes(32), IV = rfcDeriver.GetBytes(16) }; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +`Rfc2898DeriveBytes` is `IDisposable` and holds an internal HMAC. It is created to |
| 30 | +derive the AES key + IV, the method returns, and it is **never disposed** — a real |
| 31 | +resource leak on every Vault.ooo upload. The returned `Vault_oooCryptoData` holds only |
| 32 | +the derived `byte[]`s (Salt/Key/IV), **not** the deriver, so it does not escape: it is |
| 33 | +a clean, method-local leak. The correct fix is `using var rfcDeriver = …`, which is |
| 34 | +exactly what the sibling `EncryptBytes()` in the same file already does for its `aes` / |
| 35 | +`MemoryStream` / `CryptoStream` — so this is an accidental oversight, not a pattern. |
| 36 | + |
| 37 | +## What the checker says (real extractor output, `--flow-locals`) |
| 38 | + |
| 39 | +```text |
| 40 | +Vault_ooo.cs:216: error: [OWN001] IDisposable local 'rfcDeriver' is never disposed |
| 41 | + (leak) [resource: disposable] |
| 42 | +``` |
| 43 | + |
| 44 | +`acquire` is the `new Rfc2898DeriveBytes(…)`, the missing `release` is the absent |
| 45 | +`Dispose()`. Because the deriver is never released on any path the wording is |
| 46 | +"is never disposed" (vs the partial-path "may not be disposed on every path"). |
| 47 | + |
| 48 | +## The honest caveat — a second leak the extractor misses (recall gap) |
| 49 | + |
| 50 | +`DeriveCryptoData` actually leaks **two** crypto disposables. The extractor flags |
| 51 | +`rfcDeriver` (acquired via `new`) but **not** `rng = RandomNumberGenerator.Create()`, |
| 52 | +an `IDisposable` acquired via a static **factory**. The flow detector recognises `new` |
| 53 | +and the `System.IO.File.Open*/Create*` factories (`IsOwningFactory`), but not arbitrary |
| 54 | +`X.Create()` factories. Extending the owning-factory set to the common BCL crypto |
| 55 | +factories (`RandomNumberGenerator.Create()`, `SHA256.Create()`, `Aes.Create()`, …) is a |
| 56 | +separate recall slice; `after.cs` disposes both so the fix is genuinely clean. |
| 57 | + |
| 58 | +## Files |
| 59 | + |
| 60 | +- `before.cs` — the leak, reduced and self-contained (real `System.Security.Cryptography` |
| 61 | + types; no reference pack needed). The extractor catches `rfcDeriver` (OWN001). |
| 62 | +- `after.cs` — both crypto disposables scoped with `using` → silent. |
| 63 | +- `case.own` — the OwnLang reduction (disposable acquire with no release → OWN001), |
| 64 | + checked by `tests/test_corpus.py`. |
0 commit comments