Skip to content

Commit

Permalink
idr: fix overflow case for idr_for_each_entry_ul()
Browse files Browse the repository at this point in the history
idr_for_each_entry_ul() is buggy as it can't handle overflow
case correctly. When we have an ID == UINT_MAX, it becomes an
infinite loop. This happens when running on 32-bit CPU where
unsigned long has the same size with unsigned int.

There is no better way to fix this than casting it to a larger
integer, but we can't just 64 bit integer on 32 bit CPU. Instead
we could just use an additional integer to help us to detect this
overflow case, that is, adding a new parameter to this macro.
Fortunately tc action is its only user right now.

Fixes: 65a206c ("net/sched: Change act_api and act_xxx modules to use IDR")
Reported-by: Li Shuang <[email protected]>
Tested-by: Davide Caratti <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Chris Mi <[email protected]>
Signed-off-by: Cong Wang <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
congwang authored and davem330 committed Jul 2, 2019
1 parent eb1f5c0 commit e33d2b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions include/linux/idr.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,17 @@ static inline void idr_preload_end(void)
* idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
* @idr: IDR handle.
* @entry: The type * to use as cursor.
* @tmp: A temporary placeholder for ID.
* @id: Entry ID.
*
* @entry and @id do not need to be initialized before the loop, and
* after normal termination @entry is left with the value NULL. This
* is convenient for a "not found" value.
*/
#define idr_for_each_entry_ul(idr, entry, id) \
for (id = 0; ((entry) = idr_get_next_ul(idr, &(id))) != NULL; ++id)
#define idr_for_each_entry_ul(idr, entry, tmp, id) \
for (tmp = 0, id = 0; \
tmp <= id && ((entry) = idr_get_next_ul(idr, &(id))) != NULL; \
tmp = id, ++id)

/**
* idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
Expand Down
9 changes: 6 additions & 3 deletions net/sched/act_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
struct idr *idr = &idrinfo->action_idr;
struct tc_action *p;
unsigned long id = 1;
unsigned long tmp;

mutex_lock(&idrinfo->lock);

s_i = cb->args[0];

idr_for_each_entry_ul(idr, p, id) {
idr_for_each_entry_ul(idr, p, tmp, id) {
index++;
if (index < s_i)
continue;
Expand Down Expand Up @@ -292,6 +293,7 @@ static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
struct idr *idr = &idrinfo->action_idr;
struct tc_action *p;
unsigned long id = 1;
unsigned long tmp;

nest = nla_nest_start_noflag(skb, 0);
if (nest == NULL)
Expand All @@ -300,7 +302,7 @@ static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
goto nla_put_failure;

mutex_lock(&idrinfo->lock);
idr_for_each_entry_ul(idr, p, id) {
idr_for_each_entry_ul(idr, p, tmp, id) {
ret = tcf_idr_release_unsafe(p);
if (ret == ACT_P_DELETED) {
module_put(ops->owner);
Expand Down Expand Up @@ -533,8 +535,9 @@ void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
struct tc_action *p;
int ret;
unsigned long id = 1;
unsigned long tmp;

idr_for_each_entry_ul(idr, p, id) {
idr_for_each_entry_ul(idr, p, tmp, id) {
ret = __tcf_idr_release(p, false, true);
if (ret == ACT_P_DELETED)
module_put(ops->owner);
Expand Down

0 comments on commit e33d2b7

Please sign in to comment.