This repository was archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfuzz-wrapper.c
130 lines (109 loc) · 2.16 KB
/
fuzz-wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (C) 2021 Advanced Micro Devices, Inc. */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifndef PROG_NAME
#define PROG_NAME "sev-guest-get-report"
#endif
#define NR_REQ_ARGS 1
int build_cmd(const char *prog_name, const char *args, char **cmd, size_t *cmd_size)
{
int rc = EXIT_FAILURE;
size_t size = 0, buffer_size = 0;
char *buffer = NULL;
if (!prog_name || !args || !cmd || !cmd_size) {
rc = EINVAL;
goto out;
}
size = snprintf(NULL, 0, "%s %s", prog_name, args);
if (size < 0) {
rc = EIO;
goto out;
}
buffer_size = size + 1;
buffer = calloc(buffer_size, sizeof(char));
if (!buffer) {
rc = ENOMEM;
goto out;
}
size = snprintf(buffer, buffer_size, "%s %s", prog_name, args);
if (size >= buffer_size) {
rc = ENOBUFS;
goto out_free;
}
*cmd = buffer;
*cmd_size = size;
rc = EXIT_SUCCESS;
out_free:
if (buffer && rc != EXIT_SUCCESS) {
free(buffer);
buffer = NULL;
}
out:
return rc;
}
int main(int argc, char *argv[])
{
int rc = 0;
char *line = NULL, *cmd = NULL;
size_t line_size = 0, count = 0, cmd_size = 0;
if (argc != NR_REQ_ARGS) {
rc = EINVAL;
errno = rc;
perror(argv[0]);
goto exit;
}
/*
* Get the command line arguments from the file.
* NOTE: the full command must be on a single line.
*/
errno = 0;
count = getline(&line, &line_size, stdin);
if (count == -1) {
rc = errno;
perror("getline");
goto exit_free;
}
/* Check that there are no additional lines in the file */
errno = 0;
count = getline(&line, &line_size, stdin);
if (count != -1) {
rc = EFBIG;
errno = rc;
perror("getline");
goto exit_free;
}
/* Build the full command */
rc = build_cmd(PROG_NAME, line, &cmd, &cmd_size);
if (rc != EXIT_SUCCESS) {
errno = rc;
perror("build_cmd");
goto exit_free;
}
errno = 0;
rc = system(cmd);
if (rc == -1) {
rc = errno;
perror("system");
goto exit_free_cmd;
}
else if (WIFEXITED(rc) && WEXITSTATUS(rc) != EXIT_SUCCESS) {
rc = WEXITSTATUS(rc);
errno = rc;
perror("command failed");
goto exit_free_cmd;
}
rc = EXIT_SUCCESS;
exit_free_cmd:
if (cmd) {
free(cmd);
cmd = NULL;
}
exit_free:
if (line) {
free(line);
line = NULL;
}
exit:
exit(rc);
}