Skip to content
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

Strings: remove unbounded memory access #1995

Merged
merged 9 commits into from
Feb 6, 2024
Merged

Conversation

francescolavra
Copy link
Member

This changeset removes the use of string functions that access string memory without a limit (i.e. that rely on the presence of a string terminator to determine the end of a string). Instead, a new string type (sstring) is being defined; this type is a struct that includes a pointer to string memory and the length of the string; use of this type makes it unnecessary to look for the string terminator in order to determine the length of a string, and thus avoids unbounded access to string memory.
C string literals (which by definition are NULL-terminated strings) are still allowed in the kernel code, but they are converted at compile time into sstring types, e.g. by using the ss() macro.
Since it is still necessary for the kernel to be able to process NULL-terminated strings (for example, strings read by drivers from peripheral memory), the sstring_from_cstring() function is being defined: this function takes a memory pointer and a maximum length value, and returns an sstring built by parsing the memory (up to the maximum length) looking for the string terminator (if no terminator is found, the returned string length is the maximum length).
The "%s" format specifier in printf-style functions now takes an sstring argument instead of a char *; the name field has been removed from closure structs, and the function handling the "%F" format specifier has been changed to retrieve a closure name from the kernel symbol table.

Requires nanovms/lwip#11 and nanovms/mbedtls#3

This change amends format strings in various places in order to
remove unneeded "%s" format specifiers, e.g. when they can be
replaced by "%c", or when err_msg() can be called instead of
rprintf(). In addition, "%s" specifiers are being removed in a few
places where there is no corresponding string in the function
argument list.
This is being done in preparation for the removal of unbounded
string functions.
This macro was needed for using symbol functionalities from within
klib code, before dynamic relocation of kernel symbols was
implemented for klibs; now it's not needed anymore.
The "rprintf" and "intern" members of the syslog_cfg closure were
needed in order for the syslog klib to be able to call those kernel
functions, before dynamic relocation of kernel symbols was
implemented for klibs; now they are not needed anymore.
This change amends the argument list in all calls to timm() that
specify multiple tuple attributes, so that only the first attibute
is specified in the timm() call, while the additional attributes
are specified via calls to timm_append(). This is done in
preparation for the commit that will remove unbounded string
functions, which will change the timm() implementation to support
only one tuple attribute.
This change replaces a few timm() calls occurring on out of memory
conditions with references to the `timm_oom` global tuple. This
simplifies the logic in the Unix fault handler to check for out of
memory errors, and removes one use of the (soon to be removed)
buffer_compare_with_cstring_ci() function.
The name filed in the lwIP timer struct is only used in
lwip_debug() calls, thus it is unused in non-debug builds. This
change removes the name field from non-debug builds (so that unused
strings are removed from the kernel binary) and refactors the timer
code accordingly.
By using assert(), a stack trace (from which the problematic
refcount can be identified) will be printed when the condition
being checked is not verified. This change is being done in
preparation for removing unbounded kernel string functions, which
would require changing the argument list in the halt() call.
The existing check was not actually verifying that the offset and
size of the section header is consistent with the size of the ELF
file.
This changeset removes the use of string functions that access
string memory without a limit (i.e. that rely on the presence of a
string terminator to determine the end of a string). Instead, a new
string type (sstring) is being defined; this type is a struct that
includes a pointer to string memory and the length of the string;
use of this type makes it unnecessary to look for the string
terminator in order to determine the length of a string, and thus
avoids unbounded access to string memory.
C string literals (which by definition are NULL-terminated strings)
are still allowed in the kernel code, but they are converted at
compile time into sstring types, e.g. by using the ss() macro.
Since it is still necessary for the kernel to be able to process
NULL-terminated strings (for example, strings read by drivers from
peripheral memory), the sstring_from_cstring() function is being
defined: this function takes a memory pointer and a maximum length
value, and returns an sstring built by parsing the memory (up to
the maximum length) looking for the string terminator (if no
terminator is found, the returned string length is the maximum
length).
The "%s" format specifier in printf-style functions now takes an
sstring argument instead of a `char *`; the name field has been
removed from closure structs, and the function handling the "%F"
format specifier has been changed to retrieve a closure name from
the kernel symbol table.
@francescolavra francescolavra merged commit 364b517 into master Feb 6, 2024
5 checks passed
@francescolavra francescolavra deleted the feature/sstring branch February 6, 2024 11:51
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.

1 participant