Project: sleuthkit (sleuthkit/sleuthkit), language: c++
Vulnerability instance: sleuthkit.cve-2020-10233
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/sleuthkit/sleuthkit/issues/1829
## Description:
Issue: sleuthkit/sleuthkit#1829
Title: Heap-buffer-overflow in ntfs_dinode_lookup
State: closed
Created by: Google-Autofuzz
Created at: 2020-02-14 15:42:50+00:00

Issue Body:
Hello sleuthkit team,

As part of our fuzzing efforts at Google, we have identified an issue affecting
sleuthkit (tested with revision * develop c05a93a7ce7d40ab203216bb035e3567d8ce68ec).

To reproduce, we are attaching a Dockerfile which compiles the project with
LLVM, taking advantage of the sanitizers that it offers. More information about
how to use the attached Dockerfile can be found here:
https://docs.docker.com/engine/reference/builder/

Instructions:
`unzip artifacts_149443351.zip`
`docker build --build-arg SANITIZER=address --tag=autofuzz-sleuthkit-149443351 autofuzz_149443351`
`docker run --entrypoint /fuzzing/repro.sh --cap-add=SYS_PTRACE -v $PWD/autofuzz_149443351/autofuzz_149443351:/tmp/poc autofuzz-sleuthkit-149443351 "fls ntfs" /tmp/poc`
`docker run --cap-add=SYS_PTRACE -v $PWD/autofuzz_149443351/autofuzz_149443351:/tmp/poc -it autofuzz-sleuthkit-149443351`

Alternatively, and depending on the bug, you could use gcc, valgrind or other
instrumentation tools to aid in the investigation. The sanitizer error that we
encountered is here:

```
fls -- ntfs
=================================================================
==9==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x615000000500 at pc 0x000000577889 bp 0x7ffcd77ac1b0 sp 0x7ffcd77ac1a8
READ of size 1 at 0x615000000500 thread T0
    #0 0x577888 in ntfs_dinode_lookup /fuzzing/sleuthkit/tsk/fs/ntfs.c:413:17
    #1 0x57c476 in ntfs_inode_lookup /fuzzing/sleuthkit/tsk/fs/ntfs.c:2892:9
    #2 0x5328f9 in tsk_fs_file_open_meta /fuzzing/sleuthkit/tsk/fs/fs_file.c:128:9
    #3 0x579b7e in ntfs_open /fuzzing/sleuthkit/tsk/fs/ntfs.c:5217:13
    #4 0x4fc712 in main /fuzzing/sleuthkit/tools/fstools/fls.cpp:309:23
    #5 0x7feacf75f09a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a)
    #6 0x4213c9 in _start (/fuzzing/sleuthkit/tools/fstools/fls+0x4213c9)

0x615000000500 is located 0 bytes to the right of 512-byte region [0x615000000300,0x615000000500)
allocated by thread T0 here:
    #0 0x4c9213 in __interceptor_malloc (/fuzzing/sleuthkit/tools/fstools/fls+0x4c9213)
    #1 0x5dc37e in tsk_malloc /fuzzing/sleuthkit/tsk/base/mymalloc.c:32:16
    #2 0x57c458 in ntfs_inode_lookup /fuzzing/sleuthkit/tsk/fs/ntfs.c:2887:25
    #3 0x5328f9 in tsk_fs_file_open_meta /fuzzing/sleuthkit/tsk/fs/fs_file.c:128:9
    #4 0x579b7e in ntfs_open /fuzzing/sleuthkit/tsk/fs/ntfs.c:5217:13
    #5 0x4fc712 in main /fuzzing/sleuthkit/tools/fstools/fls.cpp:309:23
    #6 0x7feacf75f09a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a)

SUMMARY: AddressSanitizer: heap-buffer-overflow /fuzzing/sleuthkit/tsk/fs/ntfs.c:413:17 in ntfs_dinode_lookup
Shadow bytes around the buggy address:
  0x0c2a7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2a7fff80a0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff80b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff80c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff80d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff80e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff80f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==9==ABORTING

```

We will gladly work with you so you can successfully confirm and reproduce this
issue. Do let us know if you have any feedback surrounding the documentation.

Once you have reproduced the issue, we'd appreciate to learn your expected
timeline for an update to be released. With any fix, please attribute the report
to "Google Autofuzz project".

We are also pleased to inform you that your project is eligible for inclusion to
the OSS-Fuzz project, which can provide additional continuous fuzzing, and
encourage you to investigate integration options.


Don't hesitate to let us know if you have any questions!

