1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun.. _deprecated: 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun===================================================================== 6*4882a593SmuzhiyunDeprecated Interfaces, Language Features, Attributes, and Conventions 7*4882a593Smuzhiyun===================================================================== 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunIn a perfect world, it would be possible to convert all instances of 10*4882a593Smuzhiyunsome deprecated API into the new API and entirely remove the old API in 11*4882a593Smuzhiyuna single development cycle. However, due to the size of the kernel, the 12*4882a593Smuzhiyunmaintainership hierarchy, and timing, it's not always feasible to do these 13*4882a593Smuzhiyunkinds of conversions at once. This means that new instances may sneak into 14*4882a593Smuzhiyunthe kernel while old ones are being removed, only making the amount of 15*4882a593Smuzhiyunwork to remove the API grow. In order to educate developers about what 16*4882a593Smuzhiyunhas been deprecated and why, this list has been created as a place to 17*4882a593Smuzhiyunpoint when uses of deprecated things are proposed for inclusion in the 18*4882a593Smuzhiyunkernel. 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun__deprecated 21*4882a593Smuzhiyun------------ 22*4882a593SmuzhiyunWhile this attribute does visually mark an interface as deprecated, 23*4882a593Smuzhiyunit `does not produce warnings during builds any more 24*4882a593Smuzhiyun<https://git.kernel.org/linus/771c035372a036f83353eef46dbb829780330234>`_ 25*4882a593Smuzhiyunbecause one of the standing goals of the kernel is to build without 26*4882a593Smuzhiyunwarnings and no one was actually doing anything to remove these deprecated 27*4882a593Smuzhiyuninterfaces. While using `__deprecated` is nice to note an old API in 28*4882a593Smuzhiyuna header file, it isn't the full solution. Such interfaces must either 29*4882a593Smuzhiyunbe fully removed from the kernel, or added to this file to discourage 30*4882a593Smuzhiyunothers from using them in the future. 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunBUG() and BUG_ON() 33*4882a593Smuzhiyun------------------ 34*4882a593SmuzhiyunUse WARN() and WARN_ON() instead, and handle the "impossible" 35*4882a593Smuzhiyunerror condition as gracefully as possible. While the BUG()-family 36*4882a593Smuzhiyunof APIs were originally designed to act as an "impossible situation" 37*4882a593Smuzhiyunassert and to kill a kernel thread "safely", they turn out to just be 38*4882a593Smuzhiyuntoo risky. (e.g. "In what order do locks need to be released? Have 39*4882a593Smuzhiyunvarious states been restored?") Very commonly, using BUG() will 40*4882a593Smuzhiyundestabilize a system or entirely break it, which makes it impossible 41*4882a593Smuzhiyunto debug or even get viable crash reports. Linus has `very strong 42*4882a593Smuzhiyun<https://lore.kernel.org/lkml/CA+55aFy6jNLsywVYdGp83AMrXBo_P-pkjkphPGrO=82SPKCpLQ@mail.gmail.com/>`_ 43*4882a593Smuzhiyunfeelings `about this 44*4882a593Smuzhiyun<https://lore.kernel.org/lkml/CAHk-=whDHsbK3HTOpTF=ue_o04onRwTEaK_ZoJp_fjbqq4+=Jw@mail.gmail.com/>`_. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunNote that the WARN()-family should only be used for "expected to 47*4882a593Smuzhiyunbe unreachable" situations. If you want to warn about "reachable 48*4882a593Smuzhiyunbut undesirable" situations, please use the pr_warn()-family of 49*4882a593Smuzhiyunfunctions. System owners may have set the *panic_on_warn* sysctl, 50*4882a593Smuzhiyunto make sure their systems do not continue running in the face of 51*4882a593Smuzhiyun"unreachable" conditions. (For example, see commits like `this one 52*4882a593Smuzhiyun<https://git.kernel.org/linus/d4689846881d160a4d12a514e991a740bcb5d65a>`_.) 53*4882a593Smuzhiyun 54*4882a593Smuzhiyunopen-coded arithmetic in allocator arguments 55*4882a593Smuzhiyun-------------------------------------------- 56*4882a593SmuzhiyunDynamic size calculations (especially multiplication) should not be 57*4882a593Smuzhiyunperformed in memory allocator (or similar) function arguments due to the 58*4882a593Smuzhiyunrisk of them overflowing. This could lead to values wrapping around and a 59*4882a593Smuzhiyunsmaller allocation being made than the caller was expecting. Using those 60*4882a593Smuzhiyunallocations could lead to linear overflows of heap memory and other 61*4882a593Smuzhiyunmisbehaviors. (One exception to this is literal values where the compiler 62*4882a593Smuzhiyuncan warn if they might overflow. Though using literals for arguments as 63*4882a593Smuzhiyunsuggested below is also harmless.) 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunFor example, do not use ``count * size`` as an argument, as in:: 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun foo = kmalloc(count * size, GFP_KERNEL); 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunInstead, the 2-factor form of the allocator should be used:: 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun foo = kmalloc_array(count, size, GFP_KERNEL); 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunIf no 2-factor form is available, the saturate-on-overflow helpers should 74*4882a593Smuzhiyunbe used:: 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun bar = vmalloc(array_size(count, size)); 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunAnother common case to avoid is calculating the size of a structure with 79*4882a593Smuzhiyuna trailing array of others structures, as in:: 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun header = kzalloc(sizeof(*header) + count * sizeof(*header->item), 82*4882a593Smuzhiyun GFP_KERNEL); 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunInstead, use the helper:: 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun header = kzalloc(struct_size(header, item, count), GFP_KERNEL); 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun.. note:: If you are using struct_size() on a structure containing a zero-length 89*4882a593Smuzhiyun or a one-element array as a trailing array member, please refactor such 90*4882a593Smuzhiyun array usage and switch to a `flexible array member 91*4882a593Smuzhiyun <#zero-length-and-one-element-arrays>`_ instead. 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunSee array_size(), array3_size(), and struct_size(), 94*4882a593Smuzhiyunfor more details as well as the related check_add_overflow() and 95*4882a593Smuzhiyuncheck_mul_overflow() family of functions. 96*4882a593Smuzhiyun 97*4882a593Smuzhiyunsimple_strtol(), simple_strtoll(), simple_strtoul(), simple_strtoull() 98*4882a593Smuzhiyun---------------------------------------------------------------------- 99*4882a593SmuzhiyunThe simple_strtol(), simple_strtoll(), 100*4882a593Smuzhiyunsimple_strtoul(), and simple_strtoull() functions 101*4882a593Smuzhiyunexplicitly ignore overflows, which may lead to unexpected results 102*4882a593Smuzhiyunin callers. The respective kstrtol(), kstrtoll(), 103*4882a593Smuzhiyunkstrtoul(), and kstrtoull() functions tend to be the 104*4882a593Smuzhiyuncorrect replacements, though note that those require the string to be 105*4882a593SmuzhiyunNUL or newline terminated. 106*4882a593Smuzhiyun 107*4882a593Smuzhiyunstrcpy() 108*4882a593Smuzhiyun-------- 109*4882a593Smuzhiyunstrcpy() performs no bounds checking on the destination buffer. This 110*4882a593Smuzhiyuncould result in linear overflows beyond the end of the buffer, leading to 111*4882a593Smuzhiyunall kinds of misbehaviors. While `CONFIG_FORTIFY_SOURCE=y` and various 112*4882a593Smuzhiyuncompiler flags help reduce the risk of using this function, there is 113*4882a593Smuzhiyunno good reason to add new uses of this function. The safe replacement 114*4882a593Smuzhiyunis strscpy(), though care must be given to any cases where the return 115*4882a593Smuzhiyunvalue of strcpy() was used, since strscpy() does not return a pointer to 116*4882a593Smuzhiyunthe destination, but rather a count of non-NUL bytes copied (or negative 117*4882a593Smuzhiyunerrno when it truncates). 118*4882a593Smuzhiyun 119*4882a593Smuzhiyunstrncpy() on NUL-terminated strings 120*4882a593Smuzhiyun----------------------------------- 121*4882a593SmuzhiyunUse of strncpy() does not guarantee that the destination buffer will 122*4882a593Smuzhiyunbe NUL terminated. This can lead to various linear read overflows and 123*4882a593Smuzhiyunother misbehavior due to the missing termination. It also NUL-pads 124*4882a593Smuzhiyunthe destination buffer if the source contents are shorter than the 125*4882a593Smuzhiyundestination buffer size, which may be a needless performance penalty 126*4882a593Smuzhiyunfor callers using only NUL-terminated strings. The safe replacement is 127*4882a593Smuzhiyunstrscpy(), though care must be given to any cases where the return value 128*4882a593Smuzhiyunof strncpy() was used, since strscpy() does not return a pointer to the 129*4882a593Smuzhiyundestination, but rather a count of non-NUL bytes copied (or negative 130*4882a593Smuzhiyunerrno when it truncates). Any cases still needing NUL-padding should 131*4882a593Smuzhiyuninstead use strscpy_pad(). 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunIf a caller is using non-NUL-terminated strings, strncpy() can 134*4882a593Smuzhiyunstill be used, but destinations should be marked with the `__nonstring 135*4882a593Smuzhiyun<https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html>`_ 136*4882a593Smuzhiyunattribute to avoid future compiler warnings. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyunstrlcpy() 139*4882a593Smuzhiyun--------- 140*4882a593Smuzhiyunstrlcpy() reads the entire source buffer first (since the return value 141*4882a593Smuzhiyunis meant to match that of strlen()). This read may exceed the destination 142*4882a593Smuzhiyunsize limit. This is both inefficient and can lead to linear read overflows 143*4882a593Smuzhiyunif a source string is not NUL-terminated. The safe replacement is strscpy(), 144*4882a593Smuzhiyunthough care must be given to any cases where the return value of strlcpy() 145*4882a593Smuzhiyunis used, since strscpy() will return negative errno values when it truncates. 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun%p format specifier 148*4882a593Smuzhiyun------------------- 149*4882a593SmuzhiyunTraditionally, using "%p" in format strings would lead to regular address 150*4882a593Smuzhiyunexposure flaws in dmesg, proc, sysfs, etc. Instead of leaving these to 151*4882a593Smuzhiyunbe exploitable, all "%p" uses in the kernel are being printed as a hashed 152*4882a593Smuzhiyunvalue, rendering them unusable for addressing. New uses of "%p" should not 153*4882a593Smuzhiyunbe added to the kernel. For text addresses, using "%pS" is likely better, 154*4882a593Smuzhiyunas it produces the more useful symbol name instead. For nearly everything 155*4882a593Smuzhiyunelse, just do not add "%p" at all. 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunParaphrasing Linus's current `guidance <https://lore.kernel.org/lkml/CA+55aFwQEd_d40g4mUCSsVRZzrFPUJt74vc6PPpb675hYNXcKw@mail.gmail.com/>`_: 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun- If the hashed "%p" value is pointless, ask yourself whether the pointer 160*4882a593Smuzhiyun itself is important. Maybe it should be removed entirely? 161*4882a593Smuzhiyun- If you really think the true pointer value is important, why is some 162*4882a593Smuzhiyun system state or user privilege level considered "special"? If you think 163*4882a593Smuzhiyun you can justify it (in comments and commit log) well enough to stand 164*4882a593Smuzhiyun up to Linus's scrutiny, maybe you can use "%px", along with making sure 165*4882a593Smuzhiyun you have sensible permissions. 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunAnd finally, know that a toggle for "%p" hashing will `not be accepted <https://lore.kernel.org/lkml/CA+55aFwieC1-nAs+NFq9RTwaR8ef9hWa4MjNBWL41F-8wM49eA@mail.gmail.com/>`_. 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunVariable Length Arrays (VLAs) 170*4882a593Smuzhiyun----------------------------- 171*4882a593SmuzhiyunUsing stack VLAs produces much worse machine code than statically 172*4882a593Smuzhiyunsized stack arrays. While these non-trivial `performance issues 173*4882a593Smuzhiyun<https://git.kernel.org/linus/02361bc77888>`_ are reason enough to 174*4882a593Smuzhiyuneliminate VLAs, they are also a security risk. Dynamic growth of a stack 175*4882a593Smuzhiyunarray may exceed the remaining memory in the stack segment. This could 176*4882a593Smuzhiyunlead to a crash, possible overwriting sensitive contents at the end of the 177*4882a593Smuzhiyunstack (when built without `CONFIG_THREAD_INFO_IN_TASK=y`), or overwriting 178*4882a593Smuzhiyunmemory adjacent to the stack (when built without `CONFIG_VMAP_STACK=y`) 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunImplicit switch case fall-through 181*4882a593Smuzhiyun--------------------------------- 182*4882a593SmuzhiyunThe C language allows switch cases to fall through to the next case 183*4882a593Smuzhiyunwhen a "break" statement is missing at the end of a case. This, however, 184*4882a593Smuzhiyunintroduces ambiguity in the code, as it's not always clear if the missing 185*4882a593Smuzhiyunbreak is intentional or a bug. For example, it's not obvious just from 186*4882a593Smuzhiyunlooking at the code if `STATE_ONE` is intentionally designed to fall 187*4882a593Smuzhiyunthrough into `STATE_TWO`:: 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun switch (value) { 190*4882a593Smuzhiyun case STATE_ONE: 191*4882a593Smuzhiyun do_something(); 192*4882a593Smuzhiyun case STATE_TWO: 193*4882a593Smuzhiyun do_other(); 194*4882a593Smuzhiyun break; 195*4882a593Smuzhiyun default: 196*4882a593Smuzhiyun WARN("unknown state"); 197*4882a593Smuzhiyun } 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunAs there have been a long list of flaws `due to missing "break" statements 200*4882a593Smuzhiyun<https://cwe.mitre.org/data/definitions/484.html>`_, we no longer allow 201*4882a593Smuzhiyunimplicit fall-through. In order to identify intentional fall-through 202*4882a593Smuzhiyuncases, we have adopted a pseudo-keyword macro "fallthrough" which 203*4882a593Smuzhiyunexpands to gcc's extension `__attribute__((__fallthrough__)) 204*4882a593Smuzhiyun<https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html>`_. 205*4882a593Smuzhiyun(When the C17/C18 `[[fallthrough]]` syntax is more commonly supported by 206*4882a593SmuzhiyunC compilers, static analyzers, and IDEs, we can switch to using that syntax 207*4882a593Smuzhiyunfor the macro pseudo-keyword.) 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunAll switch/case blocks must end in one of: 210*4882a593Smuzhiyun 211*4882a593Smuzhiyun* break; 212*4882a593Smuzhiyun* fallthrough; 213*4882a593Smuzhiyun* continue; 214*4882a593Smuzhiyun* goto <label>; 215*4882a593Smuzhiyun* return [expression]; 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunZero-length and one-element arrays 218*4882a593Smuzhiyun---------------------------------- 219*4882a593SmuzhiyunThere is a regular need in the kernel to provide a way to declare having 220*4882a593Smuzhiyuna dynamically sized set of trailing elements in a structure. Kernel code 221*4882a593Smuzhiyunshould always use `"flexible array members" <https://en.wikipedia.org/wiki/Flexible_array_member>`_ 222*4882a593Smuzhiyunfor these cases. The older style of one-element or zero-length arrays should 223*4882a593Smuzhiyunno longer be used. 224*4882a593Smuzhiyun 225*4882a593SmuzhiyunIn older C code, dynamically sized trailing elements were done by specifying 226*4882a593Smuzhiyuna one-element array at the end of a structure:: 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun struct something { 229*4882a593Smuzhiyun size_t count; 230*4882a593Smuzhiyun struct foo items[1]; 231*4882a593Smuzhiyun }; 232*4882a593Smuzhiyun 233*4882a593SmuzhiyunThis led to fragile size calculations via sizeof() (which would need to 234*4882a593Smuzhiyunremove the size of the single trailing element to get a correct size of 235*4882a593Smuzhiyunthe "header"). A `GNU C extension <https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html>`_ 236*4882a593Smuzhiyunwas introduced to allow for zero-length arrays, to avoid these kinds of 237*4882a593Smuzhiyunsize problems:: 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun struct something { 240*4882a593Smuzhiyun size_t count; 241*4882a593Smuzhiyun struct foo items[0]; 242*4882a593Smuzhiyun }; 243*4882a593Smuzhiyun 244*4882a593SmuzhiyunBut this led to other problems, and didn't solve some problems shared by 245*4882a593Smuzhiyunboth styles, like not being able to detect when such an array is accidentally 246*4882a593Smuzhiyunbeing used _not_ at the end of a structure (which could happen directly, or 247*4882a593Smuzhiyunwhen such a struct was in unions, structs of structs, etc). 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunC99 introduced "flexible array members", which lacks a numeric size for 250*4882a593Smuzhiyunthe array declaration entirely:: 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun struct something { 253*4882a593Smuzhiyun size_t count; 254*4882a593Smuzhiyun struct foo items[]; 255*4882a593Smuzhiyun }; 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunThis is the way the kernel expects dynamically sized trailing elements 258*4882a593Smuzhiyunto be declared. It allows the compiler to generate errors when the 259*4882a593Smuzhiyunflexible array does not occur last in the structure, which helps to prevent 260*4882a593Smuzhiyunsome kind of `undefined behavior 261*4882a593Smuzhiyun<https://git.kernel.org/linus/76497732932f15e7323dc805e8ea8dc11bb587cf>`_ 262*4882a593Smuzhiyunbugs from being inadvertently introduced to the codebase. It also allows 263*4882a593Smuzhiyunthe compiler to correctly analyze array sizes (via sizeof(), 264*4882a593Smuzhiyun`CONFIG_FORTIFY_SOURCE`, and `CONFIG_UBSAN_BOUNDS`). For instance, 265*4882a593Smuzhiyunthere is no mechanism that warns us that the following application of the 266*4882a593Smuzhiyunsizeof() operator to a zero-length array always results in zero:: 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun struct something { 269*4882a593Smuzhiyun size_t count; 270*4882a593Smuzhiyun struct foo items[0]; 271*4882a593Smuzhiyun }; 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun struct something *instance; 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL); 276*4882a593Smuzhiyun instance->count = count; 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun size = sizeof(instance->items) * instance->count; 279*4882a593Smuzhiyun memcpy(instance->items, source, size); 280*4882a593Smuzhiyun 281*4882a593SmuzhiyunAt the last line of code above, ``size`` turns out to be ``zero``, when one might 282*4882a593Smuzhiyunhave thought it represents the total size in bytes of the dynamic memory recently 283*4882a593Smuzhiyunallocated for the trailing array ``items``. Here are a couple examples of this 284*4882a593Smuzhiyunissue: `link 1 285*4882a593Smuzhiyun<https://git.kernel.org/linus/f2cd32a443da694ac4e28fbf4ac6f9d5cc63a539>`_, 286*4882a593Smuzhiyun`link 2 287*4882a593Smuzhiyun<https://git.kernel.org/linus/ab91c2a89f86be2898cee208d492816ec238b2cf>`_. 288*4882a593SmuzhiyunInstead, `flexible array members have incomplete type, and so the sizeof() 289*4882a593Smuzhiyunoperator may not be applied <https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html>`_, 290*4882a593Smuzhiyunso any misuse of such operators will be immediately noticed at build time. 291*4882a593Smuzhiyun 292*4882a593SmuzhiyunWith respect to one-element arrays, one has to be acutely aware that `such arrays 293*4882a593Smuzhiyunoccupy at least as much space as a single object of the type 294*4882a593Smuzhiyun<https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html>`_, 295*4882a593Smuzhiyunhence they contribute to the size of the enclosing structure. This is prone 296*4882a593Smuzhiyunto error every time people want to calculate the total size of dynamic memory 297*4882a593Smuzhiyunto allocate for a structure containing an array of this kind as a member:: 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun struct something { 300*4882a593Smuzhiyun size_t count; 301*4882a593Smuzhiyun struct foo items[1]; 302*4882a593Smuzhiyun }; 303*4882a593Smuzhiyun 304*4882a593Smuzhiyun struct something *instance; 305*4882a593Smuzhiyun 306*4882a593Smuzhiyun instance = kmalloc(struct_size(instance, items, count - 1), GFP_KERNEL); 307*4882a593Smuzhiyun instance->count = count; 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun size = sizeof(instance->items) * instance->count; 310*4882a593Smuzhiyun memcpy(instance->items, source, size); 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunIn the example above, we had to remember to calculate ``count - 1`` when using 313*4882a593Smuzhiyunthe struct_size() helper, otherwise we would have --unintentionally-- allocated 314*4882a593Smuzhiyunmemory for one too many ``items`` objects. The cleanest and least error-prone way 315*4882a593Smuzhiyunto implement this is through the use of a `flexible array member`, together with 316*4882a593Smuzhiyunstruct_size() and flex_array_size() helpers:: 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun struct something { 319*4882a593Smuzhiyun size_t count; 320*4882a593Smuzhiyun struct foo items[]; 321*4882a593Smuzhiyun }; 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun struct something *instance; 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL); 326*4882a593Smuzhiyun instance->count = count; 327*4882a593Smuzhiyun 328*4882a593Smuzhiyun memcpy(instance->items, source, flex_array_size(instance, items, instance->count)); 329