SlideShare una empresa de Scribd logo
1 de 81
Descargar para leer sin conexión
Introduction



●   Jon Oberheide

●   Dan Rosenberg




      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #2
Introduction

 In reference to the HES CFP:

 “I get excited every time I see a
 conference add requirements to
 their talk selection along the lines
 of 'exploitation presentations must
 be against grsecurity/PaX' -- but
 then there never ends up being any
 presentations of this kind.”
                                  – spender pratt

    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #3
Agenda

●   A review of Linux kernel security

●   Exploitation vs. grsecurity/PaX

●   Bypassing grsecurity/PaX


      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #4
A decade of kernel security




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #5
A decade of kernel security




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #6
Upstream attitude




 ●   Security is hard when upstream ignores the problems
 ●   Linux still hasn't had its “security awakening”


       Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #7
How about last year?

●   142 CVE's assigned
    ●   30% worse than the previous worst year (2009)
    ●   Based on public CVE requests, issues tracked at
        Red Hat Bugzilla, and Eugene's tagged git tree
    ●   Missing dozens of non-CVE vulnerabilities (i.e. the
        “Dan Carpenter factor”)
●   61 (43%) discovered by six people
    ●   Kees (4), Brad (3), Tavis (7), Vasiliy (4), Dan (37),
        Nelson (6)

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #8
Kernel vulns in 2010

● 12 known exploits for local privilege
  escalation
● 13 remotely triggerable issues


● 33 potential privilege escalations




    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #9
Breakdown by Target

                                        2
                        31




                                                           76

                   33
                                                                        Core
                                                                        Distro
                                                                        Exotic
                                                                        Red Hat


   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #10
Breakdown by Impact

                               13      1
                                                  26



                                                             7



                  65                                                     Bypass
                                                        30               DOS
                                                                         Info
                                                                         Priv Esc?
                                                                         Priv Esc
                                                                         Nothing

   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg    Slide #11
Interesting exploits of 2010
●   full-nelson.c
    ●   Combined three vulns to get a NULL write
●   half-nelson.c
    ●   First Linux kernel stack overflow (not buffer overflow) exploit
●   linux-rds-exploit.c
    ●   Arbitrary write in RDS packet family
●   i-CAN-haz-MODHARDEN.c
    ●   SLUB overflow in CAN packet family
●   american-sign-language.c
    ●   Exploit payload written in ACPI's ASL/AML
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #12
Agenda

●   A review of Linux kernel security

●   Exploitation vs. grsecurity/PaX

●   Bypassing grsecurity/PaX


      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #13
Traditional Linux exploitation

● Perhaps most general exploitation
  primitive is an arbitrary kernel write
● Sometimes occurs naturally, other

  times can be constructed (e.g.
  overwriting pointers in an overflow to
  trigger a write)


    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #14
Linux exploitation examples

● Writes to known addresses (IDT)
● Function pointer overwrites


● Redirecting control flow to userspace


● Influencing privesc-related kernel data

  (eg. credentials structures)
● Relying on kallsyms and other info




    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #15
Overview of grsecurity/PaX

●   grsecurity/PaX
    ●   Third-party patchset to harden Linux
        userspace/kernel security
●   Attempts to prevent
    ●   Introduction/execution of arbitrary code
    ●   Execution of existing code out of original order
    ●   Execution of existing code in original order with
        arbitrary data


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #16
grsecurity/PaX hardening
●   Kernel hardening features:
    ●   KERNEXEC
        ●   Prevent the introduction of new executable code
    ●   UDEREF
        ●   Prevent invalid userspace pointer dereferences
    ●   HIDESYM
        ●   Hide info that may be useful to an attacker (kallsyms,
            slabinfo, kernel address leaks, etc)
    ●   MODHARDEN
        ●   Prevent auto-loading of crappy unused packet families
            (CAN, RDS, econet, etc)
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #17
Agenda

●   A review of Linux kernel security

●   Exploitation vs. grsecurity/PaX

●   Bypassing grsecurity/PaX


      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #18
The main event

 ●   A technique we call stackjacking
     ●   Enables the bypass of common grsecurity/PaX
         configurations with common exploit primatives

     ●   Independently discovered, collaboratively
         exploited, with slightly different techniques




         Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #19
Plan of attack!

   ???
                               STACK
                              JACKING                                          ROOT
    ???
                             OVERVIEW



   ???                                   ???                                    ???




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg     Slide #20
Target kernel assumptions

●   Hardened kernel with grsec/PaX
    ●   Config level GRKERNSEC_HIGH
    ●   KERNEXEC
    ●   UDEREF
    ●   HIDESYM
    ●   MODHARDEN
    ●   Etc...



        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #21
Stronger target assumptions

●   Let's make some extra assumptions
    ●   We like a challenge, and these are assumptions that
        may possibly be obtainable now or in the future
●   Stronger target assumptions
    ●   Zero knowledge of kernel address space
    ●   Fully randomized kernel text/data
    ●   Cannot introduce new code into kernel address space
    ●   Cannot modify kernel control flow (eg. data-only)


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #22
Attacker assumption #1

●   Assumption: arbitrary kmem write
    ●   A common kernel exploitation primitive
    ●   Examples: RDS, MCAST_MSFILTER
    ●   Other vulns can be turned into writes, e.g.
        overflowing into a pointer that's written to
●   Wut?
    ●   “You mean I can't escalate privs with an arbitrary
        kernel memory write normally?” NOPE.


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #23
Arbitrary write into the abyss
                                                          DARKNESS!
0xffffffff

                 kernel

0xc0000000
(TASK_SIZE)




                  user




                                                No clue where to write!
0x00000000
                                               Exploitation is infeasible.
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #24
What's the secret sauce?




ARBITRARY
  WRITE                     +                     ?                     = <3

   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #25
The Great Sandwich?




ARBITRARY
  WRITE                     +                                           = <3
                                                msuiche?




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #26
Nah, he's taken




                            +                                           = <3

   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #27
Need to know something

●   One way: arbitrary kmem disclosure
    ●   procfs (2005)
    ●   sctp (2008)
    ●   move_pages (2009)
    ●   pktcdvd (2010)
●   Just dump entire address space!
    ●   But these are rare!
    ●   And in many instances, mitigated by grsec/PaX

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #28
Something more common?

● How about a more common vuln?
● Hints...

    ●   Widely considered to be a useless vulnerability
    ●   Commonly assigned a CVSS score of 1.9 (low)
    ●   25+ such vulnerabilities reported in 2010
    ●   Often referred to as a Dan Rosenbug
●   Can you guess it???

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #29
KSTACK MEM DISCLOSURE!




ARBITRARY
  WRITE                     +              KSTACK
                                            LEAK                        = <3

   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #30
How does kstack leak help?




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #31
A bit about Linux kernel stacks

●   Each userspace thread is                                                             high address
    allocated a kernel stack
                                                                   grows down
