Coverage Report

Created: 2026-11-12 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cups/ossfuzz/fuzz_ppd.c
Line
Count
Source
1
/*
2
 * PPD, Cache and PWG fuzz program for CUPS
3
 *
4
 * This harness is a combination of
5
 * testppd.c, testcache.c and testpwg.c
6
 * 
7
 * Licensed under Apache License v2.0.
8
 * See the file "LICENSE" for more information.
9
 */
10
11
 #include "ppd-private.h"
12
 #include "file-private.h"
13
 #include <stdio.h>
14
 #include <unistd.h>
15
 #include <string.h>
16
 #include <stdlib.h>
17
 
18
 int fuzz_ppd(char *string, int len, char *filename, char *pwgname);
19
 void unlink_tempfile(void);
20
 
21
 extern int
22
 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
23
10.1k
 {
24
   /* 
25
    * We need a huge input, because it should contain
26
    * options and ppd file
27
    */
28
10.1k
   if (Size < 1000)
29
19
     return 1;
30
   
31
10.1k
   atexit(unlink_tempfile);
32
 
33
10.1k
   char *filename = (char *)malloc(sizeof(char) * 256);
34
10.1k
   char *pwgname = (char *)malloc(sizeof(char) * 256);
35
10.1k
   sprintf(filename, "/tmp/fuzz_ppd.%d.ppd", getpid());
36
10.1k
   sprintf(pwgname, "/tmp/fuzz_ppd.%d.pwg", getpid());
37
 
38
10.1k
   char *string = (char *)calloc(sizeof(char), Size + 1);
39
10.1k
   memcpy(string, Data, Size);
40
10.1k
   int len = Size;
41
 
42
10.1k
   fuzz_ppd(string, len, filename, pwgname);
43
   
44
10.1k
   unlink_tempfile();
45
10.1k
   free(filename);
46
10.1k
   free(pwgname);
47
10.1k
   free(string);
48
10.1k
   return 0;
49
10.1k
 }
50
 
51
 int fuzz_ppd(char *data, int len, char *filename, char *pwgname)
