netfilter: shift-out-of-bounds in nft_bitwise
Subsystems (2)
PATCH 1/1 netfilter: shift-out-of-bounds in nft_bitwise
In reply to a message not in the archiveFrom: Kai Ma <[email protected]>
Handle zero shift operands explicitly in nft_bitwise_eval_lshift() and nft_bitwise_eval_rshift().
Shift expressions accept values in the range [0, 31], but the carry propagation code assumes a non-zero shift and computes the carry from the adjacent 32-bit word unconditionally. For a zero shift operand, the expected result is to leave the value unchanged.
Treat zero shift as a no-op before entering the carry propagation loops. This preserves the existing behaviour for non-zero shifts and matches the expected semantics of shifting by zero.
--- net/netfilter/nft_bitwise.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
net/netfilter/nft_bitwise.c +16 -4
@@ -39,10 +39,16 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src, const struct nft_bitwise *priv) {u32 shift = priv->data.data[0 ]; -unsigned int i; +unsigned int i, n =DIV_ROUND_UP (priv->len,sizeof (u32));u32 carry =0 ; -for (i =DIV_ROUND_UP (priv->len,sizeof (u32)); i >0 ; i--) { +if (!shift) { +for (i =0 ; i < n; i++) + dst[i] = src[i]; +return ; + } + +for (i = n; i >0 ; i--) { dst[i -1 ] = (src[i -1 ] << shift) | carry; carry = src[i -1 ] >> (BITS_PER_TYPE (u32) - shift); }
@@ -52,10 +58,16 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src, const struct nft_bitwise *priv) {u32 shift = priv->data.data[0 ]; -unsigned int i; +unsigned int i, n =DIV_ROUND_UP (priv->len,sizeof (u32));u32 carry =0 ; -for (i =0 ; i <DIV_ROUND_UP (priv->len,sizeof (u32)); i++) { +if (!shift) { +for (i =0 ; i < n; i++) + dst[i] = src[i]; +return ; + } + +for (i =0 ; i < n; i++) { dst[i] = carry | (src[i] >> shift); carry = src[i] << (BITS_PER_TYPE (u32) - shift); }
Signature · 1 line
2.43.0
Quoted 5 lines · Ren Wei
From: Kai Ma <[email protected]>
Handle zero shift operands explicitly in
nft_bitwise_eval_lshift()andnft_bitwise_eval_rshift().
Quoted 9 lines
Shift expressions accept values in the range [0, 31], but the carry propagation code assumes a non-zero shift and computes the carry from the adjacent 32-bit word unconditionally. For a zero shift operand, the expected result is to leave the value unchanged.
Treat zero shift as a no-op before entering the carry propagation loops. This preserves the existing behaviour for non-zero shifts and matches the expected semantics of shifting by zero.
I think the issue here is an Undefined Behavior actually, AFAICS when shifting by the size of the type is UB and depending on the architecture used it can lead to shift-out-of-bounds due to carry being equals to .
Shouldn't this be rejected in control plane during validation? As a 0 shift operation is pointless, we can reject it right away.
In addition, please use nf target for v2.
Thanks,
Fernando.
Quoted 54 lines
Fixes 567d746b55bc ("netfilter: bitwise: add support for shifts.")Reported-by Yuan Tan <[email protected]>Reported-by Yifan Wu <[email protected]>Reported-by Juefei Pu <[email protected]>Reported-by Xin Liu <[email protected]>Signed-off-by Kai Ma <[email protected]>Signed-off-by Ren Wei <[email protected]>--- net/netfilter/nft_bitwise.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)net/netfilter/nft_bitwise.c +16
-4@@ -39,10 +39,16 @@ static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src, const struct nft_bitwise *priv) {u32 shift = priv->data.data[0 ]; -unsigned int i; +unsigned int i, n =DIV_ROUND_UP (priv->len,sizeof (u32));u32 carry =0 ; -for (i =DIV_ROUND_UP (priv->len,sizeof (u32)); i >0 ; i--) { +if (!shift) { +for (i =0 ; i < n; i++) + dst[i] = src[i]; +return ; + } + +for (i = n; i >0 ; i--) { dst[i -1 ] = (src[i -1 ] << shift) | carry; carry = src[i -1 ] >> (BITS_PER_TYPE (u32) - shift); }@@ -52,10 +58,16 @@ static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src, const struct nft_bitwise *priv) {u32 shift = priv->data.data[0 ]; -unsigned int i; +unsigned int i, n =DIV_ROUND_UP (priv->len,sizeof (u32));u32 carry =0 ; -for (i =0 ; i <DIV_ROUND_UP (priv->len,sizeof (u32)); i++) { +if (!shift) { +for (i =0 ; i < n; i++) + dst[i] = src[i]; +return ; + } + +for (i =0 ; i < n; i++) { dst[i] = carry | (src[i] >> shift); carry = src[i] << (BITS_PER_TYPE (u32) - shift); }