| 1 |
/* Copyright 2021 Google LLC |
| 2 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
you may not use this file except in compliance with the License. |
| 4 |
You may obtain a copy of the License at |
| 5 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
Unless required by applicable law or agreed to in writing, software |
| 7 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 8 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 |
See the License for the specific language governing permissions and |
| 10 |
limitations under the License. |
| 11 |
*/ |
| 12 |
#include <fcntl.h> /* open() O_RDONLY O_BINARY */ |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
/* |
| 22 |
* Libdwarf library callers can only use these headers. |
| 23 |
*/ |
| 24 |
#include "dwarf.h" |
| 25 |
#include "libdwarf.h" |
| 26 |
#ifndef O_BINARY |
| 27 |
#define O_BINARY 0 |
| 28 |
#endif |
| 29 |
|
| 30 |
/* |
| 31 |
* Fuzzer function targeting a case of dwarf_gnu_debuglink |
| 32 |
*/ |
| 33 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 34 |
char filename[256]; |
| 35 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 36 |
|
| 37 |
FILE *fp = fopen(filename, "wb"); |
| 38 |
if (!fp) { |
| 39 |
return 0; |
| 40 |
} |
| 41 |
fwrite(data, size, 1, fp); |
| 42 |
fclose(fp); |
| 43 |
|
| 44 |
int fuzz_fd = 0; |
| 45 |
Dwarf_Ptr errarg = 0; |
| 46 |
Dwarf_Handler errhand = 0; |
| 47 |
Dwarf_Error *errp = NULL; |
| 48 |
Dwarf_Debug dbg = 0; |
| 49 |
|
| 50 |
fuzz_fd = open(filename, O_RDONLY |O_BINARY); |
| 51 |
if (fuzz_fd != -1) { |
| 52 |
dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp); |
| 53 |
|
| 54 |
int res = 0; |
| 55 |
char *debuglink_path = 0; |
| 56 |
unsigned char *crc = 0; |
| 57 |
char *debuglink_fullpath = 0; |
| 58 |
unsigned debuglink_fullpath_strlen = 0; |
| 59 |
unsigned buildid_type = 0; |
| 60 |
char *buildidowner_name = 0; |
| 61 |
unsigned char *buildid_itself = 0; |
| 62 |
unsigned buildid_length = 0; |
| 63 |
char **paths = 0; |
| 64 |
unsigned paths_count = 0; |
| 65 |
unsigned i = 0; |
| 66 |
|
| 67 |
/* This is just an example if one knows |
| 68 |
of another place full-DWARF objects |
| 69 |
may be. "/usr/lib/debug" is automatically |
| 70 |
set. */ |
| 71 |
res = |
| 72 |
dwarf_add_debuglink_global_path(dbg, "/usr/include/c++/9/debug", errp); |
| 73 |
res = dwarf_gnu_debuglink(dbg, &debuglink_path, &crc, &debuglink_fullpath, |
| 74 |
&debuglink_fullpath_strlen, &buildid_type, |
| 75 |
&buildidowner_name, &buildid_itself, |
| 76 |
&buildid_length, &paths, &paths_count, errp); |
| 77 |
/* Calling dwarf_gnu_debuglink and passing in |
| 78 |
&paths here means the caller |
| 79 |
is obligated to free the array/block of strings |
| 80 |
returned. dwarf_finish() will NOT |
| 81 |
free these strings. See the libdwarf documentation. */ |
| 82 |
free(paths); |
| 83 |
/* Calling dwarf_gnu_debuglink and passing in |
| 84 |
&debuglink_fullpath means the caller |
| 85 |
is obligated to free the array/block of strings |
| 86 |
returned. dwarf_finish() will NOT |
| 87 |
free these strings. See the libdwarf documentation. */ |
| 88 |
free(debuglink_fullpath); |
| 89 |
|
| 90 |
dwarf_finish(dbg); |
| 91 |
close(fuzz_fd); |
| 92 |
} |
| 93 |
|
| 94 |
unlink(filename); |
| 95 |
return 0; |
| 96 |
} |