●   Stores stack frames for kernel
    syscalls and other metadata                                                              4k/8k
                                                                                             stack

                                                                     unused
●   Most commonly 8k, some
    distros use 4k
    ●   THREAD_SIZE =
        2*PAGE_SIZE =                                                                    low address
        2*4086 = 8192


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg      Slide #32
Kernel stack mem disclosures

●   Kstack mem disclosures
    ●   Leak of memory from the kernel stack to userspace
●   Common cause
    ●   Copying a struct on the kstack back to userspace
        with uninitialized fields
    ●   Improper initialization/memset, forgetting member
        assignment, structure padding/holes
    ●   A frequent occurrence, especially in compat


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #33
Kernel stack mem disclosures
                                                                  struct foo {
                                                                    uint32_t bar;
                                                                    uint32_t leak;
    kstack frame                     kstack frame
                                                                    uint32_t baz;
                                        foo.bar                   };
   sensitive data                   sensitive data
                                      foo.leak                    syscall() {
                                        foo.baz                     struct foo;
         .                                                          foo.bar = 1;
         .                                                          foo.baz = 2;
         .                                                          copy_to_user(foo);
                                                                  }
  1) process                   2) kstack is reused                3) foo struct is copied to
  makes syscall                on subsequent                      userspace, leaking 4
  and leaves                   syscall and struct                 bytes of kstack through
  sensitive data               overlaps with                      uninitialized foo.leak
  on kstack                    sensitive data                     member

     Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #34
Plan of attack!

  Arbitrary write
                                   STACK
                                  JACKING                                          ROOT
 Kstack disclosure
                                 OVERVIEW



       ???                                   ???                                    ???




       Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg     Slide #35
What's useful on the kstack?

●   Leak data off kstack?
    ●   Sensitive data left behind? Not really...
●   Leak addresses off kstack?
    ●   Sensitive addresses left behind? Maybe...
        ●   Pointers to known structures could be exploited
        ●   Too specific of an attack!
●   Need something more general
    ●   kstack disclosures differ widely in size/offsets

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #36
Kernel stack addresses

●   How about a leaking an address that:
    ●   Is stored on the stack; and
    ●   Points to an address on the stack
●   These are pretty common
    ●   Eg. pointers to local stack vars, saved ebp, etc
●   But what does this gain us?


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #37
Kernel stack self-discovery

●   If we can leak an pointer                                  0xcdef2000
    to the kstack off the kstack,
    we can calculate the base                                  0xcdef1234
                                                                                     kstack frame

    address of the kstack                                                             0xdeadbeef


      kstack_base = addr & ~(THREAD_SIZE – 1);                                        0xcdef1234

      kstack_base = 0xcdef1234 & ~(8192 – 1)                                              .
                                                                                          .
      kstack_base = 0xcdef0000                                 0xcdef0000
                                                                                          .

              We call this kstack self-discovery

         Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg    Slide #38
Effective kstack discovery

●   Not all kstack disclosures are alike
    ●   May only leak a few bytes, non-consecutive
    ●   How do we effectively self-discover?
●   Manual analysis
    ●   Figure out where kstack leak overlaps addresses
●   Automatic analysis
    ●   libkstack


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #39
Manual kstack self-discovery

●   Manual, offline analysis
    ●   1. prime stack with random syscall
    ●   2. leak bytes, see if any leaks match real kstack
    ●   3. repeat until we've collected enough bytes
    ●   4. construct list of priming syscalls needed for the
        particular leak to spill the beans




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #40
Automatic with libkstack

●   We can automate this process for
    runtime self-discovery with libkstack
    ●   1. prime stack with random syscall
    ●   2. leak bytes, infer whether bytes belong to a kstack
        addr
    ●   3. repeat until we have sufficient confidence to
        calculate the kstack base addr




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #41
Plan of attack!

   Arbitrary write
                                    STACK
                                   JACKING                                          ROOT
 Kstack disclosure
                                  OVERVIEW

    STACK
SELF-DISCOVERY

  Manual analysis                             ???                                    ???

 Auto with libkstack




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg     Slide #42
No longer complete darkness
                                            A random pinpoint of light!
0xffffffff

                 kernel

0xc0000000
(TASK_SIZE)




                                                                                kstack
                  user




                                  We can self-discover kstack address!
0x00000000
                                   Exploitation is...maybe feasible?
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #43
The next step

●   We now have a tiny island
    ●   Use arbitrary write to modify anything on kstack
●   Where to write?
    ●   Pointers, data, metadata on kstack
●   What to write?
    ●   No userspace addrs (UDEREF), limited kernel
●   Game over? Not yet!

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #44
Metadata on kernel stack
       Anything else of interest on the kstack???

                                                            high address
         start of stack



                                        grows down

                                                                  4k/8k
          stack pointer                                           stack

                                           unused



                                        thread_info
   current_thread_info
                                                            low address

      thread_info struct stashed at base of kstack!

   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #45
thread_info candidates
struct thread_info {
    struct task_struct  *task;
    struct exec_domain  *exec_domain;
    __u32                flags;
                                         ●                     What can we
    __u32                status;
    __u32                cpu;                                  modify within
    int                  preempt_count;
    mm_segment_t         addr_limit;                           thread_info to
    struct restart_block restart_block;
    void __user         *sysenter_return;                      escalate privs?
#ifdef CONFIG_X86_32
    unsigned long        previous_esp;
    __u8                 supervisor_stack;
#endif
    int                  uaccess_err;
};




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #46
restart_block func ptr?
struct thread_info {
    struct task_struct  *task;
    struct exec_domain  *exec_domain;   ●                      restart_block?
    __u32                flags;
    __u32                status;                                ●   Has a func ptr we
    __u32                cpu;
    int                  preempt_count;
                                                                    can overwrite and
    mm_segment_t         addr_limit;                                invoke via userspace!
    struct restart_block restart_block;
    void __user         *sysenter_return;
                                                                ●   Can't point to
#ifdef CONFIG_X86_32                                                userspace (UDEREF)
    unsigned long        previous_esp;
    __u8                 supervisor_stack;
                                                                ●   Can't point to kmem
#endif                                                              (blackbox)
    int                  uaccess_err;
};                                                              ●   Plus assuming no
                                                                    control flow mod



        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #47
task_struct pointer?
struct thread_info {
    struct task_struct  *task;
    struct exec_domain  *exec_domain;   ●                      task_struct?
    __u32                flags;
    __u32                status;                                ●   Could point it at
    __u32                cpu;
    int                  preempt_count;
                                                                    init_task_struct for
    mm_segment_t         addr_limit;                                getting creds/caps of
    struct restart_block restart_block;                             the init task
    void __user         *sysenter_return;
#ifdef CONFIG_X86_32                                            ●   But we don't know
    unsigned long        previous_esp;                              the address of
    __u8                 supervisor_stack;
#endif
                                                                    init_task_struct!
    int                  uaccess_err;
};




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #48
Attacking task_struct
struct thread_info {
    struct task_struct *task;
    ...                                         ●   task_struct->creds?
};
                                                     ●   Modify creds of our process
