-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doc: Fortify Instructions #15658
Merged
Merged
Doc: Fortify Instructions #15658
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
======== | ||
Fortify | ||
======== | ||
|
||
Overview | ||
-------- | ||
|
||
A common error in C programs is invoking functions that might exceed memory bounds, | ||
causing crashes or undefined behavior. Examples include incorrect usage of functions like | ||
``memcpy`` and ``memset``. `FORTIFY_SOURCE` is a mechanism designed to help developers quickly | ||
detect and mitigate boundary-related issues caused by improper use of library functions. | ||
|
||
Support | ||
------- | ||
|
||
`FORTIFY_SOURCE` is implemented as a software check by the compiler and is supported across all architectures. | ||
It works by adding additional validation checks to standard library function calls. | ||
|
||
Usage | ||
----- | ||
|
||
To enable `FORTIFY_SOURCE`, configure the kernel with the following option: | ||
|
||
``CONFIG_FORTIFY_SOURCE=level`` | ||
|
||
Where `level` can be set as: | ||
|
||
1. **Compile-time Checks**: | ||
Detects issues during compilation by analyzing source code. | ||
|
||
2. **Stack Variable Checks**: | ||
Extends level 1 by checking stack variables at runtime. | ||
|
||
3. **Heap Memory Checks**: | ||
Builds on level 2 by adding checks for memory allocated with ``malloc``. | ||
(Requires GCC version 12 or later.) | ||
|
||
FORTIFY_SOURCE Overview | ||
======================= | ||
|
||
`FORTIFY_SOURCE` detects potential security vulnerabilities by statically analyzing source code at compile time. | ||
It replaces standard library function calls with safer versions that include additional boundary checks. | ||
These safer versions validate the operation's boundaries and the input's validity before performing certain operations. | ||
|
||
GCC Built-in Functions | ||
----------------------- | ||
|
||
The GCC compiler internally implements two key functions for `FORTIFY_SOURCE`: | ||
|
||
- ``__builtin_object_size``: Determines the size of a statically allocated object. | ||
- ``__builtin_dynamic_object_size``: Determines the size of dynamically allocated objects (e.g., via ``malloc``). | ||
|
||
Starting with GCC 12, these functions support retrieving the size of variables allocated with ``malloc``. | ||
|
||
By passing a variable or buffer as an argument to these functions, the compiler can compute the corresponding size. | ||
Using this size, it is possible to check for potential out-of-bounds behavior in runtime operations. | ||
|
||
Example: memcpy Implementation in NuttX | ||
---------------------------------------- | ||
|
||
The following example demonstrates how `FORTIFY_SOURCE` can be used to enhance security in a ``memcpy`` | ||
implementation in NuttX: | ||
|
||
.. code-block:: c | ||
|
||
fortify_function(memcpy) | ||
FAR void *memcpy(FAR void *dest, | ||
FAR const void *src, | ||
size_t n) | ||
{ | ||
fortify_assert(n <= fortify_size(dest, 0) && n <= fortify_size(src, 0)); | ||
return __real_memcpy(dest, src, n); | ||
} | ||
|
||
In this implementation, the ``fortify_assert`` macro ensures that the size of the source and destination buffers | ||
is sufficient to handle the requested memory operation. If the assertion fails, it indicates a potential buffer | ||
overflow, helping developers quickly identify and address such vulnerabilities. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use heading levels according to https://nuttx.apache.org/docs/latest/contributing/documentation.html#headings