Skip to content

Fix a few small memory leaks; Fix use-after-free in ubus module - #443

Merged
jow- merged 3 commits into
ucode-lang:masterfrom
MattCatz:fix/ubus-nl80211-leaks
Oct 29, 2026
Merged

jow- merged 3 commits into
ucode-lang:masterfrom
MattCatz:fix/ubus-nl80211-leaks

Conversation

@MattCatz

Copy link
Copy Markdown
Contributor

I have been experimenting with some patches that enable the Hardware-assisted AddressSanitizer on aarch64 OpenWRT targets. Part of that included instrumenting ucode and libucode with the sanitizer for fun and profit. Four issues surfaced and they seem to be valid. Three of them are small memory leaks. One, 0529943, is a use after-free-fix.

These are my purposed fixes. I can include test cases if needed.

@MattCatz

Copy link
Copy Markdown
Contributor Author

@jow- The CI failures are unrelated to these changes. The failures are in the FFI module.

@jow-
jow- force-pushed the fix/ubus-nl80211-leaks branch from aecf1dd to caa1258 Compare September 23, 2026 08:45
@jow-

jow- commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

I have a counter-proposal for 76aa1d3 ("ubus: pin the connection across libubus dispatch"): #447 - the rest looks fine to me.

Would you be willing to test your originally observed UAF against #447 ?

@MattCatz

Copy link
Copy Markdown
Contributor Author

I have been running ucode with #447 applied and have not seen the issue? do you want me to rebase and drop the commit?

@jow-

jow- commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Given your feedback, I merged #447 now and would kindly ask you to rebase your fixes on top of current head, so that I can merge them.

uc_nl_request_common() allocates the request message before resolving the
generic netlink family id. When that lookup fails, the error return skips
nlmsg_free(), leaking the nl_msg and its payload buffer on every call.

The family cache is never refreshed, so a process that asks before the
driver is loaded keeps leaking for the rest of its life.

Signed-off-by: Matthew Cather <mattbob4@gmail.com>
uc_ubus_call_cb() converts every data reply into a ucode value, but keeps
only the first one unless the caller asked for multiple returns. Further
replies from a multi reply method were converted and then dropped without
a release, leaking one value per extra reply on every such call.

Signed-off-by: Matthew Cather <mattbob4@gmail.com>
uc_ubus_call_cb() converts each reply into a ucode value and stores it in
res->res as the data arrives, before the request status is known.
ubus_complete_request() keeps polling after that and can still finish with
UBUS_STATUS_TIMEOUT, or with UBUS_STATUS_CONNECTION_FAILED when the peer
goes away mid reply. A method may also send a reply and then return an
error status.

Both callers, uc_ubus_call() and uc_ubus_chan_request(), take err_return()
on a non-OK status, which is set_error() followed by return NULL, so the
value in res->res is never released. In multiple return mode the whole
accumulated reply array is lost.

Release it in uc_ubus_call_common() so both callers are covered, and clear
the field so a caller cannot hand back a stale pointer. The early
errval_return() paths all sit before ubus_invoke_async_fd(), so no data
callback can have run and res->res is still NULL there.

Signed-off-by: Matthew Cather <mattbob4@gmail.com>
@MattCatz
MattCatz force-pushed the fix/ubus-nl80211-leaks branch from caa1258 to dc5fd36 Compare October 28, 2026 23:08
@MattCatz

Copy link
Copy Markdown
Contributor Author

Some of the commits dropped off since #447 fixed them. 1f54bd2 also fixed the uloop fix.

@jow-
jow- merged commit a2c42ea into ucode-lang:master Oct 29, 2026
2 checks passed
@jow-

jow- commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants