Skip to content

[Query Create]: CWE-125 HIGH - Swift: new query for memory safety #100

Open

Description

@felickz

Target Language

swift

Query Name (Optional)

MemorySafety

Query Type

Security

Query Description

AI Scan reported 1 high-severity CWE-125 (memory safety) finding(s) in Swift public synthetic repositories that the baseline CodeQL default-setup scan did not report. Gap categories: missing-query. Existing queries considered: none.

Why CodeQL appears to have missed these (unverified until reproduced with a test database):

  • No installed Swift query covers unsafe pointer arithmetic with attacker-controlled offsets; a rule needs precise Data/UnsafePointer bounds modeling.

Linked findings (gap-key: swift/query-create/memory-safety)

Repo CWE Severity AI scan alert Summary
OversecuredVulnerableiOSApp CWE-125 high https://github.com/codeql-forks-felickz/OversecuredVulnerableiOSApp/security/code-scanning/6 The public deep-link save route accepts an unrestricted signed integer offset and uses it in unsafe pointer arithmetic while computing a copy length. Negative offsets access memory before the decoded data; offsets larger than the data length produce an invalid negative count. An attacker can trigger out-of-bounds reads or a process crash through a crafted ovia URL.

Findings come from AI Scan on public synthetic apps. Hypotheses above are unverified until reproduced with a test database. Verify first whether CodeQL already covers a case (for example via a different query or threat model) before adding code.

Expected Severity

High

Code Examples

See the linked alerts above for the vulnerable pattern; reduce each to a minimal test case with a safe counterpart that should not alert.

CWE/CVE Reference (Optional)

CWE-125

References (Optional)

