Skip to content

Commit

Permalink
idr: Fix integer overflow in idr_for_each_entry
Browse files Browse the repository at this point in the history
If there is an entry at INT_MAX then idr_for_each_entry() will increment
id after handling it.  This is undefined behaviour, and is caught by
UBSAN.  Adding 1U to id forces the operation to be carried out as an
unsigned addition which (when assigned to id) will result in INT_MIN.
Since there is never an entry stored at INT_MIN, idr_get_next() will
return NULL, ending the loop as expected.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
  • Loading branch information
Matthew Wilcox (Oracle) committed Nov 3, 2019
1 parent 797060e commit f6341c5
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion include/linux/idr.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static inline void idr_preload_end(void)
* is convenient for a "not found" value.
*/
#define idr_for_each_entry(idr, entry, id) \
for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; id += 1U)

/**
* idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
Expand Down

0 comments on commit f6341c5

Please sign in to comment.