1*8d741a5dSApple OSS Distributions# String handling in xnu 2*8d741a5dSApple OSS Distributions 3*8d741a5dSApple OSS Distributionsxnu implements most POSIX C string functions, including the inherited subset of 4*8d741a5dSApple OSS Distributionsstandard C string functions. Unfortunately, poor design choices have made many 5*8d741a5dSApple OSS Distributionsof these functions, including the more modern `strl` functions, confusing or 6*8d741a5dSApple OSS Distributionsunsafe. In addition, the advent of -fbounds-safety support in xnu is forcing 7*8d741a5dSApple OSS Distributionssome string handling practices to be revisited. This document explains the 8*8d741a5dSApple OSS Distributionsfailings of POSIX C string functions, xnu's `strbuf` functions, and their 9*8d741a5dSApple OSS Distributionsintersection with the -fbounds-safety C extension. 10*8d741a5dSApple OSS Distributions 11*8d741a5dSApple OSS Distributions## The short-form guidance 12*8d741a5dSApple OSS Distributions 13*8d741a5dSApple OSS Distributions* Use `strbuf*` when you have the length for all the strings; 14*8d741a5dSApple OSS Distributions* use `strl*` when you have the length of _one_ string, and the other is 15*8d741a5dSApple OSS Distributions guaranteed to be NUL-terminated; 16*8d741a5dSApple OSS Distributions* use `str*` when you don't have the length for any of the strings, and they 17*8d741a5dSApple OSS Distributions are all guaranteed to be NUL-terminated; 18*8d741a5dSApple OSS Distributions* stop using `strn*` functions. 19*8d741a5dSApple OSS Distributions 20*8d741a5dSApple OSS Distributions## Replacing `strncmp` 21*8d741a5dSApple OSS Distributions 22*8d741a5dSApple OSS Distributions`strncmp` is always wrong with -fbounds-safety, and it's unavailable as a 23*8d741a5dSApple OSS Distributionsresult. Given `strcmp(first, secnd, n)`, you need to know the types of `first` 24*8d741a5dSApple OSS Distributionsand `secnd` to pick a replacement. Choose according to this table: 25*8d741a5dSApple OSS Distributions 26*8d741a5dSApple OSS Distributions| strncmp(first, secnd, n) | __null_terminated first | __indexable first | 27*8d741a5dSApple OSS Distributions| ------------------------ | ------------------------- | ------------------------------- | 28*8d741a5dSApple OSS Distributions| __null_terminated secnd | n/a | strlcmp(first, secnd, n1) | 29*8d741a5dSApple OSS Distributions| __indexable secnd | strlcmp(secnd, first, n2) | strbufcmp(first, n1, secnd, n2) | 30*8d741a5dSApple OSS Distributions 31*8d741a5dSApple OSS DistributionsUsing `strncmp` with two NUL-terminated strings is uncommon and it has no 32*8d741a5dSApple OSS Distributionsdirect replacement. The first person who needs to use -fbounds-safety in a file 33*8d741a5dSApple OSS Distributionsthat does this might need to write the string function. 34*8d741a5dSApple OSS Distributions 35*8d741a5dSApple OSS DistributionsIf you try to use `strlcmp` and you get a diagnostic like this: 36*8d741a5dSApple OSS Distributions 37*8d741a5dSApple OSS Distributions> passing 'const char *__indexable' to parameter of incompatible type 38*8d741a5dSApple OSS Distributions> 'const char *__null_terminated' is an unsafe operation ... 39*8d741a5dSApple OSS Distributions 40*8d741a5dSApple OSS Distributionsthen you might need to swap the two string arguments. `strlcmp` is sensitive to 41*8d741a5dSApple OSS Distributionsthe argument order: just like for `strlcpy`, the indexable string goes first. 42*8d741a5dSApple OSS Distributions 43*8d741a5dSApple OSS Distributions# The problems with string functions 44*8d741a5dSApple OSS Distributions 45*8d741a5dSApple OSS DistributionsPOSIX/BSD string handling functions come in many variants: 46*8d741a5dSApple OSS Distributions 47*8d741a5dSApple OSS Distributions* `str` functions (strlen, strcat, etc), unsafe for writing; 48*8d741a5dSApple OSS Distributions* `strn` functions (strnlen, strncat, etc), unsafe for writing; 49*8d741a5dSApple OSS Distributions* `strl` functions (strlcpy, strlcat, etc), safe but easily misunderstood. 50*8d741a5dSApple OSS Distributions 51*8d741a5dSApple OSS Distributions`str` functions for writing (`strcpy`, `strcat`, etc) are **all** unsafe 52*8d741a5dSApple OSS Distributionsbecause they don't care about the bounds of the output buffer. Most or all of 53*8d741a5dSApple OSS Distributionsthese functions have been deprecated or outright removed from xnu. You should 54*8d741a5dSApple OSS Distributionsnever use `str` functions to write to strings. Functions that simply read 55*8d741a5dSApple OSS Distributionsstrings (`strlen`, `strcmp`, `strchr`, etc) are generally found to be safe 56*8d741a5dSApple OSS Distributionsbecause there is no confusion that their input must be NUL-terminated and there 57*8d741a5dSApple OSS Distributionsis no danger of writing out of bounds (out of not writing at all). 58*8d741a5dSApple OSS Distributions 59*8d741a5dSApple OSS Distributions`strn` functions for writing (`strncpy`, `strncat`, etc) are **all** unsafe. 60*8d741a5dSApple OSS Distributions`strncpy` doesn't NUL-terminate the output buffer, and `strncat` doesn't accept 61*8d741a5dSApple OSS Distributionsa length for the output buffer. **All** new string buffers should include space 62*8d741a5dSApple OSS Distributionsfor a NUL terminator. `strn` functions for reading (`strncmp`, `strnlen`) are 63*8d741a5dSApple OSS Distributions_generally_ safe, but `strncmp` can cause confusion over which string is bound 64*8d741a5dSApple OSS Distributionsby the given size. In extreme cases, this can create information disclosure 65*8d741a5dSApple OSS Distributionsbugs or stability issues. 66*8d741a5dSApple OSS Distributions 67*8d741a5dSApple OSS Distributions`strl` functions, from OpenBSD, only come in writing variants, and they always 68*8d741a5dSApple OSS DistributionsNUL-terminate their output. This makes the writing part safe. (xnu adds `strl` 69*8d741a5dSApple OSS Distributionscomparison functions, which do no writing and are also safe.) However, these 70*8d741a5dSApple OSS Distributionsfunctions assume the output pointer is a buffer and the input is a NUL- 71*8d741a5dSApple OSS Distributionsterminated string. Because of coexistence with `strn` functions that make no 72*8d741a5dSApple OSS Distributionssuch assumption, this mental model isn't entirely adopted by many users. For 73*8d741a5dSApple OSS Distributionsinstance, the following code is buggy: 74*8d741a5dSApple OSS Distributions 75*8d741a5dSApple OSS Distributions```c 76*8d741a5dSApple OSS Distributionschar output[4]; 77*8d741a5dSApple OSS Distributionschar input[8] = "abcdefgh"; /* not NUL-terminated */ 78*8d741a5dSApple OSS Distributionsstrlcpy(output, input, sizeof(output)); 79*8d741a5dSApple OSS Distributions``` 80*8d741a5dSApple OSS Distributions 81*8d741a5dSApple OSS Distributions`strlcpy` returns the length of the input string; in xnu's implementation, 82*8d741a5dSApple OSS Distributionsliterally by calling `strlen(input)`. Even though only 3 characters are written 83*8d741a5dSApple OSS Distributionsto `output` (plus a NUL), `input` is read until reaching a NUL character. This 84*8d741a5dSApple OSS Distributionsis always a problem from the perspective of memory disclosures, and in some 85*8d741a5dSApple OSS Distributionscases, it can also lead to stability issues. 86*8d741a5dSApple OSS Distributions 87*8d741a5dSApple OSS Distributions# Changes with -fbounds-safety 88*8d741a5dSApple OSS Distributions 89*8d741a5dSApple OSS DistributionsWhen enabling -fbounds-safety, character buffers and NUL-terminated strings are 90*8d741a5dSApple OSS Distributionstwo distinct types, and they do not implicitly convert to each other. This 91*8d741a5dSApple OSS Distributionsprevents confusing the two in the way that is problematic with `strlcpy`, for 92*8d741a5dSApple OSS Distributionsinstance. However, it creates new problems: 93*8d741a5dSApple OSS Distributions 94*8d741a5dSApple OSS Distributions* What is the correct way to transform a character buffer into a NUL-terminated 95*8d741a5dSApple OSS Distributions string? 96*8d741a5dSApple OSS Distributions* When -fbounds-safety flags that the use of a string function was improper, 97*8d741a5dSApple OSS Distributions what is the solution? 98*8d741a5dSApple OSS Distributions 99*8d741a5dSApple OSS DistributionsThe most common use of character buffers is to build a string, and then this 100*8d741a5dSApple OSS Distributionsstring is passed without bounds as a NUL-terminated string to downstream users. 101*8d741a5dSApple OSS Distributions-fbounds-safety and XNU enshrine this practice with the following additions: 102*8d741a5dSApple OSS Distributions 103*8d741a5dSApple OSS Distributions* `tsnprintf`: like `snprintf`, but it returns a NUL-terminated string; 104*8d741a5dSApple OSS Distributions* `strbuf` functions, explicitly accepting character buffers and a distinct 105*8d741a5dSApple OSS Distributions count for each: 106*8d741a5dSApple OSS Distributions * `strbuflen(buffer, length)`: like `strnlen`; 107*8d741a5dSApple OSS Distributions * `strbufcmp(a, alen, b, len)`: like `strcmp`; 108*8d741a5dSApple OSS Distributions * `strbufcasecmp(a, alen, b, blen)`: like `strcasecmp`; 109*8d741a5dSApple OSS Distributions * `strbufcpy(a, alen, b, blen)`: like `strlcpy` but returns `a` as a NUL- 110*8d741a5dSApple OSS Distributions terminated string; 111*8d741a5dSApple OSS Distributions * `strbufcat(a, alen, b, blen)`: like `strlcat` but returns `a` as a NUL- 112*8d741a5dSApple OSS Distributions terminated string; 113*8d741a5dSApple OSS Distributions* `strl` (new) functions, accepting _one_ character buffer of a known size and 114*8d741a5dSApple OSS Distributions _one_ NUL-terminated string: 115*8d741a5dSApple OSS Distributions * `strlcmp(a, b, alen)`: like `strcmp`; 116*8d741a5dSApple OSS Distributions * `strlcasecmp(a, b, alen)`: like `strcasecmp`. 117*8d741a5dSApple OSS Distributions 118*8d741a5dSApple OSS Distributions`strbuf` functions additionally all have overloads accepting character arrays 119*8d741a5dSApple OSS Distributionsin lieu of a pointer+length pair: `strbuflen(array)`, `strbufcmp(a, b)`, 120*8d741a5dSApple OSS Distributions`strbufcasecmp(a, b)`, `strbufcpy(a, b)`, `strbufcat(a, b)`. 121*8d741a5dSApple OSS Distributions 122*8d741a5dSApple OSS DistributionsIf the destination array of `strbufcpy` or `strbufcat` has a size of 0, they 123*8d741a5dSApple OSS Distributionsreturn NULL without doing anything else. Otherwise, the destination is always 124*8d741a5dSApple OSS DistributionsNUL-terminated and returned as a NUL-terminated string pointer. 125*8d741a5dSApple OSS Distributions 126*8d741a5dSApple OSS DistributionsWhile you are modifying a string, you should reference its data as some flavor 127*8d741a5dSApple OSS Distributionsof indexable pointer, and only once you're done should you convert it to a 128*8d741a5dSApple OSS DistributionsNUL-terminated string. NUL-terminated character pointers are generally not 129*8d741a5dSApple OSS Distributionssuitable for modifications as bounds are determined by contents. Overwriting 130*8d741a5dSApple OSS Distributionsany NUL character found through a `__null_terminated` pointer access will result 131*8d741a5dSApple OSS Distributionsin a trap. For instance: 132*8d741a5dSApple OSS Distributions 133*8d741a5dSApple OSS Distributions```c 134*8d741a5dSApple OSS Distributionsvoid my_string_consuming_func(const char *); 135*8d741a5dSApple OSS Distributions 136*8d741a5dSApple OSS Distributions// lots of __unsafe! 137*8d741a5dSApple OSS Distributionschar *__null_terminated my_string = __unsafe_forge_null_terminated( 138*8d741a5dSApple OSS Distributions kalloc_data(my_string_size, Z_WAITOK)); 139*8d741a5dSApple OSS Distributionsmemcpy( 140*8d741a5dSApple OSS Distributions __unsafe_forge_bidi_indexable(void *, my_string, my_string_size), 141*8d741a5dSApple OSS Distributions my_data, 142*8d741a5dSApple OSS Distributions my_string_size); 143*8d741a5dSApple OSS Distributionsmy_string_consuming_func(my_string); 144*8d741a5dSApple OSS Distributions``` 145*8d741a5dSApple OSS Distributions 146*8d741a5dSApple OSS DistributionsThis code converts the string pointer to a NUL-terminated string too early, 147*8d741a5dSApple OSS Distributionswhile it's still being modified. Keeping my_string a `__null_terminated` pointer 148*8d741a5dSApple OSS Distributionswhile it's being modified leads to more forging, which has more chances of 149*8d741a5dSApple OSS Distributionsintroducing errors, and is less ergonomic. Consider this instead: 150*8d741a5dSApple OSS Distributions 151*8d741a5dSApple OSS Distributions```c 152*8d741a5dSApple OSS Distributionsvoid my_string_consuming_func(const char *); 153*8d741a5dSApple OSS Distributions 154*8d741a5dSApple OSS Distributionschar *my_buffer = kalloc_data(my_string_size, Z_WAITOK); 155*8d741a5dSApple OSS Distributionsconst char *__null_terminated finished_string = 156*8d741a5dSApple OSS Distributions strbufcpy(my_buffer, my_string_size, my_data, my_string_size); 157*8d741a5dSApple OSS Distributionsmy_string_consuming_func(finished); 158*8d741a5dSApple OSS Distributions``` 159*8d741a5dSApple OSS Distributions 160*8d741a5dSApple OSS DistributionsThis example has two views of the same data: `my_buffer` (through which the 161*8d741a5dSApple OSS Distributionsstring is being modified) and `finished_string` (which is `const` and 162*8d741a5dSApple OSS DistributionsNUL-terminated). Using `my_buffer` as an indexable pointer allows you to modify 163*8d741a5dSApple OSS Distributionsit ergonomically, and importantly, without forging. You turn it into a 164*8d741a5dSApple OSS DistributionsNUL-terminated string at the same time you turn it into a `const` reference, 165*8d741a5dSApple OSS Distributionssignalling that you're done making changes. 166*8d741a5dSApple OSS Distributions 167*8d741a5dSApple OSS DistributionsWith -fbounds-safety enabled, you should structure the final operation modifying 168*8d741a5dSApple OSS Distributionsa character array such that you get a NUL-terminated view of it. For instance, 169*8d741a5dSApple OSS Distributionsthis plain C code: 170*8d741a5dSApple OSS Distributions 171*8d741a5dSApple OSS Distributions```c 172*8d741a5dSApple OSS Distributionschar thread_name[MAXTHREADNAMESIZE]; 173*8d741a5dSApple OSS Distributions(void) snprintf(thread_name, sizeof(thread_name), 174*8d741a5dSApple OSS Distributions "dlil_input_%s", ifp->if_xname); 175*8d741a5dSApple OSS Distributionsthread_set_thread_name(inp->dlth_thread, thread_name); 176*8d741a5dSApple OSS Distributions``` 177*8d741a5dSApple OSS Distributions 178*8d741a5dSApple OSS Distributionsbecomes: 179*8d741a5dSApple OSS Distributions 180*8d741a5dSApple OSS Distributions```c 181*8d741a5dSApple OSS Distributionschar thread_name_buf[MAXTHREADNAMESIZE]; 182*8d741a5dSApple OSS Distributionsconst char *__null_terminated thread_name; 183*8d741a5dSApple OSS Distributionsthread_name = tsnprintf(thread_name_buf, sizeof(thread_name_buf), 184*8d741a5dSApple OSS Distributions "dlil_input_%s", ifp->if_xname); 185*8d741a5dSApple OSS Distributionsthread_set_thread_name(inp->dlth_thread, thread_name); 186*8d741a5dSApple OSS Distributions``` 187*8d741a5dSApple OSS Distributions 188*8d741a5dSApple OSS DistributionsAlthough `tsnprintf` and `strbuf` functions return a `__null_terminated` 189*8d741a5dSApple OSS Distributionspointer to you for convenience, not all use cases are resolved by calling 190*8d741a5dSApple OSS Distributions`tsnprintf` or `strbufcpy` once. As a quick reference, with -fbounds-safety 191*8d741a5dSApple OSS Distributionsenabled, you can use `__unsafe_null_terminated_from_indexable(p_start, p_nul)` 192*8d741a5dSApple OSS Distributionsto convert a character array to a `__null_terminated` string if you need to 193*8d741a5dSApple OSS Distributionsperform more manipulations. (`p_start` is a pointer to the first character, and 194*8d741a5dSApple OSS Distributions`p_nul` is a pointer to the NUL character in that string.) For instance, if you 195*8d741a5dSApple OSS Distributionsbuild a string with successive calls to `scnprintf`, you would use 196*8d741a5dSApple OSS Distributions`__unsafe_null_terminated_from_indexable` at the end of the sequence to get your 197*8d741a5dSApple OSS DistributionsNUL-terminated string pointer. 198*8d741a5dSApple OSS Distributions 199*8d741a5dSApple OSS DistributionsOccasionally, you need to turn a NUL-terminated string back into "char buffer" 200*8d741a5dSApple OSS Distributions(usually to interoperate with copy APIs that need a pointer and a byte count). 201*8d741a5dSApple OSS DistributionsWhen possible, it's advised to use APIs that copy NUL-terminated pointers (like 202*8d741a5dSApple OSS Distributions`strlcpy`). Otherwise, convert the NUL-terminated string to an indexable buffer 203*8d741a5dSApple OSS Distributionsusing `__null_terminated_to_indexable` (if you don't need the NUL terminator to 204*8d741a5dSApple OSS Distributionsbe in bounds of the result pointer) or `__unsafe_null_terminated_to_indexable` 205*8d741a5dSApple OSS Distributions(if you need it). Also keep in mind that in code which pervasively deals with 206*8d741a5dSApple OSS Distributionsbuffers that have lengths and some of them happen to also be NUL-terminated 207*8d741a5dSApple OSS Distributionsstrings, it could be simply more convenient to keep string buffers in some 208*8d741a5dSApple OSS Distributionsflavor of indexable pointers instead of having conversions from and to 209*8d741a5dSApple OSS DistributionsNUL-terminated strings. 210*8d741a5dSApple OSS Distributions 211*8d741a5dSApple OSS Distributions# I have a choice between `strn*`, `strl*`, `strbuf*`. Which one do I use? 212*8d741a5dSApple OSS Distributions 213*8d741a5dSApple OSS DistributionsYou might come across cases where the same function in different families would 214*8d741a5dSApple OSS Distributionsseem like they all do the trick. For instance: 215*8d741a5dSApple OSS Distributions 216*8d741a5dSApple OSS Distributions```c 217*8d741a5dSApple OSS Distributionsstruct foo { 218*8d741a5dSApple OSS Distributions char buf1[10]; 219*8d741a5dSApple OSS Distributions char buf2[16]; 220*8d741a5dSApple OSS Distributions}; 221*8d741a5dSApple OSS Distributions 222*8d741a5dSApple OSS Distributionsvoid bar(struct foo *f) { 223*8d741a5dSApple OSS Distributions /* how do I test whether buf1 and buf2 contain the same string? */ 224*8d741a5dSApple OSS Distributions if (strcmp(f->buf1, f->buf2) == 0) { /* ... */ } 225*8d741a5dSApple OSS Distributions if (strncmp(f->buf1, f->buf2, sizeof(f->buf1)) == 0) { /* ... */ } 226*8d741a5dSApple OSS Distributions if (strlcmp(f->buf1, f->buf2, sizeof(f->buf1)) == 0) { /* ... */ } 227*8d741a5dSApple OSS Distributions if (strbufcmp(f->buf1, f->buf2) == 0) { /* ... */ } 228*8d741a5dSApple OSS Distributions} 229*8d741a5dSApple OSS Distributions``` 230*8d741a5dSApple OSS Distributions 231*8d741a5dSApple OSS DistributionsWithout -fbounds-safety, these all work the same, but when you enable it, 232*8d741a5dSApple OSS Distributions`strbufcmp` could be the only one that builds. If you do not have the privilege 233*8d741a5dSApple OSS Distributionsof -fbounds-safety to guide you to the best choice, as a rule of thumb, you 234*8d741a5dSApple OSS Distributionsshould prefer APIs in the following order: 235*8d741a5dSApple OSS Distributions 236*8d741a5dSApple OSS Distributions1. `strbuf*` APIs; 237*8d741a5dSApple OSS Distributions2. `strl*` APIs; 238*8d741a5dSApple OSS Distributions3. `str*` APIs. 239*8d741a5dSApple OSS Distributions 240*8d741a5dSApple OSS DistributionsThat is, to implement `bar`, you have a choice of `strcmp`, `strncmp` and 241*8d741a5dSApple OSS Distributions`strbufcmp`, and you should prefer `strbufcmp`. 242*8d741a5dSApple OSS Distributions 243*8d741a5dSApple OSS Distributions`strn` functions are **never** recommended. You should use `strbuflen` over 244*8d741a5dSApple OSS Distributions`strnlen` (they do the same thing, but having a separate `strbuflen` function 245*8d741a5dSApple OSS Distributionsmakes the guidance to avoid `strn` functions easier), and you should use 246*8d741a5dSApple OSS Distributions`strbufcmp`, `strlcmp` or even `strcmp` over `strncmp` (depending on whether 247*8d741a5dSApple OSS Distributionsyou know the length of each string, of just one, or of neither). 248