struct task_struct {
    ...
                                                         directly to escalate privileges?
    const struct cred *real_cred;                    ●   But in order to write
    const struct cred *cred;
    ...                                                  task_struct->creds, we need
};                                                       to know the address of
                                                         task_struct!
struct cred {
    ...                                              ●   If we could read the address
    uid_t uid;                                           of task_struct off the end of
    gid_t gid;
    ...
                                                         the kstack, we might win!
};



        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #49
Connecting the dots
                                              Expanding our visibility
0xffffffff

                 kernel

0xc0000000                                                                         creds
(TASK_SIZE)

                                                  task_struct

                                                                                kstack
                  user




                                         If we can read off the kstack,
0x00000000                               we can find task_struct/creds!
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg     Slide #50
Attacking task_struct

●   We have an arbitrary write on kstack
    ●   Can we turn this into an arbitrary read?
●   If we can get arbitrary read:
    ●   Read base of kstack to find address of task_struct
    ●   Read task_struct to find address of creds struct
    ●   Write into creds struct to set uids/gids/caps
    ●   Spawn a root shell!


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #51
Plan of attack!

   Arbitrary write
                                    STACK
                                   JACKING                                          ROOT
 Kstack disclosure
                                  OVERVIEW

    STACK                                                                         STACK
SELF-DISCOVERY                                                                   JACKING

  Manual analysis                             ???                                  ???
                                                                             Read thread / task


 Auto with libkstack                                                           Overwrite creds




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #52
The Rosengrope Technique




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #53
Vanilla kernel

●   No segmentation, user/kernel separation
    enforced by paging
●   copy_*_user functions check user pointers
    against addr_limit (per-thread variable in
    thread_info struct)
●   On vanilla, setting addr_limit to
    KERNEL_DS (ULONG_MAX) gives
    arbitrary read/write (all checks pass)

      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #54
set_fs()

 ●   Sometimes kernel wants to reuse code
     with kernel pointer arguments
     ●   kernel_sendmsg, kernel_recvmsg, etc.
 ●   Calls set_fs(KERNEL_DS) to set
     addr_limit and allow copy_*_user
     functions to copy kernel-to-kernel
 ●   Careful to make sure no user-influenced
     pointers are used

         Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #55
PAX_UDEREF

● Strict user/kernel separation using
  segmentation
● Reload segment registers at kernel

  traps, used during copy operations
    ●   Fault on invalid access




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #56
PAX_UDEREF and KERNEL_DS

● Use %gs register to keep track of
  segment for source/dest of copy
● set_fs(KERNEL_DS) sets addr_limit

  and reloads %gs register to contain
  __KERNEL_DS segment selector



    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #57
No more easy root...

● Writing KERNEL_DS to addr_limit is
  no longer sufficient
● Access checks on pointers will pass,

  but we'll still fault in copy functions
  because of incorrect segment
  registers


    Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #58
But...

 ● %gs register is reloaded on context
   switch (necessary to keep track of
   thread state)
 ● Reloaded based on contents of

   addr_limit!



     Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #59
Using KERNEL_DS trick

●   Write KERNEL_DS into addr_limit of
    current thread
●   Loop on write(pipefd, addr, size)
    ●   Eventually, thread will be scheduled out at right
        moment (before copy_from_user)
    ●   When thread resumes, %gs register will be
        reloaded with __KERNEL_DS, and read target will
        be copied into pipe buffer (kernel-to-kernel copying)
●   Restore addr_limit and read
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #60
Plan of attack!

   Arbitrary write
                                    STACK
                                   JACKING                                          ROOT
 Kstack disclosure
                                  OVERVIEW

    STACK                                  STACK                                  STACK
SELF-DISCOVERY                            GROPING                                JACKING

  Manual analysis                      KERNEL_DS trick                             ???
                                                                             Read thread / task


 Auto with libkstack                                                           Overwrite creds




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #61
Pros and cons of KERNEL_DS

●   KERNEL_DS
    ●   Pros: clean, simple, generic method to obtain
        arbitrary read from write+kleak
    ●   Cons: depends on knowing the location of
        addr_limit member of thread_info
    ●   It's possible to move thread_info out of the kstack!
●   Any alternatives?
    ●   Let's get a bit crazier...


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #62
The Obergrope Technique




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #63
The Obergrope Technique




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #64
The Obergrope Technique




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #65
Attacking the kstack frames

●   A different approach
    ●   Don't attack the thread_info metadata on kstack
    ●   Attack the kstack frames themselves!

●   End goal is a read
    ●   How to read data by writing a stack frame?




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #66
Observations

●   Lots of kernel codepaths copy data to userland,
    via copy_to_user(), put_user(), etc
●   There may be copy_to_user() calls that use a
    source address argument that is, at some point,
    stored on the kernel stack
●   If we can overwrite that source address on the
    kstack, we can control source of the
    copy_to_user() and leak data to userspace


      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #67
A problem

●   How can we write to our own kstack?
    ●   Unlikely to be able to write into our own stack while
        exploiting the vulnerability for our arbitrary write
●   Use parent/child processes
    ●   Child self-discovers kstack addr
    ●   Passes kstack addr to parent
    ●   Parent writes into child while child is in syscall



        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #68
More problems

● How can we write to stack reliably?
● We have a tricky race to win:

    ●   Parent needs to write into child's kstack between
        when the copy_to_user() source register is pushed
        and popped from the kstack
●   This is a very small race window...
           .



        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #69
Winning Linux kernel races

●   How to win Linux kernel races
    ●   Get very lucky w/scheduling on SMP machine
    ●   Cause a resource to be in contention (eg. locks)
    ●   Cause kernel to page in from slow I/O device
        (sgrakkyu)
●   Ehhh...
    ●   We might hose the kernel if we lose the race
    ●   Anything better?

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #70
A twist on winning races

●   This isn't a “standard” race though
    ●   We can have child execute ANY codepath that
        performs copy_to_user() with a src arg on kstack

●   Enter, sleepy syscalls!
    ●   Syscalls that allow us to put process to sleep for an
        arbitrary amount of time
    ●   nanosleep, wait, select, etc


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #71
Sleepy syscall conditions

● Any of these sleepy syscalls have our
  required conditions?
● Needs to:

    ●   Push a register to the stack
    ●   Go to sleep for an arbitrary amount of time
    ●   Pop that register off the stack
    ●   Use that register as the source for copy_to_user()


        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #72
