Elastic Objects: msg_msg


Kernel Exploitation: A CTF Study Path.

System V message queues (msgsnd/msgrcv) let an unprivileged user allocate objects of almost any size with controlled contents. That makes struct msg_msg the go-to spray object, and corrupting its header (m_ts, next) turns it into an out-of-bounds read, an arbitrary read, and with a stalled copy an arbitrary write.

What you will learn

  • msg_msg / msg_msgseg layout and which caches they land in
  • Using MSG_COPY to read without freeing; corrupting m_ts for OOB reads
  • Forging next for arbitrary read; combining with userfaultfd/FUSE for arbitrary write
  • Walking task_struct lists from an arbitrary read to find your own cred
  • Exploiting when modprobe_path is unavailable (STATIC_USERMODEHELPER) and under FG-KASLR

Prerequisites

Modules 04-05. Module 08 (userfaultfd) helps for the hardest challenges here.

References



Challenges

HITCON CTF 2020 · difficulty ★★★☆☆ · module: Elastic Objects: msg_msg

A driver that shares pages through mmap but forgets a corner of the VMA lifecycle; turn the stale page into root.

Mitigations: KASLR, no SMEP/SMAP (qemu64), softlockup_panic

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit. In the original, the patched kernel prints the flag in its soft-lockup report. Rewriting the console to show your real flag there would let any guest user print the placeholder and read the flag, so here the report shows the compiled-in placeholder hitcon{<FLAG WILL BE HERE>}: triggering it proves the bug but does not give the flag. Get root and read /flag (root only) instead. The shell is killed 60 seconds after boot.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): atoms.ko, bzImage, demo.c, demo.gz, initramfs.cpio.gz, linux.diff, run.sh.

Original challenge

Description

A TOken-based Memory Storage.

Points

296

Solves

17

Credits

  • Challenge author(s): david942j, for HITCON CTF 2020
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

ASIS CTF Quals 2021 · difficulty ★★★★☆ · module: Elastic Objects: msg_msg

Three bytes past the end of a tiny note and no read primitive at all: grow one with msg_msg.

Mitigations: KASLR, KPTI off (pti=off), no SMEP/SMAP (kvm64), oops=panic

Also uses techniques from: SLUB Internals: Freelist Poisoning and Heap Overflows (05-slub-freelist).

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit. The original's user-mode network is gone, so the boot-time network scripts report failures; that is harmless.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): bzImage, minimemo.c, rootfs.cpio, start-qemu.sh.

Original challenge

Description

Little notes for some little ideas.

Points

217

Solves

16

Credits

  • Challenge author(s): ptr-yudai, for ASIS CTF Quals 2021
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

corCTF 2021 · difficulty ★★★★★ · module: Elastic Objects: msg_msg

A netfilter-style firewall module on a hardened kernel where modprobe_path tricks are off the table; build arbitrary read and write from a large-object UAF.

Mitigations: SMEP, SMAP, KPTI, KASLR, SLAB_FREELIST_RANDOM, SLAB_FREELIST_HARDENED, STATIC_USERMODEHELPER

Also uses techniques from: Races: Double Fetch, userfaultfd and FUSE (08-races).

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): bzImage, firewall.c, initramfs.cpio.gz, run.sh.

Original challenge

Description

Elastic objects in kernel have more power than you think. Note that in addition to a standard initramfs and compressed kernel image, the kernel with symbols and debugging information is provided as well. A kernel config file is provided as well, but some of the important options include:

CONFIG_SLAB=y
CONFIG_SLAB_FREELIST_RANDOM=y
CONFIG_SLAB_FREELIST_HARDEN=y
CONFIG_STATIC_USERMODEHELPER=y
CONFIG_STATIC_USERMODEHELPER_PATH=""
SMEP, SMAP, and KPTI are of course on. Note that this is an easier variation of the Wall of Perdition challenge.

Points

500

Solves

0

Credits

  • Challenge author(s): - FizzBuzz101 - D3v17, for corCTF 2021
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

corCTF 2021 · difficulty ★★★★★ · module: Elastic Objects: msg_msg

The harder sibling of Fire of Salvation: a smaller UAF and function-granular KASLR.

Mitigations: SMEP, SMAP, KPTI, KASLR, FG-KASLR

Also uses techniques from: pipe_buffer, Dirty Pipe and Page Cache Overwrites (07-pipe-buffer), Races: Double Fetch, userfaultfd and FUSE (08-races).

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit. The source is the same firewall.c as Fire of Salvation (not shipped with this handout).

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): bzImage, initramfs.cpio.gz, run.sh.

Original challenge

Description

Elastic objects seem to have even more power in many more slabs! Note that this is a harder variation of the Fire of Salvation challenge.

Points

500

Solves

0

Credits

  • Challenge author(s): - FizzBuzz101 - D3v17, for corCTF 2021
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score