Skip to content

Commit

Permalink
[elfutils] create tmpfiles properly
Browse files Browse the repository at this point in the history
Now fuzz-libdwfl and fuzz-libelf can be run a few times in a row
with files triggering crashes.

It's another follow-up to google#7395
and google#7393.
  • Loading branch information
evverx committed Sep 18, 2022
1 parent 9c8bf5c commit 9546e4f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
22 changes: 14 additions & 8 deletions projects/elfutils/fuzz-libdwfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include <assert.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "libdwfl.h"
#include "system.h"

static const char *debuginfo_path = "";
static const Dwfl_Callbacks cb = {
Expand All @@ -31,14 +34,17 @@ static const Dwfl_Callbacks cb = {


int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
char filename[256];
sprintf(filename, "/tmp/libfuzzer.%d", getpid());
FILE *fp = fopen(filename, "wb");
if (!fp) {
return 0;
}
fwrite(data, size, 1, fp);
fclose(fp);
char filename[] = "/tmp/fuzz-libdwfl.XXXXXX";
int fd;
ssize_t n;

fd = mkstemp(filename);
assert(fd >= 0);

n = write_retry(fd, data, size);
assert(n == (ssize_t) size);

close(fd);

Dwarf_Addr bias = 0;
Dwfl *dwfl = dwfl_begin(&cb);
Expand Down
22 changes: 14 additions & 8 deletions projects/elfutils/fuzz-libelf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include <assert.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "system.h"


void fuzz_logic_one(char *filename, int compression_type) {
Expand Down Expand Up @@ -69,14 +72,17 @@ void fuzz_logic_twice(char *filename, int open_flags, Elf_Cmd cmd) {
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
char filename[256];
sprintf(filename, "/tmp/libfuzzer.%d", getpid());
FILE *fp = fopen(filename, "wb");
if (!fp) {
return 0;
}
fwrite(data, size, 1, fp);
fclose(fp);
char filename[] = "/tmp/fuzz-libelf.XXXXXX";
int fd;
ssize_t n;

fd = mkstemp(filename);
assert(fd >= 0);

n = write_retry(fd, data, size);
assert(n == (ssize_t) size);

close(fd);

fuzz_logic_one(filename, 0);
fuzz_logic_one(filename, 1);
Expand Down

0 comments on commit 9546e4f

Please sign in to comment.