compat_sys_waitid
asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,
                struct compat_siginfo __user *uinfo, int options,
                struct compat_rusage __user *uru)
{
        struct rusage ru;
...
        ret = sys_waitid(which, pid, (siginfo_t __user *)&info, 
                         uru ? (struct rusage __user *)&ru : NULL);
...
                ret = put_compat_rusage(&ru, uru);
...
}

int put_compat_rusage(const struct rusage *r, struct compat_rusage 
__user *ru)
{
        if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||
            __put_user(r­>ru_utime.tv_sec, &ru­>ru_utime.tv_sec) ||
...
}

      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #73
compat_sys_waitid disasm
Dump of assembler code for function compat_sys_waitid:
...
   0xffffffff810aba4e <+62>: lea    ­0x140(%rbp),%r14       1) compat_sys_waitid() stores address of ru in r14
...
   0xffffffff810aba8b <+123>: callq  0xffffffff81063b70     2) compat_sys_waitid() calls sys_waitid()
                                     <sys_waitid>
...                                                         3) sys_waitid() calls do_wait()
   0xffffffff810abaae <+158>: mov    %r14,%rdi
   0xffffffff810abab1 <+161>: callq  0xffffffff810aa700     4) do_wait() pushes r14 on kstack
                                     <put_compat_rusage>
...                                                         5) do_wait() sleeps indefinitely

Dump of assembler code for function sys_waitid:          6)     we clobber the saved r14 reg on the kstack
...
   0xffffffff81063bf9 <+137>: callq  0xffffffff810637e0 
                                                         7)     do_wait() wakes up
                                     <do_wait>
                                                         8)     do_wait() pops r14 off the kstack
...
                                                            9) do_wait() returns
Dump of assembler code for function do_wait:                10) sys_waitid() returns
...
   0xffffffff810637e6 <+6>:   push   %r14                   11) compat_sys_waitid() calls put_compat_rusage()
...
   PROCESS GOES TO SLEEP HERE                               12) put_compat_rusage() uses clobbered source addr
...
   0xffffffff810639fb <+539>: pop    %r14                   13) put_user() copies from source addr to userspace
...




            Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg          Slide #74
compat_sys_waitid reliability

●   Is this reliable across kernel versions?
    ●   Yes, tested on:
        ●   Lucid default build vmlinuz-2.6.32-24-generic
        ●   Lucid custom build vmlinuz-2.6.32.26+drm33.12
        ●   Vanilla build vmlinuz-2.6.36.3
        ●   Vanilla build + grsec vmlinuz-2.6.36.3-grsec
●   How about compilers?
    ●   Across most gcc 4.x? Needs more investigation
    ●   Potentially could runtime fingerprint compiler
        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #75
High-level exploit flow
1. jacker forks/execs groper                      8. helper wakes up from sleep

2. groper gets its own kstack addr                9. groper returns from waitid

3. groper passes kstack addr up to                10. groper leaks task_struct address
jacker                                            back to userspace

4. groper forks/execs helper                      11. groper passes leaked address
                                                  back up with jacker
5. helper goes to sleep for
a bit                                             12. steps 4-11 are repeated to leak
                                                  task/cred addresses
6. groper calls waitid on helper
                                                  13. jacker modifies groper's cred
7. jacker overwrites the required                 struct in-place
offset on groper's stack
                                                  14. groper forks off a root shell

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #76
Plan of attack!

   Arbitrary write
                                    STACK
                                   JACKING                                          ROOT
 Kstack disclosure
                                  OVERVIEW

    STACK                                  STACK                                  STACK
SELF-DISCOVERY                            GROPING                                JACKING

  Manual analysis                      KERNEL_DS trick                             ???
                                                                             Read thread / task


 Auto with libkstack                   Clobber saved reg                       Overwrite creds




        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #77
Live demo!

●   Exploit against
    live hardened
    system...




      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #78
Defenses?

●   Mitigate the exploitation vectors?
    ●   Remove thread_info metadata from kstack
    ●   RANDKSTACK
●   Eliminate all kstack disclosures?
    ●   Clear kstack between syscalls?
    ●   Compiler/toolchain foo?
●   ???

        Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #79
Greetz

●   #busticati

●   $1$kk1q85Xp$Id.gAcJOg7uelf36VQwJQ/

●   Those who were already aware of this
    bypass vector ;-)


      Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #80
Q&A


         QUESTIONS?
   Jon Oberheide                                  Dan Rosenberg
 jon@oberheide.org                           dan.j.rosenberg@gmail.com
    Duo Security                             Virtual Security Research




   Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg   Slide #81

Más contenido relacionado

Similar a HES2011 - Jon Oberheide and Dan Rosenberg - Stackjacking

Keeping your rack cool
Keeping your rack cool Keeping your rack cool
Keeping your rack cool Pavel Odintsov
 
Keeping your rack cool with one "/IP route rule"
Keeping your rack cool with one "/IP route rule"Keeping your rack cool with one "/IP route rule"
Keeping your rack cool with one "/IP route rule"Faelix Ltd
 
Designing and Attacking DRM (RSA 2008)
Designing and Attacking DRM (RSA 2008)Designing and Attacking DRM (RSA 2008)
Designing and Attacking DRM (RSA 2008)Nate Lawson
 
Security its-more-than-just-your-database-you-should-worry-about
Security its-more-than-just-your-database-you-should-worry-aboutSecurity its-more-than-just-your-database-you-should-worry-about
Security its-more-than-just-your-database-you-should-worry-aboutDavid Busby, CISSP
 
Filip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersFilip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersYury Chemerkin
 
Simplest-Ownage-Human-Observed… - Routers
 Simplest-Ownage-Human-Observed… - Routers Simplest-Ownage-Human-Observed… - Routers
Simplest-Ownage-Human-Observed… - RoutersLogicaltrust pl
 
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...Positive Hack Days
 
Ple18 web-security-david-busby
Ple18 web-security-david-busbyPle18 web-security-david-busby
Ple18 web-security-david-busbyDavid Busby, CISSP
 
Practical Forensics - Tools & Techniques by Sachin Deodhar
Practical Forensics - Tools & Techniques by Sachin DeodharPractical Forensics - Tools & Techniques by Sachin Deodhar
Practical Forensics - Tools & Techniques by Sachin DeodharPriyanka Aash
 
Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Community
 
DevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdfDevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdfkanedafromparis
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineeringjtdudley
 
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...Felipe Prado
 
Taking the hard out of hardware
Taking the hard out of hardwareTaking the hard out of hardware
Taking the hard out of hardwareRonald McCollam
 
Pack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataPack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataLambert Lum
 
2012 12-08-travis-ci-could-do
2012 12-08-travis-ci-could-do2012 12-08-travis-ci-could-do
2012 12-08-travis-ci-could-doKenichi Murahashi
 
Software Defined Data Centers - June 2012
Software Defined Data Centers - June 2012Software Defined Data Centers - June 2012
Software Defined Data Centers - June 2012Brent Salisbury
 
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...SBA Research
 
