Commit 5959f829 authored by Stefano Brivio's avatar Stefano Brivio Committed by Linus Torvalds

lib/bitmap.c: fix bitmap_cut() for partial overlapping case

Patch series "lib: Fix bitmap_cut() for overlaps, add test"

This patch (of 2):

Yury Norov reports that bitmap_cut() will not produce the right outcome if
src and dst partially overlap, with src pointing at some location after
dst, because the memmove() affects src before we store the bits that we
need to keep, that is, the bits preceding the cut -- as long as we the
beginning of the cut is not aligned to a long.

Fix this by storing those bits before the memmove().

Note that this is just a theoretical concern so far, as the only user of
this function, pipapo_drop() from the nftables set back-end implemented in
net/netfilter/nft_set_pipapo.c, always supplies entirely overlapping src
and dst.

Fixes: 20927671 ("bitmap: Introduce bitmap_cut(): cut bits and shift remaining")
Reported-by: default avatarYury Norov <yury.norov@gmail.com>
Signed-off-by: default avatarStefano Brivio <sbrivio@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Reviewed-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Link: http://lkml.kernel.org/r/cover.1592155364.git.sbrivio@redhat.com
Link: http://lkml.kernel.org/r/003e38d4428cd6091ef00b5b03354f1bd7d9091e.1592155364.git.sbrivio@redhat.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 25fd529c
...@@ -212,13 +212,13 @@ void bitmap_cut(unsigned long *dst, const unsigned long *src, ...@@ -212,13 +212,13 @@ void bitmap_cut(unsigned long *dst, const unsigned long *src,
unsigned long keep = 0, carry; unsigned long keep = 0, carry;
int i; int i;
memmove(dst, src, len * sizeof(*dst));
if (first % BITS_PER_LONG) { if (first % BITS_PER_LONG) {
keep = src[first / BITS_PER_LONG] & keep = src[first / BITS_PER_LONG] &
(~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG)); (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
} }
memmove(dst, src, len * sizeof(*dst));
while (cut--) { while (cut--) {
for (i = first / BITS_PER_LONG; i < len; i++) { for (i = first / BITS_PER_LONG; i < len; i++) {
if (i < len - 1) if (i < len - 1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment