dotnet / dotnet/dev-proxy

State/liveness keyed on PID alone is vulnerable to PID reuse

Đang mở
#1,755 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
C#
Star
832
Fork
89
Merge trung bình
12 giờ 1 phút
Pull request đã merge (30 ngày)
23

Mô tả

## Description

Dev Proxy tracks detached instances by PID alone (`state-.json`), and `StateManager` determines whether an instance is "alive" purely by checking whether a process with that PID currently exists (`StateManager.IsProcessRunning(int pid)` → `Process.GetProcessById`).

Operating systems recycle PIDs. After an instance exits (cleanly or via crash), its PID number can be reassigned to a completely unrelated process. When that happens:

- `IsProcessRunning(pid)` returns `true` for the stale state record, so Dev Proxy believes its old instance is still alive.
- Commands that key on liveness (`stop`, `status`, `LoadAllStatesAsync`, `FindSystemProxyInstanceAsync`, and the crash-recovery / orphaned-system-proxy reconciliation added in the `stop --force` fix) can act on — or refuse to act on — the wrong process.

This is a pre-existing, low-probability correctness issue in the whole detached-instance design; it is independent of any single command.

## Suggested fix

Store additional identity beyond the PID in the state file and verify it before treating a PID as "our" live instance. Options:

- Persist the process **start time** (`Process.StartTime`) alongside the PID, and in `IsProcessRunning` compare the running process's start time to the recorded value — a mismatch means the PID was reused and the record is stale.
- Optionally also persist the process name / a Dev Proxy marker as a secondary check.

`Process.StartTime` is available cross-platform in .NET and is the standard, low-cost way to disambiguate PID reuse.

## Notes

- Found while implementing the fix for #1731 (`devproxy stop --force` cannot restore the system proxy after a crashed instance). That fix relies on `asSystemProxy` state records to reconcile orphaned system-proxy registrations; PID-reuse hardening would make that reconciliation (and all liveness checks) more robust but is intentionally out of scope for that change.
- Affected code: `DevProxy/State/StateManager.cs` (`IsProcessRunning`, `LoadStateFromFileAsync`, `GetOrphanedSystemProxyStatesAsync`), `DevProxy/State/ProxyInstanceState.cs`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách đọc DevProxy/State/StateManager.cs, đặc biệt là IsProcessRunning, LoadStateFromFileAsync và GetOrphanedSystemProxyStatesAsync, sau đó kiểm tra DevProxy/State/ProxyInstanceState.cs để hiểu dữ liệu trong tệp trạng thái. Theo dõi cách các lệnh dành cho các instance đã tách sử dụng trạng thái còn hoạt động của PID. Hoàn thành khi trạng thái cũ bị từ chối nếu một PID đã được tái sử dụng, trong khi các instance hợp lệ vẫn tiếp tục được nhận diện.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp
Lĩnh vực
cli, devtools
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.