Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:29994
HistoryNov 13, 2013 - 12:00 a.m.

XADV-2013003 Linux Kernel bt8xx Video Driver IOCTL Heap Overflow

2013-11-1300:00:00
vulners.com
18

±-------------------------------------------------------------------+
| XADV-2013003 Linux Kernel bt8xx Video Driver IOCTL Heap Overflow |
±-------------------------------------------------------------------+

Vulnerable versions:

  • linux kernel 2.6.18 <=
    Testbed: ubuntu
    Type: Local
    Impact: Critical
    Vendor: http://www.kernel.org
    Author: x90c <geinblues nospam gmail dot com>
    Site: x90c.org

=========
ABSTRACT:

The bt8xx video driver is a video capture driver. It supports Bt848
Bt849, Bt878, and Bt879.

The bt8xx video driver in the linux kernel has a vulnerability to
occur kernel heap overflow. It's at do ioctl code for bt8xx and
copy_from_user() larger user-supplied data to the kernel heap buffer
than kmalloc'd kmem.

=========
DETAILS:

(1) vulnerable reason: 8 bytes v4l2_clip struct. (sizeof v4l2_clip? 8 bytes)

[~linux-2.6.18/include/linux/videodev2.h]

struct v4l2_clip
{
struct v4l2_rect c;
struct v4l2_clip __user *next;
};

v4l2_clip struct is 8 bytes!

[~linux/2.6.18/include/linux/videodev.h]

struct video_window
{
__u32 x,y; /* Position of window /
__u32 width,height; /
Its size */
__u32 chromakey;
__u32 flags;
struct video_clip __user clips; / Set only /
int clipcount;
#define VIDEO_WINDOW_INTERLACE 1
#define VIDEO_WINDOW_CHROMAKEY 16 /
Overlay by chromakey /
#define VIDEO_CLIP_BITMAP -1
/
bitmap is 1024x625, a '1' bit represents a clipped pixel */
#define VIDEO_CLIPMAP_SIZE (128 * 625)
};

*clips member varaible of video_window is a pointer.

(2) Do exploit: bttv IOCTL!

[~/linux-2.6.18/drivers/media/video/bt8xx/bttv-driver.c]

static int bttv_do_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, void *arg)
{

case VIDIOCSWIN:
{
    struct video_window *win = arg; // XXX win = arg.
    struct v4l2_window w2;

    if &#40;no_overlay &gt; 0&#41; {
        printk &#40;&quot;VIDIOCSWIN: no_overlay&#92;n&quot;&#41;;
        return -EINVAL;
    }

    w2.field = V4L2_FIELD_ANY;
    w2.w.left    = win-&gt;x;
    w2.w.top     = win-&gt;y;
    w2.w.width   = win-&gt;width;
    w2.w.height  = win-&gt;height;
    w2.clipcount = win-&gt;clipcount; // clipcount! &#40;copy size / 8&#41;
    w2.clips     = &#40;struct v4l2_clip __user *&#41;win-&gt;clips; // clips! &#40;to copy src&#41;
    retval = setup_window&#40;fh, btv, &amp;w2, 0&#41;; // XXX vulnerable setup_window&#40;&#41; called!

The ioctl argument to win struct pointer and store the win->clipcount and
win->clips to w2 struct for each. and called vulnerable setup_window().

(3) Result: kernel heap overflow occured.

[~/linux-2.6.18/drivers/media/video/bt8xx/bttv-driver.c]

static int setup_window(struct bttv_fh *fh, struct bttv *btv,
struct v4l2_window *win, int fixup)
{
struct v4l2_clip *clips = NULL;
int n,size,retval = 0;

if &#40;NULL == fh-&gt;ovfmt&#41;
    return -EINVAL;

if &#40;!&#40;fh-&gt;ovfmt-&gt;flags &amp; FORMAT_FLAGS_PACKED&#41;&#41;
    return -EINVAL;

/* XXX no win.clipcount/clips validation. */
retval = verify_window&#40;&amp;bttv_tvnorms[btv-&gt;tvnorm],win,fixup&#41;;
if &#40;0 != retval&#41;
    return retval;

/* copy clips  --  luckily v4l1 + v4l2 are binary
   compatible here ...*/

n = win-&gt;clipcount; /* XXX win&#40;ioctl arg&#41;-&gt;clipcount! */

// &#40;2&#41; less size kmalloc&#39;d. &#40; If clipcount = 0xffff, 0x4000c size kmalloc&#39;d.&#41;
size = sizeof&#40;*clips&#41;*&#40;n+4&#41;; // 0xffff+4*4&#40;0x4000C&#41;
clips = kmalloc&#40;size,GFP_KERNEL&#41;; // less size kmalloc&#39;d!

if &#40;NULL == clips&#41;
    return -ENOMEM;

/*
 * &#40;kernel heap overflow!&#41; 
 * XXX copied 8&#40;sizeof struct v4l2_clip&#41; * 0xffff=size&#40;0x7FFF8&#41; win-&gt;clips to 0x4000c heap buf!
 */
if &#40;n &gt; 0&#41; {
    if &#40;copy_from_user&#40;clips,win-&gt;clips, sizeof&#40;struct v4l2_clip&#41;*n&#41;&#41; {
        kfree&#40;clips&#41;;
        return -EFAULT;
    }

}

===============
EXPLOIT CODES:

=============
PATCH CODES:

[bt8xx_heap_overflow.patch]

    + if&#40;n &gt;= size&#41; { // n &gt;= size kmalloc&#39;d?
    +   kfree&#40;clips&#41;;
    +   return -EINVAL;
    +}
    if &#40;copy_from_user&#40;clips,win-&gt;clips, sizeof&#40;struct v4l2_clip&#41;*n&#41;&#41; {
        kfree&#40;clips&#41;;
        return -EFAULT;
    }

===============
VENDOR STATUS:

2013/11/10 - I discovered the security bug.
2013/11/10 - The advisory released.

========
GREETS:

my stuffs are more favorite than rebel's stuffs.

============
DISCLAIMER:

The authors reserve the right not to be responsible for the topicality,
correctness, completeness or quality of the information provided in this
document. Liability claims regarding damage caused by the use of any information
provided, including any kind of information which is incomplete or incorrect,
will therefore be rejected.