From: Pavel Machek <pavel@denx.de>
To: Scott Bauer <sbauer@eng.utah.edu>
Cc: linux-kernel@vger.kernel.org,
kernel-hardening@lists.openwall.com, x86@kernel.org,
ak@linux.intel.com, luto@amacapital.net, mingo@redhat.com,
tglx@linutronix.de, wmealing@redhat.com,
torvalds@linux-foundation.org,
Abhiram Balasubramanian <abhiram@cs.utah.edu>,
Scott Bauer <sbauer@plzdonthack.me>
Subject: Re: [PATCH v4 4/4] Documentation: SROP Mitigation: Add documentation for SROP cookies
Date: Sun, 24 Apr 2016 18:27:33 +0200 [thread overview]
Message-ID: <20160424162733.GF8880@amd> (raw)
In-Reply-To: <1459281207-24377-5-git-send-email-sbauer@eng.utah.edu>
Hi!
> index 0000000..ee17181
> --- /dev/null
> +++ b/Documentation/security/srop-cookies.txt
> @@ -0,0 +1,203 @@
> + Sigreturn-oriented programming and its mitigation
> +
> +
> +A good write up can be found here: https://lwn.net/Articles/676803/
> +It covers much of what is outlined in this documentation.
Umm. I'd rather have good documentation here than on the web
somewhere.
> +If you're very curious you should read the SROP paper:
> +http://www.cs.vu.nl/~herbertb/papers/srop_sp14.pdf
> +
> +If you're here to learn how to disable SROP issue the following
", issue"?
> +Command:
> +
> +# echo 1 > /proc/sys/kernel/disable-srop-protection
Can we have reverse logic, having '.../srop-protection' file? Double
negatives are not intuitive...
> +============================ Overview ============================
> +
> +Sigreturn-oriented programming(SROP) is a new technique to control
> +a compromised userland process after successfully exploiting a bug.
> +
> +Signals are delivered to the process during context switches. The
> +kernel will setup a signal frame on the process' stack which
> +contains the saved state of the process prior to the context switch.
> +The kernel then gives control the the process' signal handler. Once
> +the process has delt with the signal it will call the sigreturn
dealt
> +system call. During the context switch into the kernel, the previous
> +state of where the process was executing prior to the delivery of
> +the signal is overwritten, hence why the kernel must save the the
hence why? the the?
> +state in a signal frame on the user's stack. The kernel will remove
> +the signal frame from the process' stack and continue execution
> +where the process left off.
> +
> +The issue with this signal delivery method is if an attacker can
"is: if"?
> +construct a fake signal frame on the compromised process' stack
> +and ROP into the sigreturn system call, they have the ability to
> +easily control the flow of execution by having the kernel return
> +execution to wherever the signal frame says to continue execution.
> +
> +More importantly, SROP makes an attackers job easier. Previously
attacker's?
> +attackers would have to search for ROP gadgets to get values into
> +registers then call mprotect or mmap to get some memory where they
> +could copy and execute shellcode. If the ROP gadgets didnt exist
didn't
> +that allowed setting up a call to those functions then the attackers
> +wouldn't be able to fully exploit the bug. With SROP however attackers
, however,
> +dont' have to search for ROP gadgets to get specific values into
don't
> +registers. The attacker would simply lay out the ucontext on the stack
> +as they choose then SROP into the mprotect or mmap call.
choose,
> +======================== Mitigating SROP ========================
> +
> +In order to prevent SROP the kernel must remember or be able to derive
> +at runtime whether a sigreturn call it is currently processing is
> +in respose to a legitimate signal delivered by the kernel.
response.
> +During delivery of a signal the kernel will place the signal frame
, the kernel
> +with the saved process state on the stack. It will also place a cookie
> +above the signal frame.
> +
> +The cookie is a per-process secret xor'd with the address where it will
> +be stored. During a sigreturn the kernel will extract this cookie, and
> +then compare the extracted cookie against a new generated cookie:
> +(the per process secret xord with address where we extracted cookie from).
kill the ()s?
> +If the two cookies match, then the kernel has verified that it is handling
> +a sigreturn from a signal that was previously legitimately delivered.
> +If the cookies do not match up the kernel sends a SIGSEGV to the process,
> +terminating it.
up, the kernel.
Hmm. SIGSEGV does not neccessarily terminate a process, right? Which
register values will it use for the SIGSEGV handler?
> +After verifying the cookie, the kernel will zero out the cookie to prevent
> +any sort of leaking of the cookie.
delete 'any sort', because we know that cookie can leak, for example
due to threads sharing the mm?
> +This prevents SROP because it forces an attacker to know the cookie in order
> +to use SROP as an attack vector.
-> This makes SROP harder... ?
> +
> +
> +======================== Possible Issues ========================
you may want to search for " " in this document.
> +SROP protection will probably break any process or application which do
does.
> +some sort of checkpoint-restore in user space type things. As well as DOSEMU.
> +
> +
> +
> +===============================================================================
> +Inlined are two programs that exploit SROP:
> +
> +For 32bit:
> +
> +Compile with if you're using a 64bit kernel:
> +gcc -O0 -o srop_32 srop_32.c -g -fno-stack-protector -ffixed-ebp -ffixed-esp -m32 -DEMULATED_32
> +
> +or if you're already on a real 32bit kernel:
> +gcc -O0 -o srop_32 srop_32.c -g -fno-stack-protector -ffixed-ebp -ffixed-esp
> +
this should go to tools/ somewhere, no?
> +When run without SROP protection it will exit gracefully, when SROP is
> +enabled it will terminate with a SIGSEGV.
> +
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <signal.h>
> +
> +void syscall(void)
> +{
> + exit(1);
> +}
> +
> +void test2(void)
> +{
> + register int esp asm("esp");
> + register int ebp asm("ebp");
> + struct sigcontext scon = { 0 };
> +
> + scon.eax = 11;
> + scon.ebx = 0x41;
> + scon.ecx = 0;
> + scon.edx = 0;
> + scon.esp = scon.ebp = ebp;
> + scon.eip = (int)syscall+6;
> +
> +#if defined EMULATED_32
> + scon.fs = 0x00;
> + scon.cs = 0x23;
> + scon.ss = scon.ds = scon.es = 0x2B;
> + scon.gs = 0x63;
> +#else
> + scon.fs = 0x00;
> + scon.cs = 0x73;
> + scon.ss = scon.ds = scon.es = 0x7B;
> + scon.gs = 0x33;
> +#endif
> + esp = (int) &scon;
> + asm("movl $119, %eax\n");//NR_SIGRETURN;
> + asm("int $0x80\n");
> +}
Coding style. //.
> +For 64 bit:
> +
> +gcc -O0 -o srop srop.c -g -fno-stack-protector -ffixed-rsp -ffixed-rbp
> +When run the program exits normally, with SROP protetction it terminates
> +with a segmentationf fault.
segmentation fault.
> + REG_RIP,
> + REG_EFL,
> + REG_CSGSFS,/* Actually short cs, gs, fs, __pad0. */
", "
> +void _exit_(void)
> +{
> + exit(1);
> +}
> +
> +void test(void)
> +{
> + struct ucontext ctx = { 0 };
> + register unsigned long rsp asm("rsp");
> + register unsigned long rbp asm("rbp");
> + ctx.uc_mcontext.gregs[REG_RIP] = (unsigned long) _exit_ + 4;
Why + 4?
> + ctx.uc_mcontext.gregs[REG_RSP] = rsp;
> + ctx.uc_mcontext.gregs[REG_RBP] = rbp;
Umm. Does this work?
> + ctx.uc_mcontext.gregs[REG_CSGSFS] = 0x002b000000000033;
> + rsp = (unsigned long) &ctx;
> + asm("movq $0xf,%rax\n");
", "? asm volatile? Clobbers?
> + asm("syscall\n");
Make it single asm statemtent so that gcc does not play with you? Add
checking if syscall just returns failure?
Aha, -O0 above. So you already know the code is buggy...?
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
next prev parent reply other threads:[~2016-04-24 16:27 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-29 19:53 [PATCH v4 0/4] SROP Mitigation: Sigreturn Cookies Scott Bauer
2016-03-29 19:53 ` [PATCH v4 1/4] SROP Mitigation: Architecture independent code for signal cookies Scott Bauer
2016-03-29 23:04 ` Linus Torvalds
2016-03-31 20:25 ` Eric W. Biederman
2016-03-31 22:00 ` Linus Torvalds
2016-03-31 22:17 ` Eric W. Biederman
2016-03-29 19:53 ` [PATCH v4 2/4] x86: SROP Mitigation: Implement Signal Cookies Scott Bauer
2016-03-29 19:53 ` [PATCH v4 3/4] Sysctl: SROP Mitigation: Add Sysctl argument to disable SROP Scott Bauer
2016-03-29 19:59 ` Andi Kleen
2016-03-29 20:46 ` Scotty Bauer
2016-03-29 20:53 ` Andi Kleen
2016-03-29 19:53 ` [PATCH v4 4/4] Documentation: SROP Mitigation: Add documentation for SROP cookies Scott Bauer
2016-03-29 20:12 ` Brian Gerst
2016-04-24 16:27 ` Pavel Machek [this message]
2016-03-29 21:29 ` [PATCH v4 0/4] SROP Mitigation: Sigreturn Cookies Andy Lutomirski
2016-03-29 21:36 ` Scotty Bauer
2016-03-29 21:38 ` Andy Lutomirski
2016-03-29 22:34 ` Linus Torvalds
2016-03-29 23:14 ` Scotty Bauer
2016-03-31 20:22 ` Eric W. Biederman
2016-04-01 12:57 ` Cyrill Gorcunov
2016-03-29 22:55 ` [kernel-hardening] " Daniel Micay
2016-04-24 16:14 ` Pavel Machek
2016-03-29 22:54 ` Linus Torvalds
2016-03-29 22:55 ` Linus Torvalds
2016-03-29 23:05 ` Andy Lutomirski
2016-03-29 23:11 ` Scotty Bauer
2016-03-29 23:25 ` Linus Torvalds
2016-03-29 23:34 ` Scotty Bauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160424162733.GF8880@amd \
--to=pavel@denx.de \
--cc=abhiram@cs.utah.edu \
--cc=ak@linux.intel.com \
--cc=kernel-hardening@lists.openwall.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=mingo@redhat.com \
--cc=sbauer@eng.utah.edu \
--cc=sbauer@plzdonthack.me \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=wmealing@redhat.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox