net/mlx5e: ethtool: Fix formatting of ptp_rq0_csum_complete_tail_slow
authorKees Cook <kees@kernel.org>
Wed, 16 Apr 2025 02:01:14 +0000 (19:01 -0700)
committerJakub Kicinski <kuba@kernel.org>
Fri, 18 Apr 2025 01:54:57 +0000 (18:54 -0700)
commitcfba1d1b61ae3f32e4bc06e9860711a4488d98b7
tree7fd0486117700aee4c4f1996a5447cb570417dfb
parent151e13ece86d234213b7f224f0e26a957c0eeb3e
net/mlx5e: ethtool: Fix formatting of ptp_rq0_csum_complete_tail_slow

The new GCC 15 warning -Wunterminated-string-initialization reports:

In file included from drivers/net/ethernet/mellanox/mlx5/core/en.h:55,
                 from drivers/net/ethernet/mellanox/mlx5/core/en_stats.c:34:
drivers/net/ethernet/mellanox/mlx5/core/en_stats.h:57:46: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
   57 | #define MLX5E_DECLARE_PTP_RQ_STAT(type, fld) "ptp_rq%d_"#fld, offsetof(type, fld)
      |                                              ^~~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_stats.c:2279:11: note: in expansion of macro 'MLX5E_DECLARE_PTP_RQ_STAT'
 2279 |         { MLX5E_DECLARE_PTP_RQ_STAT(struct mlx5e_rq_stats, csum_complete_tail_slow) },
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~

This stat string is being used in ethtool_sprintf(), so it must be a
valid NUL-terminated string. Currently the string lacks the final NUL
byte (as GCC warns), but by absolute luck, the next byte in memory is a
space (decimal 32) followed by a NUL. "format" is immediately followed
by little-endian size_t:

struct counter_desc {
        char                       format[32];           /*     0    32 */
        size_t                     offset;               /*    32     8 */
};

The "offset" member is populated by the stats member offset:

 #define MLX5E_DECLARE_PTP_RQ_STAT(type, fld) "ptp_rq%d_"#fld, offsetof(type, fld)

which for this struct mlx5e_rq_stats member, csum_complete_tail_slow, is
32, or space, and then the rest of the "offset" bytes are NULs.

struct mlx5e_rq_stats {
...
        u64                        csum_complete_tail_slow; /* 32     8 */

The use of vsnprintf(), within ethtool_sprintf(), reads past the end of
"format" and sees the format string as "ptp_rq%d_csum_complete_tail_slow ",
with %d getting resolved by MLX5E_PTP_CHANNEL_IX (value 0):

                       ethtool_sprintf(data, ptp_rq_stats_desc[i].format,
                                       MLX5E_PTP_CHANNEL_IX);

With an output result of "ptp_rq0_csum_complete_tail_slow", which gets
precisely truncated to 31 characters with a trailing NUL.

So, instead of accidentally getting this correct due to the NUL bytes
at the end of the size_t that happens to follow the format string, just
make the string initializer 1 byte shorter by replacing "%d" with "0",
since MLX5E_PTP_CHANNEL_IX is already hard-coded. This results in no
initializer truncation and no need to call sprintf().

Signed-off-by: Kees Cook <kees@kernel.org>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
Link: https://patch.msgid.link/20250416020109.work.297-kees@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/mellanox/mlx5/core/en_stats.c
drivers/net/ethernet/mellanox/mlx5/core/en_stats.h