Repository navigation
lib: Bound the buffers G_get_datumparams_from_projinfo writes into - #7983
Open
Pranav-error wants to merge 1 commit into
Open
Pranav-error wants to merge 1 commit into
Pranav-error wants to merge 1 commit into
Conversation
Comment on lines
+212
to
+214
| if (exist && (G_get_datumparams_from_projinfo( | ||
| old_proj_keys, lbuf, sizeof(lbuf), lbufa, | ||
| sizeof(lbufa)) == 2)) { |
Contributor
There was a problem hiding this comment.
[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 on lines
139
to
140
| G_find_key_value("dy", projinfo), | ||
| G_find_key_value("dz", projinfo)); |
Contributor
There was a problem hiding this comment.
[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)); |
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
force-pushed
the
fix-datum-buffer-overflow
branch
from
October 4, 2026 08:14
a3d1ebf to
57095f2
Compare
Valyrian-Code
left a comment
Contributor
There was a problem hiding this comment.
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7982.
G_get_datumparams_from_projinfo()wrote thedatum,datumparams,nadgrids, andtowgs84/dx/dy/dzvalues straight from aPROJ_INFO-formatKey_Valueset into caller-supplied buffers with plainsprintf, 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.ccalls it with two 100-byte stack buffers. Anydatumvalue over 99 characters overflows the first; anytowgs84/nadgridsvalue, or the threedx/dy/dzvalues combined (joined as"towgs84=%s,%s,%s"), over roughly 90 characters overflows the second. These values come from a location'sPROJ_INFOfile, which is plain text and not length-validated anywhere — hand-edited, imported from an external CRS/WKT definition viag.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_tparameters for each buffer and switching bothsprintfcalls tosnprintf, updating both call sites (g.setprojandproj3.c) to passsizeof()their own buffers.Verified with a small program linked against a local build of
libgrass_gis: a 499-charactertowgs84value, which previously overflowed the 100-byteg.setprojbuffer via plainsprintf, now truncates safely to 99 characters plus the terminator instead. Built the gis library (grass_gistarget) clean with the change —general/g.setprojhas 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 shapeg.setprojuses, to confirm the signature change is fully consistent end to end.