IERG 4851 · Week 11

In-Class Lab: Kernel ret2usr

One 32-bit Linux guest OS running inside QEMU, five tasks, one final privilege-escalation flag.

1. How this lab runs

This is a self-contained kernel lab on ierg4851. The target is a 32-bit Linux guest OS running inside a QEMU virtual machine; the ierg4851 server is only the host used to launch it. You work on the target assigned to your own Unix account, inspect one real vulnerable module, and finish by changing the credentials of a process inside the QEMU virtual machine.

#TaskWhat you learnLevel
1Start the guest and trace the safe deviceHow a user write() reaches a module callbackWarm-up
2Debug vuln_write()Arguments, copy_from_user(), and the second copyRequired
3Measure the stack frameWhy the overwrite reaches saved EIPRequired
4Build the ret2usr requestPadding, user stub, credential primitives, and iretRequired
5Run and submitPrivilege escalation, not just a crashFlag
Predict before you run. At each task, write down what should happen, then compare it with the guest output and your GDB observations.

2. Connect to the lab machine

Open two SSH terminals and connect both to ierg4851 with your own course Unix account:

ssh ierg4851

Use the same account in both terminals. Do not use another student's account, home directory, or guest port.

3. QEMU, the launcher, and the ports

QEMU is the emulator/virtual machine. It runs the complete 32-bit x86 Linux 5.10 guest OS used in this lab. The guest OS has its own kernel, modules, devices, processes, and root filesystem inside QEMU. A kernel crash or a root shell stays inside that QEMU virtual machine; it does not give access to the ierg4851 server host.

ierg4851-kernel-lab is the course launcher. It selects the guest state for your Unix account, starts QEMU with the teaching kernel and modules, and assigns the debugging endpoint for your session. You do not need to type a QEMU command line or choose an instance number.

In the first SSH terminal, run the normal launcher:

ierg4851-kernel-lab

After the normal run, stop it with Ctrl-C. Then start a fresh debug guest in the first terminal:

ierg4851-kernel-lab debug

The debug launcher prints output similar to:

account: u1155203181
assigned web-lab port: 9201
GDB: target remote 127.0.0.1:12341
ValuePurposeUse in this lab
Unix accountChooses your private guest stateUse your own account
web-lab portPrinted by the shared launcherIgnore it for this lab
GDB portQEMU's kernel remote-debug endpoint on 127.0.0.1Use this exact port in target remote
Ports are not interchangeable. Do not put the web-lab port into GDB, and do not copy the example 12341. Use the GDB port printed for your own debug session.

4. Start your private guest

Inside the guest, confirm that you are an ordinary user and that the two teaching devices exist:

id
ls -l /dev/ierg4851_demo /dev/ierg4851_vuln
cat /usr/share/kernel-symbols
Do not choose an instance number. The launcher uses your Unix account to select your guest state and GDB port. Never copy another student's port or home directory.

5. Task 1 — the safe device

Begin with the complete user-to-kernel path. A shell redirection still performs a real open() and write(); the important point is that the bytes originate in user memory.

printf 'hello from user space\n' > /dev/ierg4851_demo
dmesg

Use the module's log line to connect the user operation to the callback:

user program
  → write(fd, user_buf, count)
  → i386 system-call entry
  → VFS resolves fd to struct file
  → file->f_op->write(file, user_buf, count, pos)
  → demo_write()
Checkpoint: identify the file descriptor, the user pointer, the count, and the kernel-owned destination. In one sentence, explain why a module cannot treat user_buf as an ordinary trusted pointer.

6. Task 2 — debug the vulnerable callback

Keep the debug guest running in the first SSH terminal. In the second SSH terminal, connect GDB to the exact port printed by the launcher. Do not start a second QEMU guest:

GDB: target remote 127.0.0.1:YOUR_GDB_PORT

In the second terminal, connect with the matching port:

gdb /opt/ierg4851-kernel-lab/vmlinux
(gdb) set architecture i386
(gdb) target remote 127.0.0.1:YOUR_GDB_PORT
(gdb) continue

After the guest shell is ready, read the module text address in the guest and load the module symbols in GDB:

cat /sys/module/ierg4851_vuln/sections/.text

If sysfs denies access for your student account, use the address exported by the guest init script:

cat /tmp/vuln-text-address
(gdb) add-symbol-file /opt/ierg4851-kernel-lab/ierg4851_vuln.ko TEXT_ADDRESS
(gdb) break vuln_write
(gdb) continue

In the first terminal, trigger the callback:

printf 'AAAA' > /dev/ierg4851_vuln

At the breakpoint, inspect the i386 arguments and follow the two copies. Your notes should distinguish:

Record: explain why copy_from_user(staging, buf, count) is not the overflow in this target, and why memcpy(tmp, staging, count) is.

7. Task 3 — measure the real stack frame

