Skip to content

Commit

Permalink
pf syncookies: fix memory leak
Browse files Browse the repository at this point in the history
We forgot to free the nvlist (and packed nvlist) on success.
While here start using the ERROUT macro to clean up error handling, and
to add SDTs for better debugging.

Reported by:	Coverity
CID:		1473150
  • Loading branch information
kprovost authored and fichtner committed Jun 2, 2022
1 parent f6f1175 commit a769e8d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions sys/netpfil/pf/pf_syncookies.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ pf_get_syncookies(struct pfioc_nv *nv)
{
nvlist_t *nvl = NULL;
void *nvlpacked = NULL;
int error;

#define ERROUT(x) ERROUT_FUNCTION(errout, x)

nvl = nvlist_create(0);
if (nvl == NULL)
return (ENOMEM);
ERROUT(ENOMEM);

nvlist_add_bool(nvl, "enabled",
V_pf_status.syncookies_mode != PF_SYNCOOKIES_NEVER);
Expand All @@ -154,21 +157,23 @@ pf_get_syncookies(struct pfioc_nv *nv)
nvlist_add_number(nvl, "lowwater", V_pf_syncookie_status.lowat);

nvlpacked = nvlist_pack(nvl, &nv->len);
if (nvlpacked == NULL) {
nvlist_destroy(nvl);
return (ENOMEM);
}
if (nvlpacked == NULL)
ERROUT(ENOMEM);

if (nv->size == 0) {
nvlist_destroy(nvl);
free(nvlpacked, M_TEMP);
return (0);
ERROUT(0);
} else if (nv->size < nv->len) {
nvlist_destroy(nvl);
free(nvlpacked, M_TEMP);
return (ENOSPC);
ERROUT(ENOSPC);
}

return (copyout(nvlpacked, nv->data, nv->len));
error = copyout(nvlpacked, nv->data, nv->len);

#undef ERROUT
errout:
nvlist_destroy(nvl);
free(nvlpacked, M_TEMP);

return (error);
}

int
Expand Down

0 comments on commit a769e8d

Please sign in to comment.