Skip to content

Commit 8214dc3

Browse files
committed
add some new ppd fuzzers (test)
1 parent 0008e72 commit 8214dc3

File tree

12 files changed

+172
-0
lines changed

12 files changed

+172
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "ppd.h"
5+
#include "cups.h"
6+
#include "ipp.h"
7+
#include "file-private.h"
8+
9+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
10+
{
11+
// Create a temporary file to simulate a PPD file
12+
char filename[256];
13+
snprintf(filename, sizeof(filename), "/tmp/fuzz_ppd_%d.ppd", getpid());
14+
FILE *file = fopen(filename, "wb");
15+
if (!file)
16+
return 0;
17+
fwrite(data, 1, size, file);
18+
fclose(file);
19+
20+
// Open the PPD file
21+
ppd_file_t *ppd = ppdOpenFile(filename);
22+
if (!ppd)
23+
{
24+
remove(filename);
25+
return 0;
26+
}
27+
28+
// Create a cache with the PPD
29+
ipp_t *attrs = ippNew(); // Create a new IPP attributes object
30+
_ppd_cache_t *cache = _ppdCacheCreateWithPPD(attrs, ppd);
31+
if (cache)
32+
{
33+
// Optionally write the cache to a file
34+
char cache_filename[256];
35+
snprintf(cache_filename, sizeof(cache_filename), "/tmp/fuzz_cache_%d.cache", getpid());
36+
_ppdCacheWriteFile(cache, cache_filename, attrs);
37+
38+
// Retrieve data from the cache
39+
_ppdCacheGetBin(cache, "output-bin");
40+
int exact;
41+
_ppdCacheGetPageSize(cache, attrs, "page-size", &exact);
42+
43+
// Destroy the cache
44+
_ppdCacheDestroy(cache);
45+
46+
// Clean up the cache file
47+
remove(cache_filename);
48+
}
49+
50+
// Clean up IPP attributes
51+
ippDelete(attrs);
52+
53+
// Close the PPD file
54+
ppdClose(ppd);
55+
56+
// Remove the temporary PPD file
57+
remove(filename);
58+
59+
return 0;
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdint.h>
2+
#include <stddef.h>
3+
#include "ppd.h"
4+
#include "cups.h"
5+
#include "file-private.h"
6+
7+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
8+
// Create a temporary file name using process id to avoid conflicts
9+
char filename[256];
10+
snprintf(filename, sizeof(filename), "/tmp/fuzz_ppd_%d.ppd", getpid());
11+
12+
// Write the data to a temporary file
13+
FILE *file = fopen(filename, "wb");
14+
if (!file) {
15+
return 0;
16+
}
17+
fwrite(data, 1, size, file);
18+
fclose(file);
19+
20+
// Open the PPD file
21+
ppd_file_t *ppd = ppdOpenFile(filename);
22+
if (!ppd) {
23+
remove(filename);
24+
return 0;
25+
}
26+
27+
// Check for conflicts
28+
ppdConflicts(ppd);
29+
30+
// Parse options from a sample string
31+
cups_option_t *options = NULL;
32+
int num_options = cupsParseOptions("SampleOption=SampleValue", 0, &options);
33+
34+
// Mark options in the PPD file
35+
cupsMarkOptions(ppd, num_options, options);
36+
37+
// Check for conflicts with specific options and choices
38+
cupsGetConflicts(ppd, "SampleOption", "SampleChoice", &options);
39+
40+
// Resolve conflicts with specific options and choices
41+
int num_resolved_options = num_options;
42+
cupsResolveConflicts(ppd, "SampleOption", "SampleChoice", &num_resolved_options, &options);
43+
44+
// Free options
45+
cupsFreeOptions(num_options, options);
46+
47+
// Close the PPD file
48+
ppdClose(ppd);
49+
50+
// Remove the temporary file
51+
remove(filename);
52+
53+
return 0;
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdint.h>
2+
#include <stddef.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include "ppd.h"
8+
#include "cups.h"
9+
#include "file-private.h"
10+
11+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12+
if (size < 1) return 0; // Ensure there is at least some data
13+
14+
// Use process ID to create a unique temporary filename
15+
char filename[256];
16+
snprintf(filename, sizeof(filename), "/tmp/fuzz_ppd_%d.ppd", getpid());
17+
18+
// Write the fuzz data to a temporary file
19+
FILE *file = fopen(filename, "wb");
20+
if (!file) return 0;
21+
fwrite(data, 1, size, file);
22+
fclose(file);
23+
24+
// Attempt to open the PPD file
25+
ppd_file_t *ppd = ppdOpenFile(filename);
26+
if (ppd) {
27+
// Fuzz ppdMarkOption
28+
if (size > 2) {
29+
ppdMarkOption(ppd, (const char *)data, (const char *)(data + 1));
30+
}
31+
32+
// Fuzz ppdFindMarkedChoice
33+
if (size > 3) {
34+
ppdFindMarkedChoice(ppd, (const char *)(data + 2));
35+
}
36+
37+
// Fuzz ppdInstallableConflict
38+
if (size > 4) {
39+
ppdInstallableConflict(ppd, (const char *)(data + 3), (const char *)(data + 4));
40+
}
41+
42+
// Close the PPD file
43+
ppdClose(ppd);
44+
}
45+
46+
// Fuzz cupsGetOption
47+
if (size > 5) {
48+
cups_option_t options[1];
49+
options[0].name = (char *)data;
50+
options[0].value = (char *)(data + 1);
51+
cupsGetOption((const char *)(data + 5), 1, options);
52+
}
53+
54+
// Clean up the temporary file
55+
unlink(filename);
56+
57+
return 0;
58+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
39.3 KB
Binary file not shown.
10.8 KB
Binary file not shown.
8.3 KB
Binary file not shown.
39.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)