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