<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Path Cybersec [Slava Moskvin] on Medium]]></title>
        <description><![CDATA[Stories by Path Cybersec [Slava Moskvin] on Medium]]></description>
        <link>https://medium.com/@slava-moskvin?source=rss-ccc00f894042------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GIqZryUJnM4rxuSqpJxvAQ.jpeg</url>
            <title>Stories by Path Cybersec [Slava Moskvin] on Medium</title>
            <link>https://medium.com/@slava-moskvin?source=rss-ccc00f894042------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 20:15:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@slava-moskvin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ZDI-25–310: Remote NULL Deref in Linux KSMBD]]></title>
            <link>https://slava-moskvin.medium.com/zdi-25-310-remote-null-deref-in-linux-ksmbd-8f3770ab0bb2?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f3770ab0bb2</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[fuzzing]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Thu, 29 May 2025 23:26:45 GMT</pubDate>
            <atom:updated>2025-06-13T09:37:13.041Z</atom:updated>
            <content:encoded><![CDATA[<h3>CVE-2025–22037/ZDI-25–310: Remote NULL Deref in Linux KSMBD</h3><h3>Overview</h3><p>This vulnerability allows a remote, unauthenticated attacker to trigger a NULL pointer dereference in the ksmbd Linux kernel subsystem, leading to a system crash or hang. Only systems with the ksmbd kernel module loaded or built-in are affected.</p><ul><li><strong>ZDI ID</strong>: ZDI-25–310</li><li><strong>CVSS Score</strong>: 6.8 (<a href="https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:N/AC:H/PR:N/UI:N/S:C/C:N/I:N/A:H&amp;version=3.0">AV:N/AC:H/PR:N/UI:N/S:C/C:N/I:N/A:H</a>)</li><li><strong>Fixed in</strong>: <a href="https://github.com/torvalds/linux/commit/c8b5b7c5da7d0c31c9b7190b4a7bba5281fc4780">commit c8b5b7c</a></li></ul><h3>Motivation</h3><p>For almost a year now, I’ve been doing vulnerability research on the Linux kernel as part of my personal projects. The kernel is just fascinating, and having the source code makes progress much faster — which is so important when you can only spare a few hours per week.</p><p>During that time, I had some success statically analyzing third-party kernel modules and found a couple of 0-days. Gradually, my focus shifted to the vanilla kernel itself — the juicy stuff. But with one of the most competitive targets out there, reading code blindly wasn’t gonna cut it.</p><p>So, I learned how to use syzkaller, came up with a few optimizations for my (unusual) environment, and started looking for overlooked syscalls not yet described in syzkaller. I found some and fuzzed them, but honestly, it felt like picking up breadcrumbs — at least with the kernel knowledge I had at the time.</p><p>Sure, I could’ve dug deeper into its internals or try to squeeze more from coverage gap visible in the <a href="https://syzkaller.appspot.com/upstream/coverage?period=month">coverage heatmap</a> — and that might’ve worked. But honestly, at some point, it started feeling less like fun and more like work.</p><h3>Fuzzer</h3><p>Looking for alternatives, I stumbled upon a blog post by Lau Lau <a href="https://x.com/notselwyn">@notselwyn</a> on <a href="https://pwning.tech/ksmbd-syzkaller/">fuzzing the ksmbd kernel subsystem with syzkaller</a>, thanks to a tip from <a href="https://www.linkedin.com/in/yunseong-kim-linux-kernel/">Yunseong Kim</a>. Lau adapted syzkaller to fuzz network stack of ksmbd—impressive work!</p><p>The gears in my head started turning. I knew a few general things about efficient fuzzing:</p><ul><li>For obscure targets, the fuzzer choice doesn’t matter much.</li><li>For high-profile targets, you have to demonastrate a novel approach:</li><li>find a new attack surface</li><li>combine fuzzers/fuzzing modes, craft a perfect input corpus, and run a well-organized campaign</li><li>or come up with a new fuzzer that does something different</li></ul><p>I wanted to avoid writing syscall/protocol definitions, so the fuzzer had to be mutational. Sure, there were already plenty of network fuzzers out there — united by their ease of use, simplicity and probaly having zero chances to find anything in the kernel.</p><p><strong>BUT</strong> — there was one thing nobody had tried: combining a network fuzzer with coverage-based feedback. <strong>AND</strong> fuzzing over network had a huge advantage. While being painfully slow, it has the potential to walk through full state machines and hit proper network APIs, potentially triggering side effects.</p><p>Looked like a fun project. And ksmbd seemed like a solid first target:</p><ul><li>network-facing → higher impact</li><li>relatively new → not fuzzed to death</li></ul><p>Still, I kept my expectations low. It had already been fuzzed, so this was mostly to test and debug the fuzzer — not much more.</p><p>After some late-night vibe coding, I had the first prototype ready: super basic mutations, coverage feedback, crash detection, and an initial corpus of legit SMB traffic. It was time to set up the target.</p><h3>Target</h3><p>Compiling and installing the kernel was straightforward::</p><pre>scripts/config -e KCOV -e KCOV_INSTRUMENT_ALL -e DEBUG_FS -e NET_9P -e NET_9P_VIRTIO -e KCOV_ENABLE_COMPARISONS -e KALLSYMS -e KALLSYMS_ALL -e DEBUG_INFO -e KASAN -d RANDOMIZE_BASE -d RANDOMIZE_MEMORY -e DEBUG_INFO_DWARF4 -d WERROR -e SECURITYFS -e CONFIGFS_FS -e KASAN_INLINE -e WARNING -e FORTIFY_SOURCE -e HARDENED_USERCOPY -e LOCKUP_DETECTOR -e SOFTLOCKUP_DETECTOR -e HARDLOCKUP_DETECTOR -e DETECT_HUNG_TASK -e UBSAN -e LOCKDEP -e PROVE_LOCKING -e DEBUG_ATOMIC_SLEEP -e PROVE_RCU -e DEBUG_VM -e REFCOUNT_FULL -e BOOTPARAM_HARDLOCKUP_PANIC -e WQ_WATCHDOG--set-str CONFIG_LOCALVERSION -ksmbd-fuzz -e CIFS_UPCALL -e CIFS_XATTR -e CIFS_POSIX -e CIFS_DEBUG -e CIFS_DFS_UPCALL -e CIFS_SWN_UPCALL -e CIFS_FSCACHE -e SMB_SERVER_SMBDIRECT -e SMB_SERVER_CHECK_CAP_NET_ADMIN -e SMB_SERVER_KERBEROS5 -e SMBFS -e CIFS_SMB_DIRECT -e CIFS_NFSD_EXPORT -e CIFS_DFS_UPCALL -e CIFS_DEBUG_DUMP_KEYS -e CIFS_DEBUG2 -e CIFS_POSIX -e CIFS_ALLOW_INSECURE_LEGACY -e CIFS_STATS2<br>make oldconfig &amp;&amp; <br>make ARCH=arm64 -j`nproc` &amp;&amp; make ARCH=arm64 -j`nproc` modules &amp;&amp; make ARCH=arm64 dtbs &amp;&amp; sudo make modules_install &amp;&amp; sudo make install</pre><p>Then, I added kernel.panic_on_warn=0 kernel.panic_on_oops=0 to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub to make sure the target VM wouldn’t reboot on kernel panic.</p><h3>Ksmbd Setup</h3><p>To configure the ksmbd server, I needed ksmbd-tools:</p><pre>sudo apt update &amp;&amp; sudo apt upgrade -y<br>sudo apt install ksmbd-tools</pre><p>I kept it simple with a minimal config in /etc/ksmbd/ksmbd.conf:</p><pre>[global]<br>workgroup = WORKGROUP<br>netbios name = ksmbd-server<br>server string = KSMBD File Server<br> ​<br>[shared]<br>path = /opt/share<br>guest ok = yes<br>writable = yes</pre><p>Then I made sure ksmbd would start after reboot:</p><pre>sudo chmod 777 /opt/share<br>sudo systemctl start ksmbd<br>sudo systemctl enable ksmbd</pre><p>And then I just started fuzzing.</p><h3>Crash discovery</h3><p>I didn’t expect much — just wanted to make sure the fuzzer was stable enough for long campaigns. But to my surprise, the fuzzer stopped just after about 40 minutes of work (it couldn’t resume on crash at the time). And I got this from the system logs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSbCE0EdhnDgkdIIO3cUag.png" /></figure><p>NULL deref, something often overlooked when fuzzing the kernel locally, but remote NULL deref has much higher implications — like remotely rendering the system unusable.</p><p>After impact analysis, I concluded:</p><ul><li>Ubuntu Desktop: Immediately reboots after triggering the bug.</li><li>Ubuntu Server: Doesn’t reboot (kernel.panic_on_oops=0 by default), but repeated triggering causes the system to hang until all kernel threads stall—basically a full DoS.</li></ul><p>So yeah, looked good enough to continue the investigation.</p><h3>Root Cause Analysis</h3><p>Unwinding the call stack and jumping around the code, I quickly found out that the NULL defer happens in the function alloc_preauth_hash (in fs/smb/server/smb2pdu.c) when dereferencing conn-&gt;preauth_info.</p><pre>static int alloc_preauth_hash(struct ksmbd_session *sess,<br>             struct ksmbd_conn *conn)<br> {<br>   if (sess-&gt;Preauth_HashValue)<br>     return 0;<br> ​<br>   sess-&gt;Preauth_HashValue = kmemdup(conn-&gt;preauth_info-&gt;Preauth_HashValue,<br>             PREAUTH_HASHVALUE_SIZE, KSMBD_DEFAULT_GFP);<br>   if (!sess-&gt;Preauth_HashValue)<br>     return -ENOMEM;<br> ​<br>   return 0;<br> }</pre><p>But why was conn-&gt;preauth_info uninitialized?</p><p>It’s allocated in smb2_handle_negotiate, and only for SMB 3.1.1 (makes sense—preauth integrity came in with that version):</p><pre>switch (conn-&gt;dialect) {<br>   case SMB311_PROT_ID:<br>     conn-&gt;preauth_info =<br>       kzalloc(sizeof(struct preauth_integrity_info),<br>         KSMBD_DEFAULT_GFP);<br>     if (!conn-&gt;preauth_info) {<br>       rc = -ENOMEM;<br>       rsp-&gt;hdr.Status = STATUS_INVALID_PARAMETER;<br>       goto err_out;<br>     }</pre><p>Further debugging showed that ksmbd correctly identified the protocol used as SMB 3.1.1, so no dialect confusion here. Going further back, there was a series of checks specifically for SMB 3.1.1:</p><pre>if (conn-&gt;dialect == SMB311_PROT_ID) {<br>     unsigned int nego_ctxt_off = le32_to_cpu(req-&gt;NegotiateContextOffset);<br> ​<br>     if (smb2_buf_len &lt; nego_ctxt_off) {<br>       ...<br>       goto err_out;<br>     }<br>     if (smb2_neg_size &gt; nego_ctxt_off) {<br>       ...<br>       goto err_out;<br>     }<br>     if (smb2_neg_size + le16_to_cpu(req-&gt;DialectCount) * sizeof(__le16) &gt;<br>         nego_ctxt_off) {<br>       ...<br>       goto err_out;<br>     }<br>   } else {<br>     if (smb2_neg_size + le16_to_cpu(req-&gt;DialectCount) * sizeof(__le16) &gt;<br>         smb2_buf_len) {<br>       ...<br>       goto err_out;<br>     }<br>   }</pre><p>If any of those fail, the function returns early without initializing conn-&gt;preauth_info (set to 0). The session stays alive, and an attacker can send a crafted session setup request that triggers the sequence smb2_sess_setup -&gt; generate_preauth_hash -&gt; alloc_preauth_hash and causes the NULL dereference.</p><h3>Wrap-up</h3><p>Overall, it was a great first test of the fuzzer that showed that there’s still a room for even such “dumb” activity as fuzzing over network. I’d really like to thank ZDI and the Linux kernel maintainers for the work involved in getting this vulnerability triaged and patched super quickly!</p><p>There’s more coming as I expand the fuzzer’s scope and capabilities — I hope I’ll be able to share more soon!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f3770ab0bb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Glitch to Die For: One Hacker, One Job, and a Ticking Clock]]></title>
            <link>https://slava-moskvin.medium.com/a-glitch-to-die-for-one-hacker-one-job-and-a-ticking-clock-85d780351148?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/85d780351148</guid>
            <category><![CDATA[glitching]]></category>
            <category><![CDATA[cyberpunk]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[offensive-security]]></category>
            <category><![CDATA[glitch]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Sat, 01 Feb 2025 12:05:19 GMT</pubDate>
            <atom:updated>2025-02-01T12:05:19.479Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bkA9kdJAAoTRMXJViT2qsw.png" /></figure><p>The rain drummed against the window, making the neon sign reflections tremble to its rhythm. Inside, only the flicker of monitors lit the cold room, filled with the scent of tobacco and cheap energy drinks. Alex Hudson thoughtfully smoked a cigarette, bracing for what promised to be a long night. The job was risky, complicated — but paid well.</p><p>The client wanted to crack a crypto wallet with a “forgotten” password, discreetly and efficiently. And above all — urgently. In this day and age, no one wanted to wait, and few could afford to. A background check on the client revealed that failure to unlock the wallet on time could render them financially insolvent due to injuries incompatible with life. The good news was that this hiked up the price. The bad news was that if Alex failed, the client’s problems could quickly become his own.</p><p>But it was worth the gamble. Alex’s past was complicated, and his future uncertain. He was already deep in debt, and around here, that meant only one thing — his odds of ending up in a ditch with a bullet in his head went up exponentially with each passing day. The real problem was that decent gigs had stopped coming Alex’s way. That botched offshore online casino job had done its damage, leaving his reputation in tatters. Since then, he’d been stuck taking whatever scraps he could get.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9_O7uhpnIVzoNsbGIJHCOw.png" /></figure><p>Taking one last drag, Alex put out his cigarette in an overflowing ashtray and snapped back to reality. He was surrounded by monitors displaying the results of a just completed scan of the crypto device’s firmware. The outcome was disappointing: the software wasn’t the latest version, but it was solid. No vulnerabilities in the code from the vendor, nor in the libraries they used.</p><p>Alex cursed under his breath, more out of habit than frustration. He had grown used to his streak of bad luck — once it broke, it never bounced back. He wasn’t expecting any freebies tonight. For a moment, Alex considered calling Case. That sly bastard knew everything there was to know about government backdoors in crypto systems. But after a couple of seconds, he dismissed the thought. Ever since the casino job, Case had been doing his best to pretend Alex didn’t exist — and Alex couldn’t blame him. Survival came first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fg8OJhQeOUcE17pKo-9Jcw.png" /></figure><p>In the end, the device had a rather limited attack surface: a PIN pad for password entry and a USB port for charging and installing updates. Brute-forcing the password was out of the question; the client’s earlier attempts to “remember” it had resulted in the device being locked. The next attempt wouldn’t be allowed for another 47 days, and Alex didn’t have that kind of time.</p><p>Installing an update was a dead end too. All updates were cryptographically signed on the manufacturer’s servers, and the signature was thoroughly verified by the impenetrable device. So, uploading a backdoored firmware that accepted “000000” to unlock the device wasn’t an option. He might have tried to find a vulnerability in the device’s signature verification algorithm, but the developers had patched everything up a couple of years ago after a notorious wave of breaches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YLnTtHXgB8YNPaWO8bTiFg.png" /></figure><p>No, the software was impenetrable. Given the deadline, Alex had better odds of storming Fort Knox armed with a toothpick than finding a vulnerability in the firmware. But what about the hardware? Lighting a second cigarette, he started carefully and methodically disassembling the device. The key was to stay calm and keep his hands steady. Breaking something now would be the most idiotic way to sign his own death warrant.</p><p>Soon, the hacker was staring at the device’s only circuit board. Under the magnifying glass, every component was laid bare. In an attempt to boost profit, the developers had stuffed run-of-the-mill mass-market junk into what was sold as the cutting edge of crypto storage. The chip handling all the crypto turned out to be a basic Synkai32, the kind you could pick up at any electronics shop for pennies.</p><p>The chip was known not just for its low cost but also for its energy efficiency and easy handling. Developers loved it for these reasons. It also lacked any form of glitching protection. And hackers loved it for that.</p><p>Alex didn’t exactly like glitching; he was fascinated by it, sure, but he didn’t like it. Too many variables — then again, that was always the case when dealing with the physical world. And Alex didn’t like the physical world much either.</p><p>He glanced away from the device and over to his messenger window. A single unread message from Finn, his fixer, stared back at him: <em>“Everything on schedule?”</em> With a sigh, Alex quickly typed out a response: <em>“Yes, ready by 5 a.m. just as we agreed.”</em> Damn Finn worked across the globe, seemingly never sleeping, with clients in every conceivable time zone. Demanding clients who didn’t tolerate delays or failures.</p><p>Alex’s eyes flicked to the clock in the corner of the screen. 10:17 p.m. Less than seven hours left. He needed to make a decision. Caffeine, nicotine, and a cocktail of other stimulants surged through his bloodstream, pushing his brain into overdrive. Thoughts ricocheted like pinballs, colliding and bouncing. Overexcited synaptic clusters pulsed, transmitting thousands of ideas per second, tossing them from one zone of his brain to another in a desperate attempt to find the optimal solution. Time was slipping away, and his mind was running out of places to search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vWAyIzLNljpLcV2JiPNF0A.png" /></figure><p>But none of this showed on the outside. Alex, pale-faced and motionless, sat like a statue fused to his chair, staring blankly at the monitor. If someone had walked in, they’d have seen nothing more than a gaunt geek, disconnected from the world, trapped in a trance before the neon glow of the screen. This was his usual state.</p><p>Then, the frantic chaos of cycling through options suddenly gave way to a calm as a simple, harsh truth settled in — there were no options. No money to run. No friends to shelter him. Alex was certain Finn already knew where he lived, and his apartment — nothing more than the cheapest dump he could find — was the farthest thing from a fortress. The walls, flimsy and tired, seemed like they were held up by little more than layers of peeling wallpaper.</p><p>No, the job had to be finished, or he’d be finished. It was as inevitable as gravity. But how? There was only one answer: glitching. Damn glitching.</p><p>“Shit,” Alex muttered. This wasn’t how the job was supposed to go.</p><p>Glitching required specialized hardware. That was the second reason he hated it. 10:30 p.m. — electronics stores were long closed, not that they’d be any help. That kind of gear wasn’t something you could buy off the shelf; it had to be custom-made.</p><p>But there was one saving grace. Some time ago, Alex had been tinkering with electronics and never threw anything away. Chugging the last of his energy drink, he pulled a dust-covered box from under the table and began sifting through it: bundles of wires, a soldering station, a few power supplies, a cheap oscilloscope, several highly capable boards with extensive input-output capabilities, and bags of miscellaneous components. He stared at the pile. Maybe — just maybe — this could work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ReY8gD3Ro75x8hcxSdi8gQ.png" /></figure><p>The next few hours passed in a constant haze of adrenaline. Alex buried himself in work, reading documentation, assembling circuits, and running tests. Tobacco smoke mixed with soldering flux fumes, forming a thick smog beneath the ceiling, but Alex didn’t notice. His mind was consumed by bytes and circuits.</p><p>The Synkai32 ran at an unknown frequency, but its maximum was 200 MHz. Not overly fast — still could be manageable with makeshift tools — barely. Glitching itself was deceptively simple: introduce a fault at just the right moment, and the system’s brain might misfire — ideally bypassing a security check. The glitch could be anything, even a laser pulse. Alex decided on the simplest approach: shorting the power to ground for a brief moment. It was all he could manage with the time he had left.</p><p>To hit the exact instruction in the processor, he needed a timing resolution of 5 nanoseconds. Alex glanced at the electronic junk scattered across his desk. Impossible. He lacked the time, the tools, and the expertise to achieve that kind of precision.</p><p>But this pinpoint precision wasn’t necessary. If he could find a spot in the code where glitching almost any instruction could lead to success, his odds would multiply. In that case, a 50-nanosecond resolution might be good enough.</p><p>It was already 2 a.m., but the stream of cars outside hadn’t slowed. Reflections of neon signs, the street noise, and the pounding in his temples all merged into the rhythm of his overnight marathon. The hardware was nearly ready, but the oscilloscope screen, assuming the cheap Chinese knockoff wasn’t lying, delivered grim news. His makeshift glitcher could manage a resolution of 112 nanoseconds at best, with far-from-ideal electrical characteristics.</p><p>Not even close to what he wanted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2dPUVTYs-MSl7U6DzACDDQ.png" /></figure><p>But even that might be enough if the glitch landed in just the right place at just the right time. Ultimately, everything came down to luck again. The thought made Alex smirk. It was time to write the glitcher software.</p><p>4 a.m. Alex jerked awake. Even the relentless adrenaline of the night hadn’t been enough to hold off exhaustion. He stared at the code on his screen. He’d written it just a couple of hours ago, but now it looked alien, as though someone else had typed it. The letters blurred and jittered before his eyes. A flick of the lighter, a deep drag — and they began to settle. Letters became words, words turned into keywords and variables, and finally, they shaped into lines of code.</p><p>From the lines, the script emerged. It would reboot the crypto wallet, wait for the right signal, then fire the glitch with randomized delay and pulse parameters, hoping to corrupt the processor’s instructions enough to bypass the PIN verification. Then it would reboot and repeat. Over and over. Until it worked — or until he ran out of time.</p><p>But what the hell was the “right signal”? Alex turned to the other monitor, where the disassembled firmware and microchip documentation glared back at him. He’d have to comb through the boot logs to try to pinpoint the exact moment the device read the PIN hash from secure memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*coexSAhu-D1FVqp3ZJ2csQ.png" /></figure><p>A couple of cigarettes later, the script was running, spitting out mountains of green text and boot logs from the crypto wallet. Everything seemed to be working as expected — for the wallet, at least. Once the script hit a glitch that disrupted the device’s normal operation, the terminal output was supposed to turn red. After that, it would narrow the parameter range and bypass the security measures in minutes. Or maybe hours. Or it might crash halfway through, because Alex hadn’t had time to debug it properly. His only hope was damn luck.</p><p>Half an hour to the deadline. It was time to think about Plan B. But not this time. He’d gotten himself into a corner. Plan B might have been an option a few jobs ago. Now there was no room for error. There was only way to help himself: buy a little more time. Alex began setting up a cell signal jammer.</p><p>4:50 a.m. The screech of tires in the alley snapped his attention to the surveillance monitor. A fully blacked-out sedan — one he hadn’t seen before. Not good. The script was still running, but the screen remained a sea of green. Not a single red line in sight.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PU1oymUw9srjoC0ifUWmNA.png" /></figure><p>05:00. Two thugs jumped out of the sedan, stormed up the stairs, and smashed through the apartment door with a deafening crash.</p><p>“Well, hello there, hacker,” sneered the first thug, clearly the leader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K1K8T65W9F3XtLnNdlOgBQ.png" /></figure><p>“Take it easy, guys. The transaction’s already on its way — check with Finn,” Alex said, raising his hands and forcing his best smile. It wasn’t convincing. In response, the cold barrel of a gun pressed against his forehead. He could smell the metallic tang of gun oil.</p><p>“Just ask him. Pull the trigger, and the transaction gets interrupted by a signal from my biomonitor. At the same time, my script wipes my entire network, including the private key to the crypto.” He silently prayed to every god he could think of, as he hoped they wouldn’t see through his bluff.</p><p>The leader gave a signal. His accomplice raised a phone to his ear. The air turned heavy with tension. Alex felt stuck in place, as though his muscles had locked up completely. He was frozen — like a deer caught in the headlights. Helpless, unable to act, unable to save his own life.</p><p>A bead of sweat dripped onto his sneaker. It would be so stupid if everything ended now — so painfully early. So much left undone… All those battles fought, only to die here, on a stupid gig. One twitch of a finger, and no one would even remember he’d ever existed.</p><p>“No reception,” said the thug with the phone.</p><p>“What kind of bullshit is this?! Think this is a joke?!” The leader’s fist drove into Alex’s stomach, folding him in half with a burst of pain.</p><p>“Problems… with the signal… in the house,” Alex gasped, clutching his stomach as he writhed in pain, trying to catch his breath.</p><p>“Call from outside,” the leader ordered, his glare fixed on Alex. “One more stunt, and your brains will paint the wall. Got it?”</p><p>Alex nodded vigorously. A brief reprieve — 30 seconds at most, if that.</p><p>A glance at the screen. Suddenly, the green wall of logs was interrupted by a flash of red — glitch parameters found! The script moved to the next phase, attempting to exploit the glitch and bypass the PIN code verification. Late-night coding sessions fueled by stimulants never ended well. A single mistake in the script could ruin everything.</p><p>Even if the code was flawless, success wasn’t guaranteed. No one could say how long it would take — or if it would work at all.</p><p>“Boss says if the job ain’t done… you know how it goes,” said the thug, gesturing to the leader.</p><p>“It’s… done,” Alex croaked, barely audible. “The device… unlocked.” He had no clue if the script had actually finished.</p><p>The leader holstered his gun and scanned the room. His eyes landed on the pile of electronics on the desk, with the crypto wallet perched at the top. He leaned over the device, examining it closely without taking his eyes off the screen. On the tiny screen, words from the recovery phrase scrolled past, already streaming through the wires to Finn. Finally, the thug nodded, disconnected the device, and slipped it into his pocket.</p><p>“Lucky you,” the leader smirked before slamming his fist into Alex’s cheek, sending him crashing to the floor. “Call it a service fee.”</p><p>After delivering a few more “reminder” kicks, the pair left. Moments later, Alex heard the roar of an engine and the squeal of tires fading into the distance.</p><p>Groaning in pain, Alex rolled onto his back, still lying on the floor. Patting his pockets, he pulled out a crumpled pack of cigarettes. Only one had survived the boots Finn’s goons. A flick of his lighter lit his battered face with a shaky flame.</p><p>Taking a long drag, Alex watched the sun rise over the city rooftops. One thought burned in his mind: <em>“Never again.”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xI4UYLsHyAe3nTc0xt96xA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85d780351148" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linux Kernel Attack Surface: beyond IOCTL. DMA-BUF]]></title>
            <link>https://slava-moskvin.medium.com/linux-kernel-attack-surface-beyond-ioctl-dma-buf-f1819a4e831c?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1819a4e831c</guid>
            <category><![CDATA[kernel]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[information-security]]></category>
            <category><![CDATA[infosec]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Thu, 26 Dec 2024 19:36:34 GMT</pubDate>
            <atom:updated>2024-12-26T19:36:34.448Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrXqK3cyTf7Kt2MTLgOQDw.png" /></figure><h3>Linux Kernel Attack Surface: beyond IOCTL. DMA-BUF</h3><p>When analyzing Linux kernel module code, I often look for instances of copy_from_user or jump straight to the IOCTL handler. This is a valid approach; however, I have always wondered if there’s more to it—whether a kernel module without any copy_from_user calls could still have an attack surface reachable from user mode. And the answer is yes.</p><p>In this post, we will discuss the Linux kernel module attack surface that doesn’t involve a single copy_from_user, using DMA buffer drivers as an example. Hopefully, in future posts in this series, we will analyze other subsystems.</p><h3>The Usual Suspects</h3><p>But first, let’s address the usual attack surfaces that could be considered trivial:</p><ul><li>IOCTL handlers: Probably the most popular way of implementing a communication channel with user space. These can be found by locating the handler in the file_operations structure (a pointer to the structure is usually an argument to the cdev_init function) or by searching for something like &quot;ioctl&quot; in the function list.</li><li>Handlers for procfs, sysfs, and debugfs entries: For example, handlers for procfs operations are usually contained within the proc_create structure, which is used as an argument for the proc_create function.</li><li>copy_from_user: This is used to copy data from user-space memory to kernel-space memory, ensuring that the kernel doesn’t directly access potentially invalid or malicious user-space pointers. As mentioned earlier, going through the list of usages of this function is a good way to analyze a module. All the usual suspects above use copy_from_user.</li></ul><h3>DMA-BUF</h3><p>The <strong>DMA-BUF (Direct Memory Access Buffer)</strong> is a Linux kernel framework designed to efficiently share memory buffers across different devices and subsystems. It allows buffers backed by physical memory to be shared seamlessly. For example:</p><ol><li>A user-mode program or driver allocates a buffer containing graphical information to perform rendering.</li><li>The buffer is passed to the GPU driver.</li><li>After rendering is complete, the same buffer holds the result, which the client can then display on the screen.</li></ol><p>As DMA buffers are often used with devices and/or to pass data between code running with different privileges (e.g. to/from a hypervisor, Secure Monitor, etc.), you can often find proprietary, vendor-specific implementations created for particular devices.</p><p>Creating a DMA-BUF to be used by other modules and user-mode applications is called <em>exporting</em>. An exporter kernel module must create a communication interface (which can be an IOCTL-based, implementation-specific interface) and implement <a href="https://docs.kernel.org/driver-api/dma-buf.html#c.dma_buf_ops">struct dma_buf_ops</a> for the buffer.</p><p>Let’s take a look at the structure:</p><pre>struct dma_buf_ops {<br>     bool cache_sgt_mapping;<br>     int (*attach)(struct dma_buf *, struct dma_buf_attachment *);<br>     void (*detach)(struct dma_buf *, struct dma_buf_attachment *);<br>     int (*pin)(struct dma_buf_attachment *attach);<br>     void (*unpin)(struct dma_buf_attachment *attach);<br>     struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *, enum dma_data_direction);<br>     void (*unmap_dma_buf)(struct dma_buf_attachment *,struct sg_table *, enum dma_data_direction);<br>     void (*release)(struct dma_buf *);<br>     int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);<br>     int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);<br>     int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);<br>     int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);<br>     void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);<br> };</pre><p>Callbacks in the structure are executed when various actions are performed on the buffer.</p><p>The most interesting callback is, of course, mmap, which is triggered when userspace executes the corresponding syscall. The callback&#39;s task is to perform all the necessary operations to memory-map the buffer and fill the <a href="https://elixir.bootlin.com/linux/v6.12.1/source/include/linux/mm_types.h#L667">vma structure</a>. What’s particularly interesting is that the exporter can define its own implementation of <a href="https://github.com/torvalds/linux/blob/master/include/linux/mm.h#L610">struct vm_operations_struct</a>:</p><pre>vma-&gt;vm_ops = &amp;vm_operations;</pre><p>This also represents an attack surface. <a href="https://labs.bluefrostsecurity.de/blog/cve-2023-2008.html">This blogpost</a> from Blue Frost Security covers a vulnerability in <strong>udmabuf</strong>’s page fault handler, which was the only custom operation in the vm_operations_struct:</p><pre>static const struct vm_operations_struct udmabuf_vm_ops = {<br>   .fault = udmabuf_vm_fault,<br> };</pre><p>The root cause was that “vm_fault-&gt;pgoff was used without verifying whether it was within the bounds of the pagesarray.&quot; And the user has control over the page offset!</p><pre>static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) {<br>   --snip--<br>     vmf-&gt;page = ubuf-&gt;pages[vmf-&gt;pgoff];<br>   --snip--<br>     get_page(vmf-&gt;page);<br>     return 0;<br> }</pre><p>In the end, they managed to gain a reference to an arbitrary page, which led to local privilege escalation. And with that, we’ve added another entry to our Linux attack surface list.</p><h3>DMA-BUF Heap</h3><p>DMA-BUF heap is a small Linux subsystem that provides a unified interface for working with DMA-bufs. Each DMA-BUF heap is implemented by a separate kernel module (including default implementations) and has a separate character device.</p><p>The kernel subsystem has to implement just one IOCTL: DMA_HEAP_IOCTL_ALLOC, which is used—you guessed it—to allocate a DMA-buf. It takes the following structure as an argument:</p><pre>struct dma_heap_allocation_data {<br>   __u64 len;<br>   __u32 fd;<br>   __u32 fd_flags;<br>   __u64 heap_flags;<br> };</pre><p>In the DMA_HEAP_IOCTL_ALLOC dispatcher, the kernel calls dma_heap_buffer_alloc.</p><pre>static long dma_heap_ioctl_allocate(struct file *file, void *data)<br> {<br> --snip--<br>   fd = dma_heap_buffer_alloc(heap, heap_allocation-&gt;len,<br>            heap_allocation-&gt;fd_flags,<br>            heap_allocation-&gt;heap_flags);</pre><p>Which, in turn, invokes the allocate callback from the heap-&gt;ops structure.</p><pre>static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,<br>          unsigned int fd_flags,<br>          unsigned int heap_flags)<br> {<br>   /*<br>    * Allocations from all heaps have to begin<br>    * and end on page boundaries.<br>    */<br>   len = PAGE_ALIGN(len);<br>   if (!len)<br>     return -EINVAL;<br>   return heap-&gt;ops-&gt;allocate(heap, len, fd_flags, heap_flags);<br> }</pre><p>Yep, we’re back to dealing with operations. The struct dma_heap_ops contains only one member: allocate. This is the function that a kernel module implementing a DMA-buf heap needs to define. The user controls the len, fd_flags, and heap_flags arguments.</p><pre>struct dma_heap_ops {<br>   struct dma_buf *(*allocate)(struct dma_heap *heap,<br>             unsigned long len,<br>             u32 fd_flags,<br>             u64 heap_flags);<br> };</pre><p>This means that the allocate operation is also part of the attack surface.</p><p>DMA-BUF heaps sometimes include their own DMA-BUF implementations, which can also contain vulnerabilities. For example, <a href="https://lore.kernel.org/linux-cve-announce/2024092742-CVE-2024-46852-91a5@gregkh/">CVE-2024–46852</a> was an off-by-one in the CMA heap’s fault handler. This flaw allowed mappings larger than the buffer size via mremap, bypassing overflow checks and potentially leading to unauthorized memory access.</p><h3>Conclusion</h3><p>The DMA-BUF and DMA-BUF heap attack surface is not huge, but as we’ve seen, it could still be a source of real bugs. And in this day and age, when finding vulnerabilities is becoming increasingly difficult, exploring less obvious attack vectors turns out to be more and more lucrative, but also undeniably fascinating for researchers like us.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1819a4e831c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding Bugs in Kernel. Part 2: Fuzzing the Actual Kernel]]></title>
            <link>https://slava-moskvin.medium.com/finding-bugs-in-kernel-part-2-fuzzing-the-actual-kernel-4c2ee3785d96?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c2ee3785d96</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[offensive-security]]></category>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[kernel]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Tue, 19 Nov 2024 12:02:40 GMT</pubDate>
            <atom:updated>2024-11-20T21:25:34.593Z</atom:updated>
            <content:encoded><![CDATA[<h3>Finding Bugs in Kernel with syzkaller. Part 2: Fuzzing the Actual Kernel</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PRxUmDi5BCrJ2h5x2UGfqA.png" /></figure><p>In the <a href="https://medium.com/@slava-moskvin/fuzzing-the-kernel-with-syzkaller-part-1-setting-up-on-mac-and-crashing-a-vulnerable-driver-b2a3949ea575">previous post</a>, we had a crash course on <strong>syzkaller</strong>, one of the most renowned Linux kernel fuzzers. We explored how to set it up in a non-trivial configuration (on macOS, no less — but the general steps also apply to Linux, of course), compile the kernel with a vulnerable driver, and configure everything for syzkaller to crash it. Now it’s time to target real-world code. So, what code?</p><p>syzkaller is constantly grinding on Google’s servers, relentlessly transforming kilowatts of power into test cases and crashes. Unfortunately, I don’t have an unlimited supply of electricity or a server budget, for that matter. So, I need to be more deliberate in choosing my target.</p><p>Thankfully, Google hosts <a href="https://syzkaller.appspot.com/upstream">syzbot</a>, a syzkaller dashboard that displays the current fuzzing status, findings, and, most importantly, coverage for Linux subsystems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QgZep0Za7PTM6IblKm4LHA.png" /></figure><p>We can use the latter to our advantage to find targets with less-than-ideal coverage. What qualifies as “less than ideal”? I don’t know, but here goes nothing:</p><ul><li><strong>More than 0%</strong>: If something isn’t covered at all, perhaps there’s a good reason for it — like the subsystem doesn’t directly consume user input or has hardware requirements that are hard to meet in a fuzzing environment.</li><li><strong>Less than, say, 15–20%</strong>: so it’s not super explored as well.</li></ul><h3>Candidates</h3><p>With that in mind, I started browsing the <a href="https://syzkaller.appspot.com/upstream/graph/coverage_subsystems_heatmap?period=month">syzbot coverage heatmap</a>. The heatmap is a massive web page — 128 MB of pure HTML. Parsing it to filter subsystems by coverage would have been a good idea, but it was already too late at night for good ideas. So, I went with the good old “read-it-to-the-end” approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A-GIqfaailD3kxtMR0YZkA.png" /></figure><p>Here are some of my notes:</p><ul><li><strong>jffs2 (~50%)</strong>, <strong>fs/jffs2 (0%)</strong> — A suspicious combination. Perhaps there’s something worth exploring here?</li><li><strong>bcachefs, fs/bcachefs — 10%</strong></li><li><strong>net/atm — 1%</strong></li><li><strong>net/dsa — 2%</strong></li><li><strong>net/smc — 19%</strong></li><li><strong>net/sunrpc — 13%</strong></li><li><strong>nfs, fs/nfs — 37k blocks, 1%</strong> — Why so low?</li><li><strong>net/tipc — 49%</strong> — High coverage, but still interesting due to a <a href="https://sam4k.com/zdi-24-821-a-remote-use-after-free-in-the-kernels-net-tipc/">recent UAF found in it</a>.</li><li><strong>orangefs — 2%</strong></li><li>Lots of other *fs subsystems are well under 10%.</li><li><strong>fs/ubifs — 17k blocks, 0%!</strong></li><li><strong>scsi? — 60k blocks, 7%</strong> — Why so low? Is it hardware-dependent?</li><li><strong>usb — 8%</strong>, <strong>usb-storage — 10%</strong></li><li><strong>virt/drivers — 14%</strong></li><li><strong>wireless/drivers — 1%</strong> — This will surely require some hacks.</li></ul><p>An interesting idea is to fuzz hardware-dependent functions like <strong>MMC</strong>, <strong>PCI</strong>, <strong>NFC</strong>, <strong>pvrusb2</strong>, <strong>qat</strong>, <strong>RAID</strong>, <strong>RDMA</strong>, etc.</p><p>In the end, I decided to try two network subsystems: <a href="https://docs.kernel.org/networking/tipc.html">TIPC</a> (despite its high coverage, I was motivated by <a href="https://sam4k.com/zdi-24-821-a-remote-use-after-free-in-the-kernels-net-tipc/">the recentfindings from sam4k</a>) and <a href="https://www.ibm.com/docs/en/linux-on-systems?topic=n-smc-protocol-support-1">SMC</a>.</p><p>Also, networking is much easier to fuzz: data flows nicely in and out through sockets, and everything can be configured with a few ioctl and setsockopt calls. Compare that to filesystems, where you have to handle binary blobs, mount and unmount them, or to other subsystems that might depend on specific hardware.</p><h3>Attack Surface</h3><p>Now it’s time to read some kernel code to figure out the attack surface. I’ll focus exclusively on TIPC from here on, though SMC is practically the same deal. The TIPC code can be found under <a href="https://github.com/torvalds/linux/tree/master/net/tipc">net/tipc/</a>.</p><p>TIPC’s Netlink legacy compatibility layer is initialized in the tipc_netlink_compat_start function. This function registers a Generic Netlink family, tipc_genl_compat_family, with the operations structure tipc_genl_compat_ops:</p><pre>static const struct genl_small_ops tipc_genl_compat_ops[] = {<br> {<br>  .cmd  = TIPC_GENL_CMD,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit  = tipc_nl_compat_recv,<br> },<br>};</pre><p>From the code, we can see that there’s essentially one operation callback: tipc_nl_compat_recv, which receives Netlink messages. The main dispatcher for these messages is the tipc_nl_compat_handle function, which uses a switch statement to handle TIPC_CMD_* commands.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*gosOqvRpfPvPjKmMv6j8sA.png" /></figure><p>That was the legacy Netlink support, but what about the newer version? We can observe a similar code flow in <a href="https://github.com/torvalds/linux/blob/master/net/tipc/netlink.c">net/tipc/netlink.c</a>. The tipc_netlink_start function registers the tipc_genl_family with an operations array, tipc_genl_v2_ops.</p><p>In this newer implementation, all operations are directly exposed rather than being hidden behind a single function:</p><pre>static const struct genl_ops tipc_genl_v2_ops[] = {<br> {<br>  .cmd = TIPC_NL_BEARER_DISABLE,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_bearer_disable,<br> },<br> {<br>  .cmd = TIPC_NL_BEARER_ENABLE,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_bearer_enable,<br> },<br> {<br>  .cmd = TIPC_NL_BEARER_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_bearer_get,<br>  .dumpit = tipc_nl_bearer_dump,<br> },<br> {<br>  .cmd = TIPC_NL_BEARER_ADD,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_bearer_add,<br> },<br> {<br>  .cmd = TIPC_NL_BEARER_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_bearer_set,<br> },<br> {<br>  .cmd = TIPC_NL_SOCK_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .start = tipc_dump_start,<br>  .dumpit = tipc_nl_sk_dump,<br>  .done = tipc_dump_done,<br> },<br> {<br>  .cmd = TIPC_NL_PUBL_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT |<br>       GENL_DONT_VALIDATE_DUMP_STRICT,<br>  .dumpit = tipc_nl_publ_dump,<br> },<br> {<br>  .cmd = TIPC_NL_LINK_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT,<br>  .doit   = tipc_nl_node_get_link,<br>  .dumpit = tipc_nl_node_dump_link,<br> },<br> {<br>  .cmd = TIPC_NL_LINK_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_node_set_link,<br> },<br> {<br>  .cmd = TIPC_NL_LINK_RESET_STATS,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit   = tipc_nl_node_reset_link_stats,<br> },<br> {<br>  .cmd = TIPC_NL_MEDIA_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_media_get,<br>  .dumpit = tipc_nl_media_dump,<br> },<br> {<br>  .cmd = TIPC_NL_MEDIA_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_media_set,<br> },<br> {<br>  .cmd = TIPC_NL_NODE_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .dumpit = tipc_nl_node_dump,<br> },<br> {<br>  .cmd = TIPC_NL_NET_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .dumpit = tipc_nl_net_dump,<br> },<br> {<br>  .cmd = TIPC_NL_NET_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_net_set,<br> },<br> {<br>  .cmd = TIPC_NL_NAME_TABLE_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .dumpit = tipc_nl_name_table_dump,<br> },<br> {<br>  .cmd = TIPC_NL_MON_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_node_set_monitor,<br> },<br> {<br>  .cmd = TIPC_NL_MON_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_node_get_monitor,<br>  .dumpit = tipc_nl_node_dump_monitor,<br> },<br> {<br>  .cmd = TIPC_NL_MON_PEER_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT |<br>       GENL_DONT_VALIDATE_DUMP_STRICT,<br>  .dumpit = tipc_nl_node_dump_monitor_peer,<br> },<br> {<br>  .cmd = TIPC_NL_PEER_REMOVE,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_peer_rm,<br> },<br>#ifdef CONFIG_TIPC_MEDIA_UDP<br> {<br>  .cmd = TIPC_NL_UDP_GET_REMOTEIP,<br>  .validate = GENL_DONT_VALIDATE_STRICT |<br>       GENL_DONT_VALIDATE_DUMP_STRICT,<br>  .dumpit = tipc_udp_nl_dump_remoteip,<br> },<br>#endif<br>#ifdef CONFIG_TIPC_CRYPTO<br> {<br>  .cmd = TIPC_NL_KEY_SET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_node_set_key,<br> },<br> {<br>  .cmd = TIPC_NL_KEY_FLUSH,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_node_flush_key,<br> },<br>#endif<br> {<br>  .cmd = TIPC_NL_ADDR_LEGACY_GET,<br>  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,<br>  .doit = tipc_nl_net_addr_legacy_get,<br> },<br>};</pre><p>The control flow is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*PAnZbQEDaKD_2s8NdaUejw.png" /></figure><p>In <a href="https://github.com/torvalds/linux/blob/master/net/tipc/socket.c">net/tipc/socket.c</a> we have protocol operations for different socket families (message, socket, stream). TIPC uses the same set of callbacks for all of them.</p><pre>static const struct proto_ops msg_ops = {<br> .owner  = THIS_MODULE,<br> .family  = AF_TIPC,<br> .release = tipc_release,<br> .bind  = tipc_bind,<br> .connect = tipc_connect,<br> .socketpair = tipc_socketpair,<br> .accept  = sock_no_accept,<br> .getname = tipc_getname,<br> .poll  = tipc_poll,<br> .ioctl  = tipc_ioctl,<br> .listen  = sock_no_listen,<br> .shutdown = tipc_shutdown,<br> .setsockopt = tipc_setsockopt,<br> .getsockopt = tipc_getsockopt,<br> .sendmsg = tipc_sendmsg,<br> .recvmsg = tipc_recvmsg,<br> .mmap  = sock_no_mmap,<br>};</pre><p>These Netlink commands, along with the usual socket operations, constitute our attack surface.</p><p>As you remember, TIPC is already covered by syzkaller. Of course, some of the commands supported by the subsystem are already defined in the fuzzer’s files: <a href="https://github.com/google/syzkaller/blob/master/sys/linux/socket_tipc.txt">syzkaller/sys/linux/socket_tipc.txt</a> and <a href="https://github.com/google/syzkaller/blob/master/sys/linux/socket_tipc_netlink.txt">syzkaller/sys/linux/socket_tipc_netlink.txt</a>.</p><p>I hoped to find discrepancies between the actual attack surface and the syscalls already defined in syzkaller. I did find one, but unfortunately, it was just a single syscall: setsockopt for the TIPC_NODELAY option.</p><p>Here’s how I described it:</p><pre>setsockopt$TIPC_NODELAY(fd sock_tipc, level const[SOL_TIPC], opt const[TIPC_NODELAY], val ptr[in, int32], len bytesize[val])</pre><p>Below are the syscalls I ended up enabling in my syzkaller configuration:</p><pre>&quot;enable_syscalls&quot;: [<br>        &quot;socket$tipc&quot;,<br>        &quot;socketpair$tipc&quot;,<br>        &quot;bind$tipc&quot;,<br>        &quot;connect$tipc&quot;,<br>        &quot;accept4$tipc&quot;,<br>        &quot;getsockname$tipc&quot;,<br>        &quot;getpeername$tipc&quot;,<br>        &quot;sendmsg$tipc&quot;,<br>        &quot;ioctl$SIOCGETLINKNAME&quot;,<br>        &quot;ioctl$SIOCGETNODEID&quot;,<br>        &quot;setsockopt$TIPC_IMPORTANCE&quot;,<br>        &quot;setsockopt$TIPC_SRC_DROPPABLE&quot;,<br>        &quot;setsockopt$TIPC_DEST_DROPPABLE&quot;,<br>        &quot;setsockopt$TIPC_CONN_TIMEOUT&quot;,<br>        &quot;setsockopt$TIPC_MCAST_BROADCAST&quot;,<br>        &quot;setsockopt$TIPC_MCAST_REPLICAST&quot;,<br>        &quot;setsockopt$TIPC_GROUP_LEAVE&quot;,<br>        &quot;setsockopt$TIPC_GROUP_JOIN&quot;,<br>        &quot;getsockopt$TIPC_IMPORTANCE&quot;,<br>        &quot;getsockopt$TIPC_SRC_DROPPABLE&quot;,<br>        &quot;getsockopt$TIPC_DEST_DROPPABLE&quot;,<br>        &quot;getsockopt$TIPC_CONN_TIMEOUT&quot;,<br>        &quot;getsockopt$TIPC_NODE_RECVQ_DEPTH&quot;,<br>        &quot;getsockopt$TIPC_SOCK_RECVQ_DEPTH&quot;,<br>        &quot;getsockopt$TIPC_GROUP_JOIN&quot;,<br>        &quot;sendmsg$TIPC_CMD_SET_LINK_TOL&quot;,<br>        &quot;sendmsg$TIPC_CMD_SET_LINK_PRI&quot;,<br>        &quot;sendmsg$TIPC_CMD_SET_LINK_WINDOW&quot;,<br>        &quot;sendmsg$TIPC_CMD_ENABLE_BEARER&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_BEARER_NAMES&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_MEDIA_NAMES&quot;,<br>        &quot;sendmsg$TIPC_CMD_SHOW_PORTS&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_REMOTE_MNG&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_MAX_PORTS&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_NETID&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_NODES&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_LINKS&quot;,<br>        &quot;sendmsg$TIPC_CMD_SET_NODE_ADDR&quot;,<br>        &quot;sendmsg$TIPC_CMD_SHOW_NAME_TABLE&quot;,<br>        &quot;sendmsg$TIPC_CMD_SHOW_LINK_STATS&quot;,<br>        &quot;sendmsg$TIPC_CMD_GET_MEDIA_NAMES&quot;,<br>        &quot;sendmsg$TIPC_CMD_DISABLE_BEARER&quot;,<br>        &quot;sendmsg$TIPC_CMD_RESET_LINK_STATS&quot;,<br>        &quot;sendmsg$TIPC_CMD_SET_NETID&quot;,<br>        &quot;socket$nl_generic&quot;,<br>        &quot;syz_genetlink_get_family_id$tipc&quot;,<br>        &quot;listen$tipc&quot;,<br>        &quot;recvmsg$tipc&quot;,<br>        &quot;shutdown$tipc&quot;,<br>        &quot;close$tipc&quot;,<br>        &quot;ppoll$tipc&quot;,<br>        &quot;getsockopt$TIPC_SOCK_RECVQ_USED&quot;,<br>        &quot;syz_genetlink_get_family_id$tipc2&quot;,<br>        &quot;sendmsg$TIPC_NL_BEARER_DISABLE&quot;,<br>        &quot;sendmsg$TIPC_NL_BEARER_ENABLE&quot;,<br>        &quot;sendmsg$TIPC_NL_BEARER_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_BEARER_ADD&quot;,<br>        &quot;sendmsg$TIPC_NL_BEARER_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_SOCK_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_PUBL_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_LINK_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_LINK_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_LINK_RESET_STATS&quot;,<br>        &quot;sendmsg$TIPC_NL_MEDIA_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_MEDIA_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_NODE_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_NET_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_NET_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_NAME_TABLE_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_MON_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_MON_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_MON_PEER_GET&quot;,<br>        &quot;sendmsg$TIPC_NL_PEER_REMOVE&quot;,<br>        &quot;sendmsg$TIPC_NL_UDP_GET_REMOTEIP&quot;,<br>        &quot;sendmsg$TIPC_NL_KEY_SET&quot;,<br>        &quot;sendmsg$TIPC_NL_KEY_FLUSH&quot;<br>      ]</pre><h3>Performance Improvements</h3><p>The default way of using syzkaller is with QEMU: syzkaller spins up and manages as many QEMU instances as you need. As you know, QEMU can operate in two modes: pure emulation (<a href="https://www.qemu.org/docs/master/devel/index-tcg.html">TCG mode</a>) or <a href="https://www.qemu.org/docs/master/system/introduction.html">accelerated mode</a>, which leverages a hypervisor (like KVM on Linux) for better performance.</p><p>But macOS doesn’t have KVM. Thankfully, another accelerator is available: HVF (Apple’s Hypervisor Framework). The only thing I had to do was change the QEMU options to make them compatible with HVF.</p><p>Anyone who has worked with complex QEMU VMs knows how “easy” and “intuitive” the configuration process is. So after some trial and error, I managed to adapt the default configuration:</p><pre>qemu-system-aarch64 -m 2048 -smp 2 -chardev socket,id=SOCKSYZ,server=on,wait=off,host=localhost,port=42150 -mon chardev=SOCKSYZ,mode=control -display none -serial stdio -no-reboot -name VM-2 -device virtio-rng-pci -machine virt,virtualization=on,gic-version=max -cpu max,sve=off,pauth=off -accel tcg,thread=multi -device virtio-net-pci,netdev=net0 -netdev user,id=net0,restrict=on,hostfwd=tcp:127.0.0.1:18538-:22 -hda linux_kernel/rootfs.ext3 -snapshot -kernel linux_kernel/Image -append root=/dev/vda console=ttyAMA0 console=ttyAMA0 root=/dev/vda</pre><p>to these parameters, which finally worked with HVF:</p><pre>qemu-system-aarch64 -m 2048 -smp 2 -chardev socket,id=SOCKSYZ,server=on,wait=off,host=localhost,port=63361 -mon chardev=SOCKSYZ,mode=control -display none -serial stdio -no-reboot -name VM-0 -device virtio-rng-pci -machine virt -accel hvf -device virtio-net-pci,netdev=net0 -netdev user,id=net0,restrict=on,hostfwd=tcp:127.0.0.1:32746-:22 -hda linux_kernel/rootfs.ext3 -snapshot -kernel linux_kernel/Image-6.10.8 -append &quot;debug earlyprintk=serial slub_debug=UZ console=ttyAMA0 root=/dev/vda&quot; -cpu cortex-a57</pre><p>syzkaller allows you to specify additional QEMU options, but unfortunately, those platform-dependent options are hardcoded. Since, realistically, nobody but me is likely occupied with running syzkaller on macOS, I simply patched the options directly in the code and called it a day.</p><p>In the end, switching from TCG to HVF made the setup 3–5 times faster with the same configuration (number of VMs, processes, and RAM), so I was pretty happy with this optimization.</p><h3>Config and build</h3><p>If you’re new in this whole building the kernel thing, check out my <a href="https://medium.com/@slava-moskvin/kernel-quest-from-brain-surgery-to-bug-hunting-with-kasan-0d276383f308">previous post</a>. Here, the proccess is the same, we just need to enable TIPC and SMC with a couple of dependencies so they’re built into the kernel.</p><pre>make ARCH=arm64 defconfig<br>scripts/config -e KCOV -e KCOV_INSTRUMENT_ALL -e DEBUG_FS -e NET_9P -e NET_9P_VIRTIO -e TIPC -e KCOV_ENABLE_COMPARISONS -e KALLSYMS -e KALLSYMS_ALL -e DEBUG_INFO -e KASAN -d RANDOMIZE_BASE -d RANDOMIZE_MEMORY -e SMC -e INET -e INFINIBAND</pre><p>Then, as usual:</p><pre>make ARCH=arm64 oldconfig<br>make ARCH=arm64 -j $(nproc)</pre><h3>Fuzzing</h3><p>It’s always helps to lower your expectations when fuzzing the kernel. So, as usual, I just started the campaign, made sure it was running smoothly, and went back to sleep. And yes, let me emphasize that under no circumstances did I check on the fuzzer in the middle of the night 😅.</p><p>The next morning, I was greeted with this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DGP5rFNNGMU7-tuGwMN8w.png" /></figure><p>As you can see, those “KASAN: slab-use-after-free” messages look pretty promising. Nice!</p><p>Well, not so fast. Automatic reproduction failed, so I started by digging into the crash logs. The call trace already looked suspicious because none of the listed functions were explicitly part of TIPC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Shwp_PIDt9FJUdWBhZ_5ag.png" /></figure><p>After reading <a href="https://marliere.net/posts/1/">several articles</a> and <a href="https://github.com/google/syzkaller/blob/master/docs/reproducing_crashes.md">docs</a> on reproducing syzkaller crashes, I managed to get my head around it. Unfortunately, the bugs just refused to trigger.</p><p>You’ll notice there are plenty of bug reports on the <a href="https://syzkaller.appspot.com/upstream">syzbot page</a> marked as inconclusive. I guess I can classify my crashes the same way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7wIENyk3EhxvfY5QDc-5Q.png" /></figure><p>Oh well, time to move on to the next target. But that’s a story for another time.</p><p>Thanks for reading, and let’s stay in touch!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c2ee3785d96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fuzzing the Kernel with syzkaller. Part 1: Setting up on Mac and Crashing a Vulnerable Driver]]></title>
            <link>https://slava-moskvin.medium.com/fuzzing-the-kernel-with-syzkaller-part-1-setting-up-on-mac-and-crashing-a-vulnerable-driver-b2a3949ea575?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2a3949ea575</guid>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[offensive-security]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[kernel]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 14:31:26 GMT</pubDate>
            <atom:updated>2024-09-17T14:31:26.844Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pi0EQAVjbk18wvnL_QfoWg.png" /></figure><p>syzkaller is one of the best fuzzers for the Linux kernel. It supports coverage (through KCOV) and provides a way to declaratively describe syscalls you want to fuzz. It also highly encourages you to use KASAN (and you can even use more sanitizers supported by the kernel). We got to know KASAN in the <a href="https://slava-moskvin.medium.com/kernel-quest-from-brain-surgery-to-bug-hunting-with-kasan-0d276383f308">previous post</a> and it&#39;s a really good thing that allows you to catch more bugs during fuzzing.</p><p>Linux has around <a href="https://github.com/torvalds/linux/blob/v6.11-rc6/arch/x86/entry/syscalls/syscall_64.tbl">450 syscalls</a>, which at first glance might not seem like a lot of attack surface to fuzz. But actually, the attack surface is huge because those syscalls provide you with communication channels to numerous kernel subsystems. Even the simplest syscall open has a different implementation for every filesystem driver, every node under /dev, and so on. That&#39;s why syzkaller&#39;s <a href="https://github.com/google/syzkaller/tree/master/sys/linux">system call descriptions</a>, although vast, still don&#39;t cover every last bit of the attack surface.</p><p>Another good thing about syzkaller is that, unlike a lot of tools out there, it runs not just on the author&#39;s machine, but actually, while the setup is not the simplest, it works perfectly out of the box. The docs even tell you how to set up fuzzing not only for x86_64, but also for arm/arm64, riscv64, and s390x.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*TgWfZiI-u4-IxGRYTUd82g.png" /></figure><p>That is, if you’re using Linux to host fuzzing virtual machines. I, in my typical manner, didn’t strive for an easy way out; I wanted to my use macbook to fuzz as it was the most powerful machine in my house and because I simply liked working on it. That was a part of the problem. Another issue was that while syzkaller <em>supports</em> fuzzing kernel modules, my impression is that it&#39;s definetely not the first class citizen and the tool focuses on fuzzing the mainline kernel (at understandably so). In my opinion, third-party kernel modules can present a good target for security research because many of them are not widely adopted and present less incentive for security researchers out there. So, it looked to me like a perfect start for getting into kernel fuzzing.</p><h3>Setting up vanila syzkaller</h3><p>The first step is to set up syzkaller with default syscall descriptions, just to make sure everything works. My Mac has an arm64 CPU, so I’m going to need to compile everything for this architecture, and I also need a Linux VM to compile the kernel and build a root filesystem.</p><p>Thankfully, I could just follow this <a href="https://github.com/google/syzkaller/blob/master/docs/linux/setup_linux-host_qemu-vm_arm64-kernel.md">guide from the docs</a> to build a rootfs with buildroot and compile the kernel. The only small change is that we don&#39;t need to use cross-compilation and can just compile natively. So, during buildroot configuration, instead of</p><pre>Toolchain type<br>     External toolchain - Linaro AArch64</pre><p>select buildroot toolchain. Here, during compilation, I had this weird error: ld: cannot find crti.o: No such file or directory. But the solution was pretty simple—I just used the latest release candidate version instead of LTS/stable and it just worked.</p><p>Same for the kernel: omit the CROSS_COMPILE environment variable and don&#39;t change the CONFIG_CROSS_COMPILE option in the kernel .config file. I also needed to install qemu-system. The docs suggest installing from source, but really, QEMU is available in any package manager at this point, so brew install qemu worked just fine.</p><p>Then to the compilation of syzkaller ifself. I needed to compile it on my host, macos — or so I thought at the time. syzkaller can be compiled in just 3 simple commands, um..., what?</p><pre>$ git clone https://github.com/google/syzkaller<br>$ cd syzkaller<br>$ make<br>Makefile:31: run command via tools/syz-env for best compatibility, see:<br>Makefile:32: https://github.com/google/syzkaller/blob/master/docs/contributing.md#using-syz-env<br>exit status 1<br>Makefile:44: *** unknown target darwin/arm64.  Stop.</pre><p>After digging through the docs and Makefiles, I found that when compiling syzkaller, you can specify the OS and architecture for both host and target:</p><pre># There are 3 OS/arch pairs:<br> #  - HOSTOS/HOSTARCH: pair where syz-manager will run.<br> #  - TARGETOS/TARGETVMARCH: pair of the target OS under test.<br> #  - TARGETOS/TARGETARCH: pair of the target test process.<br> #<br> # The last 2 differ for e.g. amd64 OS and 386 test processes (compat syscall testing).<br> # All pairs default to the current machine. All but BUILD can be overriden.<br> #<br> # For example, to test linux/amd64 on linux/amd64, you just run:<br> #    make<br> # To test linux/arm64 from darwin/amd64 host, run:<br> #    make HOSTOS=darwin HOSTARCH=amd64 TARGETOS=linux TARGETARCH=arm64<br> # To test x86 compat syscalls, run:<br> #    make TARGETVMARCH=amd64 TARGETARCH=386</pre><p>So, I compiled syzkaller with the newly obtained knowledge.</p><pre>make HOSTOS=darwin HOSTARCH=arm64 TARGETOS=linux TARGETARCH=arm64<br> ************************************************************************************<br> Executor will not be built<br> Building executor for linux is not supported on darwin<br> ************************************************************************************</pre><p>And managed to build all important stuff, but not the executor—this one gets uploaded to the fuzzing VM and executes fuzzing programs, so you know, kind of important. So I fired up my Linux VM once more:</p><pre>git clone https://github.com/google/syzkaller<br>cd syzkaller<br>make</pre><p>And then I just copied it to the host.</p><pre>scp user@192.168.65.7:/home/user/linux_kernel/syzkaller/bin/linux_arm64/syz-executor bin/linux_arm64</pre><p>With everything set up, I wrote this simple config:</p><pre>{<br>     &quot;name&quot;: &quot;QEMU-aarch64&quot;,<br>     &quot;target&quot;: &quot;linux/arm64&quot;,<br>     &quot;http&quot;: &quot;:56700&quot;,<br>     &quot;workdir&quot;: &quot;/Users/user/proj/linux_kernel/syzkaller_workdir&quot;,<br>     &quot;kernel_obj&quot;: &quot;/Users/user/proj/linux_kernel/linux-6.8.12-builddir/media/user/linux_disk/syzkaller/linux-6.8.12&quot;,<br>     &quot;syzkaller&quot;: &quot;/Users/user/proj/linux_kernel/syzkaller&quot;,<br>     &quot;image&quot;: &quot;/Users/user/proj/linux_kernel/rootfs.ext3&quot;,<br>     &quot;sshkey&quot;: &quot;/Users/user/.ssh/id_rsa&quot;,<br>     &quot;procs&quot;: 8,<br>     &quot;type&quot;: &quot;qemu&quot;,<br>     &quot;vm&quot;: {<br>         &quot;count&quot;: 4,<br>         &quot;qemu&quot;: &quot;/opt/homebrew/bin/qemu-system-aarch64&quot;,<br>         &quot;cmdline&quot;: &quot;console=ttyAMA0 root=/dev/vda&quot;,<br>         &quot;kernel&quot;: &quot;/Users/user/proj/linux_kernel/Image&quot;,<br>         &quot;cpu&quot;: 2,<br>         &quot;mem&quot;: 2048<br>     }<br> }</pre><p>Then I fired it up with syzkaller/bin/syz-manager -config=syzkaller.conf at saw this sweet, sweet console output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8KclmbwDTg9mr5Brf2qHFg.png" /></figure><h3>Testing with vulnerable driver</h3><p>Then it was just a small matter of testing how syzkaller handles out-of-tree kernel modules. Instead of immediately starting with something from real life, I wrote a tiny vulnerable kernel module to test this out in a controlled environment. It has just one communication interface which boils down to:</p><pre>#define IOCTL_CMD _IOR(&#39;v&#39;, &#39;1&#39;, int32_t*)<br> ​<br> struct ioctl_arg{<br>     size_t size;<br>     char data[1];<br> };<br> ​<br> static long vuln_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {<br>     char buf[256];<br>     struct ioctl_arg to;<br>     int res;<br>     switch (cmd) {<br>         case IOCTL_CMD:<br>             res = copy_from_user(&amp;to, (int32_t*) arg, sizeof(to));<br>             if (res !=0 ) {<br>                 return -1337;<br>             }<br>             res = copy_from_user(buf, (int32_t*) arg, to.size);<br>             if (res != 0) {<br>                 return -1338;<br>             }<br>             if (copy_to_user((int32_t*) arg, &amp;value, sizeof(value))) {<br>                 return -EFAULT;<br>             }<br>             pr_info(&quot;vuln_ioctl: IOCTL_CMD called, value = %d\n&quot;, value);<br>             break;<br>         default:<br>             return -EINVAL;<br>     }<br>     return 0;<br> ​</pre><p>The vulnerability here is pretty simple: the code copies the first part of user data. Then it uses size it got from the user (to.size) to as the size of the data to copy. Of course, the copy operation is to the fixed sized buffer. Good enough to bypass compiler checks and it was already on my mind since the <a href="https://slava-moskvin.medium.com/kernel-quest-from-brain-surgery-to-bug-hunting-with-kasan-0d276383f308">last article</a>.</p><p>The client was even shorter and looked like this:</p><pre>int fd = open(&quot;/dev/vuln_ioctl&quot;, O_RDWR);<br>struct ioctl_arg* arg;<br>arg = (struct ioctl_arg*)malloc(1024);<br>arg-&gt;size=1024;<br>​<br>ioctl(fd, IOCTL_CMD, arg);</pre><p>I compiled the module with KASAN and uploaded it and the client to the QEMU VM. And lo and behold, after ./vuln_ioctl_client we have a nice stack trace followed by a kernel panic. Cool, now the only step left is to repeat it, but with syzkaller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eJxsrDhIUSD0CaOmb3sawA.png" /></figure><p>First, I made it so vuln_ioctl is loaded on every reboot automatically by adding this line to /etc/init.d/S50sshd:</p><pre># insmod /vuln_ioctl.ko</pre><p>Reboot, and indeed it worked, perfect.</p><pre># lsmod<br>Module                  Size  Used by    Tainted: G<br>vuln_ioctl             12288  0</pre><p>Then I had to write a description of the communication interface. It sounds intimidating, but once you find the right doc and start taking inspiration from descriptions that are already there, it’s super simple — at least for this case.</p><p>To add a new syscall description, you have to create a file syzkaller/sys/&lt;OS&gt;/your_name.txt. My description was pretty simple and straightforward.</p><pre>// include files to get constants<br>include &lt;uapi/linux/fcntl.h&gt;<br>include &lt;vuln_ioctl.h&gt;<br>// resource is value that need to be passed from output of one syscall to input of another syscall<br>resource fd_vuln_ioctl[fd]<br>// custom version of openat (basically) open syscall for constants necessary for the kernel module <br>openat$vuln_ioctl(fd const[AT_FDCWD], file ptr[in, string[&quot;/dev/vuln_ioctl&quot;]], flags flags[open_flags], mode const[0]) fd_vuln_ioctl<br>// ioctl desciption for the kernel module<br>ioctl$IOCTL_CMD(fd fd_vuln_ioctl, cmd const[IOCTL_CMD], arg ptr[in, ioctl_arg])<br>​<br>ioctl_arg {<br> size  len[data, int64]<br> data  array[int8]<br>}</pre><p>Notice that the description uses constants AT_FDCWD and IOCTL_CMD, so I needed to add my header file vuln_ioctl.hsomewhere syzkaller is aware of.</p><pre># in /media/user/linux_disk/syzkaller/linux-6.8.12<br>$ cp vuln_ioctl/vuln_ioctl.h ./include/</pre><p>The next step was to execute make extract so that syzkaller can extract all necessary constants for every relevant architecture and put it into syzkaller/sys/&lt;OS&gt;/your_name.txt.const. Simple enough, but this proved to be the hardest step in the whole endeavour. Everything was done in a Linux VM, so the blame is not on the lack of support for macos.</p><p>Here’s a list of what I’ve tried:</p><ol><li>make extract TARGETOS=linux SOURCEDIR=/media/user/linux_disk/syzkaller/linux-6.8.12 HOSTARCH=arm64 TARGETOS=linux TARGETARCH=arm64 - just a bunch of errors in return.</li><li>Recompiling the kernel after each failed make extract attempt (it turned out that make extract deletes build artifacts). Totally useless as make extract doesn&#39;t rely on built files.</li><li>bin/syz-extract -os linux -arch arm64 -sourcedir /media/user/linux_disk/syzkaller/linux-6.8.12 -builddir /media/user/linux_disk/syzkaller/linux-6.8.12 -vv 10 - to extract constants only for arm64. Got error: ‘KASAN_SHADOW_SCALE_SHIFT’ undeclared. Solved it with make ARCH=arm64 defconfig in the kernel directory. Also, it turned out that defconfig is also necessary after each make extract attempt.</li><li>Once again bin/syz-extract -os linux -arch arm64 -sourcedir /media/user/linux_disk/syzkaller/linux-6.8.12 -builddir /media/user/linux_disk/syzkaller/linux-6.8.12 -vv 10 only to get ‘struct common_audit_data’ has no member named ‘smack_audit_data’. This one is super easy—just add CONFIG_SECURITY_SMACK=y to .config.</li><li>And one more time bin/syz-extract -os linux -arch arm64 -sourcedir /media/user/linux_disk/syzkaller/linux-6.8.12 -builddir /media/user/linux_disk/syzkaller/linux-6.8.12 -vv 10 generating linux/arm64—this time it completes without errors. Yahoo! The next step is make generate—only to get buried under the avalanche of sys/linux/blah.txt:332:32: string value &quot;bleh\x00&quot; exceeds buffer length 1. No problem! This behavior was already described in <a href="https://github.com/google/syzkaller/issues/2749">this GitHub issue</a>. The suggested solution was bin/syz-extract -os linux -sourcedir /media/user/linux_disk/syzkaller/linux-6.8.12 -vv 10 -build vuln_ioctl.txt to extract constants just for one file. It was going well until it started to complain that a bunch of cross-compilers aren&#39;t installed.</li><li>Easy-peasy! Just sudo apt install gcc-s390x-linux-gnu gcc-riscv64-linux-gnu gcc-powerpc64le-linux-gnu gcc-mips64el-linux-gnu gcc-arm-linux-gnueabi gcc-x86-64-linux-gnu and repeat the previous command. To get vuln_ioctl.txt: IOCTL_CMD is unsupported on all arches (typo?).</li><li>Everything’s fine. I just copied vuln_ioctl.h to every possible include directory I could find. To get the same result.</li><li>straceing syz-extract to make sure it reads vuln_ioctl.h. Well yeah... it reads it, alright. Just doesn&#39;t want to understand it.</li><li>Something so devious my mind decided to wipe it out to make bin/syz-extract -os=linux -sourcedir=/media/user/linux_disk/syzkaller/linux-6.8.12 -arch arm64 -build vuln_ioctl.txt work again. And only to make make generate fail.</li><li>At this point, it seemed like syzkaller doesn&#39;t want to play ball with the kernel module. But what if I statically compile it <em>into</em> the kernel? I changed the kernel build configuration, included the module in the kernel build process, and compiled it for good measure. Still... the result was the same.</li><li>Thankfully, syzkaller provides a Docker container and a wrapper around it, syz-env. Unfortunately, the container image in the Google Docker repo was built only for x86_64. Piece of cake! The Dockerfile was available, so I removed everything that wasn&#39;t needed/was broken on arm64 and rebuilt it. I&#39;ll spare you the details, but it also failed. Some kind of problem with flatbuffers. No idea, probably a mistake on my side.</li></ol><p>At this point, I was starting to feel desperate. I wanted to make this work the right way. And the thing was that I did get a generated .const file, but the damn ??? were a total killjoy.</p><pre># contents of my vuln_ioctl.txt.const file:<br># Code generated by syz-sysgen. DO NOT EDIT.<br>arches = arm64<br>AT_FDCWD = arm64:18446744073709551516<br>IOCTL_CMD = ???<br>__NR_ioctl = arm64:29<br>__NR_openat = arm64:56</pre><p>I mean, in this example, it would be fine to be a bad boy and violate the DO NOT EDIT instruction and insert the constant&#39;s value manually. But in a real-life scenario, I could deal with tens if not hundreds of constants, and this approach just would not be sustainable. Unless... I could make it sustainable.</p><p>You see, I read in the docs that to extract constants, syzkaller actually compiles small programs with hardcoded constants&#39; values. Nobody forbids me to do the same, but with a much more crude implementation. It looked like a valid option that would allow me to unblock this mess. For that case, I just wrote the program manually (yes, all 7 lines!):</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;vuln_ioctl.h&gt;<br>​<br>int main() {<br>   printf(&quot;IOCTL_CMD=%ld\n&quot;, IOCTL_CMD);<br>   return 0;<br>}</pre><p>At with the magic result revealed to be 2148038193, I quickly fixed the .const file and continued to the next step of the plan, which was</p><pre>$ make generate TARGETOS=linux SOURCEDIR=/media/user/linux_disk/syzkaller/linux-6.8.12 HOSTARCH=arm64 TARGETOS=linux TARGETARCH=arm64</pre><p>And finally, finally it was a command that finished without an error! That was definetely a good sign. Unfortunatelly, my luck ran out on the very next command.</p><pre>$ make TARGETOS=linux SOURCEDIR=/media/user/linux_disk/syzkaller/linux-6.8.12 HOSTARCH=arm64 TARGETOS=linux TARGETARCH=arm64<br>--snip--<br>./pkg/flatrpc/flatrpc.h:11:41: error: static assertion failed: Non-compatible flatbuffers version included  static_assert(FLATBUFFERS_VERSION_MAJOR == 23 <br>--snip--</pre><p>flatbuffers strikes again! Further investigation showed that syzkaller ships with flatbuffers v.2, but my distro&#39;s package manage had only v.23. The solution seemed natural: just copy flatbuffers v.23 headers to syzkaller include directory and rerun the make. Well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vfDzldLe3b3U3s2J5cVc6A.png" /></figure><p>After some poking around, I decided to turn the tables and instead install the same flatbuffers version that syzkaller came with. Thankfully, at least bulding flatbuffers didn&#39;t cause too much troubles.</p><pre>$ git clone --branch v2.0.8 https://github.com/google/flatbuffers.git<br>$ cd flatbuffers<br>$ cmake -DCMAKE_BUILD_TYPE=Release -G &quot;Unix Makefiles&quot; -DFLATBUFFERS_BUILD_TESTS=OFF<br>$ make -j`nproc`<br>$ sudo make install</pre><p>It was time to try to build syzkaller once again. If it failed, I didn’t have any other ideas or plans, probably it would mean a failed project for me. I was holding my breath while watching the progress of the final make inside the syzkaller directory. Weirdly, it just finished, no errors.</p><p>Now I just had to copy vuln_ioctl.txt and vuln_ioctl.txt.const to the mac and repeat all the steps, including flatbuffers. And... it also worked. A strange feeling, but I thought that I might get used to it.</p><p>My syzkaller configuration was going to be very similar to the first one I used with the exception of a white list of syscalls to fuzz to speed things up:</p><pre>&quot;enable_syscalls&quot;: [ &quot;openat$vuln_ioctl&quot;, &quot;ioctl$IOCTL_CMD&quot;, &quot;mmap&quot;]</pre><p>After launching syzkaller with bin/syz-manager -config ../syzkaller-vuln_ioctl.conf, it brought up a web interface indicating that the process was started successfully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9vz5VhpdYUyspqGY0so4mw.png" /></figure><p>The number of executions per minute was about 60–80 initially, but quickly climbed down to 11 as my laptop begun overheating and thermal throttling. But better than nothing, alright? I also suspect that 12 exec/s means not 11 syscalls/s but 11 syzkaller programs that consist of multiple syscalls.</p><p>Nevertheless, it meant that I had to wait. After 30–60 minutes and several cups of delicious black tea syzkaller finally catched a crash!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7R4bAKKXnLTHw_w-DTGI0A.png" /></figure><p>The logs saved by syzkaller are the same that I got when triggering the vulnerability with vuln_ioctl_client, which means the crash was exactly the crash I was after! And it means that this story has finally arrived to</p><h3>Conclusion</h3><p>Yes, it’s 100% possible to use arm64 mac as a host machine for syzkaller, and it&#39;s also possible to fuzz an out-of-tree kernel module. Would anybody sane use this combo to do some serious fuzzing? Probably not, but we don&#39;t choose the easy path!</p><p>There’s still some work to be done:</p><ol><li>I’m not sure that coverage worked like it’s supposed to (in the WebUI screenshot you can see that coverage sits at 0).</li><li>Reproduction with syz-execprog didn&#39;t work at all.</li><li>Although the vulnerable module was built with KASAN, the crash log shows that the crash is triggered not by KASAN, but in a check somewhere deep inside of copy_from_user. But that&#39;s most likely how it&#39;s supposed to go.</li><li>Check if HW acceleration is on for qemu.</li><li>Try out Hardware Tag-Based KASAN and check if less coverage worth it for performance benefit.</li></ol><p>But that’s for some time in the future. For now, tune in for part 2 where I’ll fuzz something more interesting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2a3949ea575" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hunting Bugs in Linux Kernel With KASAN: How to Use it & What’s the Benefit?]]></title>
            <link>https://slava-moskvin.medium.com/kernel-quest-from-brain-surgery-to-bug-hunting-with-kasan-0d276383f308?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d276383f308</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[pentest]]></category>
            <category><![CDATA[kernel]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Tue, 10 Sep 2024 09:22:01 GMT</pubDate>
            <atom:updated>2024-09-15T19:15:17.809Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eMw4zcSGsBD8AGaLICvU9Q.png" /></figure><p>It all started when I had brain surgery. After several days in the hospital, I got home. But I guess the surgeon hit something in my brain after all because, at home, I started to feel a strong desire to read Linux kernel module code. I decided to start with a 3rd party module that I use, but its usage is not widespread, making it an easier target.</p><p>After just a couple of hours, I found something that looked like a vulnerability. Digging deeper confirmed it — judging by the code, it was possible to trigger kernel memory overread. But that was just a theory. To prove it in practice, I needed to create a PoC.</p><p>The vulnerability was nothing crazy, just a double fetch, like in the picture below. But the thing is that I had no idea if it was enough to just create two threads to win the race condition or if I should employ some nasty tricks. Well, there was only one way to find out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WKtvJm2DeOuEItHb5fNvw.png" /></figure><h3>Working on a PoC</h3><p>I started with a simple PoC that could communicate with the kernel module using the simplest commands. When I successfully got a response, I moved on to creating a PoC for the vulnerability. My expectation was that triggering the bug would crash the system. But when I launched the PoC, nothing happened. I double-checked that the communication with the module was good and the proper branch in the IOCTL handler was triggered. Still, no crash.</p><p>Well, it must be that the double fetch happens so quickly that the bug is impossible to trigger without additional tricks. I spent several days looking for those, and let me tell you: people have come up with crazier stuff than I could have possibly imagined. But most of those were already patched, and the rest either didn’t work or my IQ level was too low for them. In the end, I just put it aside, deciding that it was too hard to trigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*qS8hNVW40XIv5rwgvJghlg.png" /></figure><h3>Starting Fresh</h3><p>Until a few months later, when I decided to come back with fresh energy and fresh ideas:</p><ol><li>I could create my own kernel module that resembles my target, add a lot of debug prints, and try to trigger the double fetch.</li><li>If that works, since the target is open source, I can pull the same trick and add a bunch of debug prints there.</li><li>Alternatively, I could use Kernel Address Sanitizer (KASAN), a tool built into the kernel designed to catch various memory-related bugs, including those that are harder to catch, like my memory overread.</li></ol><p>To be honest, I’d managed to reach my age without ever trying KASAN, so I wasn’t sure how easy it is to use and if it works somewhere other than the developer machine. So I opted for options 1 → 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62akISwqPuaOcg-5cy7sTA.png" /></figure><p>Code-code-code and the training kernel module was ready. Code-code-code and a simple two-threaded PoC was ready too. ./poc &amp;&amp; dmesg showed that it’s all real: debug prints reported that you can trigger the bug after not-so-many tries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*zaMjX2VoTLTXyhFuzSlV4w.png" /></figure><p>The third code session to add as many debug prints as humanly possible to the target, adapt the PoC, and voila: I could see how printk() prints kernel heap metadata. So what was the problem the first time?</p><p>It was just a simple mistake: I overlooked a crucial piece of code that essentially placed a limit on how much memory you can read — still enough to read past the buffer boundary, but not enough for the read to crash the kernel.</p><h3>A Better Solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lAjLMd-X1aQJs4yqb1MpCQ.png" /></figure><p>So why KASAN in the title? It’s simple: after all that nonsense, I decided to try KASAN and it turned out it could save me A LOT of time — days shaved to an hour or two to compile the kernel with KASAN enabled. Also, with KASAN, you get a simple binary answer to whether the behavior you trigger is a vulnerability: yes or no. It’s easy to mess up debug prints or overlook something, but with KASAN the answer is definitive. Let’s dive deeper into that.</p><p>KASAN creates so-called shadow memory to keep track of memory reads and writes. Each byte of shadow memory represents 8 bytes of real memory. Byte values 0x00 to 0x07 indicate normal memory that’s legal to access, while negative numbers correspond to special cases, e.g., 0xff — memory that was freed, 0xfc — red zone between allocated buffers, etc.</p><p>Then each memory access is instrumented to check if each read or write attempt operates on “legal” memory or is trying to do something nasty.</p><p>For more information about KASAN inner workings, feel free to check out these articles:</p><p><a href="https://www.starlab.io/blog/kasan-what-is-it-how-does-it-work-and-what-are-the-strange-numbers-at-the-end">KASAN: What is it? How does it work? And what are the strange numbers at the end? — Star Lab Software</a></p><p><a href="https://blogs.oracle.com/linux/post/linux-slub-allocator-internals-and-debugging-3#:~:text=KASan%20uses%20shadow%20memory%20to,bytes%20of%20kernel%20address%20space">https://blogs.oracle.com/linux/post/linux-slub-allocator-internals-and-debugging-3#:~:text=KASan uses shadow memory to,bytes of kernel address space</a>.</p><h3>Setting Up KASAN</h3><p>To make use of KASAN, we have to recompile the kernel. So make sure that you have about 40 GB of free space to store the sources and build artifacts and let’s go.</p><p>First, let’s install everything we need for compilation:</p><pre>$ sudo apt update<br>$ sudo apt install build-essential libncurses-dev bison flex libssl</pre><p>Then let’s download the kernel source code:</p><pre>$ apt source linux-image-$(uname -r)<br>$ cd linux-*</pre><p>Now we have to edit the kernel configuration to turn on KASAN:</p><pre>$ make ARCH=arm64 menuconfig</pre><p>Navigate to <strong>Kernel hacking -&gt; Memory Debugging</strong> and enable <strong>KASAN: runtime memory debugger</strong>. Choose the type of KASAN that best fits your needs (e.g., generic KASAN).</p><p>You can examine your .config to check if KASAN was enabled. This one option was enough for me, but feel free to get crazy and enable more.</p><p>That’s it, start the build and go get a cup of tea or two:</p><pre>$ make ARCH=arm64 -j $(nproc)<br>$ make ARCH=arm64 modules<br>$ make ARCH=arm64 dtbs</pre><p>The Linux kernel compiles quite nicely for such a huge project, but in my particular case, I encountered this cryptic error:</p><pre>LD [M]  net/hsr/hsr.ko<br>  LD [M]  net/qrtr/qrtr.ko<br>  LD [M]  net/qrtr/qrtr-smd.ko<br>  LD [M]  net/qrtr/qrtr-mhi.ko<br>make: *** [Makefile:246: __sub-make] Error 2</pre><p>So I resumed the build with just one job to get better error output:</p><pre>$ make ARCH=arm64 -j 1<br>  SYNC    include/generated/autoconf.h<br>  CALL    scripts/checksyscalls.sh<br>  DESCEND bpf/resolve_btfids<br>  INSTALL libsubcmd_headers<br>  UPD     include/generated/utsversion.h<br>  CC      init/version-timestamp.o<br>BTF: .tmp_vmlinux.btf: pahole (pahole) is not available<br>Failed to generate BTF for vmlinux<br>Try to disable CONFIG_DEBUG_INFO_BTF<br>make[2]: *** [scripts/Makefile.vmlinux:37: vmlinux] Error 1<br>make[1]: *** [/media/sl4v/a6417fe8-1e5e-4957-86d7-0413e50d4dc6/kasan/linux-6.6.15/Makefile:1176: vmlinux] Error 2<br>make: *** [Makefile:246: __sub-make] Error 2</pre><p>Thankfully, the error message plainly told me what to do: go to .config and set CONFIG_DEBUG_INFO_BTF to n, and it worked. I resumed the compilation process, and the rest of the way was smooth.</p><p>After the tea ceremony you had, proceed by installing modules and the kernel itself:</p><pre>$ sudo make ARCH=arm64 modules_install<br>$ sudo make ARCH=arm64 install</pre><p>And finish by updating the bootloader configuration and rebooting:</p><pre>$ sudo update-grub<br>$ sudo reboot</pre><p>Now you should take a careful look at the options you’re offered by the bootloader during boot: you should have one new option — that’s your kernel with KASAN — select it.</p><p>The boot should take much more time and memory — that’s the price we have to pay for all the instrumentation and memory tracking KASAN is doing for us. After the boot is finished, verify that you have KASAN enabled:</p><pre>$ sudo dmesg | grep -i kasan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HhCPooAWBbwGngRnS7iptg.png" /></figure><p>Phew, I hope you didn’t forget why we came here. Once we have a kernel with KASAN up and running, we can load the target module in question and execute our PoC to get the following output:</p><p>KASAN was kind enough to include a shadow memory dump, and we can see that the buggy read was at the address, and the corresponding shadow memory byte is 0xfc — the red zone between kernel buffers. This already tells us what happened: the kernel module tried to read data past the allocated buffer.</p><p>So, KASAN is not only very helpful for getting a binary answer on whether a bug is triggered or not, but it also provides an opportunity to debug and find the root cause if the bug was found during fuzzing or similar processes.</p><h3>Further reading</h3><p><a href="https://www.starlab.io/blog/kasan-what-is-it-how-does-it-work-and-what-are-the-strange-numbers-at-the-end">KASAN: What is it? How does it work? And what are the strange numbers at the end? — Star Lab Software</a></p><p><a href="https://blogs.oracle.com/linux/post/linux-slub-allocator-internals-and-debugging-3#:~:text=KASan%20uses%20shadow%20memory%20to,bytes%20of%20kernel%20address%20space">https://blogs.oracle.com/linux/post/linux-slub-allocator-internals-and-debugging-3#:~:text=KASan uses shadow memory to,bytes of kernel address space</a>.</p><p><a href="https://www.infradead.org/~mchehab/kernel_docs/dev-tools/kasan.html">The Kernel Address Sanitizer (KASAN) — The Linux Kernel 5.10.0-rc1+ documentation</a></p><p><a href="https://static.lwn.net/kerneldoc/dev-tools/kasan.html">Kernel Address Sanitizer (KASAN) — The Linux Kernel documentation</a></p><p><a href="https://blog.fuzzing-project.org/23-Kernel-Address-Sanitizer-KASAN.html">Kernel Address Sanitizer (KASAN) | The Fuzzing Project</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d276383f308" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Choosing Cybersecurity Projects That Advance Your Skills — From Endless Struggles to Endless Bugs]]></title>
            <link>https://slava-moskvin.medium.com/choosing-cybersecurity-projects-that-advance-your-skills-from-endless-struggles-to-endless-bugs-fed0913a14a3?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/fed0913a14a3</guid>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[offensive-security]]></category>
            <category><![CDATA[soft-skills]]></category>
            <category><![CDATA[project-management]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Mon, 08 Jul 2024 10:27:00 GMT</pubDate>
            <atom:updated>2024-07-08T10:27:00.008Z</atom:updated>
            <content:encoded><![CDATA[<h3>Choosing Cybersecurity Projects That Advance Your Skills — From Endless Struggles to Endless Bugs</h3><h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*alO_Xx70KSTKaUVLCqO5Ng.png" /></figure><p>There’s no shortage of new and exciting in our field. A Windows 0-day drops. A macOS fuzzer got open-sourced. Cutting-edge Chrome exploitation technique just got revealed. It’s like being a kid in a candy store — everything is vibrant, new, and tempting. I don’t know about you, but after reading something like this I immediately feel a rush to go and find new vulnerabilities in Windows, use the fuzzer, and learn the exploitation technique.</p><p>The problem is that I know nothing about Windows kernel, macOS and Chrome. That doesn’t look like a good background for Windows/macOS/Chrome 0-day project. Well, I know that now.</p><p>Feeling inspired is a fantastic start; it fuels real-world accomplishments. However, passion alone doesn’t guarantee results. It’s easy to get lured into biting off more than we can chew. In fact, I started new projects only to fail countless times, just enough to almost stop trying. In this post, let’s explore how and why we fail when we get too ambitious for our own good and what we can do about that.</p><h3>Struggles</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rJ-vlr5yezRwPHfFTlfb3w.png" /></figure><p>My early career path was far from straightforward. More like a path I took home at 4 AM after partying all night. I devoured countless articles on reverse engineering, each filled with new tools and techniques. It seemed like authors effortlessly identified and exploited vulnerabilities, so I was thinking why can’t I do the same. One day, I felt ready to go from thoughts to action.</p><p>The article that gave me the final push was <a href="https://devttys0.com/2015/04/hacking-the-d-link-dir-890l/">a post in /dev/ttys0 blog</a>. The guy had a thing for D-Link and pwned it several times in a row. Since I had a similar model at home, I figured I’d follow his footsteps and snag my own 0-day.</p><p>Following the guide, I downloaded and unpacked my router’s firmware using Binwalk, revealing a full file system filled with binaries. I got a full file system with a lot of binaries. Next, found the CGI binary I needed to analyze and emulated it with Qemu. So far, so good. And then I loaded it into IDA. The architecture was MIPS, which I knew nothing about.</p><p>The only thing that I could recognize was function calls (they were marked by a different color). While the blog described the vulnerability in detail (unsanitized user input finally arriving in system()), I had no idea where to do from here. I also was able to see some structure in the assembly code, I was able to roughly understand which queries the binary handled, judging by the strings. Needless to say that I was pretty far from knowing what I was doing and was nowhere near the level of skill required to understand the code and look for bugs. Predictably, the project ended in failure.</p><p>Some time later I took a couple of reverse engineering lessons, I felt pumped up and ready to try again, but this time with different piece of software. But guess what? The same old story played out. It took a couple of another failed tries for me to even stop trying at all. It just felt like I wasn’t cut out for it and it just wasn’t meant to be.</p><h3>Turnaround</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*44qCrN7TvVx9g8HlxEWIGg.png" /></figure><p>Then, things took a turn when I found myself in an environment of seasoned reverse engineers. They were on a different level; I didn’t even have enough vocabulary to talk to them. Nevertheless, it got me inspired to give reverse engineering a try again.</p><p>This time around, I kept my ambitions in check. Instead of doing anything crazy like going for 0-days and except immediate result, I started with what I should have been doing: solving the simplest RE challenges I could find. I solved those until I got completely bored with them — a sign that I should move on. Right around then, a local antivirus company started a RE challenge, so I decided to participate.</p><p>That was something completely new to me as the challenges were all about Windows GUI crackmes, and I had zero experience in reverse engineering them. On the other hand, I had additional motivation since a lot of folks from my job were also competing and I didn’t want to land face-first in the mud.</p><p>In the first challenge, I used hardware breakpoints to track my inputs, which helped me find the WinAPI function the GUI form used to get text from user. Figuring this out was my first real win. This challenge made just a series of simple checks against user input, easy enough.</p><p>They ramped up the things with the second challenge. It interpreted inputs as hex characters, which were then processed through a system of 20 or 30 linear equations. I had to rewrite them from assembly to Python to put them into Z solver — a popular thing in CTFs those days.</p><p>The third and the final challenge remains unsolved for me still.</p><p>But competing alongside veterans who had years of experience under their belts, I realized two things. First, it was extremely rewarding to finally play on the same field with them. The second, I understood just how much I was lagging behind — they were days ahead of me, solving challenges as easy as I was breathing. Still, it was cool to get a bit closer to speaking the same language with them and to see how their approach was different from mine. I finally felt like I had grown.</p><h3>Finding My Niche</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*0DoIzIjz4y7QOkpvETHONA.png" /></figure><p>Another thing was happening at the same time. Long story short, there was a lack of pentesting tasks and plenty of RE tasks. Having always dreamt of RE, I decided to give it a shot. On the next project, I made sure to first complete my usual pentesting tasks and then picked up with the smallest possible RE task. In fact, it was so insignificant that nobody would notice if it wasn’t done — an opposite of a project-critical task. In other words, a perfect starting task for someone in my position.</p><p>The task was to find out how the software we were auditing stored passwords. The software was a Windows GUI application — the type of software I had just gotten plenty (by my standards at the time) of experience with.</p><p>Using hardware breakpoints — my usual technique at the time — I found the place in the code responsible for checking passwords. And with a little help from my more experienced colleague, I managed debug and reverse engineer the encryption algorithm. A huge win for me at the time. It didn’t even make it into the final report — that’s how insignificant this was for us at the time. But at least the ice was broken. I knew it, and my team knew it.</p><p>I continued this chain of small tasks. On the next project, I chose the worst and most outdated, off-the-market controller to reverse. Eventually, I teamed up on a serious task with an experienced colleague. We could make a whole lot on that project, and I’m proud to say that I helped him as much as he helped me. We found over 10 bugs, including a memory overwrite leading to RCE. Hell, we even came up with a way to exploit it and make the controller blink. All without any debugging, pure static analysis.</p><p>After nailing that project, I had enough confidence in myself, and my boss — in me, so I could take on any crucial task on my own.</p><h3>Final Reflections</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*NK2NcN54LAZDiqndvHPkdQ.png" /></figure><p>Picking the right tasks, ones that match what you’re into and what you’re good at, is one of the most important skills we can have. Don’t expect to stumble upon a zero-day vulnerability right off the bat. It’s also important to distinguish your interest, enthusiasm, and motivation from overconfidence. There’s a fine line.</p><p>Let’s compare two perspectives:</p><ol><li>Imagine someone excited by the latest vulnerability in macOS. They immediately want to do the same and fuzz macOS kernel extensions (aka drivers) to find a 0day. The plan is to just use the blogpost they read as a guideline and to apply it to a different kext.</li><li>Another person is also excited. This person might start by diving into the macOS internals, learning from previous research, understanding attack surface, analyzing targeted kexts and reviewing existing fuzzers to select the best option.</li></ol><p>Clearly, the second person is more likely to succeed. They understand the task’s complexity, the challenges that come with it and what’s needed to be done for a successful research project. What is crucial is that they understand what they know and what they don’t.</p><p>While enthusiasm is a great start, it needs to be paired with realistic goals. From what I’ve seen, the best task is the one you know how to solve 70% of. It’s challenging enough to strike the balance between confidence and the excitement of the unknown. 90% is probably time to automate some of that stuff or move on. 50% zone is a gamble — you might pull it off, or you might crash and burn. And lower than that is closer to learning basics of the topic than a full fledged project. Choosing such task as a project has a low chance of success.</p><p>And I’m not saying that failing is the end of the world, but failing without learning anything is. For me, a lot of times it was like playing a videogame without saves: you die — you have to repeat it from the start. As a result, I got somewhat good in the early stage, but still had no idea what to do next.</p><p>And let’s go back to our example above. Let’s say the first person fails the task. They might just shrug it off, because there’s just no plan to begin with, not enough data and knowledge to understand what went wrong. But the second person? They might look back, figure out what was off, e.g. the chosen kext wasn’t good for fuzzing or there wasn’t enough knowledge base to successfully implement a certain part of the plan. So they can work on their strategy, on their knowledge and adjust for next time.</p><p>So, while I’m all for keeping that optimism high, just suggesting to channel it wisely. Make sure it’s guiding you towards challenges that are just right — not too easy, not impossible, but just right to push your boundaries and grow your skills.</p><p>This were some stories from my side. If you also have a story about overcoming challenges in your career, I’d love to read it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fed0913a14a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Found Bugs, Got paid, Stayed poor: Making a Living with Bug Bounties]]></title>
            <link>https://slava-moskvin.medium.com/found-bugs-got-paid-stayed-poor-making-a-living-with-bug-bounties-04ba1fbbab73?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/04ba1fbbab73</guid>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[bug-bounty]]></category>
            <category><![CDATA[offensive-security]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Sat, 22 Jun 2024 20:21:12 GMT</pubDate>
            <atom:updated>2024-06-22T20:50:21.598Z</atom:updated>
            <content:encoded><![CDATA[<p>Early 2021. After feeling unsatisfied with my job for quite some time and battling burnout, I decided to take a sabbatical. “To hell with it,” I thought. “I’m not spending another day working for ‘the man’! I’m my own boss now!”</p><p>For the next several months, I decided to try three different strategies:</p><ul><li>Creating a reverse engineering course.</li><li>Giving private reverse engineering lessons.</li><li>And, of course, hunting for bug bounties!</li></ul><h3>The Setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SUcvjXROXXnh2nIFqB3gYA.png" /></figure><p>Every security guy dreams of making a living off bug bounties. I always pictured this lifestyle as lounging on a sunbed at the beach, sipping cocktails, and occasionally glancing at the screen of my laptop the check the fuzzing campaign. In reality, my journey began in the grim spring of a megapolis, with my family’s bank account only stretching to cover us for 3–4 months.</p><p>My experience with bug bounties was quite limited at the time. I had secured a reward for a couple of random vulnerabilities I stumbled upon but had never pursued it systematically. However, the little experience I had taught me some important lessons:</p><ul><li>Reading posts like “I got $30,000 for hacking Facebook” is very exciting, but achieving that requires an enormous amount of skill, determination, experience, and probably a bit of luck.</li><li>High reward == high competition. Sure, I’d love to earn tens of thousands of dollars for hacking Windows, but entering that arena means competing against security researchers with years of experience, firms that discover and sell bugs to the highest bidder, and even Microsoft’s internal teams.</li></ul><p>To get my feet wet, I needed something more predictable and sustainable. I figured working on low-profile targets, let’s say with a 30% chance of finding vulnerabilities, was better than aiming for high-profile software with a mere 1% success rate. At least this way, I had a solid chance of making some money and could estimate the profitability of this endeavor without spending weeks or months hitting a wall and coming out empty-handed.</p><p>With that strategy in mind, I started searching for a target. The search didn’t take too long. Having worked in industrial security for a long time, I knew this field offered the perfect combination of factors:</p><ul><li>Tons of low-quality legacy software, often with little to no consideration for security.</li><li>An intimidating and less sexy appearance, resulting in less attention from the cybersecurity community and, consequently, less competition for me.</li></ul><p>Choosing hardware as my target was out of the question, as I didn’t have any budget to purchase it, and I highly doubted a bug bounty platform would buy it to verify my findings. My choice landed on Schneider Electric’s IGSS SCADA. I had <a href="https://www.cisa.gov/news-events/ics-advisories/icsa-20-084-02">some experience</a> finding bugs in it previously, so I was hopeful that it was still soft in some places.</p><p>I was already experimenting with going for bug bounties, so with tools and approach I decided to stick with my guns: manual reverse engineering with IDA and x64dbg.</p><h3>The Payoff</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4DmQAoGyQPWSyv9TM3lHnA.png" /></figure><p>Finding vulnerabilities in subpar ICS software gradually lost its allure over four years on the job, but then it suddenly appeared in a different light. Every bug I found gave me a dopamine rush, knowing it was my only means of making money at the time. And the euphoria from receiving that SWIFT transfer notification is hard to forget.</p><p>My strategy seemed successful at first glance: I discovered <a href="https://www.cisa.gov/news-events/ics-advisories/icsa-22-046-01">10 vulnerabilities</a>, ranging from information disclosure to remote code execution. In total, I made around $3,000 on that.</p><p>So, why do I consider this endeavor a failure?</p><p>I didn’t meticulously track the time spent on it daily, but the whole active research phase consumed about 3 months. During this period, I also juggled other activities, none of which were financially rewarding. When you break it down monthly, the income was 30–50% less than what my family needed to maintain a normal life without anything extra. I watched as our savings account slowly depleted, each day inching closer to zero, a constant, looming reminder of the risks of going self-employed.</p><h3>Time per Bug</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*cdtwTnLJzCI5KiLxDvBSrg.png" /></figure><p>When you consider the months of work and subtract the time spent on various unsuccessful targets and methods, the actual productive time is significantly lower. And if we divide by that new number, we’d get a higher pay per month. Unfortunately, reality doesn’t align with such wishful recalculations, and my failures were also paid from my pocket.</p><p>But, if I had a faster method to find those bugs, financials would be much better. Fuzzing could’ve been helpful, but</p><ul><li>the majority of the bugs were of pure logical nature</li><li>My focus was on network services, but didn’t have network traffic for the majority of them. That meant that I had to reconstruct the protocol manually. And a careful look at the protocol implementation was enough to identify the vulnerabilities.</li></ul><p>Another major mistake involved the quality of my reports. No, my reports weren’t bad — they were too good. I spent far more time than I should have crafting some of the highest quality reports you’ve ever seen, complete with root cause analysis. Now, having seen the reports other people submit to bug bounty programs, I realize that my drive for report perfectionism was at my own expense.</p><h3>Unstable Income</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*bxOj7HChfKSsepEuBFBeBQ.png" /></figure><p>Being an employee has a quite amusing side effect: you get paid on the same date every month. As a bug bounty hunter, however, the money arrive whenever the bug bounty program decides to pay out. I was far from a top-tier researcher, and my target was as low-profile as they come. So, sometimes I had to wait weeks or even months for my submissions to be triaged and paid.</p><p>It took over three months from the submission of my final vulnerability to receive the payout. I’m not sure if that’s normal for my situation, but it’s definitely another factor that puts the stability of this income source into perspective.</p><h3>Lessons Learned</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Ov8RHgJ7BIcS-Lehllkl5g.png" /></figure><p>I didn’t end up sipping cocktails on the beach while watching the bugs pouring, but I’m genuinely happy I went through this experiment at that point in my life. Big thanks to my wife for supporting me through this. I learned a lot from the experience.</p><p>Choosing obscure, low-profile targets proved to be a great training ground, but:</p><ul><li>The payouts aren’t that high (which, of course, varies depending on your cost of living). It might work out with strategies to significantly cut the time spent per bug.</li><li>You’re given the lowest priority on the platform. Unfortunately, this can mean longer wait times.</li></ul><p>In hindsight, I should’ve quickly pivoted to either coming up with a quick and dirty way to find bugs as fast as possible, or jump to targets that are just one step higher. Probably, as combination of those would be even better.</p><p>Working for “the man” can actually be lovely: you don’t need to hustle for clients, “the man” compensates you for the time you spend learning from mistakes, being sick, or even procrastinating. And oh, that wonderful notification from the banking app every month!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04ba1fbbab73" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[File Fuzzing: Easy and Really Fast with this AFL++ Feature]]></title>
            <link>https://slava-moskvin.medium.com/file-fuzzing-easy-and-really-fast-with-this-afl-feature-2bba1366b412?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bba1366b412</guid>
            <category><![CDATA[offensive-security]]></category>
            <category><![CDATA[hacking-tools]]></category>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[pentest]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Mon, 22 Apr 2024 17:02:09 GMT</pubDate>
            <atom:updated>2024-11-15T19:11:14.325Z</atom:updated>
            <content:encoded><![CDATA[<h3>File Fuzzing: Easy and Really Fast with AFL++ Persistent Mode</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Or5gkTFQ5ovdeT55AwCwmQ.png" /></figure><p>One day, I saw that <strong>srelay</strong> exists. It’s a pretty old SOCKS4 and SOCKS5 proxy server with the latest update from 2018. Hand on heart, it&#39;s hard to call it popular, but it’s far from being dead either. I also wasn’t able to find any information about the history of srelay vulnerabilities. To top it all off, the code base is really compact, so it sounded like a good opportunity to try out some of AFL++&#39;s new features.</p><h3>Reconnaissance</h3><p>After briefly reading the source code, we can split the target’s functionality into several distinct parts that parse user data:</p><ol><li>Config parsing</li><li>SOCKS server</li><li>Proxy client: SOCKS, HTTP</li></ol><p>I would argue that each of these parts needs its own fuzzing harness based on the vastly different data formats they parse:</p><ul><li>The text-based config looks nothing like SOCKS or HTTP requests.</li><li>SOCKS and HTTP are obviously different protocols with request formats that are not alike.</li><li>While the SOCKS client and server operate on a similar protocol, they still expect different types of data to parse. Mixing it would just confuse the fuzzer.</li></ul><p>On top of that, splitting the fuzzing campaign into several subcampaigns allows us to isolate pieces of code with different functionalities into their own harnesses, making the whole thing much more manageable.</p><p>We can start with fuzzing the config parser. It’s the easiest parser to fuzz in the code: just one neat function int readconf(FILE *fp). Ok, but is a config parser vulnerability cool? Well, it’s not as cool as a vulnerability exploitable over a network, so you might get fewer respect points from the cool kids on the block. But if we calculate the CVSS for a code execution vulnerability that we can trigger, let&#39;s say, through a world-writable config file, we’ll get a CVSS score of 6.4 in the worst-case scenario. Not too shabby.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZwFi_5EMYqTEggNKIEvpSQ.png" /></figure><p>Maybe we’ll cover the rest of the functionality another time, but for now, readconf has won a place in my heart and it looks like a good option to try out features all the cool guys are using.</p><h3>Desperate times call for desperate measures</h3><p>Having a function like int readconf(FILE *fp) to fuzz is the dream of every master fuzzer. We can follow the beaten path and write a usual AFL harness around the function:</p><pre>int main(int argc, char *argv[]) {<br>    FILE *fp = fopen(argv[1], &quot;r&quot;);<br>    int result = readconf(fp);<br>    fclose(fp);<br>    return result;<br>}</pre><p>That’s a good approach and it will work. But what if we want to spice things up and use persistent fuzzing instead of the usual fork server? The fork server mode is self-explanatory: on each fuzzing iteration, a new fork of the target process is started. But in persistent mode, AFL++ will execute our target function in a loop: no forks — more speed, and that’s what we want.</p><p>AFL++ has had persistent mode for some time, but implementing a harness for it always felt kind of clumsy, to be honest. Your code is supposed to look like this <a href="https://github.com/AFLplusplus/AFLplusplus/blob/stable/instrumentation/README.persistent_mode.md">according to the docs</a>:</p><pre>__AFL_FUZZ_INIT();<br>main() {<br>#ifdef __AFL_HAVE_MANUAL_CONTROL<br>  __AFL_INIT();<br>#endif<br>  unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF;  // must be after __AFL_INIT<br>                                                 // and before __AFL_LOOP!<br>  while (__AFL_LOOP(10000)) {<br>    int len = __AFL_FUZZ_TESTCASE_LEN;  <br>    target_function(buf, len);<br>  }<br>  return 0;<br>}</pre><p>This works, but we can do better than that. To find out how, we will have to do the unthinkable — go dig deeper into the docs. And <a href="https://aflplus.plus/docs/fuzzing_in_depth/">there</a>, we find that:</p><blockquote><em>libfuzzer </em><em>LLVMFuzzerTestOneInput() harnesses are the defacto standard for fuzzing, and they can be used with AFL++ (and honggfuzz) as well!</em></blockquote><p>Great news: on top of better (in my opinion) syntax, we get the ability to use our harness with not one, but three fuzzers for exactly 0 additional cost.</p><p>To refresh our memory, a minimal libfuzzer harness is just 4 lines of code:</p><pre>extern &quot;C&quot; int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {<br>  DoSomethingWithData(Data, Size);<br>  return 0;<br>}</pre><p>Let’s rewrite it for our case:</p><pre>// include or copy target function code <br>// and everything it needs to work<br>int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {<br>    char tmpfilename[] = &quot;readconf_fuzzXXXXXX&quot;;<br>    FILE *fp = fopen(tmpfilename, &quot;wb&quot;);<br>  fwrite(Data, 1, Size, fp);<br>  int result = readconf(fp);<br>    fclose(fp);<br>    return result;<br>}</pre><p>We lose extern &quot;C&quot; since the code is C, not C++, then write the data buffer to a file and pass the FILE pointer to the target function.</p><p>But it’s 2024 already and storing files in the filesystem is so out of fashion. And we already get less respect from our friends because of the function we chose to fuzz! Oh yeah, and storing something in the filesystem instead of memory will slow things down.</p><p>We could save files in /tmp, which is memory-backed, but what if we didn’t have to involve the filesystem at all, thus totally eliminating the middle man. Fuzzing is a game of speed, and doing so will allow us to gain some by not having to wait for the system call execution.</p><p>We can achieve this with fmemopen — a function from the C standard library that&#39;s been there for years, but is still not well known. It allows us to open a memory buffer as a file and returns a FILE pointer that&#39;s compatible with all file operations. With it, our fuzzing function will look like this, nothing unnecessary, everything stays in memory:</p><pre>int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {<br>    FILE *fp = fmemopen(Data, Size, &quot;r&quot;);<br>    readconf(fp);<br>    fclose(fp);<br>    return 0;<br>}</pre><p>Just 6 lines of code, so concise, but so powerful! No, I’m not crying, you’re!</p><h3>Fuzzing</h3><p>Let’s compile our code:</p><pre>afl-clang-fast -fsanitize=fuzzer -o harness harness.c</pre><p>After fixing a bunch of C stuff Immediately after, we can start the fuzzer:</p><pre>afl-fuzz -i inputs -o outputs ./harness</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s_DTHJqD8mnWN3Xyl1SXdg.png" /></figure><p>The execution speed is 120,000 runs per second. Really fast, probably too fast to be true. Normally I’d add some logging and investigate the situation. But we introduced a lot of optimizations and compiled the harness using afl-clang-fast. There are <a href="https://x9security.com/fuzzing-explained-with-afl/">reports</a> of persistence mode achieving more or less the same order of speed we have here. On top of that, I used really powerful hardware to run it. So I’d say it’s fine for our learning purposes.</p><p>The next morning, we’re presented with this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LkhcOkJufnT33cR6OPlrVQ.png" /></figure><p>Unfortunately for us, no bugs were found. We can also see that last new find is more than 6 hours. It means that it&#39;s been 6 hours since AFL++ saw a new execution path. Considering that it also cycled through all possible mutations 2008 times, it’s safe to say that we can conclude our fuzzing campaign.</p><p>Yes, we don’t have any findings, but maybe the real findings were the things we’ve learned along the way?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bba1366b412" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Local LLM for Ultimate Hacker Chat?]]></title>
            <link>https://slava-moskvin.medium.com/local-llm-for-ultimate-hacker-chat-b67c49f9f953?source=rss-ccc00f894042------2</link>
            <guid isPermaLink="false">https://medium.com/p/b67c49f9f953</guid>
            <dc:creator><![CDATA[Path Cybersec [Slava Moskvin]]]></dc:creator>
            <pubDate>Thu, 07 Mar 2024 21:10:34 GMT</pubDate>
            <atom:updated>2024-03-07T21:10:34.554Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dfXm9573PWmqYWREVJQqpQ.jpeg" /></figure><h3>The Idea</h3><p>I’m quite intrigued by AI capabilities, LLMs in particular. I use ChatGPT in my work almost every day, certainly every week. One big downside, however, is that I can’t use it with private data. Imagine how cool it’d be to feed an LLM hundreds of customers’ docs to be able to quickly get answers. Or put in artifacts from firmware (e.g., strings) and see what insights you can get with that. I know, genius!</p><p>Of course, there are self-hosted alternatives to ChatGPT/Copilot/Gemini/etc. So, I decided to run some experiments on my own PC. As I couldn’t use any private data for that, my goal was simple: to use existing research and blog posts on hacking to create an ultimate hacking LLM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0FDRickFvlo3NUG6o01TWg.png" /></figure><p>With enough knowledge, anyone can snag an open-source model from <a href="https://huggingface.co/">https://huggingface.co</a>, throw in some code, or harness an open-source UI for a chat. My non-existent ML knowledge was just enough to copy and paste code from examples from the Internet.</p><p>Stuck with a “mere” 16GB of RAM (which was more like 10 on my Windows 11 setup), each attempt insisted on loading models from SSD into RAM before transferring them to the GPU, a bottleneck for every query. To add insult to injury, my setup barely supported models over 3 billion parameters (for context, RTX can run LLAMA 13B, ChatGPT 3.5 hits 175B).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DV2V9T8CN-pkjGzf5EiZeQ.png" /></figure><p>So, I deemed that experiment a failure and moved on. Until Chat with RTX came along. It’s a tool from NVIDIA that allows you to chat with an LLM using an RTX video card. It’s specifically tailored to chat with any set of documents you have on your PC.</p><h3>About Chat with RTX</h3><p>The first impression is that it’s very fast. Both to set up and run. The setup can be done even by a simple individual as myself: just double-click on the installer downloaded from NVIDIA’s website. Loading documents takes no longer than a minute. Chat with RTX uses no more than a couple of minutes and 2.5 GBs of RAM to process the whole dataset and answers after that in an instant. Using the beefier LLAMA 13B (as an alternative to the only other supported model, Mistral 7B), it consumes 12.5 GB of video memory. But what about the quality of the answers? Well, let’s see.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*ETmy665K78AxgFC3mBXlug.png" /></figure><h3>The Chat</h3><p>To start with, I decided to download every post from the Google Project Zero blog. That made for 194 files filled with hacking knowledge. With Chat with RTX locked and loaded, I decided to ask it about techniques it took away from the posts… It didn’t go very well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QbG-ajcAX8I-Q7IXbp5ifA.png" /></figure><p>I tried to be sneaky and ask it not about “hacking” but about “security evaluation” techniques it knows from the files I provided. Instead, it started to describe the security evaluation of files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nhUvjvIEt91JjDRha-6IZg.png" /></figure><p>Asking about “exploitation techniques” improved things: a somewhat coherent response that could be considered good for an LLM only familiar with P0 posts, which focus on specific techniques.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5S7lq8vowGcE3MlxMOFXtA.png" /></figure><p>Then it changed its mood to uncooperative again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*os9MOOMVHtY3mJ9DsQv9ow.png" /></figure><p>Begging didn’t help. The model is too modest despite knowing everything that’s in the P0 blog, and NVIDIA is too scared of giving you any hacking-related info (even from your own documents!).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fkJeTa25S6Y-_mUlX_KtOQ.png" /></figure><p>Even simpler questions also turned out not to be that easy. Asked to summarize the files, it listed 4 random files from the folder and tried to describe each file individually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XfKJZyQ_nwUHMiveMkQ-Lg.png" /></figure><p>Of course, there were more than 4 files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*wXCqB9PGwXCodQL4WDVsiw.png" /></figure><p>Formulating the request differently didn’t help either:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s9bNfGHZohwD5eLFcQDACA.png" /></figure><p>Desperate, I just asked it what it can do. To answer that, it took data from a random file, for some reason:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5NFaKeUuoigLdrwmrRAsvQ.png" /></figure><p>On the other hand, it was somewhat decent, or at least coherent when asked to chat about a single document:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73crz__qH-_bF-iL9wBWtg.png" /></figure><p>It even remembered the context and guessed that SLUB is somehow related to memory allocation. It also told me that the bug was “in some random kernel subsystem,” which begged the question: what is the subsystem?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*caI2L-xzo3TH4BRpUjNyPQ.png" /></figure><p>Aaaand the magic is over:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HxCleXGXo5G3icYS6YctRg.png" /></figure><p>Okay, maybe LLAMA 13B is too heavy and doesn’t produce the best results. MAYBE. Okay, let’s try Mistral 7B. Welp… It became even worse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wTwFlh-9iohHgEiWg0haWg.png" /></figure><p>Okay, it’s somewhat usable when asked to chat about a specific document. Not that useful for me, but still. Maybe it can analyze a set of files in a different way. For example, list all files on a specific topic.</p><p>Asked to list all files related to iOS, it picked just 3 random files. Even searching for files with “iOS” and “iPhone” in the names produces more results than that!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G_N883DMl_COcpP6Z4OFTA.png" /></figure><h3>Conclusion</h3><p>You can see that I’m not that enthusiastic about the results. For my purposes, Chat with RTX may be marginally better than grep in very narrow use cases. Maybe I just misused the tool, or maybe the topic is too complex, or my dataset is just improper. Another possibility is that it needs a single file for a single narrow topic. If either of those is the case, let me know in the comments how I can improve this.</p><p>Still, maybe it could be useful for some more generic (and SFW, unlike scary hacking!) topics. But it’s a step to make local LLMs more accessible to the general public. I hope to see more progress in that direction in 1–2 years.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b67c49f9f953" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>