<https
This is a story about how we solved a Buffer Overrun (BO) problem in the Clean & Clear® website. We’re sharing this story because we think it’s something that many developers face at least once in their career and may not know how to solve it.
\[This is a summary of the talk I gave at StrangeLoop 2011.\]
\[Update: here is a video of the talk.\]
Buffer overruns can be hard to reason about, but they’re pretty easy to test for. One way to do this is with fuzz testing, in which you feed random data into your program and see what happens. This is reasonably effective, but it’s also slow, and it doesn’t necessarily test all the possible inputs.
Another approach, which we’ve been using at Clean & Clear for a few years now, is to statically analyze our code for buffer overruns. We built this into our build system—it runs automatically every time we build, and it catches a lot of bugs quickly and cheaply.
To understand how this works, you first have to know how buffer overruns actually happen. If you don’t know that already, you might want to read the Wikipedia article on it first—but don’t worry if you don’t understand all the technical details; I didn’t when I started out on this project either.
The first step is to prepare the mortar mix by adding water to it. The mixture has to be added in such a way that the water is absorbed by the sand and cement. After this, it is necessary to add lime juice and a little bit of alum as well as any additives that may be required during the process. These ingredients will make sure that the sand and cement are combined properly.
Next, one needs to get some bricks. These bricks are usually available at any construction site where brickwork is done. It is possible to find bricks of different sizes and shapes.
After all these materials have been gathered together, it is time for mixing them together in such a way that they form a paste which can then be poured over the area where brickwork has been done. When this paste dries up completely, one can remove it from the area with ease using a scraper or any other similar tool.*
Now that you’ve seen how to write an exploit, let’s talk about how to stop them.
The first thing we tried was non-executable stacks. These are actually quite easy to implement; you just mark the stack as non-executable in the operating system. This prevented most of the existing exploits from working. So we were saved, right?
Wrong. The very next thing people did was a technique called return-to-libc. I won’t go into details here, but the point is that it means “non-executable stack” is not enough: you also have to make sure there are no return addresses on the stack which lead to executable code in other places in memory, such as shared libraries.
So then we tried Position Independent Executables (PIE), which randomize the address at which shared libraries are loaded into memory, so that you can’t predict where they will be and thus can’t use return-to-libc attacks. But of course attackers soon found a way around this too: Return-Oriented Programming (ROP). In short, ROP works by finding tiny snippets of code in shared library functions—single assembly instructions like “pop all the arguments off the stack” or “jump
We use a lot of brick mortar mix in our work, and have found that the ratio used by masons is not always the best one for our applications. We choose the mix based on what we are building, and the size of our bricks.
The standard brick mortar mix is 4 parts sand to 1 part Portland cement. When doing very small jobs you can mix it by hand, but for larger ones, it’s faster with a paddle mixer.
For some of our projects we have found that a 3:1 sand to cement ratio works better, and sometimes even a 2:1 ratio gives us the results we want. A weaker mixture will not hold up over time as well as a stronger one, but is easier to work with than the standard 4:1 mixture. In general, we want something in between those two extremes. It’s also possible to make your own mortar mix out of portland cement and lime.
When building with bricks, it’s important to use good quality bricks that are dry enough to absorb water from the mortar without dissolving them (this is why they should be left outdoors for several months before using).
The best way to test your brick is by dropping it into water; if bubbles come out then
To make mortar, you need to mix cement (usually Portland Cement) with sand in a 3:1 ratio. Mixing by volume is perfectly adequate for home use, but if you want more consistent results, measure by weight. For example, mix one 94-pound bag of cement with three cubic feet of sand. That’s the right proportions for a Type S mortar mix.
In addition to the cement and sand, you’ll need to add hydrated lime and water. The amount of lime and water depends on the amount of cement used in the mixture. You can find the exact percentages in any standard masonry text or by consulting your building supply dealer.
To calculate how much lime you’ll need, multiply the cement used by 0.25. To calculate how much water you’ll need, multiply the cement used by 0.45.
For example, if you use one 94-pound bag of cement and three cubic feet of sand, you would need about 1 pound (1/4 of 94 pounds is about 24) of hydrated lime and about 42 pounds (0.45 x 94 is 42) of water for a Type S mortar mix.