Skip to content

lib: Bound the buffers G_get_datumparams_from_projinfo writes into - #7983

Open
Pranav-error wants to merge 1 commit into
OSGeo:mainfrom
Pranav-error:fix-datum-buffer-overflow
Open

Pranav-error wants to merge 1 commit into
OSGeo:mainfrom
Pranav-error:fix-datum-buffer-overflow

Conversation

@Pranav-error

Copy link
Copy Markdown
Contributor

Fixes #7982.

G_get_datumparams_from_projinfo() wrote the datum, datumparams, nadgrids, and towgs84/dx/dy/dz values straight from a PROJ_INFO-format Key_Value set into caller-supplied buffers with plain sprintf, and the function signature had no size parameter at all — so it had no way to bound the write regardless of how big the caller's buffer actually was.

general/g.setproj/main.c calls it with two 100-byte stack buffers. Any datum value over 99 characters overflows the first; any towgs84/nadgrids value, or the three dx/dy/dz values combined (joined as "towgs84=%s,%s,%s"), over roughly 90 characters overflows the second. These values come from a location's PROJ_INFO file, which is plain text and not length-validated anywhere — hand-edited, imported from an external CRS/WKT definition via g.proj, or shipped in a downloaded sample or shared location.

lib/gis/proj3.c's own caller uses 256-byte buffers — so the function already has two different "safe" sizes depending on which caller you look at, which is the real problem: there is no size the function itself can assume.

Fixed by adding explicit size_t parameters for each buffer and switching both sprintf calls to snprintf, updating both call sites (g.setproj and proj3.c) to pass sizeof() their own buffers.

Verified with a small program linked against a local build of libgrass_gis: a 499-character towgs84 value, which previously overflowed the 100-byte g.setproj buffer via plain sprintf, now truncates safely to 99 characters plus the terminator instead. Built the gis library (grass_gis target) clean with the change — general/g.setproj has no CMake target in this tree to build standalone, so I also compiled and linked a small driver program against the installed header and built library using the exact call shape g.setproj uses, to confirm the signature change is fully consistent end to end.

Copilot AI balanced review requested due to automatic review settings October 1, 2026 02:59

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread general/g.setproj/main.c Outdated
Comment on lines +212 to +214
if (exist && (G_get_datumparams_from_projinfo(
old_proj_keys, lbuf, sizeof(lbuf), lbufa,
sizeof(lbufa)) == 2)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
if (exist && (G_get_datumparams_from_projinfo(
old_proj_keys, lbuf, sizeof(lbuf), lbufa,
sizeof(lbufa)) == 2)) {
if (exist &&
(G_get_datumparams_from_projinfo(old_proj_keys, lbuf, sizeof(lbuf),
lbufa, sizeof(lbufa)) == 2)) {

Comment thread lib/gis/datum.c Outdated
Comment on lines 139 to 140
G_find_key_value("dy", projinfo),
G_find_key_value("dz", projinfo));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
G_find_key_value("dy", projinfo),
G_find_key_value("dz", projinfo));
G_find_key_value("dy", projinfo),
G_find_key_value("dz", projinfo));

@Pranav-error Pranav-error changed the title Bound the buffers G_get_datumparams_from_projinfo writes into lib: Bound the buffers G_get_datumparams_from_projinfo writes into Oct 4, 2026
G_get_datumparams_from_projinfo() wrote the datum, datumparams,
nadgrids, and towgs84/dx/dy/dz values straight from a PROJ_INFO-format
Key_Value set into caller-supplied buffers with plain sprintf, and the
function signature had no size parameter at all, so it had no way to
bound the write regardless of how big the caller's buffer was.

general/g.setproj/main.c calls it with two 100-byte stack buffers.
Any datum value over 99 characters overflows the first; any
towgs84/nadgrids value, or the three dx/dy/dz values combined (joined
as "towgs84=%s,%s,%s"), over roughly 90 characters overflows the
second. These values come from a location's PROJ_INFO file, which is
plain text and not length-validated anywhere -- hand-edited, imported
from an external CRS/WKT definition via g.proj, or shipped in a
downloaded sample or shared location.

lib/gis/proj3.c's own caller uses 256-byte buffers -- so the function
already has two different "safe" sizes depending on which caller you
look at, which is the real problem: there is no size the function
itself can assume.

Added explicit size_t parameters for each buffer and switched both
sprintf calls to snprintf, updating both call sites (g.setproj and
proj3.c) to pass sizeof() their own buffers.

Verified with a small program linked against a local build of
libgrass_gis: a 499-character towgs84 value, which previously
overflowed the 100-byte g.setproj buffer via plain sprintf, now
truncates safely to 99 characters plus the terminator instead. Built
the gis library (grass_gis target) clean with the change.

Fixes OSGeo#7982.

Signed-off-by: sai pranav <rajasaipranav0@gmail.com>
@Pranav-error
Pranav-error force-pushed the fix-datum-buffer-overflow branch from a3d1ebf to 57095f2 Compare October 4, 2026 08:14

@Valyrian-Code Valyrian-Code left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a maintainer, just had a look. Both callers are updated and the sizeof arguments are real arrays, not pointers. Nothing in grass-addons calls this one either.

One thing: snprintf truncates quietly and the return is still 1 or 2, so a caller cannot tell a full value from a cut one. Hard to hit at 256 bytes though.

@Pranav-error

Copy link
Copy Markdown
Contributor Author

Thanks for checking. Right, a cut value isn't reported to the caller. I kept the return values as they are, since reporting truncation would change what the function returns; I can add it if the maintainers want that.

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 this pull request may close these issues.

G_get_datumparams_from_projinfo() has no size parameter, overflows caller buffers

3 participants