Files
android_external_gptfdisk/diskio.h
Jeff Sharkey 83fdc99960 Add a fuzzer for gptfdisk.
Since gptfdisk is used to parse MBR and GPT partition tables from
untrusted USB and SD card storage devices, we should get a fuzzer
wired up to hunt for security issues.

To enable the fuzzer, we create a new "diskio-heap" implementation
for backing I/O operations, which allows us to treat the fuzzer
input as a fake block device.  These changes are as minimal as
possible to avoid future merge conflicts.

The single corpus input is a sample block device with a single
empty GPT partition created using these commands:

$ losetup /dev/loop0 typical.bin
$ gdisk /dev/loop0

And the final table is:

Number  Start (sector)    End (sector)  Size       Code  Name
   1              34              38   2.5 KiB     8300  Linux filesystem

Bug: 170783842
Test: SANITIZE_HOST=address make ${FUZZER_NAME} && ${ANDROID_HOST_OUT}/fuzz/$(get_build_var HOST_ARCH)/${FUZZER_NAME}/${FUZZER_NAME}
Change-Id: I21a2a5f7f1019365accf8fd74c958aaafe7f7ff7
2020-10-13 19:55:52 -06:00

95 lines
2.2 KiB
C++

//
// C++ Interface: diskio
//
// Description: Class to handle low-level disk I/O for GPT fdisk
//
//
// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
// This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
// under the terms of the GNU GPL version 2, as detailed in the COPYING file.
#ifndef __DISKIO_H
#define __DISKIO_H
#include <string>
#include <stdint.h>
#include <sys/types.h>
#ifdef _WIN32
#include <windows.h>
#include <winioctl.h>
#else
#include <sys/ioctl.h>
#endif
#ifdef __sun__
#include <sys/dkio.h>
#endif
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__)
#define fstat64 fstat
#define stat64 stat
#endif
#include "support.h"
//#include "parttypes.h"
using namespace std;
/***************************************
* *
* DiskIO class and related structures *
* *
***************************************/
class DiskIO {
protected:
string userFilename;
string realFilename;
string modelName;
int isOpen;
int openForWrite;
#ifdef _WIN32
HANDLE fd;
#else
int fd;
#endif
#ifdef ENABLE_HEAP_DISKIO
const unsigned char* data;
size_t size;
off_t off;
#endif
public:
DiskIO(void);
~DiskIO(void);
void MakeRealName(void);
#ifdef ENABLE_HEAP_DISKIO
int OpenForRead(const unsigned char* data, size_t size);
#endif
int OpenForRead(const string & filename);
int OpenForRead(void);
int OpenForWrite(const string & filename);
int OpenForWrite(void);
void Close();
int Seek(uint64_t sector);
int Read(void* buffer, int numBytes);
int Write(void* buffer, int numBytes);
int DiskSync(void); // resync disk caches to use new partitions
int GetBlockSize(void);
int GetPhysBlockSize(void);
string GetModel(void) {return modelName;}
uint32_t GetNumHeads(void);
uint32_t GetNumSecsPerTrack(void);
int IsOpen(void) {return isOpen;}
int IsOpenForWrite(void) {return openForWrite;}
string GetName(void) const {return realFilename;}
uint64_t DiskSize(int* err);
}; // class DiskIO
#endif