January 29, 2016 - Shuah Khan

An Introduction to Using KASan to Detect Dynamic Memory Errors

The Next Generation Media Controller added several new interfaces to add and remove media graph objects. I started running tests with KASan enabled to detect dynamic memory errors introduced in this work, and I ended up finding some out-of-bounds address access and use-after-free bugs. This article will introduce you to KASan and provide some examples of how it can be valuable.

What is KASan?

Kernel Address sanitizer (KASan) is a dynamic memory error detector. It provides a fast and comprehensive solution for finding use-after-free and out-of-bounds bugs. KASan uses compile-time instrumentation to allocate shadow memory to keep track of out-of-bounds and use-after-free bugs. Each memory allocation gets the same area allocated in the shadow memory and accesses to allocated areas results in corresponding shadow memory accesses which then KASan flags when out-of-bounds access or use-after-free accesses occur.

How to Use KASan

The dependencies are:

  • gcc 4.9.2 or later
    • gcc 5.2 or later is required to detect  out of bounds accesses to stack or global variables
  • SLUB allocator
  • Supported only on x86_64 architecture.

It is very easy to use it, simply enable CONFIG_KASAN = y.

You can choose between outline/inline compiler instrumentation types by enabling the  CONFIG_KASAN_OUTLINE or CONFIG_KASAN_INLINE kernel configuration options. I chose CONFIG_KASAN_INLINE because it is 1.1 to 2 times faster compared to the outline option. However, the outline option produces smaller binary. KASan allows disabling instrumentation for specific files or directories. Simply add KASAN_SANITIZE_main.o := n to disable a single file or KASAN_SANITIZE := n for all files in a directory to the respective makefile.

Finding Errors in the Next Generation Media Controller

The out-of-bounds access bug I was able to find using KASan was a result of a cut and paste error which resulted in allocating memory for an incorrect, smaller data structure. This problem would have been very hard to find and debug without KASan. The fix is a single line change shown below:

The KASan report for the out-of-bounds bug I found looked like the following (please note: this report is shortened to show only the important details):

Learn More

For more information on KASan, please refer to the Linux Kernel documentation for it, and this article from LWN. I’ve found KASan to be very easy to use and to be extremely useful; I plan to use it whenever I make changes to the Kernel related to memory allocation, and you should too.

Shuah Khan

About Shuah Khan

Shuah Khan is a Senior Linux Kernel Developer at Samsung's Open Source Group. She is a Linux Kernel Maintainer and Contributor who focuses on Linux Media Core and Power Management. She maintains Kernel Selftest framework. She has contributed to IOMMU, and DMA areas. In addition, she is helping with stable release kernel testing. She authored Linux Kernel Testing and Debugging paper published on the Linux Journal and writes Linux Journal kernel news articles. She has presented at several Linux conferences and Linux Kernel Developer Keynote Panels. She served on the Linux Foundation Technical Advisory Board. Prior to joining Samsung, she worked as a kernel and software developer at HP and Lucent.

Development / Linux KASan / Linux Kernel /

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Anti-Spam