Why Architecture in Web Development matters
Why Architecture in Web Development mattersWhy Architecture in Web Development matters
Why Architecture in Web Development mattersLars Jankowfsky
 

Similar a HES2011 - Jon Oberheide and Dan Rosenberg - Stackjacking (20)

Keeping your rack cool
Keeping your rack cool Keeping your rack cool
Keeping your rack cool
 
Keeping your rack cool with one "/IP route rule"
Keeping your rack cool with one "/IP route rule"Keeping your rack cool with one "/IP route rule"
Keeping your rack cool with one "/IP route rule"
 
Designing and Attacking DRM (RSA 2008)
Designing and Attacking DRM (RSA 2008)Designing and Attacking DRM (RSA 2008)
Designing and Attacking DRM (RSA 2008)
 
Security its-more-than-just-your-database-you-should-worry-about
Security its-more-than-just-your-database-you-should-worry-aboutSecurity its-more-than-just-your-database-you-should-worry-about
Security its-more-than-just-your-database-you-should-worry-about
 
Filip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersFilip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routers
 
Simplest-Ownage-Human-Observed… - Routers
 Simplest-Ownage-Human-Observed… - Routers Simplest-Ownage-Human-Observed… - Routers
Simplest-Ownage-Human-Observed… - Routers
 
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...
Если нашлась одна ошибка — есть и другие. Один способ выявить «наследуемые» у...
 
Ple18 web-security-david-busby
Ple18 web-security-david-busbyPle18 web-security-david-busby
Ple18 web-security-david-busby
 
Practical Forensics - Tools & Techniques by Sachin Deodhar
Practical Forensics - Tools & Techniques by Sachin DeodharPractical Forensics - Tools & Techniques by Sachin Deodhar
Practical Forensics - Tools & Techniques by Sachin Deodhar
 
Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph
 
DevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdfDevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdf
 
Ceph Internals
Ceph InternalsCeph Internals
Ceph Internals
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineering
 
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...
DEF CON 27 - TRAVIS PALMER - first try dns cache poisoning with ipv4 and ipv6...
 
Taking the hard out of hardware
Taking the hard out of hardwareTaking the hard out of hardware
Taking the hard out of hardware
 
Pack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary dataPack/Unpack: manipulate binary data
Pack/Unpack: manipulate binary data
 
2012 12-08-travis-ci-could-do
2012 12-08-travis-ci-could-do2012 12-08-travis-ci-could-do
2012 12-08-travis-ci-could-do
 
Software Defined Data Centers - June 2012
Software Defined Data Centers - June 2012Software Defined Data Centers - June 2012
Software Defined Data Centers - June 2012
 
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...
SBA Live Academy - After the overflow: self-defense techniques (Linux Kernel)...
 
Why Architecture in Web Development matters
Why Architecture in Web Development mattersWhy Architecture in Web Development matters
Why Architecture in Web Development matters
 

Más de Hackito Ergo Sum

HES2011 - Sebastien Tricaud - Capture me if you can
HES2011 - Sebastien Tricaud - Capture me if you canHES2011 - Sebastien Tricaud - Capture me if you can
HES2011 - Sebastien Tricaud - Capture me if you canHackito Ergo Sum
 
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum Crackme
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum CrackmeHES2011 - Eloi Vanderbeken - Hackito Ergo Sum Crackme
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum CrackmeHackito Ergo Sum
 
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe ShockwaveHES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe ShockwaveHackito Ergo Sum
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHackito Ergo Sum
 
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
HES2011 - joernchen - Ruby on Rails from a Code Auditor PerspectiveHES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
HES2011 - joernchen - Ruby on Rails from a Code Auditor PerspectiveHackito Ergo Sum
 
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X Sandbox
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X SandboxHES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X Sandbox
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X SandboxHackito Ergo Sum
 
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7Hackito Ergo Sum
 
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...Hackito Ergo Sum
 
HES2011 - Itzik Kolter - Let me Stuxnet You
HES2011 - Itzik Kolter - Let me Stuxnet YouHES2011 - Itzik Kolter - Let me Stuxnet You
HES2011 - Itzik Kolter - Let me Stuxnet YouHackito Ergo Sum
 

Más de Hackito Ergo Sum (9)

HES2011 - Sebastien Tricaud - Capture me if you can
HES2011 - Sebastien Tricaud - Capture me if you canHES2011 - Sebastien Tricaud - Capture me if you can
HES2011 - Sebastien Tricaud - Capture me if you can
 
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum Crackme
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum CrackmeHES2011 - Eloi Vanderbeken - Hackito Ergo Sum Crackme
HES2011 - Eloi Vanderbeken - Hackito Ergo Sum Crackme
 
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe ShockwaveHES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
 
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
HES2011 - joernchen - Ruby on Rails from a Code Auditor PerspectiveHES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
 
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X Sandbox
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X SandboxHES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X Sandbox
HES2011 - Richard Johnson - A Castle Made of Sand Adobe Reader X Sandbox
 
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7
HES2011 - Tarjei Mandt – Kernel Pool Exploitation on Windows 7
 
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...
HES2011 - Yuval Vadim Polevoy – Money Is In The Eye Of The Beholder: New And ...
 
HES2011 - Itzik Kolter - Let me Stuxnet You
HES2011 - Itzik Kolter - Let me Stuxnet YouHES2011 - Itzik Kolter - Let me Stuxnet You
HES2011 - Itzik Kolter - Let me Stuxnet You
 

