ethtool: tsconfig: reject zero-valued tx_type and rx_filter bitsets

The ffs()/fls() guard in ethnl_set_tsconfig() was meant to enforce
that the user selects exactly one tx_type (and one rx_filter)
at a time (off / none are explicit types with non-zero values).
However, both ffs(0) and fls(0) return 0, so the guard passes
a zero-valued bitset through.

The subsequent ffs(req_tx_type) - 1 would produce -1, if user selected
no bit. net_hwtstamp_validate() catches the invalid -1 downstream,
but returns a generic error (-ERANGE) without telling the user
what went wrong. Return -EINVAL + extack instead.

Replace the ffs()/fls() comparison with a hweight32() == 1 check.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Reviewed-by: Joe Damato <joe@dama.to>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Link: https://patch.msgid.link/20260812162230.1837788-1-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2026-08-12 09:22:30 -07:00
parent a7c44619c6
commit 77e80af7d2

View File

@@ -359,8 +359,10 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
if (ret < 0)
goto err_free_hwprov;
/* Select only one tx type at a time */
if (ffs(req_tx_type) != fls(req_tx_type)) {
/* Select exactly one tx type at a time */
if (hweight32(req_tx_type) != 1) {
NL_SET_BAD_ATTR(info->extack,
tb[ETHTOOL_A_TSCONFIG_TX_TYPES]);
ret = -EINVAL;
goto err_free_hwprov;
}
@@ -380,8 +382,10 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
if (ret < 0)
goto err_free_hwprov;
/* Select only one rx filter at a time */
if (ffs(req_rx_filter) != fls(req_rx_filter)) {
/* Select exactly one rx filter at a time */
if (hweight32(req_rx_filter) != 1) {
NL_SET_BAD_ATTR(info->extack,
tb[ETHTOOL_A_TSCONFIG_RX_FILTERS]);
ret = -EINVAL;
goto err_free_hwprov;
}