{"id":254,"date":"2018-02-19T16:49:28","date_gmt":"2018-02-20T00:49:28","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=254"},"modified":"2018-02-19T16:49:28","modified_gmt":"2018-02-20T00:49:28","slug":"debugging-linux-kernels-with-kaslr","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/kaslr\/","title":{"rendered":"Debugging Linux Kernels with KASLR"},"content":{"rendered":"<p>This tutorial shows how to use VisualKernel to debug Linux kernels with KASLR enabled.<\/p>\n<p>KASLR (Kernel Address Space Layout Randomization) is a technique that\u00a0provides an extra layer of protection\u00a0against certain types of attacks by changing the\u00a0kernel load address each time the system is started. Although it increases the system security, it complicates debugging a KASLR-enabled kernel, as the debugger would require extra steps in order to map\u00a0the functions and data in memory to the corresponding debug symbols, that won&#8217;t have matching addresses anymore, as the addresses of kernel symbols change after each restart:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-255\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-addrs.png\" alt=\"\" width=\"817\" height=\"456\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-addrs.png 817w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-addrs-300x167.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-addrs-768x429.png 768w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/p>\n<p>In this tutorial we will\u00a0create a basic kernel module under a KASLR-enabled kernel and will show how to debug it in both regular and post-mortem modes. Before you begin, install VisualKernel 3.0 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the\u00a0VisualKernel Module Project Wizard:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-256\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj.png\" alt=\"\" width=\"941\" height=\"653\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj.png 941w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj-300x208.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj-768x533.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj-392x272.png 392w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-prj-130x90.png 130w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/li>\n<li>Select &#8220;Create a new kernel module&#8221; -&gt; &#8220;Hello, World&#8221;:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-257\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newmodule.png\" alt=\"\" width=\"886\" height=\"693\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newmodule.png 886w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newmodule-300x235.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newmodule-768x601.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/li>\n<li>Select the\u00a0target machine running a KASLR-enabled kernel:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-258\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-target.png\" alt=\"\" width=\"886\" height=\"693\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-target.png 886w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-target-300x235.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-target-768x601.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/li>\n<li>Proceed with the default file access settings (store sources on Windows, upload modified sources on build):<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-259\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-store.png\" alt=\"\" width=\"886\" height=\"693\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-store.png 886w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-store-300x235.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-store-768x601.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/li>\n<li>Finally select the debugging settings that match your system.\u00a0In this tutorial we will demonstrate debugging a crashed kernel, so we will use the direct VMWare debug connection that\u00a0runs on top of the kernel itself and won&#8217;t be affected by the kernel crash:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-260\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-kgdb.png\" alt=\"\" width=\"886\" height=\"693\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-kgdb.png 886w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-kgdb-300x235.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-kgdb-768x601.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. Build it by pressing Ctrl-Shift-B and,\u00a0set a\u00a0breakpoint in the init() function and start your module.\u00a0VisualKernel will load the module and the breakpoint will trigger:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-261\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sysopen.png\" alt=\"\" width=\"1196\" height=\"782\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sysopen.png 1196w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sysopen-300x196.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sysopen-768x502.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sysopen-1024x670.png 1024w\" sizes=\"(max-width: 1196px) 100vw, 1196px\" \/><\/li>\n<li>Despite the use of KASLR, VisualKernel was able to\u00a0map the\u00a0code addresses in the memory to the meaningful symbol names and source file locations. This is achieved by querying the <strong>\/proc\/kallsyms<\/strong> file before starting the debug session, comparing\u00a0its\u00a0contents with the actual kernel symbols and adjusting the\u00a0section offsets\u00a0automatically:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-262\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-addr.png\" alt=\"\" width=\"1196\" height=\"782\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-addr.png 1196w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-addr-300x196.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-addr-768x502.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-addr-1024x670.png 1024w\" sizes=\"(max-width: 1196px) 100vw, 1196px\" \/><\/li>\n<li>Now we will show how to debug a crashed kernel with KASLR enabled. Replace the main\u00a0source file contents with the following code:\n<pre class=\"\">#include &lt;linux\/init.h&gt;\r\n#include &lt;linux\/module.h&gt;\r\n#include &lt;linux\/timer.h&gt;\r\n\r\nMODULE_LICENSE(\"Proprietary\");\r\n\r\nstruct timer_list s_Timer;\r\n\r\nvoid timer_callback(unsigned long unused) \r\n{\r\n panic(\"Attach debugger now!\");\r\n}\r\n\r\nstatic int __init KASLRDemo_init(void)\r\n{\r\n printk(\"KASLRDemo: Hello, world!\\n\");\r\n init_timer(&amp;s_Timer);\r\n setup_timer(&amp;s_Timer, timer_callback, 0);\r\n mod_timer(&amp;s_Timer, jiffies + msecs_to_jiffies(200));\r\n return 0;\r\n}\r\n\r\nstatic void __exit KASLRDemo_exit(void)\r\n{\r\n printk(\"KASLRDemo: Goodbye, world!\\n\");\r\n}\r\n\r\nmodule_init(KASLRDemo_init);\r\nmodule_exit(KASLRDemo_exit);<\/pre>\n<\/li>\n<li>Then open VisualKernel Project properties and enable debugging of crashed kernels and\u00a0the use of offline kallsyms dumps. Click &#8220;create\/update&#8221; to automatically create a dump\u00a0of the <strong>kallsyms<\/strong> file:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-269\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/settings.png\" alt=\"\" width=\"869\" height=\"594\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/settings.png 869w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/settings-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/settings-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/settings-130x90.png 130w\" sizes=\"(max-width: 869px) 100vw, 869px\" \/><\/li>\n<li>The dump\u00a0simply contains the raw contents of the <strong>\/proc\/kallsyms<\/strong> file.\u00a0If you restart your target system often, you can configure it to\u00a0simply\u00a0read <strong>\/proc\/kallsyms<\/strong> to a text file on each start and upload its contents\u00a0to a location where VisualKernel can access it:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-265\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-dump2.png\" alt=\"\" width=\"1196\" height=\"782\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-dump2.png 1196w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-dump2-300x196.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-dump2-768x502.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-dump2-1024x670.png 1024w\" sizes=\"(max-width: 1196px) 100vw, 1196px\" \/><\/li>\n<li>Build the modified kernel module and load it without starting a debug session:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-266\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-insmod.png\" alt=\"\" width=\"817\" height=\"456\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-insmod.png 817w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-insmod-300x167.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-insmod-768x429.png 768w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/li>\n<li>The\u00a0Linux system will crash.\u00a0Go back to the\u00a0Visual Studio window and start debugging by pressing F5. VisualKernel will use the dump file you created to\u00a0adjust the symbol offsets for the kernel and will recover the call stack. Right-click on the &#8220;KASLRDemo + &lt;address&gt;&#8221; line and select &#8220;Load Symbols&#8221;:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-267\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-dump.png\" alt=\"\" width=\"1215\" height=\"762\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-dump.png 1215w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-dump-300x188.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-dump-768x482.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-dump-1024x642.png 1024w\" sizes=\"(max-width: 1215px) 100vw, 1215px\" \/><\/li>\n<li>VisualKernel will\u00a0load the symbols for your module and will show the location in the source file that caused the crash:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-268\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-panic.png\" alt=\"\" width=\"1215\" height=\"762\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-panic.png 1215w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-panic-300x188.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-panic-768x482.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-panic-1024x642.png 1024w\" sizes=\"(max-width: 1215px) 100vw, 1215px\" \/><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use VisualKernel to debug Linux kernels with KASLR enabled. KASLR (Kernel Address Space Layout Randomization)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[44],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/254"}],"collection":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":1,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/254\/revisions\/270"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}