kmemcheck.txt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. GETTING STARTED WITH KMEMCHECK
  2. ==============================
  3. Vegard Nossum <vegardno@ifi.uio.no>
  4. Contents
  5. ========
  6. 0. Introduction
  7. 1. Downloading
  8. 2. Configuring and compiling
  9. 3. How to use
  10. 3.1. Booting
  11. 3.2. Run-time enable/disable
  12. 3.3. Debugging
  13. 3.4. Annotating false positives
  14. 4. Reporting errors
  15. 5. Technical description
  16. 0. Introduction
  17. ===============
  18. kmemcheck is a debugging feature for the Linux Kernel. More specifically, it
  19. is a dynamic checker that detects and warns about some uses of uninitialized
  20. memory.
  21. Userspace programmers might be familiar with Valgrind's memcheck. The main
  22. difference between memcheck and kmemcheck is that memcheck works for userspace
  23. programs only, and kmemcheck works for the kernel only. The implementations
  24. are of course vastly different. Because of this, kmemcheck is not as accurate
  25. as memcheck, but it turns out to be good enough in practice to discover real
  26. programmer errors that the compiler is not able to find through static
  27. analysis.
  28. Enabling kmemcheck on a kernel will probably slow it down to the extent that
  29. the machine will not be usable for normal workloads such as e.g. an
  30. interactive desktop. kmemcheck will also cause the kernel to use about twice
  31. as much memory as normal. For this reason, kmemcheck is strictly a debugging
  32. feature.
  33. 1. Downloading
  34. ==============
  35. As of version 2.6.31-rc1, kmemcheck is included in the mainline kernel.
  36. 2. Configuring and compiling
  37. ============================
  38. kmemcheck only works for the x86 (both 32- and 64-bit) platform. A number of
  39. configuration variables must have specific settings in order for the kmemcheck
  40. menu to even appear in "menuconfig". These are:
  41. o CONFIG_CC_OPTIMIZE_FOR_SIZE=n
  42. This option is located under "General setup" / "Optimize for size".
  43. Without this, gcc will use certain optimizations that usually lead to
  44. false positive warnings from kmemcheck. An example of this is a 16-bit
  45. field in a struct, where gcc may load 32 bits, then discard the upper
  46. 16 bits. kmemcheck sees only the 32-bit load, and may trigger a
  47. warning for the upper 16 bits (if they're uninitialized).
  48. o CONFIG_SLAB=y or CONFIG_SLUB=y
  49. This option is located under "General setup" / "Choose SLAB
  50. allocator".
  51. o CONFIG_FUNCTION_TRACER=n
  52. This option is located under "Kernel hacking" / "Tracers" / "Kernel
  53. Function Tracer"
  54. When function tracing is compiled in, gcc emits a call to another
  55. function at the beginning of every function. This means that when the
  56. page fault handler is called, the ftrace framework will be called
  57. before kmemcheck has had a chance to handle the fault. If ftrace then
  58. modifies memory that was tracked by kmemcheck, the result is an
  59. endless recursive page fault.
  60. o CONFIG_DEBUG_PAGEALLOC=n
  61. This option is located under "Kernel hacking" / "Memory Debugging"
  62. / "Debug page memory allocations".
  63. In addition, I highly recommend turning on CONFIG_DEBUG_INFO=y. This is also
  64. located under "Kernel hacking". With this, you will be able to get line number
  65. information from the kmemcheck warnings, which is extremely valuable in
  66. debugging a problem. This option is not mandatory, however, because it slows
  67. down the compilation process and produces a much bigger kernel image.
  68. Now the kmemcheck menu should be visible (under "Kernel hacking" / "Memory
  69. Debugging" / "kmemcheck: trap use of uninitialized memory"). Here follows
  70. a description of the kmemcheck configuration variables:
  71. o CONFIG_KMEMCHECK
  72. This must be enabled in order to use kmemcheck at all...
  73. o CONFIG_KMEMCHECK_[DISABLED | ENABLED | ONESHOT]_BY_DEFAULT
  74. This option controls the status of kmemcheck at boot-time. "Enabled"
  75. will enable kmemcheck right from the start, "disabled" will boot the
  76. kernel as normal (but with the kmemcheck code compiled in, so it can
  77. be enabled at run-time after the kernel has booted), and "one-shot" is
  78. a special mode which will turn kmemcheck off automatically after
  79. detecting the first use of uninitialized memory.
  80. If you are using kmemcheck to actively debug a problem, then you
  81. probably want to choose "enabled" here.
  82. The one-shot mode is mostly useful in automated test setups because it
  83. can prevent floods of warnings and increase the chances of the machine
  84. surviving in case something is really wrong. In other cases, the one-
  85. shot mode could actually be counter-productive because it would turn
  86. itself off at the very first error -- in the case of a false positive
  87. too -- and this would come in the way of debugging the specific
  88. problem you were interested in.
  89. If you would like to use your kernel as normal, but with a chance to
  90. enable kmemcheck in case of some problem, it might be a good idea to
  91. choose "disabled" here. When kmemcheck is disabled, most of the run-
  92. time overhead is not incurred, and the kernel will be almost as fast
  93. as normal.
  94. o CONFIG_KMEMCHECK_QUEUE_SIZE
  95. Select the maximum number of error reports to store in an internal
  96. (fixed-size) buffer. Since errors can occur virtually anywhere and in
  97. any context, we need a temporary storage area which is guaranteed not
  98. to generate any other page faults when accessed. The queue will be
  99. emptied as soon as a tasklet may be scheduled. If the queue is full,
  100. new error reports will be lost.
  101. The default value of 64 is probably fine. If some code produces more
  102. than 64 errors within an irqs-off section, then the code is likely to
  103. produce many, many more, too, and these additional reports seldom give
  104. any more information (the first report is usually the most valuable
  105. anyway).
  106. This number might have to be adjusted if you are not using serial
  107. console or similar to capture the kernel log. If you are using the
  108. "dmesg" command to save the log, then getting a lot of kmemcheck
  109. warnings might overflow the kernel log itself, and the earlier reports
  110. will get lost in that way instead. Try setting this to 10 or so on
  111. such a setup.
  112. o CONFIG_KMEMCHECK_SHADOW_COPY_SHIFT
  113. Select the number of shadow bytes to save along with each entry of the
  114. error-report queue. These bytes indicate what parts of an allocation
  115. are initialized, uninitialized, etc. and will be displayed when an
  116. error is detected to help the debugging of a particular problem.
  117. The number entered here is actually the logarithm of the number of
  118. bytes that will be saved. So if you pick for example 5 here, kmemcheck
  119. will save 2^5 = 32 bytes.
  120. The default value should be fine for debugging most problems. It also
  121. fits nicely within 80 columns.
  122. o CONFIG_KMEMCHECK_PARTIAL_OK
  123. This option (when enabled) works around certain GCC optimizations that
  124. produce 32-bit reads from 16-bit variables where the upper 16 bits are
  125. thrown away afterwards.
  126. The default value (enabled) is recommended. This may of course hide
  127. some real errors, but disabling it would probably produce a lot of
  128. false positives.
  129. o CONFIG_KMEMCHECK_BITOPS_OK
  130. This option silences warnings that would be generated for bit-field
  131. accesses where not all the bits are initialized at the same time. This
  132. may also hide some real bugs.
  133. This option is probably obsolete, or it should be replaced with
  134. the kmemcheck-/bitfield-annotations for the code in question. The
  135. default value is therefore fine.
  136. Now compile the kernel as usual.
  137. 3. How to use
  138. =============
  139. 3.1. Booting
  140. ============
  141. First some information about the command-line options. There is only one
  142. option specific to kmemcheck, and this is called "kmemcheck". It can be used
  143. to override the default mode as chosen by the CONFIG_KMEMCHECK_*_BY_DEFAULT
  144. option. Its possible settings are:
  145. o kmemcheck=0 (disabled)
  146. o kmemcheck=1 (enabled)
  147. o kmemcheck=2 (one-shot mode)
  148. If SLUB debugging has been enabled in the kernel, it may take precedence over
  149. kmemcheck in such a way that the slab caches which are under SLUB debugging
  150. will not be tracked by kmemcheck. In order to ensure that this doesn't happen
  151. (even though it shouldn't by default), use SLUB's boot option "slub_debug",
  152. like this: slub_debug=-
  153. In fact, this option may also be used for fine-grained control over SLUB vs.
  154. kmemcheck. For example, if the command line includes "kmemcheck=1
  155. slub_debug=,dentry", then SLUB debugging will be used only for the "dentry"
  156. slab cache, and with kmemcheck tracking all the other caches. This is advanced
  157. usage, however, and is not generally recommended.
  158. 3.2. Run-time enable/disable
  159. ============================
  160. When the kernel has booted, it is possible to enable or disable kmemcheck at
  161. run-time. WARNING: This feature is still experimental and may cause false
  162. positive warnings to appear. Therefore, try not to use this. If you find that
  163. it doesn't work properly (e.g. you see an unreasonable amount of warnings), I
  164. will be happy to take bug reports.
  165. Use the file /proc/sys/kernel/kmemcheck for this purpose, e.g.:
  166. $ echo 0 > /proc/sys/kernel/kmemcheck # disables kmemcheck
  167. The numbers are the same as for the kmemcheck= command-line option.
  168. 3.3. Debugging
  169. ==============
  170. A typical report will look something like this:
  171. WARNING: kmemcheck: Caught 32-bit read from uninitialized memory (ffff88003e4a2024)
  172. 80000000000000000000000000000000000000000088ffff0000000000000000
  173. i i i i u u u u i i i i i i i i u u u u u u u u u u u u u u u u
  174. ^
  175. Pid: 1856, comm: ntpdate Not tainted 2.6.29-rc5 #264 945P-A
  176. RIP: 0010:[<ffffffff8104ede8>] [<ffffffff8104ede8>] __dequeue_signal+0xc8/0x190
  177. RSP: 0018:ffff88003cdf7d98 EFLAGS: 00210002
  178. RAX: 0000000000000030 RBX: ffff88003d4ea968 RCX: 0000000000000009
  179. RDX: ffff88003e5d6018 RSI: ffff88003e5d6024 RDI: ffff88003cdf7e84
  180. RBP: ffff88003cdf7db8 R08: ffff88003e5d6000 R09: 0000000000000000
  181. R10: 0000000000000080 R11: 0000000000000000 R12: 000000000000000e
  182. R13: ffff88003cdf7e78 R14: ffff88003d530710 R15: ffff88003d5a98c8
  183. FS: 0000000000000000(0000) GS:ffff880001982000(0063) knlGS:00000
  184. CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033
  185. CR2: ffff88003f806ea0 CR3: 000000003c036000 CR4: 00000000000006a0
  186. DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
  187. DR3: 0000000000000000 DR6: 00000000ffff4ff0 DR7: 0000000000000400
  188. [<ffffffff8104f04e>] dequeue_signal+0x8e/0x170
  189. [<ffffffff81050bd8>] get_signal_to_deliver+0x98/0x390
  190. [<ffffffff8100b87d>] do_notify_resume+0xad/0x7d0
  191. [<ffffffff8100c7b5>] int_signal+0x12/0x17
  192. [<ffffffffffffffff>] 0xffffffffffffffff
  193. The single most valuable information in this report is the RIP (or EIP on 32-
  194. bit) value. This will help us pinpoint exactly which instruction that caused
  195. the warning.
  196. If your kernel was compiled with CONFIG_DEBUG_INFO=y, then all we have to do
  197. is give this address to the addr2line program, like this:
  198. $ addr2line -e vmlinux -i ffffffff8104ede8
  199. arch/x86/include/asm/string_64.h:12
  200. include/asm-generic/siginfo.h:287
  201. kernel/signal.c:380
  202. kernel/signal.c:410
  203. The "-e vmlinux" tells addr2line which file to look in. IMPORTANT: This must
  204. be the vmlinux of the kernel that produced the warning in the first place! If
  205. not, the line number information will almost certainly be wrong.
  206. The "-i" tells addr2line to also print the line numbers of inlined functions.
  207. In this case, the flag was very important, because otherwise, it would only
  208. have printed the first line, which is just a call to memcpy(), which could be
  209. called from a thousand places in the kernel, and is therefore not very useful.
  210. These inlined functions would not show up in the stack trace above, simply
  211. because the kernel doesn't load the extra debugging information. This
  212. technique can of course be used with ordinary kernel oopses as well.
  213. In this case, it's the caller of memcpy() that is interesting, and it can be
  214. found in include/asm-generic/siginfo.h, line 287:
  215. 281 static inline void copy_siginfo(struct siginfo *to, struct siginfo *from)
  216. 282 {
  217. 283 if (from->si_code < 0)
  218. 284 memcpy(to, from, sizeof(*to));
  219. 285 else
  220. 286 /* _sigchld is currently the largest know union member */
  221. 287 memcpy(to, from, __ARCH_SI_PREAMBLE_SIZE + sizeof(from->_sifields._sigchld));
  222. 288 }
  223. Since this was a read (kmemcheck usually warns about reads only, though it can
  224. warn about writes to unallocated or freed memory as well), it was probably the
  225. "from" argument which contained some uninitialized bytes. Following the chain
  226. of calls, we move upwards to see where "from" was allocated or initialized,
  227. kernel/signal.c, line 380:
  228. 359 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  229. 360 {
  230. ...
  231. 367 list_for_each_entry(q, &list->list, list) {
  232. 368 if (q->info.si_signo == sig) {
  233. 369 if (first)
  234. 370 goto still_pending;
  235. 371 first = q;
  236. ...
  237. 377 if (first) {
  238. 378 still_pending:
  239. 379 list_del_init(&first->list);
  240. 380 copy_siginfo(info, &first->info);
  241. 381 __sigqueue_free(first);
  242. ...
  243. 392 }
  244. 393 }
  245. Here, it is &first->info that is being passed on to copy_siginfo(). The
  246. variable "first" was found on a list -- passed in as the second argument to
  247. collect_signal(). We continue our journey through the stack, to figure out
  248. where the item on "list" was allocated or initialized. We move to line 410:
  249. 395 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  250. 396 siginfo_t *info)
  251. 397 {
  252. ...
  253. 410 collect_signal(sig, pending, info);
  254. ...
  255. 414 }
  256. Now we need to follow the "pending" pointer, since that is being passed on to
  257. collect_signal() as "list". At this point, we've run out of lines from the
  258. "addr2line" output. Not to worry, we just paste the next addresses from the
  259. kmemcheck stack dump, i.e.:
  260. [<ffffffff8104f04e>] dequeue_signal+0x8e/0x170
  261. [<ffffffff81050bd8>] get_signal_to_deliver+0x98/0x390
  262. [<ffffffff8100b87d>] do_notify_resume+0xad/0x7d0
  263. [<ffffffff8100c7b5>] int_signal+0x12/0x17
  264. $ addr2line -e vmlinux -i ffffffff8104f04e ffffffff81050bd8 \
  265. ffffffff8100b87d ffffffff8100c7b5
  266. kernel/signal.c:446
  267. kernel/signal.c:1806
  268. arch/x86/kernel/signal.c:805
  269. arch/x86/kernel/signal.c:871
  270. arch/x86/kernel/entry_64.S:694
  271. Remember that since these addresses were found on the stack and not as the
  272. RIP value, they actually point to the _next_ instruction (they are return
  273. addresses). This becomes obvious when we look at the code for line 446:
  274. 422 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  275. 423 {
  276. ...
  277. 431 signr = __dequeue_signal(&tsk->signal->shared_pending,
  278. 432 mask, info);
  279. 433 /*
  280. 434 * itimer signal ?
  281. 435 *
  282. 436 * itimers are process shared and we restart periodic
  283. 437 * itimers in the signal delivery path to prevent DoS
  284. 438 * attacks in the high resolution timer case. This is
  285. 439 * compliant with the old way of self restarting
  286. 440 * itimers, as the SIGALRM is a legacy signal and only
  287. 441 * queued once. Changing the restart behaviour to
  288. 442 * restart the timer in the signal dequeue path is
  289. 443 * reducing the timer noise on heavy loaded !highres
  290. 444 * systems too.
  291. 445 */
  292. 446 if (unlikely(signr == SIGALRM)) {
  293. ...
  294. 489 }
  295. So instead of looking at 446, we should be looking at 431, which is the line
  296. that executes just before 446. Here we see that what we are looking for is
  297. &tsk->signal->shared_pending.
  298. Our next task is now to figure out which function that puts items on this
  299. "shared_pending" list. A crude, but efficient tool, is git grep:
  300. $ git grep -n 'shared_pending' kernel/
  301. ...
  302. kernel/signal.c:828: pending = group ? &t->signal->shared_pending : &t->pending;
  303. kernel/signal.c:1339: pending = group ? &t->signal->shared_pending : &t->pending;
  304. ...
  305. There were more results, but none of them were related to list operations,
  306. and these were the only assignments. We inspect the line numbers more closely
  307. and find that this is indeed where items are being added to the list:
  308. 816 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  309. 817 int group)
  310. 818 {
  311. ...
  312. 828 pending = group ? &t->signal->shared_pending : &t->pending;
  313. ...
  314. 851 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
  315. 852 (is_si_special(info) ||
  316. 853 info->si_code >= 0)));
  317. 854 if (q) {
  318. 855 list_add_tail(&q->list, &pending->list);
  319. ...
  320. 890 }
  321. and:
  322. 1309 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  323. 1310 {
  324. ....
  325. 1339 pending = group ? &t->signal->shared_pending : &t->pending;
  326. 1340 list_add_tail(&q->list, &pending->list);
  327. ....
  328. 1347 }
  329. In the first case, the list element we are looking for, "q", is being returned
  330. from the function __sigqueue_alloc(), which looks like an allocation function.
  331. Let's take a look at it:
  332. 187 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
  333. 188 int override_rlimit)
  334. 189 {
  335. 190 struct sigqueue *q = NULL;
  336. 191 struct user_struct *user;
  337. 192
  338. 193 /*
  339. 194 * We won't get problems with the target's UID changing under us
  340. 195 * because changing it requires RCU be used, and if t != current, the
  341. 196 * caller must be holding the RCU readlock (by way of a spinlock) and
  342. 197 * we use RCU protection here
  343. 198 */
  344. 199 user = get_uid(__task_cred(t)->user);
  345. 200 atomic_inc(&user->sigpending);
  346. 201 if (override_rlimit ||
  347. 202 atomic_read(&user->sigpending) <=
  348. 203 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
  349. 204 q = kmem_cache_alloc(sigqueue_cachep, flags);
  350. 205 if (unlikely(q == NULL)) {
  351. 206 atomic_dec(&user->sigpending);
  352. 207 free_uid(user);
  353. 208 } else {
  354. 209 INIT_LIST_HEAD(&q->list);
  355. 210 q->flags = 0;
  356. 211 q->user = user;
  357. 212 }
  358. 213
  359. 214 return q;
  360. 215 }
  361. We see that this function initializes q->list, q->flags, and q->user. It seems
  362. that now is the time to look at the definition of "struct sigqueue", e.g.:
  363. 14 struct sigqueue {
  364. 15 struct list_head list;
  365. 16 int flags;
  366. 17 siginfo_t info;
  367. 18 struct user_struct *user;
  368. 19 };
  369. And, you might remember, it was a memcpy() on &first->info that caused the
  370. warning, so this makes perfect sense. It also seems reasonable to assume that
  371. it is the caller of __sigqueue_alloc() that has the responsibility of filling
  372. out (initializing) this member.
  373. But just which fields of the struct were uninitialized? Let's look at
  374. kmemcheck's report again:
  375. WARNING: kmemcheck: Caught 32-bit read from uninitialized memory (ffff88003e4a2024)
  376. 80000000000000000000000000000000000000000088ffff0000000000000000
  377. i i i i u u u u i i i i i i i i u u u u u u u u u u u u u u u u
  378. ^
  379. These first two lines are the memory dump of the memory object itself, and the
  380. shadow bytemap, respectively. The memory object itself is in this case
  381. &first->info. Just beware that the start of this dump is NOT the start of the
  382. object itself! The position of the caret (^) corresponds with the address of
  383. the read (ffff88003e4a2024).
  384. The shadow bytemap dump legend is as follows:
  385. i - initialized
  386. u - uninitialized
  387. a - unallocated (memory has been allocated by the slab layer, but has not
  388. yet been handed off to anybody)
  389. f - freed (memory has been allocated by the slab layer, but has been freed
  390. by the previous owner)
  391. In order to figure out where (relative to the start of the object) the
  392. uninitialized memory was located, we have to look at the disassembly. For
  393. that, we'll need the RIP address again:
  394. RIP: 0010:[<ffffffff8104ede8>] [<ffffffff8104ede8>] __dequeue_signal+0xc8/0x190
  395. $ objdump -d --no-show-raw-insn vmlinux | grep -C 8 ffffffff8104ede8:
  396. ffffffff8104edc8: mov %r8,0x8(%r8)
  397. ffffffff8104edcc: test %r10d,%r10d
  398. ffffffff8104edcf: js ffffffff8104ee88 <__dequeue_signal+0x168>
  399. ffffffff8104edd5: mov %rax,%rdx
  400. ffffffff8104edd8: mov $0xc,%ecx
  401. ffffffff8104eddd: mov %r13,%rdi
  402. ffffffff8104ede0: mov $0x30,%eax
  403. ffffffff8104ede5: mov %rdx,%rsi
  404. ffffffff8104ede8: rep movsl %ds:(%rsi),%es:(%rdi)
  405. ffffffff8104edea: test $0x2,%al
  406. ffffffff8104edec: je ffffffff8104edf0 <__dequeue_signal+0xd0>
  407. ffffffff8104edee: movsw %ds:(%rsi),%es:(%rdi)
  408. ffffffff8104edf0: test $0x1,%al
  409. ffffffff8104edf2: je ffffffff8104edf5 <__dequeue_signal+0xd5>
  410. ffffffff8104edf4: movsb %ds:(%rsi),%es:(%rdi)
  411. ffffffff8104edf5: mov %r8,%rdi
  412. ffffffff8104edf8: callq ffffffff8104de60 <__sigqueue_free>
  413. As expected, it's the "rep movsl" instruction from the memcpy() that causes
  414. the warning. We know about REP MOVSL that it uses the register RCX to count
  415. the number of remaining iterations. By taking a look at the register dump
  416. again (from the kmemcheck report), we can figure out how many bytes were left
  417. to copy:
  418. RAX: 0000000000000030 RBX: ffff88003d4ea968 RCX: 0000000000000009
  419. By looking at the disassembly, we also see that %ecx is being loaded with the
  420. value $0xc just before (ffffffff8104edd8), so we are very lucky. Keep in mind
  421. that this is the number of iterations, not bytes. And since this is a "long"
  422. operation, we need to multiply by 4 to get the number of bytes. So this means
  423. that the uninitialized value was encountered at 4 * (0xc - 0x9) = 12 bytes
  424. from the start of the object.
  425. We can now try to figure out which field of the "struct siginfo" that was not
  426. initialized. This is the beginning of the struct:
  427. 40 typedef struct siginfo {
  428. 41 int si_signo;
  429. 42 int si_errno;
  430. 43 int si_code;
  431. 44
  432. 45 union {
  433. ..
  434. 92 } _sifields;
  435. 93 } siginfo_t;
  436. On 64-bit, the int is 4 bytes long, so it must the union member that has
  437. not been initialized. We can verify this using gdb:
  438. $ gdb vmlinux
  439. ...
  440. (gdb) p &((struct siginfo *) 0)->_sifields
  441. $1 = (union {...} *) 0x10
  442. Actually, it seems that the union member is located at offset 0x10 -- which
  443. means that gcc has inserted 4 bytes of padding between the members si_code
  444. and _sifields. We can now get a fuller picture of the memory dump:
  445. _----------------------------=> si_code
  446. / _--------------------=> (padding)
  447. | / _------------=> _sifields(._kill._pid)
  448. | | / _----=> _sifields(._kill._uid)
  449. | | | /
  450. -------|-------|-------|-------|
  451. 80000000000000000000000000000000000000000088ffff0000000000000000
  452. i i i i u u u u i i i i i i i i u u u u u u u u u u u u u u u u
  453. This allows us to realize another important fact: si_code contains the value
  454. 0x80. Remember that x86 is little endian, so the first 4 bytes "80000000" are
  455. really the number 0x00000080. With a bit of research, we find that this is
  456. actually the constant SI_KERNEL defined in include/asm-generic/siginfo.h:
  457. 144 #define SI_KERNEL 0x80 /* sent by the kernel from somewhere */
  458. This macro is used in exactly one place in the x86 kernel: In send_signal()
  459. in kernel/signal.c:
  460. 816 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  461. 817 int group)
  462. 818 {
  463. ...
  464. 828 pending = group ? &t->signal->shared_pending : &t->pending;
  465. ...
  466. 851 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
  467. 852 (is_si_special(info) ||
  468. 853 info->si_code >= 0)));
  469. 854 if (q) {
  470. 855 list_add_tail(&q->list, &pending->list);
  471. 856 switch ((unsigned long) info) {
  472. ...
  473. 865 case (unsigned long) SEND_SIG_PRIV:
  474. 866 q->info.si_signo = sig;
  475. 867 q->info.si_errno = 0;
  476. 868 q->info.si_code = SI_KERNEL;
  477. 869 q->info.si_pid = 0;
  478. 870 q->info.si_uid = 0;
  479. 871 break;
  480. ...
  481. 890 }
  482. Not only does this match with the .si_code member, it also matches the place
  483. we found earlier when looking for where siginfo_t objects are enqueued on the
  484. "shared_pending" list.
  485. So to sum up: It seems that it is the padding introduced by the compiler
  486. between two struct fields that is uninitialized, and this gets reported when
  487. we do a memcpy() on the struct. This means that we have identified a false
  488. positive warning.
  489. Normally, kmemcheck will not report uninitialized accesses in memcpy() calls
  490. when both the source and destination addresses are tracked. (Instead, we copy
  491. the shadow bytemap as well). In this case, the destination address clearly
  492. was not tracked. We can dig a little deeper into the stack trace from above:
  493. arch/x86/kernel/signal.c:805
  494. arch/x86/kernel/signal.c:871
  495. arch/x86/kernel/entry_64.S:694
  496. And we clearly see that the destination siginfo object is located on the
  497. stack:
  498. 782 static void do_signal(struct pt_regs *regs)
  499. 783 {
  500. 784 struct k_sigaction ka;
  501. 785 siginfo_t info;
  502. ...
  503. 804 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  504. ...
  505. 854 }
  506. And this &info is what eventually gets passed to copy_siginfo() as the
  507. destination argument.
  508. Now, even though we didn't find an actual error here, the example is still a
  509. good one, because it shows how one would go about to find out what the report
  510. was all about.
  511. 3.4. Annotating false positives
  512. ===============================
  513. There are a few different ways to make annotations in the source code that
  514. will keep kmemcheck from checking and reporting certain allocations. Here
  515. they are:
  516. o __GFP_NOTRACK_FALSE_POSITIVE
  517. This flag can be passed to kmalloc() or kmem_cache_alloc() (therefore
  518. also to other functions that end up calling one of these) to indicate
  519. that the allocation should not be tracked because it would lead to
  520. a false positive report. This is a "big hammer" way of silencing
  521. kmemcheck; after all, even if the false positive pertains to
  522. particular field in a struct, for example, we will now lose the
  523. ability to find (real) errors in other parts of the same struct.
  524. Example:
  525. /* No warnings will ever trigger on accessing any part of x */
  526. x = kmalloc(sizeof *x, GFP_KERNEL | __GFP_NOTRACK_FALSE_POSITIVE);
  527. o kmemcheck_bitfield_begin(name)/kmemcheck_bitfield_end(name) and
  528. kmemcheck_annotate_bitfield(ptr, name)
  529. The first two of these three macros can be used inside struct
  530. definitions to signal, respectively, the beginning and end of a
  531. bitfield. Additionally, this will assign the bitfield a name, which
  532. is given as an argument to the macros.
  533. Having used these markers, one can later use
  534. kmemcheck_annotate_bitfield() at the point of allocation, to indicate
  535. which parts of the allocation is part of a bitfield.
  536. Example:
  537. struct foo {
  538. int x;
  539. kmemcheck_bitfield_begin(flags);
  540. int flag_a:1;
  541. int flag_b:1;
  542. kmemcheck_bitfield_end(flags);
  543. int y;
  544. };
  545. struct foo *x = kmalloc(sizeof *x);
  546. /* No warnings will trigger on accessing the bitfield of x */
  547. kmemcheck_annotate_bitfield(x, flags);
  548. Note that kmemcheck_annotate_bitfield() can be used even before the
  549. return value of kmalloc() is checked -- in other words, passing NULL
  550. as the first argument is legal (and will do nothing).
  551. 4. Reporting errors
  552. ===================
  553. As we have seen, kmemcheck will produce false positive reports. Therefore, it
  554. is not very wise to blindly post kmemcheck warnings to mailing lists and
  555. maintainers. Instead, I encourage maintainers and developers to find errors
  556. in their own code. If you get a warning, you can try to work around it, try
  557. to figure out if it's a real error or not, or simply ignore it. Most
  558. developers know their own code and will quickly and efficiently determine the
  559. root cause of a kmemcheck report. This is therefore also the most efficient
  560. way to work with kmemcheck.
  561. That said, we (the kmemcheck maintainers) will always be on the lookout for
  562. false positives that we can annotate and silence. So whatever you find,
  563. please drop us a note privately! Kernel configs and steps to reproduce (if
  564. available) are of course a great help too.
  565. Happy hacking!
  566. 5. Technical description
  567. ========================
  568. kmemcheck works by marking memory pages non-present. This means that whenever
  569. somebody attempts to access the page, a page fault is generated. The page
  570. fault handler notices that the page was in fact only hidden, and so it calls
  571. on the kmemcheck code to make further investigations.
  572. When the investigations are completed, kmemcheck "shows" the page by marking
  573. it present (as it would be under normal circumstances). This way, the
  574. interrupted code can continue as usual.
  575. But after the instruction has been executed, we should hide the page again, so
  576. that we can catch the next access too! Now kmemcheck makes use of a debugging
  577. feature of the processor, namely single-stepping. When the processor has
  578. finished the one instruction that generated the memory access, a debug
  579. exception is raised. From here, we simply hide the page again and continue
  580. execution, this time with the single-stepping feature turned off.
  581. kmemcheck requires some assistance from the memory allocator in order to work.
  582. The memory allocator needs to
  583. 1. Tell kmemcheck about newly allocated pages and pages that are about to
  584. be freed. This allows kmemcheck to set up and tear down the shadow memory
  585. for the pages in question. The shadow memory stores the status of each
  586. byte in the allocation proper, e.g. whether it is initialized or
  587. uninitialized.
  588. 2. Tell kmemcheck which parts of memory should be marked uninitialized.
  589. There are actually a few more states, such as "not yet allocated" and
  590. "recently freed".
  591. If a slab cache is set up using the SLAB_NOTRACK flag, it will never return
  592. memory that can take page faults because of kmemcheck.
  593. If a slab cache is NOT set up using the SLAB_NOTRACK flag, callers can still
  594. request memory with the __GFP_NOTRACK or __GFP_NOTRACK_FALSE_POSITIVE flags.
  595. This does not prevent the page faults from occurring, however, but marks the
  596. object in question as being initialized so that no warnings will ever be
  597. produced for this object.
  598. Currently, the SLAB and SLUB allocators are supported by kmemcheck.