Skip to content

Commit

Permalink
rhashtable: Fix unprotected RCU dereference in __rht_ptr
Browse files Browse the repository at this point in the history
The rcu_dereference call in rht_ptr_rcu is completely bogus because
we've already dereferenced the value in __rht_ptr and operated on it.
This causes potential double readings which could be fatal.  The RCU
dereference must occur prior to the comparison in __rht_ptr.

This patch changes the order of RCU dereference so that it is done
first and the result is then fed to __rht_ptr.  The RCU marking
changes have been minimised using casts which will be removed in
a follow-up patch.

Fixes: ba6306e ("rhashtable: Remove RCU marking from...")
Reported-by: "Gong, Sishuai" <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
herbertx authored and davem330 committed Jul 29, 2020
1 parent 19016d9 commit 1748f6a
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions include/linux/rhashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ static inline void rht_unlock(struct bucket_table *tbl,
local_bh_enable();
}

static inline struct rhash_head __rcu *__rht_ptr(
struct rhash_lock_head *const *bkt)
static inline struct rhash_head *__rht_ptr(
struct rhash_lock_head *p, struct rhash_lock_head __rcu *const *bkt)
{
return (struct rhash_head __rcu *)
((unsigned long)*bkt & ~BIT(0) ?:
return (struct rhash_head *)
((unsigned long)p & ~BIT(0) ?:
(unsigned long)RHT_NULLS_MARKER(bkt));
}

Expand All @@ -365,25 +365,26 @@ static inline struct rhash_head __rcu *__rht_ptr(
* access is guaranteed, such as when destroying the table.
*/
static inline struct rhash_head *rht_ptr_rcu(
struct rhash_lock_head *const *bkt)
struct rhash_lock_head *const *p)
{
struct rhash_head __rcu *p = __rht_ptr(bkt);

return rcu_dereference(p);
struct rhash_lock_head __rcu *const *bkt = (void *)p;
return __rht_ptr(rcu_dereference(*bkt), bkt);
}

static inline struct rhash_head *rht_ptr(
struct rhash_lock_head *const *bkt,
struct rhash_lock_head *const *p,
struct bucket_table *tbl,
unsigned int hash)
{
return rht_dereference_bucket(__rht_ptr(bkt), tbl, hash);
struct rhash_lock_head __rcu *const *bkt = (void *)p;
return __rht_ptr(rht_dereference_bucket(*bkt, tbl, hash), bkt);
}

static inline struct rhash_head *rht_ptr_exclusive(
struct rhash_lock_head *const *bkt)
struct rhash_lock_head *const *p)
{
return rcu_dereference_protected(__rht_ptr(bkt), 1);
struct rhash_lock_head __rcu *const *bkt = (void *)p;
return __rht_ptr(rcu_dereference_protected(*bkt, 1), bkt);
}

static inline void rht_assign_locked(struct rhash_lock_head **bkt,
Expand Down

0 comments on commit 1748f6a

Please sign in to comment.