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