Skip to content

Harden memory-safety and RPC boundary handling in core C paths (excluding workflow @latest) - #303

Merged
AKuHAK merged 2 commits into
masterfrom
copilot/check-bad-code-vulnerabilities
May 17, 2026
Merged

AKuHAK merged 2 commits into
masterfrom
copilot/check-bad-code-vulnerabilities

Conversation

Copilot AI commented May 17, 2026

Copy link
Copy Markdown
Contributor

This PR addresses unsafe code paths flagged during security review, focusing on memory-safety and trust-boundary handling in runtime C code. Workflow @latest usage was intentionally left unchanged per scope.

  • String formatting hardening

    • Replaced unbounded formatting with bounded APIs:
      • vsprintf → vsnprintf in UI text formatting
      • sprintf → snprintf for NVRAM backup path and model/color string writes
    • Prevents stack/heap overwrite risk from oversized formatted output.
  • RPC boundary and buffer safety

    • Added request validation in IOP RPC handler (MechaScmdHeader) for:
      • null/undersized RPC payloads
      • oversized inputlength values
    • Updated EE-side RPC call path (MechaScmd) to validate both input and output lengths and copy only caller-requested output size.
  • Allocation failure handling in rendering paths

    • Added null checks for malloc/memalign in texture creation code.
    • Added early cleanup/return on allocation failure to avoid dereference of invalid memory.
  • Pointer lifecycle cleanup fixes

    • Initialized texture pointers that may be conditionally assigned.
    • Removed/guarded risky free paths in unsupported-version UI flow to avoid invalid/double-free scenarios.
  • ROMVER read safety

    • Switched to bounded, zero-initialized ROMVER buffers and guarded usage on short reads before index-based access.
int MechaScmd(u8 cmd, void *input, u8 inputlength, void *output, u8 outputlength)
{
    struct MechaScmdParams *params = (struct MechaScmdParams *)RpcBuffer;
    if (inputlength > sizeof(params->input) || outputlength > sizeof(params->output) ||
        (inputlength > 0 && input == NULL))
        return 0;

    /* ... RPC call ... */

    if (output && outputlength > 0)
        memcpy(output, params->output, outputlength);
    return params->result;
}

@AKuHAK
AKuHAK marked this pull request as ready for review May 17, 2026 18:19
@AKuHAK
AKuHAK merged commit 2dc0145 into master May 17, 2026
4 checks passed
@AKuHAK
AKuHAK deleted the copilot/check-bad-code-vulnerabilities branch May 17, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants