Skip to content

ocpp fix - #475

Closed
nashif wants to merge 5 commits into
zephyrproject-rtos:mainfrom
nashif:ocpp_fix
Closed

nashif wants to merge 5 commits into
zephyrproject-rtos:mainfrom
nashif:ocpp_fix

Conversation

@nashif

@nashif nashif commented Oct 24, 2026

Copy link
Copy Markdown
Member
  • net: ocpp: increase idtag buffer size to hold full 20 char tag
  • net: ocpp: avoid use after free on init failure
  • net: ocpp: fix session-list use-after-free in meter-values notify
  • net: ocpp: terminate ChangeConfiguration and idTagInfo strings
  • net: ocpp: access matched sessions only under ilock

idtag buffer was exactly 20 bytes, so a spec maximum 20 character tag
left no room for the null terminator. Add one byte, and reject longer
tags in ocpp_authorize() as strncpy would again leave the buffer
unterminated for the JSON encoder.

Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
ocpp_upstream_init() starts the wsreader thread with ctx, so a later
failure of the cp allocation freed ctx while that thread was already
using it. Allocate cp before ocpp_upstream_init() so nothing fails
after the thread starts, and clear gctx on failure so the public API
does not dereference the freed context.

Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
The PDU_METER_VALUES loop walked the session list and dropped ilock
before per-session get meter values while holding the loop node. A
concurrent ocpp_session_close(), reachable via a RemoteStopTransaction
from the Central System, could unlink and free that node.

Pin the session with is_busy so close() defers the unlink and free.
A deferred close also marks the session inactive and closing, so it
is no longer matched by lookups or remote stop once the user closed
it. close() checks list membership before touching the session, so
closing an already freed handle stays harmless as before.

Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
ChangeConfiguration and the idTagInfo fields used strncpy with the
full buffer size, so a string from the central system that filled the
buffer was left unterminated and later read past its end.

Size the ChangeConfiguration buffers to hold the full OCPP CiString
limit plus the terminator and always terminate the copies, matching
the bounded copies already used for GetConfiguration and the remote
start idTag.

Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
ocpp_remote_stop_transaction() read idcon after dropping ilock, and
the response path checked the session with ocpp_session_is_valid()
then wrote resp_status and idtxn without the lock. A request that
timed out lets the user close and free the session while a late
response is still being handled, so both wrote or read freed memory.

Copy idcon while the session is matched, and validate and update the
response fields in one ilock section.

Signed-off-by: Saravanan Sekar <saravanan@linumiz.com>
@github-actions

Copy link
Copy Markdown

🤖 AI review (advisory)

💬 Comment: worth a look, nothing blocking.

Findings: 1 · Highest severity: low · Confidence: high

One minor issue remains where public session management APIs dereference gctx without checking for NULL if called after ocpp_init() fails.

1 finding(s) are posted as inline comments on the changed lines (1 low).

Commit message issues

Commit 97df27f ("net: ocpp: avoid use after free on init failure") states that clearing gctx prevents public API entry points from operating on a freed context after an initialization failure, but ocpp_session_open(), ocpp_session_close(), and ocpp_session_is_valid() still dereference gctx without a NULL check.


This review is generated by a language model with tool access to the source tree. It is advisory; verify before acting on it.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline note(s) from the AI review.

return 0;

out:
gctx = NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Public API entry points dereference NULL gctx after ocpp_init() failure · 🔵 low

Clearing gctx on the ocpp_init() failure path avoids dereferencing freed memory, but ocpp_session_open(), ocpp_session_close(), and ocpp_session_is_valid() still dereference gctx (&gctx->ilock) without checking for NULL, contrary to the commit message's claim that the public API is protected after init failure. Happens when ocpp_init() fails and returns via out:, setting gctx = NULL, followed by a call to ocpp_session_open(), ocpp_session_close(), or any API calling ocpp_session_is_valid(). Null pointer dereference in k_mutex_lock(&gctx->ilock, K_FOREVER) if any public session or request API is called after ocpp_init() fails.

Fix: Check gctx == NULL in ocpp_session_open(), ocpp_session_close(), and ocpp_session_is_valid() before locking gctx->ilock.

AI review, advisory; verify before acting on it.

@sonarqubecloud

Copy link
Copy Markdown

@nashif nashif closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants