Skip to content

Latest commit

 

History

History
101 lines (59 loc) · 10.9 KB

bug-types.md

File metadata and controls

101 lines (59 loc) · 10.9 KB

This file is meant to settle and document how the Gato programming language will eliminate or mitigate as many types of bugs and vulnerabilities as possible. I used the Wikipedia article on software bugs as a starting point to build this list of bug types, and I've included links to dedicated Wikipedia articles in most section titles. If you see something wrong or missing, please open an issue or pull request.

Resources to understand the stack:

Vulnerabilities to prevent:

Existing solutions: I don't know of any language which completely solves this problem. According to its wiki, Haskell can overflow in non-obvious ways. Interpreted programs in languages like JavaScript and Python typically can't overflow the stack allocated by the OS, but the interpreter typically enforces a recursion depth limit (Python allows changing the limit, but doing so is unsafe). Rust doesn't seem to have any protection against stack overflows, it's easy to trigger one in the Rust Playground. At the time I'm writing this, Zig hasn't yet implemented safe recursion.

Envisioned solutions:

  • The compiler is in charge of preventing a stack overflow. During compilation, it keeps track of a program's call graph (i.e. which functions call which functions), taking into account that functions can have callbacks. This enables it to detect cycles in the graph (both direct and indirect recursions), and automatically prevent them from resulting in a stack overflow, either by removing the cycles entirely (i.e. the calls between the functions in a compiled module would form a directed acyclic graph) or by injecting mitigations into the compiled code at any point where a cycle begins (at a small performance cost). Having all the necessary information, the compiler injects instructions to ask the operating system for the amount of stack space a program needs when it's launched and when it enters recursions (if they haven't been removed). If the necessary stack space can't be obtained, a program exits with a proper error code and message.
  • Calling a library written in an unsafe language automatically happens in a subprocess. sigaltstack is used to handle stack overflows in such subprocesses.

Unintentionally infinite loops

Examples:

Envisioned solutions:

  • Encourage explicit limits on loops, through syntactic sugar that makes it easy to define the maximum number of iterations and/or maximum run time of a loop.
  • If Gato ends up getting a while loop keyword, the compiler only allows its use if it can prove that the loop will end.
  • Looping through recursive function calls is only allowed if the compiler can prove that the call chain will end and won't result in an unhandled exception due to a stack overflow.
  • Calling a function written in an unsafe language automatically happens in a subprocess which is interrupted if it exceeds its maximum allowed run time.

Missing or excessively long timeouts

Envisioned mitigation: the maximum run time of a function can be properly declared and easily enforced. In simple cases the maximum run time of a function can be determined during compilation, but more complex programs require keeping track of elapsed time during execution.

Unintentionally high algorithmic complexity

Examples: Accidentally Quadratic. A particularly tricky one was Rust hash iteration+reinsertion.

Envisioned mitigation: the language supports declaring the costs of a function. The compiler tries its best to verify those costs, ideally by both proving them mathematically and confirming them with measurements. Tools to measure a function's complexity already exist for other languages (Python, Rust).

Division by zero and other nonsensical mathematical operation (e.g. addition of amounts in different currencies)

Envisioned solution:

  1. The language supports defining precisely which values a variable is expected to hold, not merely the type of values or how many of them there are. This enables static analysis to detect some bugs that can't be caught by traditional type checkers, including division by zero, because the analyzer knows whether zero is one of the possible values for the divisor, whereas a type checker can only confirm that the divisor is of a compatible numerical type.
  2. In addition to precisely defining the values that a single function argument can have, the language supports defining constraints spanning multiple arguments (a.k.a. contracts). This enables detecting nonsensical operations through static analysis. Run time enforcement of preconditions is also possible and results in an exception being raised when a precondition is violated.

Envisioned mitigation: the index of the first element in a container is 1, not 0. Coupled with previously mentioned language features, this enables detecting at compile time the subset of off-by-one errors that result in out-of-bounds access attempts.

Arithmetic overflow and underflow

Envisioned solution: an overflow or underflow is an error by default. Wrapping or saturating must be explicitly requested by the programmer. Syntactic sugar is provided to facilitate that. During static analysis, a computation which can be proven not to overflow or underflow is marked as safe. Unconstrained numerical types, like Python's int, are available for use cases when defining limits is deemed undesirable or unnecessary by the programmer.

Envisioned mitigation: the ability to declare the expected precision of important values, combined with the previously mentioned ability to specify the exact range of values a variable holds, enables static analysis to detect unsafe computations. (I have no expertise or experience in this area, so I might be wrong.)

TODO

TODO

Uncoordinated parallel computing (non-atomic operations on shared resources without locking, a.k.a. race condition)

A good description of the problem: Rust Prevents Data Races, Not Race Conditions.

Envisioned solution: for every write to shared memory, all shared variables directly or indirectly involved in the write must have been locked since before any of them became involved. This rule is enforced by the compiler. A variable is involved in a write if it can affect the value being written or whether the write happens. A variable is shared if multiple processes/threads/whatevers use it concurrently.

Out of scope: a program implementing writes to shared resources in a way that leaves the compiler unaware that they are shared resources could still have race conditions. This includes any program that unsafely delegates writes to another program like an SQL database.

Envisioned solution: always avoid at least one of the conditions necessary to reach a deadlock state. For example, a transaction can avoid deadlocks by safely acquiring all the locks it needs when it begins. This can be achieved by either declaration or automated detection of the needed locks.

Out of scope: a program implementing locks in a way that leaves the compiler unaware of them could still end up in a deadlock or livelock. This includes any program that unsafely delegates locking to another program like an SQL database.

Envisioned mitigation: the language supports declaring that a function's running time must not depend on the values of specific variables. The compiler does all it can to enforce that requirement, which includes trying to prevent the CPU from executing some instructions on the secrets in variable time.

TODO

Envisioned mitigation: same as Python's (== is the equality operator, := is the assignment-in-an-expression operator).

Envisioned mitigation: refuse to print legacy control characters when writing to a terminal, except through functions designed to do it in a safe way. This should prevent programs written in Gato from being abused to transmit an exploit to a terminal.

Envisioned mitigation: outgoing network connections/requests are initiated in different ways that separate them based on whether the destination address should be private or public. If the provided address doesn't meet the expectation, an exception is raised.