Hack in the Box Segv Hunt Rules & Guidelines

As part of our presence at Hack In The Box Amsterdam 2019, we are running a challenge for all vulnerability hunters based on the power of automated variant analysis with Semmle QL. Participants will use this technology to explore a codebase and find a SEGV. They will gain points toward the Haxpo Capture the Flag. But the best QL writers will also get additional cool prizes, including a drone worth $500!

 

 

  • Haxpo CTF Flag: If you write the right QL queries up to step 1 (see the challenge instructions below), you will obtain a special flag that will give you points for the Haxpo Capture the Flag. But if you are not participating to the CTF, then you will get some cool swag!
  • 1st prize: Yuneec Mantis Foldable 4K Camera Drone with WiFi Remote. At the end of the conference, during the 4pm coffee break on Friday 10th, the participant who has written the best QL queries and who has gone the farthest in the challenge will get this $500 prize.   
  • Surprise Prizes: Impress us with your QL queries, by reaching Step 3 (see the challenge instructions below), and tweet proudly with @Semmle @hitb #segvhunt . The three most retweeted participants will get some surprise prizes.   

Participation

Just come at our booth in the Haxpo exhibition hall, on May 9th and 10th, and register. We can give you a crash course on QL, and get you ready to start.   

Challenge Instructions

The goal of this challenge is to find unsafe uses of alloca in the GNU C Library (glibc). alloca is used to allocate a buffer on the stack. It is usually implemented by simply subtracting the size parameter from the stack pointer and returning the new value of the stack pointer. This means that it has two important benefits:

  1. The memory allocated by alloca is automatically freed when the current function returns.

  2. It is extremely fast.

But alloca can also be unsafe because it does not check whether there is enough stack space left for the buffer. If the requested buffer size is too big, then alloca might return an invalid pointer. This can cause the application to crash with a SIGSEGV when it attempts to read or write the buffer. Therefore alloca is only intended to be used to allocate small buffers. It is the programmer's responsibility to check that the size isn't too big.

The GNU C Library contains hundreds of calls to alloca. In this challenge, you will use Semmle QL to find those calls. Of course many of those calls are safe, so the main goal of the challenge is to refine your query to reduce the number of false positives. If you follow the challenge all the way to the end then you might even find a bug in glibc that is reproducible from a standard command-line application.

Setup instructions

The quickest way to get started with QL is to use LGTM's query console. However, if you prefer, you can also install QL and write your queries offline. Instructions for installing QL are included at the end of this document.

If you are attending HITBSecConf2019 - Amsterdam, then please visit us at our booth in the exhibition hall. We can give you a quick QL demo to get you started and, if you are interested, we can also help you with the offline setup instructions.

Documentation links

If you get stuck, try searching our documentation and blog posts for help and ideas. Below are a few links to help you get started:

Challenge

The challenge is split into several steps, each of which contains multiple questions. But you do not need to submit separate answers to each individual question. One query per step is sufficient.

Step 0: finding the definition of alloca

  • Question 00: alloca is a macro. Find the definition of this macro and the name of the function that it expands to.

Step 1: finding the calls to alloca and filtering out small allocation sizes

  • Question 10: Find all the calls to alloca (using the function name that you found in step 0).
  • Question 11: Use the upperBound and lowerBound predicates from the SimpleRangeAnalysis library to filter out results which are safe because the allocation size is small. You can classify the allocation size as small if it is less than 65536. But don't forget that negative sizes are very dangerous.

Step 2: filtering out calls that are guarded by __libc_use_alloca

The correct way to use alloca in glibc is to first check that the allocation is safe by calling __libc_use_alloca. You can see a good example of this at getopt.c:252. That code uses __libc_use_alloca to check if it is safe to use alloca. If not, it uses malloc instead. In this step, you will identify calls to alloca that are safe because they are guarded by a call to __libc_use_alloca.

  • Question 20: Find all calls to __libc_use_alloca.
  • Question 21: Find all guard conditions where the condition is a call to __libc_use_alloca.
  • Question 22: Sometimes the result of __libc_use_alloca is assigned to a variable, which is then used as the guard condition. For example, this happens at setsourcefilter.c:38-41. Enhance your query, using local dataflow, so that it also finds this guard condition.
  • Question 23: Sometimes the call to __libc_use_alloca is wrapped in a call to __builtin_expect. For example, this happens at setenv.c:185. Enhance your query so that it also finds this guard condition.
  • Question 24: Sometimes the result of __libc_use_alloca is negated with the ! operator. For example, this happens at getaddrinfo.c:2291-2293. Enhance your query so that it can also handle negations.
  • Question 25: Find calls to alloca that are safe because they are guarded by a call to __libc_use_alloca.

Step 3: combine steps 1 and 2 to filter out safe calls

  • Question 30: use your answer from step 2 to enhance your query from step 1 by filtering out calls to alloca that are safe because they are guarded by a call to __libc_use_alloca.

Step 4: taint tracking

In this step, you'll use a taint tracking query to find an unsafe call to alloca where the allocation size is controlled by a value read from a file.

  • Question 40: Find calls to fopen. (Be aware that fopen is another macro.)
  • Question 41: Write a taint tracking query. The source should be a call to fopen and the sink should be the size argument of an unsafe call to alloca. To help you get started, here is the boilerplate for the query:

Step 5: searching for a PoC (optional)

  • Question 50: The GNU C Library includes several command-line applications. (It contains 24 main functions.) Demonstrate that the bug is real by showing that you can trigger a SIGSEGV in one of these command-line applications.

Setup instructions for running QL offline

We hope you enjoyed this challenge! If you are interested in continuing to use QL for security research, then we recommend installing QL on your own computer. This will enable you to run queries offline. We have also provided these offline instructions for posterity, because the query results on LGTM will change over time as the source code evolves. But the instructions below use a snapshot corresponding to revision 3332218, which is the revision for which we designed this challenge.

To run QL queries offline, follow these steps:

  1. Install the Eclipse IDE.
  2. Download QL for Eclipse. Installation instructions can be found here.
  3. Download this snapshot, which corresponds to revision 3332218.
  4. Import the glibc snapshot.

You can download other snapshots for offline use from LGTM. For example, you can download a snapshot for the latest revision of glibc here. Every project on LGTM has a download link for downloading the latest snapshot.

 

Other terms

  • Employees of Semmle (the company behind LGTM) and their immediate families are not eligible to enter the challenge.
  • Semmle reserves the right to change or withdraw the challenge and/or prizes at any time, but will make every effort to notify participants when such changes occur. For example, prizes may need to be changed if they cannot be sourced, or dates may need to change for logistical reasons.
  • Semmle reserves the right to remove a participant at their discretion for any reason at any time, for example, because of misconduct or abusive behaviour.
  • Every effort will be made to notify the winners as soon as possible. Participants should give us a way to contact them. For the first place prize, the drone will be delivered at our booth at 4pm on Friday 10th, the winner will be notified shortly before. For the Surprise prizes, the contest will be closed at 6pm on Friday 10th, and the winners will be announced on twitter, and the prizes will be shipped to them.

Get started