Project

General

Profile

Actions

Bug #15471

closed

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

Added by Jim Pingle about 1 month ago. Updated 13 days ago.

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

100%

Estimated time:
Plus Target Version:
24.08
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 about 1 month 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 about 1 month 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 about 1 month ago

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

Updated by Jim Pingle about 1 month 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 #5

Updated by Jim Pingle 20 days ago

  • Plus Target Version changed from 24.07 to 24.08
Actions #6

Updated by Jim Pingle 13 days ago

  • Status changed from Feedback to Resolved

Tested on a system where I could reproduce the problem before with LCDProc and even after 17 hours there is zero increased memory usage, where before it was showing increased usage almost immediately.

Seems safe to call this resolved.

Actions

Also available in: Atom PDF