Skip to contentSkip to conversation

fs/xfs/xfs_super.c:1100 xfs_init_percpu_counters() error: buffer overflow 'mp->m_free' 2 <= u32max

PATCH In review Dan Carpenter 2 participants 2 messages Feb 12, 2025 – Feb 13, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 2/5 fs/xfs/xfs_super.c:1100 xfs_init_percpu_counters() error: buffer overflow 'mp->m_free' 2 <= u32max

tree:   git://git.infradead.org/users/hch/xfs xfs-freecounters
head:   3bbffc404041adfe745be0979943086ab448915d
commit: 401cd937aa9d718776bb7ce5379d248388c093bf [2/5] xfs: generalize the freespace and reserved blocks handling
config: riscv-randconfig-r073-20250212 (https://download.01.org/0day-ci/archive/20250212/[email protected]/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project 6807164500e9920638e2ab0cdb4bf8321d24f8eb)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/xfs/xfs_super.c:1100 xfs_init_percpu_counters() error: buffer overflow 'mp->m_free' 2 <= u32max

vim +1100 fs/xfs/xfs_super.c

8757c38f2cf6e5 Ian Kent          2019-11-04  1066  static int
8757c38f2cf6e5 Ian Kent          2019-11-04  1067  xfs_init_percpu_counters(
8757c38f2cf6e5 Ian Kent          2019-11-04  1068  	struct xfs_mount	*mp)
8757c38f2cf6e5 Ian Kent          2019-11-04  1069  {
8757c38f2cf6e5 Ian Kent          2019-11-04  1070  	int			error;
401cd937aa9d71 Christoph Hellwig 2025-02-09  1071  	enum xfs_free_counter	i;

In GCC enums are unsigned int where possible.

8757c38f2cf6e5 Ian Kent          2019-11-04  1072  
8757c38f2cf6e5 Ian Kent          2019-11-04  1073  	error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
8757c38f2cf6e5 Ian Kent          2019-11-04  1074  	if (error)
8757c38f2cf6e5 Ian Kent          2019-11-04  1075  		return -ENOMEM;
8757c38f2cf6e5 Ian Kent          2019-11-04  1076  
8757c38f2cf6e5 Ian Kent          2019-11-04  1077  	error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
8757c38f2cf6e5 Ian Kent          2019-11-04  1078  	if (error)
8757c38f2cf6e5 Ian Kent          2019-11-04  1079  		goto free_icount;
8757c38f2cf6e5 Ian Kent          2019-11-04  1080  
8757c38f2cf6e5 Ian Kent          2019-11-04  1081  	error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
8757c38f2cf6e5 Ian Kent          2019-11-04  1082  	if (error)
401cd937aa9d71 Christoph Hellwig 2025-02-09  1083  		goto free_ifree;
8757c38f2cf6e5 Ian Kent          2019-11-04  1084  
7099bd0f243fa7 Christoph Hellwig 2024-04-22  1085  	error = percpu_counter_init(&mp->m_delalloc_rtextents, 0, GFP_KERNEL);
2229276c528326 Darrick J. Wong   2022-04-12  1086  	if (error)
2229276c528326 Darrick J. Wong   2022-04-12  1087  		goto free_delalloc;
2229276c528326 Darrick J. Wong   2022-04-12  1088  
401cd937aa9d71 Christoph Hellwig 2025-02-09  1089  	for (i = 0; i < XC_FREE_NR; i++) {
401cd937aa9d71 Christoph Hellwig 2025-02-09  1090  		error = percpu_counter_init(&mp->m_free[i].count, 0,
401cd937aa9d71 Christoph Hellwig 2025-02-09  1091  				GFP_KERNEL);
7099bd0f243fa7 Christoph Hellwig 2024-04-22  1092  		if (error)
401cd937aa9d71 Christoph Hellwig 2025-02-09  1093  			goto free_freecounters;

Assume i is zero

401cd937aa9d71 Christoph Hellwig 2025-02-09  1094  	}
7099bd0f243fa7 Christoph Hellwig 2024-04-22  1095  
8757c38f2cf6e5 Ian Kent          2019-11-04  1096  	return 0;
8757c38f2cf6e5 Ian Kent          2019-11-04  1097  
401cd937aa9d71 Christoph Hellwig 2025-02-09  1098  free_freecounters:
401cd937aa9d71 Christoph Hellwig 2025-02-09  1099  	while (--i > 0)

This should be: while (i-- > 0) {.

401cd937aa9d71 Christoph Hellwig 2025-02-09 @1100  		percpu_counter_destroy(&mp->m_free[i].count);
                                                                                                   ^

Out of bounds.

7099bd0f243fa7 Christoph Hellwig 2024-04-22  1101  	percpu_counter_destroy(&mp->m_delalloc_rtextents);
2229276c528326 Darrick J. Wong   2022-04-12  1102  free_delalloc:
2229276c528326 Darrick J. Wong   2022-04-12  1103  	percpu_counter_destroy(&mp->m_delalloc_blks);
8757c38f2cf6e5 Ian Kent          2019-11-04  1104  free_ifree:
8757c38f2cf6e5 Ian Kent          2019-11-04  1105  	percpu_counter_destroy(&mp->m_ifree);
8757c38f2cf6e5 Ian Kent          2019-11-04  1106  free_icount:
8757c38f2cf6e5 Ian Kent          2019-11-04  1107  	percpu_counter_destroy(&mp->m_icount);
8757c38f2cf6e5 Ian Kent          2019-11-04  1108  	return -ENOMEM;
8757c38f2cf6e5 Ian Kent          2019-11-04  1109  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 1 line · Dan Carpenter

In GCC enums are unsigned int where possible.

Thanks! I'l lswitch to a plain int for the iterator again. Would be so nice if the compiler could warn about such things.