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