Skip to content

Commit d551159

Browse files
zdohnaltillkamppeter
authored andcommitted
driverless: Fix uninitialized buffer and parsing ippfind output
(cherry picked from commit 72d8c47)
1 parent 4b2a931 commit d551159

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

cupsfilters/ipp.c

+47-11
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ get_printer_attributes5(http_t *http_printer,
191191
{
192192
const char *uri;
193193
int have_http, uri_status, host_port, i = 0, total_attrs = 0, fallback,
194-
cap = 0;
194+
cap = 0, uri_alloc = 0;
195195
char scheme[10], userpass[1024], host_name[1024], resource[1024];
196196
ipp_t *request, *response = NULL;
197197
ipp_attribute_t *attr;
@@ -247,7 +247,18 @@ get_printer_attributes5(http_t *http_printer,
247247
if(resolve_uri_type == CUPS_BACKEND_URI_CONVERTER)
248248
uri = resolve_uri(raw_uri);
249249
else
250+
{
250251
uri = ippfind_based_uri_converter(raw_uri, resolve_uri_type);
252+
if (uri != NULL)
253+
uri_alloc = 1;
254+
}
255+
256+
if (uri == NULL)
257+
{
258+
log_printf(get_printer_attributes_log,
259+
"get-printer-attibutes: Cannot resolve URI: %s\n", raw_uri);
260+
return NULL;
261+
}
251262

252263
/* Extract URI componants needed for the IPP request */
253264
uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, uri,
@@ -261,6 +272,7 @@ get_printer_attributes5(http_t *http_printer,
261272
log_printf(get_printer_attributes_log,
262273
"get-printer-attributes: Cannot parse the printer URI: %s\n",
263274
uri);
275+
if (uri_alloc == 1) free(uri);
264276
return NULL;
265277
}
266278

@@ -273,6 +285,7 @@ get_printer_attributes5(http_t *http_printer,
273285
log_printf(get_printer_attributes_log,
274286
"get-printer-attributes: Cannot connect to printer with URI %s.\n",
275287
uri);
288+
if (uri_alloc == 1) free(uri);
276289
return NULL;
277290
}
278291
} else
@@ -370,6 +383,7 @@ get_printer_attributes5(http_t *http_printer,
370383
} else {
371384
/* Suitable response, we are done */
372385
if (have_http == 0) httpClose(http_printer);
386+
if (uri_alloc == 1) free(uri);
373387
return response;
374388
}
375389
} else {
@@ -398,6 +412,7 @@ get_printer_attributes5(http_t *http_printer,
398412
}
399413

400414
if (have_http == 0) httpClose(http_printer);
415+
if (uri_alloc == 1) free(uri);
401416
return NULL;
402417
}
403418

@@ -418,21 +433,19 @@ ippfind_based_uri_converter (const char *uri, int is_fax)
418433
char *ippfind_argv[100], /* Arguments for ippfind */
419434
*ptr_to_port = NULL,
420435
*reg_type,
421-
*resolved_uri, /* Buffer for resolved URI */
436+
*resolved_uri = NULL, /* Buffer for resolved URI */
422437
*resource_field = NULL,
423438
*service_hostname = NULL,
424439
/* URI components... */
425440
scheme[32],
426441
userpass[256],
427442
hostname[1024],
428443
resource[1024],
429-
buffer[8192], /* Copy buffer */
444+
*buffer = NULL, /* Copy buffer */
430445
*ptr; /* Pointer into string */;
431446
cups_file_t *fp; /* Post-processing input file */
432447
int status; /* Status of GET request */
433448

434-
resolved_uri = (char *)malloc(2048 * (sizeof(char)));
435-
436449
status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
437450
userpass, sizeof(userpass),
438451
hostname, sizeof(hostname), &port, resource,
@@ -445,10 +458,16 @@ ippfind_based_uri_converter (const char *uri, int is_fax)
445458

446459
/* URI is not DNS-SD-based, so do not resolve */
447460
if ((reg_type = strstr(hostname, "._tcp")) == NULL) {
448-
free(resolved_uri);
449461
return strdup(uri);
450462
}
451463

464+
resolved_uri = (char *)malloc(MAX_URI_LEN * (sizeof(char)));
465+
if (resolved_uri == NULL) {
466+
fprintf(stderr, "resolved_uri malloc: Out of memory\n");
467+
goto error;
468+
}
469+
memset(resolved_uri, 0, MAX_URI_LEN);
470+
452471
reg_type --;
453472
while (reg_type >= hostname && *reg_type != '.')
454473
reg_type --;
@@ -523,26 +542,38 @@ ippfind_based_uri_converter (const char *uri, int is_fax)
523542

524543
fp = cupsFileStdin();
525544

526-
while ((bytes = cupsFileGetLine(fp, buffer, sizeof(buffer))) > 0) {
545+
buffer = (char*)malloc(MAX_OUTPUT_LEN * sizeof(char));
546+
if (buffer == NULL) {
547+
fprintf(stderr, "buffer malloc: Out of memory.\n");
548+
goto error;
549+
}
550+
memset(buffer, 0, MAX_OUTPUT_LEN);
551+
552+
while ((bytes = cupsFileGetLine(fp, buffer, MAX_OUTPUT_LEN)) > 0) {
527553
/* Mark all the fields of the output of ippfind */
528554
ptr = buffer;
555+
556+
/* ignore new lines */
557+
if (bytes < 3)
558+
goto read_error;
559+
529560
/* First, build the DNS-SD-service-name-based URI ... */
530561
while (ptr && !isalnum(*ptr & 255)) ptr ++;
531562

532563
service_hostname = ptr;
533-
ptr = memchr(ptr, '\t', sizeof(buffer) - (ptr - buffer));
564+
ptr = memchr(ptr, '\t', MAX_OUTPUT_LEN - (ptr - buffer));
534565
if (!ptr) goto read_error;
535566
*ptr = '\0';
536567
ptr ++;
537568

538569
resource_field = ptr;
539-
ptr = memchr(ptr, '\t', sizeof(buffer) - (ptr - buffer));
570+
ptr = memchr(ptr, '\t', MAX_OUTPUT_LEN - (ptr - buffer));
540571
if (!ptr) goto read_error;
541572
*ptr = '\0';
542573
ptr ++;
543574

544575
ptr_to_port = ptr;
545-
ptr = memchr(ptr, '\t', sizeof(buffer) - (ptr - buffer));
576+
ptr = memchr(ptr, '\t', MAX_OUTPUT_LEN - (ptr - buffer));
546577
if (!ptr) goto read_error;
547578
*ptr = '\0';
548579
ptr ++;
@@ -566,9 +597,12 @@ ippfind_based_uri_converter (const char *uri, int is_fax)
566597
output_of_fax_uri = 1; /* fax-uri requested from fax-capable device */
567598

568599
read_error:
569-
continue;
600+
memset(buffer, 0, MAX_OUTPUT_LEN);
570601
}
571602

603+
if (buffer != NULL)
604+
free(buffer);
605+
572606
/*
573607
* Wait for the child processes to exit...
574608
*/
@@ -615,6 +649,8 @@ ippfind_based_uri_converter (const char *uri, int is_fax)
615649
*/
616650

617651
error:
652+
if (resolved_uri != NULL)
653+
free(resolved_uri);
618654
return (NULL);
619655
}
620656

cupsfilters/ipp.h

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ extern "C" {
3838
#endif
3939

4040
#define LOGSIZE 4 * 65536
41+
#define MAX_OUTPUT_LEN 8192
42+
#define MAX_URI_LEN 2048
43+
4144
char get_printer_attributes_log[LOGSIZE];
4245

4346
const char *resolve_uri(const char *raw_uri);

0 commit comments

Comments
 (0)