From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42936C433E1 for ; Wed, 12 Aug 2020 01:34:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 08C612076C for ; Wed, 12 Aug 2020 01:34:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="FDXauKdY" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 08C612076C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AA07D8D0041; Tue, 11 Aug 2020 21:34:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A79AB8D0001; Tue, 11 Aug 2020 21:34:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B6128D0041; Tue, 11 Aug 2020 21:34:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0203.hostedemail.com [216.40.44.203]) by kanga.kvack.org (Postfix) with ESMTP id 852C88D0001 for ; Tue, 11 Aug 2020 21:34:31 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4B5EA181AEF10 for ; Wed, 12 Aug 2020 01:34:31 +0000 (UTC) X-FDA: 77140196742.11.rain80_0c0d43326fe7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id EAAB5180F8B86 for ; Wed, 12 Aug 2020 01:34:30 +0000 (UTC) X-HE-Tag: rain80_0c0d43326fe7 X-Filterd-Recvd-Size: 3482 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf49.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Aug 2020 01:34:30 +0000 (UTC) Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 854E2206B2; Wed, 12 Aug 2020 01:34:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597196069; bh=n3akit/kS5Ki7INVqk0MzU8of1pDmKLYQp0MI3SprD8=; h=Date:From:To:Subject:In-Reply-To:From; b=FDXauKdYbndztCxrzvOrb80z4p+jFEzdSW0t5MUGRXl7OuadNV3U+1mJ7BeLJU+jL Goq3RinOPckOc8EH6qQFLtZqDDWM4/ZXDhW/9G0y9yG9y0xS+G3SEqNtqcTwunobPt bJK+oxrldcpmnsQmuDk2jd2p+Llz869G+b7EHoEM= Date: Tue, 11 Aug 2020 18:34:29 -0700 From: Andrew Morton To: akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, linux-mm@kvack.org, linux@rasmusvillemoes.dk, mm-commits@vger.kernel.org, pablo@netfilter.org, sbrivio@redhat.com, torvalds@linux-foundation.org, yury.norov@gmail.com Subject: [patch 080/165] lib/bitmap.c: fix bitmap_cut() for partial overlapping case Message-ID: <20200812013429.C42K7Ccjw%akpm@linux-foundation.org> In-Reply-To: <20200811182949.e12ae9a472e3b5e27e16ad6c@linux-foundation.org> User-Agent: s-nail v14.8.16 X-Rspamd-Queue-Id: EAAB5180F8B86 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Stefano Brivio Subject: 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. Link: http://lkml.kernel.org/r/cover.1592155364.git.sbrivio@redhat.com Link: http://lkml.kernel.org/r/003e38d4428cd6091ef00b5b03354f1bd7d9091e.1592155364.git.sbrivio@redhat.com Fixes: 2092767168f0 ("bitmap: Introduce bitmap_cut(): cut bits and shift remaining") Signed-off-by: Stefano Brivio Reported-by: Yury Norov Reviewed-by: Andy Shevchenko Cc: Rasmus Villemoes Cc: Pablo Neira Ayuso Signed-off-by: Andrew Morton --- lib/bitmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/lib/bitmap.c~bitmap-fix-bitmap_cut-for-partial-overlapping-case +++ a/lib/bitmap.c @@ -212,13 +212,13 @@ void bitmap_cut(unsigned long *dst, cons unsigned long keep = 0, carry; int i; - memmove(dst, src, len * sizeof(*dst)); - if (first % BITS_PER_LONG) { keep = src[first / BITS_PER_LONG] & (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG)); } + memmove(dst, src, len * sizeof(*dst)); + while (cut--) { for (i = first / BITS_PER_LONG; i < len; i++) { if (i < len - 1) _