mm/shrinker: avoid out-of-bounds read in set_shrinker_bit()
Subsystems (2)
PATCH 1/1 mm/shrinker: avoid out-of-bounds read in set_shrinker_bit()
set_shrinker_bit() reads info->unit[shrinker_id_to_index(shrinker_id)] before checking shrinker_id against info->map_nr_max, so an id past the currently visible map_nr_max reads past the unit[] array before the WARN_ON_ONCE() catches it.
Move the load into the bounded branch.
--- mm/shrinker.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/shrinker.c +3 -2
@@ -197,12 +197,13 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) {if (shrinker_id >=0 && memcg && !mem_cgroup_is_root (memcg)) {struct shrinker_info *info; -struct shrinker_info_unit *unit;rcu_read_lock (); info =rcu_dereference (memcg->nodeinfo[nid]->shrinker_info); - unit = info->unit[shrinker_id_to_index (shrinker_id)];if (!WARN_ON_ONCE (shrinker_id >= info->map_nr_max)) { +struct shrinker_info_unit *unit; + + unit = info->unit[shrinker_id_to_index (shrinker_id)];/* Pairs with smp mb in shrink_slab() */ smp_mb__before_atomic ();set_bit (shrinker_id_to_offset (shrinker_id), unit->map);
Signature · 1 line
2.53.0
Hi David,
Quoted 4 lines · David Carlier
set_shrinker_bit()reads info->unit[shrinker_id_to_index(shrinker_id)] before checkingshrinker_idagainstinfo->map_nr_max, so an id past the currently visiblemap_nr_maxreads past the unit[] array before theWARN_ON_ONCE()catches it.
Did you run into this problem in practice, of just find it via code inspection? It's virtually impossible to happened, which is why WARN_ON_ONCE() was added to catch it.
Quoted 8 lines
Move the load into the bounded branch.
Fixes 307bececcd12 ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}")Signed-off-by David Carlier <[email protected]>--- mm/shrinker.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Anyway, It LGTM, so:
Thanks,
Qi
Quoted 21 lines
mm/shrinker.c +3
-2@@ -197,12 +197,13 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) {if (shrinker_id >=0 && memcg && !mem_cgroup_is_root (memcg)) {struct shrinker_info *info; -struct shrinker_info_unit *unit;rcu_read_lock (); info =rcu_dereference (memcg->nodeinfo[nid]->shrinker_info); - unit = info->unit[shrinker_id_to_index (shrinker_id)];if (!WARN_ON_ONCE (shrinker_id >= info->map_nr_max)) { +struct shrinker_info_unit *unit; + + unit = info->unit[shrinker_id_to_index (shrinker_id)];/* Pairs with smp mb in shrink_slab() */ smp_mb__before_atomic ();set_bit (shrinker_id_to_offset (shrinker_id), unit->map);
Hi Qi,
Quoted 4 lines · Qi Zheng
Hi David,
On 5/11/26 2:37 AM, David Carlier wrote:
Quoted 8 lines
Quoted 4 lines
set_shrinker_bit()reads info->unit[shrinker_id_to_index(shrinker_id)] before checkingshrinker_idagainstinfo->map_nr_max, so an id past the currently visiblemap_nr_maxreads past the unit[] array before theWARN_ON_ONCE()catches it.Did you run into this problem in practice, of just find it via code inspection? It's virtually impossible to happened, which is why
WARN_ON_ONCE()was added to catch it.
Definitely code inspection.
Quoted 10 lines
Quoted 9 lines
Move the load into the bounded branch.
Fixes 307bececcd12 ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}")Signed-off-by David Carlier <[email protected]>--- mm/shrinker.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 6 lines
Anyway, It LGTM, so:
Reviewed-by Qi Zheng <[email protected]>Thanks,
Qi
Thanks !
Quoted 23 lines
Quoted 22 lines
mm/shrinker.c +3
-2@@ -197,12 +197,13 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) {if (shrinker_id >=0 && memcg && !mem_cgroup_is_root (memcg)) {struct shrinker_info *info; -struct shrinker_info_unit *unit;rcu_read_lock (); info =rcu_dereference (memcg->nodeinfo[nid]->shrinker_info); - unit = info->unit[shrinker_id_to_index (shrinker_id)];if (!WARN_ON_ONCE (shrinker_id >= info->map_nr_max)) { +struct shrinker_info_unit *unit; + + unit = info->unit[shrinker_id_to_index (shrinker_id)];/* Pairs with smp mb in shrink_slab() */ smp_mb__before_atomic ();set_bit (shrinker_id_to_offset (shrinker_id), unit->map);
Quoted 9 lines · David Carlier
set_shrinker_bit()reads info->unit[shrinker_id_to_index(shrinker_id)] before checkingshrinker_idagainstinfo->map_nr_max, so an id past the currently visiblemap_nr_maxreads past the unit[] array before theWARN_ON_ONCE()catches it.
Quoted 4 lines
Move the load into the bounded branch.
Fixes 307bececcd12 ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}")Signed-off-by David Carlier <[email protected]>
Thanks.