# MIT License
#
# Copyright (c) 2022 raptor
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

rules:
  - id: raptor-insecure-api-sprintf-vsprintf
    metadata:
      author: Marco Ivaldi <raptor@0xdeadbeef.info>
      references:
        - https://cwe.mitre.org/data/definitions/676
        - https://cwe.mitre.org/data/definitions/120
        - https://cwe.mitre.org/data/definitions/787
        - https://g.co/kgs/PCHQjJ
      confidence: HIGH
    message: >-
      The program invokes a potentially dangerous function that could
      introduce a vulnerability if it is used incorrectly, but the function
      can also be used safely.
      A buffer overflow condition exists when a program attempts to put
      more data in a buffer than it can hold, or when a program attempts to
      put data in a memory area outside of the boundaries of a buffer. The
      simplest type of error, and the most common cause of buffer
      overflows, is the classic case in which the program copies the buffer
      without restricting how much is copied. Other variants exist, but the
      existence of a classic overflow strongly suggests that the programmer
      is not considering even the most basic of security protections.
    severity: ERROR
    languages:
      - c
      - cpp
    patterns:
      - pattern-either:
        - pattern: sprintf($BUF, $FMT, ...)
        - pattern: vsprintf($BUF, $FMT, ...)
        # swprintf() and vswprintf() should have a size parameter
      - metavariable-regex:
          metavariable: $FMT
          # NOTE: some format string modifiers are not handled
          regex: '(".*%l?s.*"|".*%S.*"|[a-zA-Z_][a-zA-Z0-9_]*)'
