Skip to content

Commit

Permalink
More strdup checks (Issue #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Dec 29, 2020
1 parent 6868546 commit 5285563
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
11 changes: 10 additions & 1 deletion pappl/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,19 @@ _papplJobSubmitFile(
else
{
// Abort the job...
job->state = IPP_JSTATE_ABORTED;
job->state = IPP_JSTATE_ABORTED;
job->completed = time(NULL);

papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to allocate filename.");
unlink(filename);

pthread_rwlock_wrlock(&job->printer->rwlock);
cupsArrayRemove(job->printer->active_jobs, job);
cupsArrayAdd(job->printer->completed_jobs, job);
pthread_rwlock_unlock(&job->printer->rwlock);

if (!job->system->clean_time)
job->system->clean_time = time(NULL) + 60;
}
}

Expand Down
6 changes: 6 additions & 0 deletions pappl/mainloop-subcommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,12 @@ copy_printer(_pappl_ml_printer_t *p) // I - Printer to copy
np->seen = p->seen;
np->device_uri = strdup(p->device_uri);
np->device_id = p->device_id ? strdup(p->device_id) : NULL;

if (!np->device_uri || (p->name && !np->name) || (p->device_id && !np->device_id))
{
free_printer(np);
return (NULL);
}
}

return (np);
Expand Down
9 changes: 7 additions & 2 deletions pappl/printer-raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,13 @@ _papplPrinterRunRaw(
}

// Finish the job...
job->filename = strdup(filename);
job->state = IPP_JSTATE_PENDING;
if ((job->filename = strdup(filename)) == NULL)
{
unlink(filename);
goto abort_job;
}

job->state = IPP_JSTATE_PENDING;

_papplPrinterCheckJobs(printer);
continue;
Expand Down
13 changes: 12 additions & 1 deletion pappl/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ papplPrinterCreate(
printer->usb_vendor_id = 0x1209; // See <pid.codes>
printer->usb_product_id = 0x8011;

if (!printer->name || !printer->dns_sd_name || !printer->resource || (device_id && !printer->device_id) || !printer->device_uri || !printer->driver_name || !printer->attrs)
{
// Failed to allocate one of the required members...
_papplPrinterDelete(printer);
return (NULL);
}

if (papplSystemGetDefaultPrintGroup(system, print_group, sizeof(print_group)))
papplPrinterSetPrintGroup(printer, print_group);

Expand Down Expand Up @@ -365,7 +372,11 @@ papplPrinterCreate(
*ptr = '\0';

snprintf(temp_id, sizeof(temp_id), "MFG:%s;MDL:%s;CMD:%s;", mfg, mdl, cmd);
printer->device_id = strdup(temp_id);
if ((printer->device_id = strdup(temp_id)) == NULL)
{
_papplPrinterDelete(printer);
return (NULL);
}
}

// charset-configured
Expand Down
6 changes: 6 additions & 0 deletions pappl/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ copy_resource(_pappl_resource_t *r) // I - Resource to copy
newr->filename = strdup(r->filename);
if (r->language)
newr->language = strdup(r->language);

if (!newr->path || !newr->format || (r->filename && !newr->filename) || (r->language && !newr->language))
{
free_resource(newr);
return (NULL);
}
}

return (newr);
Expand Down
2 changes: 1 addition & 1 deletion pappl/system-ipp.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ ipp_get_system_attributes(
if (!ra || cupsArrayFind(ra, "system-up-time"))
ippAddInteger(client->response, IPP_TAG_SYSTEM, IPP_TAG_INTEGER, "system-up-time", (int)(time(NULL) - system->start_time));

if (!ra || cupsArrayFind(ra, "system-uuid"))
if (system->uuid && (!ra || cupsArrayFind(ra, "system-uuid")))
ippAddString(client->response, IPP_TAG_SYSTEM, IPP_TAG_URI, "system-uuid", NULL, system->uuid);

if (!ra || cupsArrayFind(ra, "system-xri-supported"))
Expand Down
20 changes: 17 additions & 3 deletions pappl/system-loadsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ papplSystemLoadState(
else if (!strcasecmp(line, "NextPrinterID") && value)
system->next_printer_id = (int)strtol(value, NULL, 10);
else if (!strcasecmp(line, "UUID") && value)
system->uuid = strdup(value);
{
if ((system->uuid = strdup(value)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR, "Unable to allocate memory for system UUID.");
return (false);
}
}
else if (!strcasecmp(line, "<Printer") && value)
{
// Read a printer...
Expand Down Expand Up @@ -285,7 +291,14 @@ papplSystemLoadState(
}

if ((job_value = cupsGetOption("filename", num_options, options)) != NULL)
job->filename = strdup(job_value);
{
if ((job->filename = strdup(job_value)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_ERROR, "Error creating job %s for printer %s", job_name, printer->name);
break;
}
}

if ((job_value = cupsGetOption("state", num_options, options)) != NULL)
job->state = (ipp_jstate_t)strtol(job_value, NULL, 10);
if ((job_value = cupsGetOption("state_reasons", num_options, options)) != NULL)
Expand All @@ -306,7 +319,8 @@ papplSystemLoadState(
{
// Load the file attributes from the spool directory...
int attr_fd; // Attribute file descriptor
char job_attr_filename[256]; // Attribute filename
char job_attr_filename[256];
// Attribute filename

if ((attr_fd = papplJobOpenFile(job, job_attr_filename, sizeof(job_attr_filename), system->directory, "ipp", "r")) < 0)
{
Expand Down
17 changes: 14 additions & 3 deletions pappl/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ papplSystemCreate(
system->admin_gid = (gid_t)-1;
system->auth_service = auth_service ? strdup(auth_service) : NULL;

if (!system->name || !system->dns_sd_name || (spooldir && !system->directory) || (logfile && !system->logfile) || (subtypes && !system->subtypes) || (auth_service && !system->auth_service))
goto fatal;

// Make sure the system name and UUID are initialized...
papplSystemSetHostname(system, NULL);
papplSystemSetUUID(system, NULL);
Expand All @@ -208,7 +211,8 @@ papplSystemCreate(
char newspooldir[256]; // Spool directory

snprintf(newspooldir, sizeof(newspooldir), "%s/pappl%d.d", tmpdir, (int)getpid());
system->directory = strdup(newspooldir);
if ((system->directory = strdup(newspooldir)) == NULL)
goto fatal;
}

if (mkdir(system->directory, 0700) && errno != EEXIST)
Expand All @@ -228,7 +232,8 @@ papplSystemCreate(

snprintf(newlogfile, sizeof(newlogfile), "%s/pappl%d.log", tmpdir, (int)getpid());

system->logfile = strdup(newlogfile);
if ((system->logfile = strdup(newlogfile)) == NULL)
goto fatal;
}

_papplLogOpen(system);
Expand Down Expand Up @@ -463,7 +468,13 @@ papplSystemRun(pappl_system_t *system) // I - System
// main name...
strlcpy(header, "Unknown PAPPL/" PAPPL_VERSION " CUPS IPP/2.0", sizeof(header));
}
system->server_header = strdup(header);

if ((system->server_header = strdup(header)) == NULL)
{
papplLog(system, PAPPL_LOGLEVEL_FATAL, "Unable to allocate Server header value.");
system->is_running = false;
return;
}

// Make the static attributes...
make_attributes(system);
Expand Down

0 comments on commit 5285563

Please sign in to comment.