Do not guess the offset from the C source or rely only on p &tmp. For this compiled module, use the instruction that computes the destination of the second copy. The relevant address is ebp-0x2c; the saved return address is at ebp+0x04.

(gdb) disassemble vuln_write
(gdb) disassemble /r vuln_write.cold
(gdb) info registers ebp esp
(gdb) x/wx $ebp+4
(gdb) p/x $ebp-0x2c
(gdb) p/d (long)($ebp+4) - (long)($ebp-0x2c)

Record these three values in your worksheet:

ValueYour observation
temporary object used by memcpy$ebp - 0x2c = __________________
saved return address$ebp + 4 = ______________
distancesaved_return - ($ebp - 0x2c) = ______ bytes

Now explain the relevant frame layout:

lower addresses
  tmp[32]
  compiler padding / saved frame data
  saved EBP
  saved return EIP  <-- control-flow target
higher addresses
Record: derive the offset from the disassembly of your guest module. In the supplied teaching build, the calculation is 0x2c + 0x04 = 48.

8. Task 4 — build the ret2usr request

The supplied harness maps a small user-space code stub, places the credential-transition logic in that stub, and writes the return-address overwrite through the vulnerable device. Run the symbol command in the guest shell, not on the ierg4851 host:

cat /usr/share/kernel-symbols
commit_creds  ADDRESS_A
prepare_kernel_cred  ADDRESS_B

Use the address on the prepare_kernel_cred line as the second argument, and the address on the commit_creds line as the third argument. The first argument is the byte offset derived in Task 3. For example, if the guest prints:

c1089850 T commit_creds
c1089a90 T prepare_kernel_cred

the student command is:

/usr/bin/ret2usr 48 0xc1089a90 0xc1089850
Important: names such as YOUR_PREPARE_KERNEL_CRED and YOUR_COMMIT_CREDS are placeholders, not literal text and not environment variables. Replace them with the addresses printed by your own guest. If GDB is stopped at vuln_write when you run the command, switch to the GDB terminal and type continue. The expected student result is the guest flag, not an interactive shell.

The payload has two parts: padding up to the saved return address, then the four-byte address of the user-space stub. The target is the saved return address in kernel space:

payload[0 .. OFFSET-1] = padding
payload[OFFSET .. OFFSET+3] = address of USER_STUB
write(fd, payload, OFFSET + 4)

After the overwritten return executes in kernel privilege, the stub calls prepare_kernel_cred(0), passes the result to commit_creds(), constructs an iret return frame, and returns to the user-space success function.

Before running: say which bytes overwrite saved EIP, why the stub must be executable in the teaching guest, and why commit_creds() changes the security impact from control-flow redirection to privilege escalation.

9. Task 5 — run the exploit and submit the flag

In the guest shell, run the parameterized harness only after you have recorded your own offset and symbols:

cat /usr/share/kernel-symbols
/usr/bin/ret2usr YOUR_OFFSET YOUR_PREPARE_KERNEL_CRED YOUR_COMMIT_CREDS

Replace all three uppercase names with values from your own guest: YOUR_OFFSET is the distance measured in Task 3, YOUR_PREPARE_KERNEL_CRED is the address on the prepare_kernel_cred line, and YOUR_COMMIT_CREDS is the address on the commit_creds line. They are placeholders, not literal arguments. For the example symbols and offset 48, the exact command is:

/usr/bin/ret2usr 48 0xc1089a90 0xc1089850

If GDB is stopped at vuln_write, type continue in the GDB terminal after starting the command. The successful result is the account-specific guest flag printed in the guest shell.

A successful student run prints your account-specific guest flag. Submit it using the scoring tools already installed in your home directory:

cd ~/ierg4851/contactless-ctf
python3 score/keygen.py

Copy the address: printed by keygen.py. Then copy the exact flag printed by the guest. It starts with IERG4851_KERNEL_RET2USR_ and ends with eight hexadecimal characters:

python3 score/redeem.py \
  --flag IERG4851_KERNEL_RET2USR_<8 hex> \
  --to YOUR_ADDRESS
myscore

OK 250 is the receipt. The flag belongs to your Unix account, so redeem your own flag to the address registered by your own keygen.py. There is no separate form or Blackboard submission for this in-class lab. Stop the guest with Ctrl-C after redeeming.

The scoreboard listens on ierg4851 localhost only. From your own computer, open a third terminal and create an SSH tunnel:

ssh -N -L 8080:127.0.0.1:8080 ierg4851

Then open http://127.0.0.1:8080/ in your browser and use the IERG 4851 scoreboard username and password provided by the instructor. Keep the tunnel running while viewing the board. Run myscore on ierg4851 to see your own points and rank.

Success condition: a crash is not enough. Your record must show the measured frame, the payload layout, the credential transition, and the exact flag.
Do not run this on the host. The module and harness are intentionally vulnerable and are authorized only inside the assigned disposable guest.

10. What to hand in during class

This page is the complete student lab manual. There is no separate checkpoint sheet.