0

I was reading the paper Hiding in the Shadows: Empowering ARM for Stealthy Virtual Machine Introspection and I was wondering whether the steps they are described in paragraph "2.3 Debug Exceptions" were correct or not:

AArch64 allows to generate Software Step exceptions by setting the SS bit of the Monitor Debug System Control MDSCR_EL1 and Saved Program Status Register SPSR of the target exception level. For instance, to single-step a hit breakpoint in EL1 the monitor must set the MDSCR_EL1.SS and SPSR_EL1.SS bits. After returning to the trapped instruction, the SPSR will be written to the process state PSTATE register in EL1. Consequently, the CPU executes the next instruction and generates a Software Step exception.

I have tried to understand how single-stepping happens in freeBSD, and I am noticing a mismatch.

I am basing the following lines of code to the release 12.3.0 of freeBSD (4 December 2021), commit: 70cb68e7a00ac0310a2d0ca428c1d5018e6d39e1. I chose to base this question on freeBSD because, in my opinion, following its code is easier than Linux, but the same principles shall be common to both families.

According to my understanding, this is what happens in freeBSD:

1- Ptrace single step is invoked, arriving in the architecture-independent code proc_sstep(), in sys_process.c:

int proc_sstep(struct thread *td)
{

    PROC_ACTION(ptrace_single_step(td));
}

2- Architecture-dependent code ptrace_single_step()is called, in arm64/ptrace_machdep.c:

int ptrace_single_step(struct thread *td)
{

    td->td_frame->tf_spsr |= PSR_SS;
    td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
    return (0);
}

Here single step bit (number 21) is set in the "Process State" of the tracee (tracee = thread that is traced) and a flag is set.

3- After a while, the traced task will be selected for scheduling. In cpu_throw() of swtch.S (where the new thread takes place), the flags of the new thread are checked, to see if it must single step:

/* If we are single stepping, enable it */
ldr w5, [x4, #PCB_FLAGS]
set_step_flag w5, x6

4- set_step_flag macro in defined in the same swtch.S:

.macro set_step_flag pcbflags, tmp
    tbz \pcbflags, #PCB_SINGLE_STEP_SHIFT, 999f
    mrs \tmp, mdscr_el1
    orr \tmp, \tmp, #1
    msr mdscr_el1, \tmp
    isb
999:
.endm

Here, if the single-step flag is set, it sets the single step bit of register MDSCR_EL1 (bit in position 0).

4- To the best of my understanding, the combination of single step bit on SPSR_EL1 of the "Pstate" + single step bit on MDSCRL_EL1 implies that the tracee execute 1 instruction and it traps.

5- Trap is recognized as a EXCP_SOFTSTP_EL0 and it is handled in do_el0_sync() function of trap.c:

case EXCP_SOFTSTP_EL0:
    td->td_frame->tf_spsr &= ~PSR_SS;
    td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
    WRITE_SPECIALREG(mdscr_el1,
        READ_SPECIALREG(mdscr_el1) & ~DBG_MDSCR_SS);
    call_trapsignal(td, SIGTRAP, TRAP_TRACE,
        (void *)frame->tf_elr, exception);
    userret(td, frame);
    break;

Here, all the flags are reset and the traced thread receives a SIGTRAP (sent by itself, I think). Being traced, it will stop. And the tracer, at this point, can return from a possible waitpid().

What I could observe differs from the paper explanation. Can you check and correct the steps that I listed, please ?

  • How does this differ from the paper? To me it looks like it conforms to it... – Siguza Dec 09 '21 at 17:53
  • Hello @Siguza, what I do not agree is the sentence: `After returning to the trapped instruction, the SPSR will be written to the process state PSTATE register in EL1.`. In my opinion, the instruction traps (generating an exception) because MDSCR+PSTATE are set, but, according to the paper, `PSTATE SS bit` is written after the instruction traps... – ParisHilton Dec 10 '21 at 03:06
  • 2
    In the example in the paper, the instruction initially traps because a hardware breakpoint is set. Then every subsequent instruction traps because the SS bit is set. But it's all the same anyway. After the exception is before the exception. – Siguza Dec 10 '21 at 14:35
  • Yes, thanks, I confirm. – MM92x Jan 05 '22 at 02:53

0 Answers0