Mitigation Strategy: Strict evbuffer
Usage and Bounds Checking
Mitigation Strategy: Strict evbuffer
Usage and Bounds Checking
* **Description:**
1. **Initialization:** Use `evbuffer_new()` for proper initialization.
2. **Adding Data:** Use only `evbuffer_add()`, `evbuffer_add_printf()`, `evbuffer_add_vprintf()`, or `evbuffer_prepend()`. *Never* directly modify the underlying memory.
3. **Reading Data:** *Always* call `evbuffer_get_length()` before reading. Use `evbuffer_remove()`, `evbuffer_remove_buffer()`, `evbuffer_copyout()`, or `evbuffer_drain()`. *Never* read directly. Ensure the output buffer for `evbuffer_copyout()` is large enough.
4. **`evbuffer_pullup()` Caution:** Use only when necessary; prefer `evbuffer_copyout()`.
5. **Draining:** Use `evbuffer_drain()` after processing.
6. **Freeing:** Use `evbuffer_free()` when finished.
7. **Expanding:** Use `evbuffer_expand()` and check its return value.
* **Threats Mitigated:**
* **Buffer Overflow (Severity: Critical):** Prevents writing beyond `evbuffer` bounds.
* **Buffer Underflow (Severity: High):** Prevents reading before valid data in `evbuffer`.
* **Use-After-Free (Severity: Critical):** Proper freeing with `evbuffer_free()`.
* **Data Corruption (Severity: High):** Enforces `evbuffer` API usage.
* **Impact:**
* **Buffer Overflow/Underflow/Use-After-Free:** Risk reduced to near zero with correct implementation.
* **Data Corruption:** Risk significantly reduced.
* **Currently Implemented:**
* **Network Input Handling:** Implemented in `network_input.c` (`handle_incoming_data()`, `process_packet()`). `evbuffer_get_length()` checks are present.
* **File Processing:** Partially implemented in `file_processor.c`. `evbuffer_add()` is used, but length checks are missing in `parse_file_chunk()`.
* **Missing Implementation:**
* **File Processing:** Missing length checks in `file_processor.c`, `parse_file_chunk()` before `evbuffer_remove()`.
* **Logging:** Review `logger.c` (which uses `evbuffer` internally) for consistent bounds checking.
Mitigation Strategy: Resource Limits and Connection Management (using libevent
APIs)
Mitigation Strategy: Resource Limits and Connection Management (using libevent
APIs)
* **Description:**
1. **Connection Limits:** Use `event_base_set_max_conn()` (if available in your `libevent` version) to set a maximum number of concurrent connections. If unavailable, implement custom tracking within your `libevent` callbacks.
2. **Timeouts:** Use `bufferevent_set_timeouts()` for `bufferevent`-based connections to set read, write, and connect timeouts. For individual events, use the timeout parameter in `event_add()`.
3. **Non-Blocking Handling:** When using `EVLOOP_NONBLOCK`, ensure your loop checks the return value of `event_base_loop()`. If it returns 0 (no events ready), avoid busy-waiting.
* **Threats Mitigated:**
* **Connection Exhaustion DoS (Severity: High):** `event_base_set_max_conn()` directly limits connections.
* **Slowloris Attack (Severity: High):** `bufferevent_set_timeouts()` and `event_add()` timeouts prevent slow connections.
* **Resource Starvation (Severity: High):** Timeouts and connection limits prevent resource exhaustion.
* **Impact:**
* **Connection Exhaustion DoS/Slowloris/Resource Starvation:** Risk significantly reduced with appropriate limits and timeouts.
* **Currently Implemented:**
* **Connection Limits:** Implemented globally (hardcoded limit of 1000) in `main.c` (needs review to use `event_base_set_max_conn()` if available).
* **Timeouts:** Read/write timeouts (30 seconds) set for `bufferevent`s in `connection_handler.c`.
* **Missing Implementation:**
* **`event_base_set_max_conn()`:** Check `libevent` version and use this function if available.
* **Non-Blocking Handling:** Review the main event loop (using `EVLOOP_NONBLOCK`) to ensure it doesn't busy-wait.
Mitigation Strategy: Backend Selection (libevent
configuration)
Mitigation Strategy: Backend Selection (libevent
configuration)
* **Description:**
1. **Backend Choice:** During `event_base_new()`, `libevent` usually selects the best backend. You can *optionally* use `event_config_avoid_method()` (with an `event_config` object passed to `event_base_new_with_config()`) to *exclude* specific backends if you have a strong reason to do so (e.g., a known issue with a particular backend on a specific OS version). Generally, relying on `libevent`'s automatic selection is best.
* **Threats Mitigated:**
* **Backend-Specific Vulnerabilities (Severity: Variable):** Allows (though rarely needed) excluding a problematic backend.
* **Impact:**
* **Backend-Specific Vulnerabilities:** Risk potentially reduced by excluding a specific, vulnerable backend (but this is an uncommon scenario).
* **Currently Implemented:**
* **Backend Choice:** The application uses the default `libevent` backend selection.
* **Missing Implementation:**
* None. The default behavior is generally correct. Only use `event_config_avoid_method()` if a specific, documented issue exists with a particular backend on the target platform.
Mitigation Strategy: Safe Signal Handling (using libevent
's signal API)
Mitigation Strategy: Safe Signal Handling (using libevent
's signal API)
* **Description:**
1. **`evsignal_new`:** Use `evsignal_new()` to create an event for the signal.
2. **`evsignal_add`:** Use `evsignal_add()` to add the signal event to the `event_base`.
3. **Signal Callback:** Keep the callback minimal and thread-safe. Avoid blocking operations. Set a flag or write to a self-pipe.
* **Threats Mitigated:**
* **Race Conditions (Severity: Medium):** Avoids races with traditional signal handlers.
* **Deadlocks (Severity: High):** Avoids blocking in the callback.
* **Application Crashes (Severity: High):** Prevents crashes from signal delivery.
* **Impact:**
* **Race Conditions/Deadlocks/Crashes:** Risk significantly reduced.
* **Currently Implemented:**
* **`SIGINT` and `SIGTERM` Handling:** Implemented using `evsignal_new` and `evsignal_add` in `main.c`.
* **Missing Implementation:**
* **Other Signals:** Review if handling for other signals (e.g., `SIGHUP`) is needed.
Mitigation Strategy: Stay Up-to-Date and Avoid Deprecated libevent
Features
Mitigation Strategy: Stay Up-to-Date and Avoid Deprecated libevent
Features
* **Description:**
1. **Regular Updates:** Update `libevent` to the latest stable release.
2. **Documentation Review:** Review release notes and documentation for deprecated features.
3. **Code Review:** Ensure no deprecated features are used; replace them with recommended alternatives.
4. **Avoid Experimental Features:** Do not use features marked as experimental.
* **Threats Mitigated:**
* **Vulnerabilities in Deprecated Features (Severity: Variable):** Avoids using potentially vulnerable code.
* **Compatibility Issues (Severity: Medium):** Ensures compatibility with future releases.
* **Impact:**
* **Vulnerabilities/Compatibility:** Risk significantly reduced.
* **Currently Implemented:**
* **`libevent` Version:** Currently using `libevent` 2.1.12.
* **Update Process:** Informal process exists, but it's not consistently followed.
* **Missing Implementation:**
* **Formal Update Process:** A formal process for updating `libevent` is needed.
* **Deprecated Feature Check:** A code review is needed to identify and replace deprecated features.