nanovms / nanos

A kernel designed to run one and only one application in a virtualized environment
https://nanos.org
Apache License 2.0
2.58k stars 133 forks source link

Strings: remove unbounded memory access #1995

Closed francescolavra closed 7 months ago

francescolavra commented 7 months ago

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 https://github.com/nanovms/lwip/pull/11 and https://github.com/nanovms/mbedtls/pull/3