Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe use of strncpy() throughout OpenAMP #576

Closed
tarmasr opened this issue Mar 25, 2024 · 3 comments · Fixed by #620
Closed

Unsafe use of strncpy() throughout OpenAMP #576

tarmasr opened this issue Mar 25, 2024 · 3 comments · Fixed by #620
Labels

Comments

@tarmasr
Copy link

tarmasr commented Mar 25, 2024

As part of a code review of a project that uses OpenAMP, I came across multiple instances of unsafe strncpy() use in the OpenAMP code base.

The specific issue is that the strncpy() function does NOT 0-terminate the destination buffer if the length of the input string is >= size of the destination buffer. This may be contrary to expectations, but it is long-standing and documented behaviour (see for example https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html)

As a result, code that expects the destination buffer to be 0-terminated after a call to strncpy() -- and that is most code, because the destination buffer is typically used as a regular 0-terminated string elsewhere -- might read beyond the end of the buffer.

Safe use of strncpy() follows each call by an explicit 0-termination, like so:

char buffer[...];
strncpy(buffer, source, sizeof(buffer));
buffer[sizeof(buffer)-1] = '\0';

In the open-amp code base, strncpy() is used in 5 places as far as I can tell; none uses the safe idiom, so all are potentially vulnerable to buffer over-reads. I found strncpy() in the following locations in the open-amp sources:

apps/system/linux/machine/generic/platform_info.c (lines 146 and 165, functions sk_unix_client() and sk_unix_server())

lib/include/openamp/remoteproc.h (line 584, function remoteproc_init_mem())

lib/rpmsg/rpmsg.c (lines 144 and 278, functions rpmsg_send_ns_message() and rpmsg_register_endpoint())

A quick review of the buffers involved shows that each of them is used in at least one location as a (presumed) 0-terminated string (typically through a %s parameter in printf() or similar), so these are all latent problems. I would not classify them as security vulnerabilities, but in my opinion these cases should be fixed even if not exploitable.

@arnopo
Copy link
Collaborator

arnopo commented Mar 25, 2024

Hello @tarmasr

you are right for Rpmsg we should at least have similar protection as the one added in Linux:
https://elixir.bootlin.com/linux/latest/source/drivers/rpmsg/rpmsg_ns.c#L51

Could you send a PR to fix the library?

@tarmasr
Copy link
Author

tarmasr commented Mar 26, 2024

Thanks @arnopo

There is a pull request here: #577

This PR adds 0-termination of the destination buffer after each call of strncpy() in the open-amp project. In one place optional {...} braces were added to an else branch because its if branch now needs braces.

I cannot process the PR any further because it needs to be signed off by an approved maintainer.

Is this what you need for review, or do I need to do anything else?

Copy link

This issue has been marked as a stale issue because it has been open (more than) 45 days with no activity.

@github-actions github-actions bot added the Stale label May 11, 2024
@arnopo arnopo linked a pull request Oct 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants