Repository navigation
Conversation
backendSNMPSupplies() calculates the percent value using signed integer variables and writes the result into a fixed-sized buffer value. Unexpectedly large supplies level values can lead to signed integer overflow and, as a result, to undefined behavior. Also If the percent strings are longer than the value buffer expected, concatenation may cause truncation and potentially lead to out-of-bounds buffer access. Fixes: a484588 (Merge changes from CUPS 1.6svn-r9939.) Signed-off-by: Elizaveta Tereshkina <teryoshkina.ea@gmail.com>
Member
|
Already reported and fixed as a security issue. |
Contributor
Author
|
@michaelrsweet But this doesn't consider the possible integer overflow in the earlier expression |
Member
|
We assume that even if we get an undefined result, the subsequent range checks will ensure that "percent" is in the range 0 to 100. |
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.
Describe the bug
The
backendSNMPSupplies()function calculates thepercentvalue using signed integer variables and writes the result into a fixed-sized buffervalue[CUPS_MAX_SUPPLIES * 4].The values level, max_capacity, and others are taken from the cash file (
backend_init_supplies()function) or directly from the SNMP responses inbackend_walk_cb(). Both functions don't check values.On the one hand, unexpectedly large
supplieslevel values in the expression100 * supplies[i].levelcan lead to signed integer overflow and, as a result, to undefined behavior.On the other hand, even small values can lead to problem. The results are written to
char value[CUPS_MAX_SUPPLIES * 4]. Commas are placed between values:*ptr++ = ',';. The buffer is filled usingsnprintf()/cupsCopyString()and the pointer is moved on each iteration usingptr += strlen(ptr). The buffer size isCUPS_MAX_SUPPLIES * 4, so with maxnum_supplieseachpercentvalue should be no more than three characters, plus a comma as a delimiter. If the percent strings are longer than the buffer expected, concatenation may cause truncation and potentially lead to out-of-bounds buffer access. Even if thesnprintf()limits the number of written bytes, it may truncate the output. The separator insertion doesn't handle truncation and doesn't check whether there is enough space left. This may result overwriting of the null terminator, also calling thestrlen(ptr)may caused undefined behavior in the next iteration.Solution
I believe the solution is to add check directly to the
backendSNMPSupplies()function. If thelevelaccidentally exceeds themax_capacity, then thepercentis 100. In another case, it is better to expand the type of multiplication operation and then after division cast it to int. So thepercentwill be from 0 to 100, and as I understand the writing logic will work as well. Also may be is good to check values in thebackend_walk_cb()function, but in that case I don't know how to check them in thebackend_init_supplies()function.