After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 547950 - gstrfuncs.c: integer overflows and buffer overruns
gstrfuncs.c: integer overflows and buffer overruns
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2008-08-15 19:00 UTC by Christian Biere
Modified: 2018-05-24 11:30 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Partial bugfix (3.30 KB, patch)
2008-08-15 19:06 UTC, Christian Biere
rejected Details | Review

Description Christian Biere 2008-08-15 19:00:44 UTC
The code in glib/gstrfuncs.c is not very robust. It causes undefined behavior for extreme but perfectly valid parameters.

1. Trying to store the result of strlen() in a variable of type "int" can cause an integer overflow on any 64-bit platform. See g_str_has_prefix() and g_str_has_suffix().

2. Especially on a 32-bit platform adding or multiplying size_t values can easily cause a wrap around and result in allocating too little memory, resulting in a buffer overrun afterwards. Instead size_t values should instead saturate at SIZE_MAX. Assuming SIZE_MAX memory cannot be allocated, the application will properly terminate.
Comment 1 Christian Biere 2008-08-15 19:06:33 UTC
Created attachment 116695 [details] [review]
Partial bugfix

The patch fixes some of the mentioned issues but shouldn't be considered a complete fix.
Comment 2 Paolo Borelli 2008-08-31 11:52:07 UTC
Some review comments:

- Coding Style: missing spaces before (
- Use gsize, G_MAXSIZE etc
- add unit tests in glib/tests/strfunc.c
Comment 3 Philip Withnall 2017-11-16 10:45:02 UTC
Review of attachment 116695 [details] [review]:

This patch isn’t really going anywhere. In order for it to be accepted, I’d like to see:
 • Unit tests covering all the possible overflow cases (because that code is not going to be hit – and therefore not going to be tested – at any other time, normally)
 • Handling of overflow by returning errors, rather than saturating the size and truncating strings (which is not detectable by the caller)
 • Low performance overheads for any checks, since they cover really exceptional cases

::: gstrfuncs.c.orig
@@ +245,3 @@
+
+static inline size_t
+size_smult(size_t a, size_t b)

We now have g_size_checked_mul() as API, which might be useful here.
Comment 4 GNOME Infrastructure Team 2018-05-24 11:30:43 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/156.