https://cwe.mitre.org/data/definitions/125.html

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    enhancement
    severity:high
    on Oct 16, 2026
  2. self-assigned this
    on Oct 16, 2026
  3. felickz commented on Oct 31, 2026

    @felickz
    Author

    Update: additional medium and low severity findings fit this gap (gap-key swift/query-create/memory-safety). They are listed with full context so the Copilot agent can re-validate each one against the proposed change and extend it where it does not cover them.

    Linked findings

    Repo CWE Severity AI scan alert Summary
    iGoat-Swift CWE-125 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/34 Data.aes() always passes keyLength = size_t(kCCKeySizeAES128) (16) to CCCrypt() while the key pointer comes from the caller-supplied `
    iGoat-Swift CWE-125 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/46 String.aesEncrypt(key:iv:options:) / String.aesDecrypt(key:iv:options:) hard-code keyLength = size_t(kCCKeySizeAES128) (16 bytes) whil

    Handling

    Create a new CodeQL query. No existing query covers these patterns (most findings are gap type query-absent or missing-query). Because coverage of lower-severity patterns can be heuristic, target Community Packs unless precision is shown to be high enough for github/codeql.

    Patterns

    • query-absent (2 findings), for example iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Extensions/Data+Extension.swift

    Finding details

    1. iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Extensions/Data+Extension.swift:11 (CWE-125, medium)
    • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/34
    • AI Scan message: Data.aes() always passes keyLength = size_t(kCCKeySizeAES128) (16) to CCCrypt() while the key pointer comes from the caller-supplied `
    • Context for the agent: Data+Extension.swift:11 hard-codes keyLength 16 for CCCrypt while the key pointer comes from a caller buffer of unchecked length. CodeQL would need a query relating buffer size to length arguments of unsafe pointer crypto calls.
    • Why CodeQL appears to miss it: query-absent: Fixed-length 16-byte key size passed to CCCrypt with a caller-supplied buffer can cause an out-of-bounds read, matching the memory safety query request.
    • Recommended action from the comparison: write-new-query (classification confidence: medium)
    2. iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Extensions/NSString+Extension.swift:10 (CWE-125, medium)
    • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/46
    • AI Scan message: String.aesEncrypt(key:iv:options:) / String.aesDecrypt(key:iv:options:) hard-code keyLength = size_t(kCCKeySizeAES128) (16 bytes) whil
    • Context for the agent: NSString+Extension.swift:10 hard-codes a 16-byte key length for CCCrypt calls with a caller-supplied key. Same query need as other CCCrypt length mismatch cases.
    • Why CodeQL appears to miss it: query-absent: Same fixed key length versus caller key buffer pattern in String AES helpers.
    • Recommended action from the comparison: write-new-query (classification confidence: medium)
  4. added
    precision:low
    tags:experimental
    on Nov 3, 2026
  5. felickz commented on Nov 3, 2026

    @felickz
    Author

    Upstream triage

    precision:low Audit-grade or weak signal. Community Packs, always tags:experimental. tags:experimental Goes to Community Packs: performance, false-positive or synthetic-only concerns.

    Community Packs, experimental. Recommended for the Community Packs repository, not github/codeql. The issue is labeled tags:experimental for this.

    Linked pull request: #103

    Why. New query, so github/codeql would need very-high precision. The pointer-offset query is a sound taint design over public Swift APIs, but its barrier only sees if/guard comparisons (precondition, assert and helpers are missed) and it matches stdlib names by string. The bundled key-length query is a syntactic audit check without taint. No stock Swift query covers this, so experimental Community Packs fits once a real-project true positive exists.

    Change New query
    Target New Swift queries: unsafe pointer offset (CWE-125/787) and fixed AES key length passed to CCCrypt
    Precision assessed low (declared medium in the pull request). Pointer query is taint based but its barrier only sees if/guard comparisons and range checks; the bundled key-length query is a syntactic audit check (any Data key parameter plus kCCKeySizeAES128, no local count check), no taint.
    Real-world basis partial. Public Swift stdlib/Foundation APIs (UnsafeMutablePointer, UnsafeBufferPointer, Data(bytes:count:), CCCrypt), but attacker-controlled offsets are rare; the key-length rule fits one wrapper shape from a sample app.
    Stock overlap none. No CWE-125/787 or unsafe-pointer query in stock swift-queries 1.3.10; swift-all has only PointerTypes flow models. Checked the local pack and github/codeql code search.
    Concerns, overlap and next steps

    False-positive concerns

    • Validation through precondition, assert, a helper function or a caller is not recognized, so already-validated offsets are still reported.
    • Any comparison on the variable counts as a bound, and sources include all local and remote FlowSource nodes, widening reports beyond remote input.
    • Key-length query reports every Data key parameter used with a fixed 16 byte length even when callers or construction guarantee the size.

    Performance concerns

    • Hand-written guardedBy barrier joins CFG dominance and localExprFlow for every checked DeclRefExpr; not measured on a large database.

    Heuristics used

    • Stdlib type and member names matched by string
    • Guard shapes limited to if/guard comparisons, contains and ~=
    • Structural match on kCCKeySizeAES128 and withUnsafeBytes

    What an upstream change would look like. Community Packs swift/src/<category>/ (the Swift pack has no queries yet) with swift/test inline-expectation tests; URLComponents summaries stay QL SummaryModelCsv since Swift has no MaD YAML.

    Before opening an upstream pull request

    • Show at least one true positive on a revision of a real project; current tests are reduced from a vulnerable sample app.
    • Split the unrelated crypto key length query into its own change or drop it; it has no attacker-controlled input.
    • Recognize precondition/assert/helper validation or document it as a limitation, and add negative tests for it.
    • Report results and runtime on a large public Swift corpus before proposing anything for github/codeql.

    Split note. Two unrelated queries are bundled: swift/untrusted-pointer-offset (medium, taint based, the real subject of the issue) and swift/untrusted-crypto-key-length (low, audit grade, no taint). Overall precision is the lower one.

    This is a recommendation only. A human reviews it before any pull request is opened against github/codeql or the Community Packs repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CWE-125enhancementprecision:lowquery-createseverity:hightags:experimental

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions