Commit 0cd39cb
authored
Fixes after AVSS scanning #298
Fixes for https://github.com/apache/tooling-agents/blob/main/ASVS/reports/logging-log4net/f57d7b3/issues.md
---
## Issue: FINDING-001 - Filter chain modification methods lack synchronization, creating potential race with FilterEvent under active logging
**Labels:** bug, security, priority:low
**Description:**
### Summary
The `AddFilter` and `ClearFilters` methods in `AppenderSkeleton.cs` lack proper synchronization, creating a race condition with `FilterEvent` during active logging operations. This can lead to inconsistent filter chain state, potentially causing filters to be skipped, `NullReferenceException`, or lost filter entries.
### Details
**CWE:** CWE-362 (Concurrent Execution using Shared Resource with Improper Synchronization)
**ASVS:** 15.4.1 (L3)
**Data Flow:**
- `AddFilter` (no lock) → modifies `FilterHead`/`_tailFilter`/`filter.Next`
- `FilterEvent` (under `LockObj` in `DoAppend`) reads `FilterHead` and traverses `f.Next`
**Attack Vector:**
In-process code within the trust boundary calling `AddFilter`/`ClearFilters` concurrently with active logging—for example, during dynamic reconfiguration while the appender is receiving log events.
**Impact:**
Inconsistent filter chain state during traversal in `FilterEvent`, resulting in:
- Filters being skipped during evaluation
- `NullReferenceException` during chain traversal
- Lost filter entries
### Remediation
Add `lock(LockObj)` to both `AddFilter` and `ClearFilters` methods to synchronize with the `DoAppend` hot path and ensure thread-safe filter chain modifications.
### Acceptance Criteria
- [x] Fixed: `AddFilter` method wrapped with `lock(LockObj)`
- [x] Fixed: `ClearFilters` method wrapped with `lock(LockObj)`
- [x] Test added: Concurrent filter modification during active logging
### References
- File: `src/log4net/Appender/AppenderSkeleton.cs`
- Source Report: 15.4.1.md
### Priority
**Low** - Requires in-process code with concurrent reconfiguration during active logging. Limited to availability/integrity impact within the logging subsystem.
---
## Issue: FINDING-002 - InterProcessLock Mutex Not Released When Underlying File Stream Is Null
**Labels:** bug, security, priority:low
**Description:**
### Summary
When `InterProcessLock.AcquireLock()` is called and the underlying `_stream` is null (due to a prior file open failure), the named Mutex is acquired but never released. This causes a resource leak that blocks other processes attempting to use InterProcessLock on the same file, potentially leading to deadlock or resource exhaustion.
### Details
**CWE:** CWE-772 (Missing Release of Resource after Effective Lifetime)
**ASVS:** 1.4.3 (L2)
**Data Flow:**
1. `InterProcessLock.AcquireLock()` called with `_stream == null`
2. `_mutex.WaitOne()` acquires the named Mutex
3. `_recursiveWatch` is incremented
4. Method returns null without releasing the mutex
5. Caller (`FileAppender.Append`) does not enter try/finally block
6. `ReleaseLock()` is never called
7. Named system Mutex remains held indefinitely
**Attack Vector:**
Not directly exploitable by external attackers. Requires environmental file open failure (e.g., permissions, disk full, file locked by another process).
**Impact:**
- Named Mutex remains held indefinitely
- Other processes using InterProcessLock on the same file are blocked
- Potential deadlock across processes
- Resource exhaustion if multiple locks are leaked
### Remediation
Release the named Mutex immediately when `AcquireLock()` detects that `_stream` is null:
1. Decrement `_recursiveWatch`
2. Call `_mutex.ReleaseMutex()`
3. Return null
Ensure all code paths that acquire the mutex properly release it, even in error conditions.
### Acceptance Criteria
- [x] Fixed: Mutex released when `_stream` is null in `AcquireLock()`
- [x] Fixed: `_recursiveWatch` properly decremented in error path
- [x] Test added: Verify mutex released when file stream is null
### References
- File: `src/log4net/Appender/FileAppender.cs`
- Source Report: 1.4.3.md
### Priority
**Low** - Requires environmental file system failure. Impact limited to inter-process synchronization and resource exhaustion within logging subsystem.
---
## Issue: FINDING-003 - Finalizer path lacks exception protection, risking process termination
**Labels:** bug, security, priority:low
**Description:**
### Summary
The `~AppenderSkeleton()` finalizer calls `Close()` which in turn calls `OnClose()` without exception protection. An unhandled exception on the finalizer thread will terminate the entire process in .NET Framework 2.0+ and .NET Core/5+.
### Details
**CWE:** CWE-755 (Improper Handling of Exceptional Conditions)
**ASVS:** 16.5.4 (L3)
**Data Flow:**
GC finalizer thread → `~AppenderSkeleton()` → `Close()` → `OnClose()` (subclass implementation) → unhandled exception → **process termination**
**Attack Vector:**
If a subclass implementation of `OnClose()` throws an unhandled exception during finalization (e.g., due to resource cleanup failure, network timeout, or malformed state), the finalizer thread will propagate the exception and terminate the entire application process.
**Impact:**
- Complete application/service termination
- Denial of service
- Loss of in-flight data
- Ungraceful shutdown without proper cleanup
### Remediation
1. Wrap the finalizer's call to `Close()` in a try-catch block:
```csharp
catch (Exception ex) when (!ex.IsFatal())
{
// Log if possible, otherwise suppress
}
```
2. Consider protecting `Close()` itself with exception handling
3. Ensure `_isClosed` is set in a finally block to prevent repeated finalization attempts
### Acceptance Criteria
- [x] Fixed: Finalizer wrapped with try-catch for non-fatal exceptions
- [x] Fixed: `_isClosed` flag set in finally block
- [x] Code review: Verify fatal exceptions (OutOfMemoryException, StackOverflowException) are not caught
### References
- File: `src/log4net/Appender/AppenderSkeleton.cs`
- Source Report: 16.5.4.md
### Priority
**Low** - Requires specific failure conditions during finalization. However, impact is severe (process termination) when triggered. Recommend prioritizing fix despite low likelihood.7 files changed
Lines changed: 195 additions & 15 deletions
File tree
- src
- changelog/3.3.2
- log4net.Tests/Appender
- log4net/Appender
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
151 | 153 | | |
152 | 154 | | |
153 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
154 | 186 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
71 | 76 | | |
72 | | - | |
73 | 77 | | |
74 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
| |||
111 | 119 | | |
112 | 120 | | |
113 | 121 | | |
| 122 | + | |
114 | 123 | | |
115 | 124 | | |
116 | 125 | | |
| |||
198 | 207 | | |
199 | 208 | | |
200 | 209 | | |
201 | | - | |
202 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
203 | 218 | | |
204 | 219 | | |
205 | 220 | | |
| |||
248 | 263 | | |
249 | 264 | | |
250 | 265 | | |
251 | | - | |
| 266 | + | |
252 | 267 | | |
253 | 268 | | |
254 | 269 | | |
| |||
332 | 347 | | |
333 | 348 | | |
334 | 349 | | |
335 | | - | |
| 350 | + | |
336 | 351 | | |
337 | 352 | | |
338 | 353 | | |
| |||
456 | 471 | | |
457 | 472 | | |
458 | 473 | | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
| 474 | + | |
464 | 475 | | |
465 | | - | |
466 | | - | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
467 | 485 | | |
468 | 486 | | |
469 | 487 | | |
| |||
475 | 493 | | |
476 | 494 | | |
477 | 495 | | |
478 | | - | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
479 | 503 | | |
480 | 504 | | |
481 | 505 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
657 | 657 | | |
658 | 658 | | |
659 | 659 | | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
660 | 663 | | |
661 | 664 | | |
662 | 665 | | |
| |||
0 commit comments