bug#79780: Resource leak in change_file_context() in src/chcon.c (coreutils Latest)

Pádraig Brady <[email protected]> Fri, 7 Nov 2025 15:55:23 +0000
Newsgroups gmane.comp.gnu.core-utils.bugs
Message-ID <[email protected]>
tag 79780 notabug
close 79780
stop

details below...

On 07/11/2025 10:28, Ray steven wrote:
> Subject: [BUG] Resource leak in change_file_context() in src/chcon.c
> (coreutils 9.x)
> 
> Hello Coreutils maintainers,
> 
> I discovered a potential resource leak at lines 170-171 in the function
> `change_file_context()` in `src/chcon.c`.
> 
> Bug Summary
> ------------
> A resource leak occurs when `specified_context` is NULL and
> `compute_context_from_mask()` fails. In this error path, the previously
> obtained `file_context` (via `getfileconat()` or `lgetfileconat()`) is not
> released, resulting in a memory leak. According to the official
> documentation, the caller must use `freecon()` to manually release the
> memory returned by `getfileconat()` or `lgetfileconat()`.
> 
> Suggested Fix
> -------------
> Call `freecon(file_context);` before returning when
> `compute_context_from_mask()` fails. For example:
> 
>      if (compute_context_from_mask(file_context, &context) != 0)
>      {
>          freecon(file_context);
>          return 1;
>      }

compute_context_from_mask() will do the free upon failure,
so there is no need for the explicit freecon() in this case.

thanks,
Padraig