Project

General

Profile

Actions

Bug #15471

open

Memory leak in pfSense module function ``pfSense_get_ifaddrs()``

Added by Jim Pingle 12 days ago. Updated 2 days ago.

Status:
Feedback
Priority:
High
Assignee:
Category:
PHP Interpreter
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:
Plus Target Version:
24.07
Release Notes:
Default
Affected Version:
Affected Architecture:

Description

Moving this over from #15459 since it does not appear to be a problem in LCDProc but in the base system pfSense PHP module.

There is a memory leak in the pfSense module function pfSense_get_ifaddrs(). The function appears to leak some memory in certain contexts, and sometimes more/less memory than others. It's still unclear how/why but here are a few things that we seem to have determined so far:

  • The function leaks 56 bytes on most calls, which happens to be the size of the ifaddrs struct, though the only apparent allocation made on that structure is also freed at the end.
  • When run on some interfaces it leaks 112 bytes (2x56).
  • When run on WireGuard interfaces it doesn't leak at all.

I checked some other similar functions and they did not appear to leak anything (e.g. pfSense_getall_interface_addresses() does not leak) as far as I could tell.

While the amount of memory is small, the function gets run dozens/hundreds of times in certain code paths and since LCDProc runs indefinitely in a loop, it adds up significantly over time.


Related issues

Has duplicate Bug #15066: PHP allocation failure in pfsense-utils.incDuplicate

Actions
Actions #1

Updated by Kristof Provost 10 days ago

  • Status changed from New to Feedback
  • Assignee set to Kristof Provost

We array_init() 'addr' (which causes PHP to allocate memory), but potentially
break out before adding 'addr' to anything.
That causes PHP to hold on to the memory until it stops running, when it frees
all of its memory. That means valgrind (or similar tools) don't actually see
this leak.

Explicitly release the allocated memory if we're not going to be using 'addr'.

https://gitlab.netgate.com/pfSense/FreeBSD-ports/-/commit/bb47d60831c94402a6f753c6af1a9d3d68022569

Actions #2

Updated by Georgiy Tyutyunnik 3 days ago

Was able to reproduce on 24.03 and 2.7.2 CE via calling the leaking function in endless loop.

tested on:
Version 2.8.0-DEVELOPMENT (amd64)
built on Thu May 16 5:30:00 UTC 2024
FreeBSD 15.0-CURRENT

This version fixes the issue

Actions #3

Updated by Marcos M 3 days ago

  • Has duplicate Bug #15066: PHP allocation failure in pfsense-utils.inc added
Actions #4

Updated by Jim Pingle 2 days ago

  • Assignee changed from Kristof Provost to Jim Pingle
  • % Done changed from 0 to 100

A quick test of the function looks good compared to my previous tests, but I'd prefer to keep this open for now until we have Plus dev snapshots going to test since that's where I could reproduce it reliably in a more complete and practical way.

Reassigning to me since it's waiting on me to test.

Actions

Also available in: Atom PDF