Google AutoFuzz Team
[artifacts_149443351.zip](https://github.com/sleuthkit/sleuthkit/files/4205160/artifacts_149443351.zip)




Comments:
Comment by micrictor on 2020-02-22 22:05:23+00:00:
The issue is in the guarding logic at https://github.com/sleuthkit/sleuthkit/blob/8e646f96f5047768d63647844f300de7a5b5fb34/tsk/fs/ntfs.c#L378. 

I'll make a PR to correct this in the next couple days.

---

Comment by carnil on 2020-03-09 11:56:27+00:00:
This issue appears to be relating to [CVE-2020-10233](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-10233).

Attachments:
https://github.com/sleuthkit/sleuthkit/files/4205160/artifacts_149443351.zip

Commit References:
c05a93a7ce7d40ab203216bb035e3567d8ce68ec
8e646f96f5047768d63647844f300de7a5b5fb34

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
export CFLAGS="$CFLAGS -Wno-error=non-c-typedef-for-linkage"
export CXXFLAGS="$CXXFLAGS -Wno-error=non-c-typedef-for-linkage"
./bootstrap
./configure --enable-static --disable-shared --disable-java
make -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool
RUN git clone https://github.com/sleuthkit/sleuthkit sleuthkit
RUN git -C sleuthkit checkout 25405809cbc7a72741d59d70193907f9afac3824
WORKDIR $SRC/sleuthkit
COPY build.sh $SRC/

Additional files:
filename: sleuthkit_fls_fuzzer.cc
content: // Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stddef.h>
#include <stdint.h>

#include "sleuthkit/tsk/tsk_tools_i.h"
#include "sleuthkit_mem_img.h"

#ifndef FSTYPE
#error Define FSTYPE as a valid value of TSK_FS_TYPE_ENUM.
#endif

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  TSK_IMG_INFO *img;
  TSK_FS_INFO *fs;

  img = mem_open(data, size);
  if (img == nullptr)
    return 0;

  fs = tsk_fs_open_img(img, 0, FSTYPE);
  if (fs != nullptr) {
    tsk_fs_fls(fs, TSK_FS_FLS_FULL, fs->root_inum, TSK_FS_DIR_WALK_FLAG_RECURSE,
               nullptr, 0);

    fs->close(fs);
  }

  img->close(img);
  return 0;
}

filename: sleuthkit_mem_img.h
content: // Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEM_IMG_H
#define MEM_IMG_H

#include <stddef.h>
#include <stdint.h>

#include "sleuthkit/tsk/tsk_tools_i.h"

typedef struct {
  TSK_IMG_INFO img_info;
  const uint8_t *data;
  size_t size;
} IMG_MEM_INFO;

static ssize_t mem_read(TSK_IMG_INFO *img_info, TSK_OFF_T offset, char *buf,
                        size_t len) {
  IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info);
  // Bounds-checking exists in the real drivers.
  if (offset > mem_info->size) {
    return -1;
  }
  ssize_t read_len = len;
  if (offset + len > mem_info->size) {
    read_len = mem_info->size - offset;
  }
  if (memcpy(buf, mem_info->data + offset, read_len) == nullptr) {
    return -1;
  } else {
    return read_len;
  }
}

static void mem_close(TSK_IMG_INFO *img_info) {
  IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info);
  tsk_deinit_lock(&(img_info->cache_lock));
  free(mem_info);
}

static void mem_imgstat(TSK_IMG_INFO *img_info, FILE *hFile) {}

TSK_IMG_INFO *mem_open(const uint8_t *data, size_t size) {
  IMG_MEM_INFO *inmemory_img =
      reinterpret_cast<IMG_MEM_INFO *>(malloc(sizeof(IMG_MEM_INFO)));
  TSK_IMG_INFO *img;
  if (inmemory_img == nullptr) {
    return nullptr;
  }
  img = reinterpret_cast<TSK_IMG_INFO *>(inmemory_img);
  img->itype = TSK_IMG_TYPE_RAW;
  img->read = mem_read;
  img->close = mem_close;
  img->imgstat = mem_imgstat;
  img->size = size;
  img->sector_size = 512;
  tsk_init_lock(&(img->cache_lock));
  inmemory_img->data = data;
  inmemory_img->size = size;
  return img;
}

#endif // # MEM_IMG_H

filename: sleuthkit_mmls_fuzzer.cc
content: // Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stddef.h>
#include <stdint.h>

#include "sleuthkit/tsk/tsk_tools_i.h"
#include "sleuthkit_mem_img.h"

#ifndef VSTYPE
#error Define VSTYPE as a valid value of TSK_VS_TYPE_ENUM.
#endif

static TSK_WALK_RET_ENUM part_act(TSK_VS_INFO *vs, const TSK_VS_PART_INFO *part,
                                  void *ptr) {
  return TSK_WALK_CONT;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  TSK_IMG_INFO *img;
  TSK_VS_INFO *vs;

  img = mem_open(data, size);
  if (img == nullptr)
    return 0;

  vs = tsk_vs_open(img, 0, VSTYPE);
  if (vs != nullptr) {
    tsk_vs_part_walk(vs, 0, vs->part_count - 1, TSK_VS_PART_FLAG_ALL, part_act,
                     nullptr);

    tsk_vs_close(vs);
  }

  img->close(img);
  return 0;
}