Último

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Último (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 

HES2011 - Jon Oberheide and Dan Rosenberg - Stackjacking

  • 1.
  • 2. Introduction ● Jon Oberheide ● Dan Rosenberg Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #2
  • 3. Introduction In reference to the HES CFP: “I get excited every time I see a conference add requirements to their talk selection along the lines of 'exploitation presentations must be against grsecurity/PaX' -- but then there never ends up being any presentations of this kind.” – spender pratt Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #3
  • 4. Agenda ● A review of Linux kernel security ● Exploitation vs. grsecurity/PaX ● Bypassing grsecurity/PaX Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #4
  • 5. A decade of kernel security Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #5
  • 6. A decade of kernel security Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #6
  • 7. Upstream attitude ● Security is hard when upstream ignores the problems ● Linux still hasn't had its “security awakening” Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #7
  • 8. How about last year? ● 142 CVE's assigned ● 30% worse than the previous worst year (2009) ● Based on public CVE requests, issues tracked at Red Hat Bugzilla, and Eugene's tagged git tree ● Missing dozens of non-CVE vulnerabilities (i.e. the “Dan Carpenter factor”) ● 61 (43%) discovered by six people ● Kees (4), Brad (3), Tavis (7), Vasiliy (4), Dan (37), Nelson (6) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #8
  • 9. Kernel vulns in 2010 ● 12 known exploits for local privilege escalation ● 13 remotely triggerable issues ● 33 potential privilege escalations Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #9
  • 10. Breakdown by Target 2 31 76 33 Core Distro Exotic Red Hat Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #10
  • 11. Breakdown by Impact 13 1 26 7 65 Bypass 30 DOS Info Priv Esc? Priv Esc Nothing Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #11
  • 12. Interesting exploits of 2010 ● full-nelson.c ● Combined three vulns to get a NULL write ● half-nelson.c ● First Linux kernel stack overflow (not buffer overflow) exploit ● linux-rds-exploit.c ● Arbitrary write in RDS packet family ● i-CAN-haz-MODHARDEN.c ● SLUB overflow in CAN packet family ● american-sign-language.c ● Exploit payload written in ACPI's ASL/AML Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #12
  • 13. Agenda ● A review of Linux kernel security ● Exploitation vs. grsecurity/PaX ● Bypassing grsecurity/PaX Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #13
  • 14. Traditional Linux exploitation ● Perhaps most general exploitation primitive is an arbitrary kernel write ● Sometimes occurs naturally, other times can be constructed (e.g. overwriting pointers in an overflow to trigger a write) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #14
  • 15. Linux exploitation examples ● Writes to known addresses (IDT) ● Function pointer overwrites ● Redirecting control flow to userspace ● Influencing privesc-related kernel data (eg. credentials structures) ● Relying on kallsyms and other info Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #15
  • 16. Overview of grsecurity/PaX ● grsecurity/PaX ● Third-party patchset to harden Linux userspace/kernel security ● Attempts to prevent ● Introduction/execution of arbitrary code ● Execution of existing code out of original order ● Execution of existing code in original order with arbitrary data Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #16
  • 17. grsecurity/PaX hardening ● Kernel hardening features: ● KERNEXEC ● Prevent the introduction of new executable code ● UDEREF ● Prevent invalid userspace pointer dereferences ● HIDESYM ● Hide info that may be useful to an attacker (kallsyms, slabinfo, kernel address leaks, etc) ● MODHARDEN ● Prevent auto-loading of crappy unused packet families (CAN, RDS, econet, etc) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #17
  • 18. Agenda ● A review of Linux kernel security ● Exploitation vs. grsecurity/PaX ● Bypassing grsecurity/PaX Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #18
  • 19. The main event ● A technique we call stackjacking ● Enables the bypass of common grsecurity/PaX configurations with common exploit primatives ● Independently discovered, collaboratively exploited, with slightly different techniques Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #19
  • 20. Plan of attack! ??? STACK JACKING ROOT ??? OVERVIEW ??? ??? ??? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #20
  • 21. Target kernel assumptions ● Hardened kernel with grsec/PaX ● Config level GRKERNSEC_HIGH ● KERNEXEC ● UDEREF ● HIDESYM ● MODHARDEN ● Etc... Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #21
  • 22. Stronger target assumptions ● Let's make some extra assumptions ● We like a challenge, and these are assumptions that may possibly be obtainable now or in the future ● Stronger target assumptions ● Zero knowledge of kernel address space ● Fully randomized kernel text/data ● Cannot introduce new code into kernel address space ● Cannot modify kernel control flow (eg. data-only) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #22
  • 23. Attacker assumption #1 ● Assumption: arbitrary kmem write ● A common kernel exploitation primitive ● Examples: RDS, MCAST_MSFILTER ● Other vulns can be turned into writes, e.g. overflowing into a pointer that's written to ● Wut? ● “You mean I can't escalate privs with an arbitrary kernel memory write normally?” NOPE. Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #23
  • 24. Arbitrary write into the abyss DARKNESS! 0xffffffff kernel 0xc0000000 (TASK_SIZE) user No clue where to write! 0x00000000 Exploitation is infeasible. Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #24
  • 25. What's the secret sauce? ARBITRARY WRITE + ? = <3 Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #25
  • 26. The Great Sandwich? ARBITRARY WRITE + = <3 msuiche? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #26
  • 27. Nah, he's taken + = <3 Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #27
  • 28. Need to know something ● One way: arbitrary kmem disclosure ● procfs (2005) ● sctp (2008) ● move_pages (2009) ● pktcdvd (2010) ● Just dump entire address space! ● But these are rare! ● And in many instances, mitigated by grsec/PaX Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #28
  • 29. Something more common? ● How about a more common vuln? ● Hints... ● Widely considered to be a useless vulnerability ● Commonly assigned a CVSS score of 1.9 (low) ● 25+ such vulnerabilities reported in 2010 ● Often referred to as a Dan Rosenbug ● Can you guess it??? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #29
  • 30. KSTACK MEM DISCLOSURE! ARBITRARY WRITE + KSTACK LEAK = <3 Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #30
  • 31. How does kstack leak help? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #31
  • 32. A bit about Linux kernel stacks ● Each userspace thread is high address allocated a kernel stack grows down ● Stores stack frames for kernel syscalls and other metadata 4k/8k stack unused ● Most commonly 8k, some distros use 4k ● THREAD_SIZE = 2*PAGE_SIZE = low address 2*4086 = 8192 Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #32
  • 33. Kernel stack mem disclosures ● Kstack mem disclosures ● Leak of memory from the kernel stack to userspace ● Common cause ● Copying a struct on the kstack back to userspace with uninitialized fields ● Improper initialization/memset, forgetting member assignment, structure padding/holes ● A frequent occurrence, especially in compat Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #33
  • 34. Kernel stack mem disclosures struct foo {   uint32_t bar;   uint32_t leak; kstack frame kstack frame   uint32_t baz; foo.bar }; sensitive data sensitive data foo.leak syscall() { foo.baz   struct foo; .   foo.bar = 1; .   foo.baz = 2; .   copy_to_user(foo); } 1) process 2) kstack is reused 3) foo struct is copied to makes syscall on subsequent userspace, leaking 4 and leaves syscall and struct bytes of kstack through sensitive data overlaps with uninitialized foo.leak on kstack sensitive data member Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #34
  • 35. Plan of attack! Arbitrary write STACK JACKING ROOT Kstack disclosure OVERVIEW ??? ??? ??? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #35
  • 36. What's useful on the kstack? ● Leak data off kstack? ● Sensitive data left behind? Not really... ● Leak addresses off kstack? ● Sensitive addresses left behind? Maybe... ● Pointers to known structures could be exploited ● Too specific of an attack! ● Need something more general ● kstack disclosures differ widely in size/offsets Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #36
  • 37. Kernel stack addresses ● How about a leaking an address that: ● Is stored on the stack; and ● Points to an address on the stack ● These are pretty common ● Eg. pointers to local stack vars, saved ebp, etc ● But what does this gain us? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #37
  • 38. Kernel stack self-discovery ● If we can leak an pointer 0xcdef2000 to the kstack off the kstack, we can calculate the base 0xcdef1234 kstack frame address of the kstack 0xdeadbeef kstack_base = addr & ~(THREAD_SIZE – 1); 0xcdef1234 kstack_base = 0xcdef1234 & ~(8192 – 1) . . kstack_base = 0xcdef0000 0xcdef0000 . We call this kstack self-discovery Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #38
  • 39. Effective kstack discovery ● Not all kstack disclosures are alike ● May only leak a few bytes, non-consecutive ● How do we effectively self-discover? ● Manual analysis ● Figure out where kstack leak overlaps addresses ● Automatic analysis ● libkstack Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #39
  • 40. Manual kstack self-discovery ● Manual, offline analysis ● 1. prime stack with random syscall ● 2. leak bytes, see if any leaks match real kstack ● 3. repeat until we've collected enough bytes ● 4. construct list of priming syscalls needed for the particular leak to spill the beans Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #40
  • 41. Automatic with libkstack ● We can automate this process for runtime self-discovery with libkstack ● 1. prime stack with random syscall ● 2. leak bytes, infer whether bytes belong to a kstack addr ● 3. repeat until we have sufficient confidence to calculate the kstack base addr Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #41
  • 42. Plan of attack! Arbitrary write STACK JACKING ROOT Kstack disclosure OVERVIEW STACK SELF-DISCOVERY Manual analysis ??? ??? Auto with libkstack Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #42
  • 43. No longer complete darkness A random pinpoint of light! 0xffffffff kernel 0xc0000000 (TASK_SIZE) kstack user We can self-discover kstack address! 0x00000000 Exploitation is...maybe feasible? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #43
  • 44. The next step ● We now have a tiny island ● Use arbitrary write to modify anything on kstack ● Where to write? ● Pointers, data, metadata on kstack ● What to write? ● No userspace addrs (UDEREF), limited kernel ● Game over? Not yet! Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #44
  • 45. Metadata on kernel stack Anything else of interest on the kstack??? high address start of stack grows down 4k/8k stack pointer stack unused thread_info current_thread_info low address thread_info struct stashed at base of kstack! Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #45
  • 46. thread_info candidates struct thread_info {     struct task_struct  *task;     struct exec_domain  *exec_domain;     __u32                flags; ● What can we     __u32                status;     __u32                cpu; modify within     int                  preempt_count;     mm_segment_t         addr_limit; thread_info to     struct restart_block restart_block;     void __user         *sysenter_return; escalate privs? #ifdef CONFIG_X86_32     unsigned long        previous_esp;     __u8                 supervisor_stack; #endif     int                  uaccess_err; }; Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #46
  • 47. restart_block func ptr? struct thread_info {     struct task_struct  *task;     struct exec_domain  *exec_domain; ● restart_block?     __u32                flags;     __u32                status; ● Has a func ptr we     __u32                cpu;     int                  preempt_count; can overwrite and     mm_segment_t         addr_limit; invoke via userspace!     struct restart_block restart_block;     void __user         *sysenter_return; ● Can't point to #ifdef CONFIG_X86_32 userspace (UDEREF)     unsigned long        previous_esp;     __u8                 supervisor_stack; ● Can't point to kmem #endif (blackbox)     int                  uaccess_err; }; ● Plus assuming no control flow mod Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #47
  • 48. task_struct pointer? struct thread_info {     struct task_struct  *task;     struct exec_domain  *exec_domain; ● task_struct?     __u32                flags;     __u32                status; ● Could point it at     __u32                cpu;     int                  preempt_count; init_task_struct for     mm_segment_t         addr_limit; getting creds/caps of     struct restart_block restart_block; the init task     void __user         *sysenter_return; #ifdef CONFIG_X86_32 ● But we don't know     unsigned long        previous_esp; the address of     __u8                 supervisor_stack; #endif init_task_struct!     int                  uaccess_err; }; Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #48
  • 49. Attacking task_struct struct thread_info {     struct task_struct *task;     ... ● task_struct->creds? }; ● Modify creds of our process struct task_struct {     ... directly to escalate privileges?     const struct cred *real_cred; ● But in order to write     const struct cred *cred;     ... task_struct->creds, we need }; to know the address of task_struct! struct cred {     ... ● If we could read the address     uid_t uid; of task_struct off the end of     gid_t gid;     ... the kstack, we might win! }; Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #49
  • 50. Connecting the dots Expanding our visibility 0xffffffff kernel 0xc0000000 creds (TASK_SIZE) task_struct kstack user If we can read off the kstack, 0x00000000 we can find task_struct/creds! Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #50
  • 51. Attacking task_struct ● We have an arbitrary write on kstack ● Can we turn this into an arbitrary read? ● If we can get arbitrary read: ● Read base of kstack to find address of task_struct ● Read task_struct to find address of creds struct ● Write into creds struct to set uids/gids/caps ● Spawn a root shell! Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #51
  • 52. Plan of attack! Arbitrary write STACK JACKING ROOT Kstack disclosure OVERVIEW STACK STACK SELF-DISCOVERY JACKING Manual analysis ??? ??? Read thread / task Auto with libkstack Overwrite creds Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #52
  • 53. The Rosengrope Technique Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #53
  • 54. Vanilla kernel ● No segmentation, user/kernel separation enforced by paging ● copy_*_user functions check user pointers against addr_limit (per-thread variable in thread_info struct) ● On vanilla, setting addr_limit to KERNEL_DS (ULONG_MAX) gives arbitrary read/write (all checks pass) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #54
  • 55. set_fs() ● Sometimes kernel wants to reuse code with kernel pointer arguments ● kernel_sendmsg, kernel_recvmsg, etc. ● Calls set_fs(KERNEL_DS) to set addr_limit and allow copy_*_user functions to copy kernel-to-kernel ● Careful to make sure no user-influenced pointers are used Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #55
  • 56. PAX_UDEREF ● Strict user/kernel separation using segmentation ● Reload segment registers at kernel traps, used during copy operations ● Fault on invalid access Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #56
  • 57. PAX_UDEREF and KERNEL_DS ● Use %gs register to keep track of segment for source/dest of copy ● set_fs(KERNEL_DS) sets addr_limit and reloads %gs register to contain __KERNEL_DS segment selector Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #57
  • 58. No more easy root... ● Writing KERNEL_DS to addr_limit is no longer sufficient ● Access checks on pointers will pass, but we'll still fault in copy functions because of incorrect segment registers Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #58
  • 59. But... ● %gs register is reloaded on context switch (necessary to keep track of thread state) ● Reloaded based on contents of addr_limit! Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #59
  • 60. Using KERNEL_DS trick ● Write KERNEL_DS into addr_limit of current thread ● Loop on write(pipefd, addr, size) ● Eventually, thread will be scheduled out at right moment (before copy_from_user) ● When thread resumes, %gs register will be reloaded with __KERNEL_DS, and read target will be copied into pipe buffer (kernel-to-kernel copying) ● Restore addr_limit and read Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #60
  • 61. Plan of attack! Arbitrary write STACK JACKING ROOT Kstack disclosure OVERVIEW STACK STACK STACK SELF-DISCOVERY GROPING JACKING Manual analysis KERNEL_DS trick ??? Read thread / task Auto with libkstack Overwrite creds Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #61
  • 62. Pros and cons of KERNEL_DS ● KERNEL_DS ● Pros: clean, simple, generic method to obtain arbitrary read from write+kleak ● Cons: depends on knowing the location of addr_limit member of thread_info ● It's possible to move thread_info out of the kstack! ● Any alternatives? ● Let's get a bit crazier... Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #62
  • 63. The Obergrope Technique Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #63
  • 64. The Obergrope Technique Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #64
  • 65. The Obergrope Technique Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #65
  • 66. Attacking the kstack frames ● A different approach ● Don't attack the thread_info metadata on kstack ● Attack the kstack frames themselves! ● End goal is a read ● How to read data by writing a stack frame? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #66
  • 67. Observations ● Lots of kernel codepaths copy data to userland, via copy_to_user(), put_user(), etc ● There may be copy_to_user() calls that use a source address argument that is, at some point, stored on the kernel stack ● If we can overwrite that source address on the kstack, we can control source of the copy_to_user() and leak data to userspace Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #67
  • 68. A problem ● How can we write to our own kstack? ● Unlikely to be able to write into our own stack while exploiting the vulnerability for our arbitrary write ● Use parent/child processes ● Child self-discovers kstack addr ● Passes kstack addr to parent ● Parent writes into child while child is in syscall Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #68
  • 69. More problems ● How can we write to stack reliably? ● We have a tricky race to win: ● Parent needs to write into child's kstack between when the copy_to_user() source register is pushed and popped from the kstack ● This is a very small race window... . Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #69
  • 70. Winning Linux kernel races ● How to win Linux kernel races ● Get very lucky w/scheduling on SMP machine ● Cause a resource to be in contention (eg. locks) ● Cause kernel to page in from slow I/O device (sgrakkyu) ● Ehhh... ● We might hose the kernel if we lose the race ● Anything better? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #70
  • 71. A twist on winning races ● This isn't a “standard” race though ● We can have child execute ANY codepath that performs copy_to_user() with a src arg on kstack ● Enter, sleepy syscalls! ● Syscalls that allow us to put process to sleep for an arbitrary amount of time ● nanosleep, wait, select, etc Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #71
  • 72. Sleepy syscall conditions ● Any of these sleepy syscalls have our required conditions? ● Needs to: ● Push a register to the stack ● Go to sleep for an arbitrary amount of time ● Pop that register off the stack ● Use that register as the source for copy_to_user() Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #72
  • 73. compat_sys_waitid asmlinkage long compat_sys_waitid(int which, compat_pid_t pid,                 struct compat_siginfo __user *uinfo, int options,                 struct compat_rusage __user *uru) {         struct rusage ru; ...         ret = sys_waitid(which, pid, (siginfo_t __user *)&info,                           uru ? (struct rusage __user *)&ru : NULL); ...                 ret = put_compat_rusage(&ru, uru); ... } int put_compat_rusage(const struct rusage *r, struct compat_rusage  __user *ru) {         if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)) ||             __put_user(r­>ru_utime.tv_sec, &ru­>ru_utime.tv_sec) || ... } Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #73
  • 74. compat_sys_waitid disasm Dump of assembler code for function compat_sys_waitid: ...    0xffffffff810aba4e <+62>: lea    ­0x140(%rbp),%r14 1) compat_sys_waitid() stores address of ru in r14 ...    0xffffffff810aba8b <+123>: callq  0xffffffff81063b70  2) compat_sys_waitid() calls sys_waitid()                                      <sys_waitid> ... 3) sys_waitid() calls do_wait()    0xffffffff810abaae <+158>: mov    %r14,%rdi    0xffffffff810abab1 <+161>: callq  0xffffffff810aa700  4) do_wait() pushes r14 on kstack                                      <put_compat_rusage> ... 5) do_wait() sleeps indefinitely Dump of assembler code for function sys_waitid: 6) we clobber the saved r14 reg on the kstack ...    0xffffffff81063bf9 <+137>: callq  0xffffffff810637e0  7) do_wait() wakes up                                      <do_wait> 8) do_wait() pops r14 off the kstack ... 9) do_wait() returns Dump of assembler code for function do_wait: 10) sys_waitid() returns ...    0xffffffff810637e6 <+6>: push   %r14 11) compat_sys_waitid() calls put_compat_rusage() ...    PROCESS GOES TO SLEEP HERE 12) put_compat_rusage() uses clobbered source addr ...    0xffffffff810639fb <+539>: pop    %r14 13) put_user() copies from source addr to userspace ... Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #74
  • 75. compat_sys_waitid reliability ● Is this reliable across kernel versions? ● Yes, tested on: ● Lucid default build vmlinuz-2.6.32-24-generic ● Lucid custom build vmlinuz-2.6.32.26+drm33.12 ● Vanilla build vmlinuz-2.6.36.3 ● Vanilla build + grsec vmlinuz-2.6.36.3-grsec ● How about compilers? ● Across most gcc 4.x? Needs more investigation ● Potentially could runtime fingerprint compiler Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #75
  • 76. High-level exploit flow 1. jacker forks/execs groper 8. helper wakes up from sleep 2. groper gets its own kstack addr 9. groper returns from waitid 3. groper passes kstack addr up to 10. groper leaks task_struct address jacker back to userspace 4. groper forks/execs helper 11. groper passes leaked address back up with jacker 5. helper goes to sleep for a bit 12. steps 4-11 are repeated to leak task/cred addresses 6. groper calls waitid on helper 13. jacker modifies groper's cred 7. jacker overwrites the required struct in-place offset on groper's stack 14. groper forks off a root shell Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #76
  • 77. Plan of attack! Arbitrary write STACK JACKING ROOT Kstack disclosure OVERVIEW STACK STACK STACK SELF-DISCOVERY GROPING JACKING Manual analysis KERNEL_DS trick ??? Read thread / task Auto with libkstack Clobber saved reg Overwrite creds Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #77
  • 78. Live demo! ● Exploit against live hardened system... Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #78
  • 79. Defenses? ● Mitigate the exploitation vectors? ● Remove thread_info metadata from kstack ● RANDKSTACK ● Eliminate all kstack disclosures? ● Clear kstack between syscalls? ● Compiler/toolchain foo? ● ??? Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #79
  • 80. Greetz ● #busticati ● $1$kk1q85Xp$Id.gAcJOg7uelf36VQwJQ/ ● Those who were already aware of this bypass vector ;-) Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #80
  • 81. Q&A QUESTIONS? Jon Oberheide Dan Rosenberg jon@oberheide.org dan.j.rosenberg@gmail.com Duo Security Virtual Security Research Stackjacking Your Way to grsecurity/PaX Bypass – Jon Oberheide / Dan Rosenberg Slide #81