52
10.1k
 {
53
10.1k
   int num_options = 0, // number of fuzz-generated options
54
10.1k
       finishings[1024],
55
10.1k
       width,
56
10.1k
       length;
57
10.1k
   cups_option_t *options = NULL;
58
10.1k
   _ppd_cache_t *pc = NULL,
59
10.1k
                *pc2;
60
10.1k
   ppd_choice_t *ppd_bin;
61
10.1k
   ppd_attr_t *attr;
62
10.1k
   ppd_size_t minsize,
63
10.1k
             maxsize,
64
10.1k
             *size;
65
10.1k
   cups_page_header2_t header;
66
10.1k
   ipp_t *job;
67
10.1k
   char *ppdsize = NULL,
68
10.1k
        *legacy = NULL,
69
10.1k
        *pwg = NULL,
70
10.1k
        *ppdmedia = NULL,
71
10.1k
        *marked_option = NULL,
72
10.1k
        *options_str = NULL;
73
10.1k
   char **cups_options = NULL,
74
10.1k
        **cups_values = NULL;
75
10.1k
   int elem_counter = 0, counter = 0;
76
10.1k
   ppd_file_t *ppd = NULL;
77
10.1k
   int ret = 1;
78
 
79
   /*
80
    * Create and fill variables (options)
81
    * with fuzz-generated values
82
    */
83
 
84
10.1k
   ppdsize = strdup(data);
85
10.1k
   len -= strlen(ppdsize) + 1;
86
10.1k
   if (len <= 0)
87
1
     goto cleanup;
88
10.1k
   data += strlen(ppdsize) + 1;
89
 
90
10.1k
   legacy = strdup(data);
91
10.1k
   len -= strlen(legacy) + 1;
92
10.1k
   if (len <= 0)
93
1
     goto cleanup;
94
10.1k
   data += strlen(legacy) + 1;
95
 
96
10.1k
   pwg = strdup(data);
97
10.1k
   len -= strlen(pwg) + 1;
98
10.1k
   if (len <= 0)
99
1
     goto cleanup;
100
10.1k
   data += strlen(pwg) + 1;
101
 
102
10.1k
   ppdmedia = strdup(data);
103
10.1k
   len -= strlen(ppdmedia) + 1;
104
10.1k
   if (len <= 0)
105
1
     goto cleanup;
106
10.1k
   data += strlen(ppdmedia) + 1;
107
 
108
10.1k
   marked_option = strdup(data);
109
10.1k
   len -= strlen(marked_option) + 1;
110
10.1k
   if (len <= 0)
111
1
     goto cleanup;
112
10.1k
   data += strlen(marked_option) + 1;
113
   
114
10.1k
   options_str = strdup(data);
115
10.1k
   len -= strlen(options_str) + 1;
116
10.1k
   if (len <= 0)
117
1
     goto cleanup;
118
10.1k
   data += strlen(options_str) + 1;
119
 
120
10.1k
   char buf[12] = {0};
121
10.1k
   if (!strncpy(buf, data, 11))
122
0
     goto cleanup;
123
   
124
10.1k
   length = atoi(buf);
125
10.1k
   data += strlen(buf);
126
10.1k
   len -= strlen(buf);
127
 
128
10.1k
   if (!strncpy(buf, data, 11))
129
0
     goto cleanup;
130
 
131
10.1k
   width = atoi(buf);
132
10.1k
   data += strlen(buf);
133
10.1k
   len -= strlen(buf);
134
 
135
   /*
136
    * Create and fill the array of cups options
137
    * and values to check correct work of 
138
    * ppdMarkOption(), cupsGetOption(), 
139
    * cupsGetConflicts(), cupsResolveConflicts()
140
    * and ppdInstallableConflict() functions
141
   */
142
 
143
10.1k
   cups_options = (char **)malloc(sizeof(char *) * 2);
144
10.1k
   cups_values = (char **)malloc(sizeof(char *) * 2);
145
 
146
306k
   for (int i = 0; i < strlen(options_str); i++)
147
296k
   {
148
296k
     cups_options[elem_counter] = (char *)malloc(sizeof(char));
149
296k
     cups_values[elem_counter] = (char *)malloc(sizeof(char));
150
296k
     cups_options[elem_counter][0] = '\0';
151
296k
     cups_values[elem_counter][0] = '\0';
152
296k
     if (!options_str[i])
153
0
       break;
154
       
155
296k
     counter = 0;
156
2.81M
     while(options_str[i] != '=' && options_str[i] && options_str[i] != ' ')
157
2.51M
     {
158
2.51M
       cups_options[elem_counter] = (char *)realloc(cups_options[elem_counter], sizeof(char) * (counter + 2));
159
2.51M
       cups_options[elem_counter][counter] = options_str[i];
160
2.51M
       counter++;
161
2.51M
       i++;
162
2.51M
     }
163
296k
     cups_options[elem_counter][counter] = '\0';
164
296k
     if (options_str[i] == '=')
165
26.3k
     {
166
26.3k
       ++i;
167
26.3k
       counter = 0;
168
1.74M
       while(options_str[i] != ' ' && options_str[i])
169
1.71M
       {
170
1.71M
         cups_values[elem_counter] = (char *)realloc(cups_values[elem_counter], sizeof(char) * (counter + 2));
171
1.71M
         cups_values[elem_counter][counter] = options_str[i];
172
1.71M
         counter++;
173
1.71M
         i++;
174
1.71M
       }
175
26.3k
       cups_values[elem_counter][counter] = '\0';
176
26.3k
     }
177
296k
     elem_counter++;
178
296k
     cups_options = (char **)realloc(cups_options, sizeof(char *) * (elem_counter + 1));
179
296k
     cups_values = (char **)realloc(cups_values, sizeof(char *) * (elem_counter + 1));
180
296k
   }
181
10.1k
   if (len <= 0)
182
1
     goto cleanup;
183
   
184
   /*
185
    * Create and fill .ppd file
186
    * with fuzz-generated data
187
   */
188
   
189
10.1k
   FILE *fp = fopen(filename, "wb");
190
10.1k
   if (!fp)
191
0
     goto cleanup;
192
 
193
10.1k
   fwrite(data, sizeof(*data), len, fp);
194
10.1k
   fclose(fp);
195
 
196
10.1k
   if ((ppd = ppdOpenFile(filename)) == NULL)
197
2.22k
   {
198
2.22k
     ppd_status_t err; /* Last error in file */
199
2.22k
     int line;         /* Line number in file */
200
2.22k
     ppdLastError(&line);
201
2.22k
     ppdErrorString(err);
202
2.22k
     goto cleanup;
203
2.22k
   }
204
   
205
7.89k
   pc = _ppdCacheCreateWithPPD(NULL, ppd);
206
   
207
   /*
208
    * Do pwg tests from testpwg.c
209
    */
210
   
211
7.89k
   char *pagesize;
212
7.89k
   _ppdCacheWriteFile(pc, pwgname, NULL);
213
7.89k
   pc2 = _ppdCacheCreateWithFile(pwgname, NULL);
214
7.89k
   _ppdCacheDestroy(pc2);
215
7.89k
   ppdPageSize(ppd, ppdsize);
216
7.89k
   pagesize = _ppdCacheGetPageSize(pc, NULL, ppdsize, NULL);
217
7.89k
   job = ippNew();
218
7.89k
   ippDelete(job);
219
7.89k
   pwgMediaForPWG(pwg);
220
7.89k
   pwgMediaForLegacy(legacy);
221
7.89k
   pwgMediaForPPD(ppdmedia);
222
7.89k
   pwgMediaForSize(width, length);
223
 
224
7.89k
   num_options = cupsParseOptions(options_str, num_options, &options);
225
7.89k
   ppdMarkDefaults(ppd);
226
7.89k
   cupsMarkOptions(ppd, num_options, options);
227
7.89k
   ppdConflicts(ppd);
228
 
229
7.89k
   _ppdCacheGetFinishingValues(ppd, pc, (int)sizeof(finishings) / sizeof(finishings[0]), finishings);
230
7.89k
   cupsRasterInterpretPPD(&header, ppd, num_options, options, NULL);
231
   
232
7.89k
   if (strlen(marked_option) > 0)
233
7.67k
   {
234
7.67k
     char *choice = (char *)calloc(1, sizeof(char));
235
40.0k
     for (int i = 0; i < strlen(marked_option); i++)
236
32.6k
     {
237
32.6k
       if (!marked_option[i] || marked_option[i] != ' ')
238
32.3k
       {
239
32.3k
         choice = (char *)realloc(choice, sizeof(char) * (i + 2));
240
32.3k
         choice[i] = marked_option[i];
241
32.3k
         choice[i + 1] = '\0';
242
32.3k
       }
243
336
       else
244
336
         break;
245
32.6k
     }
246
7.67k
     ppdFindAttr(ppd, choice, marked_option + strlen(choice));
247
7.67k
     ppdFindNextAttr(ppd, choice, NULL);
248
7.67k
     if ((ppd_bin = ppdFindMarkedChoice(ppd, choice)) != NULL)
249
57
       _ppdCacheGetBin(pc, ppd_bin->choice);
250
7.67k
     char buffer[1024] = {0};
251
7.67k
     ppdLocalizeIPPReason(ppd, choice, marked_option + strlen(choice), buffer, sizeof(buffer));
252
275k
     for (int i = 0; i < elem_counter; i++)
253
267k
     {
254
267k
       ppdMarkOption(ppd, cups_options[i], cups_values[i]);
255
267k
       cupsGetOption(cups_options[i], num_options, options);
256
267k
       num_options = cupsGetConflicts(ppd, cups_options[i], cups_values[i], &options);
257
267k
       cupsResolveConflicts(ppd, cups_options[i], cups_values[i], &num_options, &options);
258
267k
       ppdInstallableConflict(ppd, cups_options[i], cups_values[i]);
259
267k
     }
260
7.67k
     ppdInstallableConflict(ppd, options_str, choice);
261
7.67k
     ppdLocalizeMarkerName(ppd, choice);
262
7.67k
     free(choice);
263
7.67k
   }
264
 
265
47.3k
   for (int i = 0; i < 5; i++)
266
39.4k
     ppdEmitString(ppd, i, 0.0);
267
   
268
7.89k
   ppdPageSizeLimits(ppd, &minsize, &maxsize);
269
7.89k
   ppdPageSize(ppd, NULL);
270
 
271
7.89k
   ret = 0;
272
 
273
10.1k
 cleanup:
274
306k
   for (int i = 0; i < elem_counter; i++)
275
296k
   {
276
296k
     free(cups_options[i]);
277
296k
     free(cups_values[i]);
278
296k
   }
279
   
280
10.1k
   free(cups_options);
281
10.1k
   free(cups_values);
282
 
283
10.1k
   cupsFreeOptions(num_options, options);
284
10.1k
   if (pc)
285
7.89k
     _ppdCacheDestroy(pc);
286
10.1k
   if (ppd)
287
7.89k
     ppdClose(ppd);
288
   
289
10.1k
   free(options_str);
290
10.1k
   free(ppdsize);
291
10.1k
   free(marked_option);
292
10.1k
   free(legacy);
293
10.1k
   free(pwg);
294
10.1k
   free(ppdmedia);
295
10.1k
   return ret;
296
7.89k
 }
297
 
298
 void unlink_tempfile(void)
299
20.2k
 {
300
20.2k
   char filename[256];
301
20.2k
   sprintf(filename, "/tmp/fuzz_ppd.%d.ppd", getpid());
302
20.2k
   unlink(filename);
303
20.2k
   sprintf(filename, "/tmp/fuzz_ppd.%d.pwg", getpid());
304
20.2k
   unlink(filename);
305
   sprintf(filename, "%s.N", filename);
306
20.2k
   unlink(filename);
307
20.2k
 }
308