Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp1112969pxx; Tue, 27 Oct 2020 08:32:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgY7DWgYhAEQQDvynvo0VFyMnXEOUmL0kC53zMLaZFmj3WnuGjL+giFH0TV15HWXq57wOa X-Received: by 2002:a17:906:3641:: with SMTP id r1mr2849942ejb.405.1603812724370; Tue, 27 Oct 2020 08:32:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603812724; cv=none; d=google.com; s=arc-20160816; b=Ru3Y5ubyaNwYjh+N8oGAOOOoWRonNiI0dYKJXewdUoU6RMr5KEpFp9sAl4pc1Nb9lC gsD0Q3Jf+jEJkNhjE8OE4NLFd07fcizQAtxFFtGLluISVrt6bJDw9f/xZ4535U4Z3Xqw zxuVJLkcZRfsufV7d3FBgALEpPaRpYytX2wc9CkLhAswejT8GBH1Xs/dhNz9JXqsKsUr ZLVcAnXqCjW1Q8/lmIW8upJbGIO2PPcEX/qU+0QEVxswQOpBYsLaULeDaKYbq07JZmeq mY6FhxWlYAHFKwUPxT47oG2cyuefXfv+FRYStYDFYKlAIH9fkfvbSBqvc70WQpOwF+QN 070g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dkaeVDRpFf7aMKS2LrhmqNpKVwugJLJmNGemohspdGE=; b=SVsvZdSUussnsV3BGCbtYSeum9bEXj65+QzHe6GezeNn3uYVz4jq4ehdNlsYFeOJdv M5/Wwh/SGIURlN6t7EEG9QlqETrs+w9n3uJEct+5P05qK3Gtdewa0LX+5P35Vr2veo07 NahPRojVobUi1+s5ZeB6tUM20m6yCmUJOFC52Y+JrEYts4hIhmSsCqsFSxWs52Nf5AWa 8sM+VnyFMh8uW0qfG48Wuz8IsLI/txRqevrhq/IbckMKZSb4pmsTN2AsYar+OE1L+Ndg G7kElsVzRRIvluSntAinh8trYcTudbkWjhpLy5z0otWRCjGEOmCneBJJf4wZYIGpU6DE 27gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1wT5iViz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn20si1634554ejc.324.2020.10.27.08.31.40; Tue, 27 Oct 2020 08:32:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1wT5iViz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761167AbgJ0OiP (ORCPT + 99 others); Tue, 27 Oct 2020 10:38:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:37282 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761140AbgJ0OiI (ORCPT ); Tue, 27 Oct 2020 10:38:08 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 444B6206B2; Tue, 27 Oct 2020 14:38:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809487; bh=pgU48Lf+PiMP7v6usSDcWLCT0cPhAWdBgvnWQMGxPRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1wT5iVizgcbMV94Y5plw3SzGkMJivyICVn00hMiVja5zCJn56P9meRuvqHzFEtywM nyahEPAY7DROvrWWkJ455Whq+C8SYBatzkwJVoZ1QWFiLzoXRe9m/7I2BAz6RO3aWw SduZzR4LxcGSe6LVQqqjDaPOB1+QuZ1rhxHp95zk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, coverity-bot , Kees Cook , "Matthew Wilcox (Oracle)" , Sasha Levin Subject: [PATCH 5.4 212/408] ida: Free allocated bitmap in error path Date: Tue, 27 Oct 2020 14:52:30 +0100 Message-Id: <20201027135504.933326356@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135455.027547757@linuxfoundation.org> References: <20201027135455.027547757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox (Oracle) [ Upstream commit a219b856a2b993da234108307be772448f22b0ce ] If a bitmap needs to be allocated, and then by the time the thread is scheduled to be run again all the indices which would satisfy the allocation have been allocated then we would leak the allocation. Almost impossible to hit in practice, but a trivial fix. Found by Coverity. Fixes: f32f004cddf8 ("ida: Convert to XArray") Reported-by: coverity-bot Reviewed-by: Kees Cook Signed-off-by: Matthew Wilcox (Oracle) Signed-off-by: Sasha Levin --- lib/idr.c | 1 + tools/testing/radix-tree/idr-test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/idr.c b/lib/idr.c index c2cf2c52bbde5..4d2eef0259d2c 100644 --- a/lib/idr.c +++ b/lib/idr.c @@ -470,6 +470,7 @@ int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max, goto retry; nospc: xas_unlock_irqrestore(&xas, flags); + kfree(alloc); return -ENOSPC; } EXPORT_SYMBOL(ida_alloc_range); diff --git a/tools/testing/radix-tree/idr-test.c b/tools/testing/radix-tree/idr-test.c index 8995092d541ec..3b796dd5e5772 100644 --- a/tools/testing/radix-tree/idr-test.c +++ b/tools/testing/radix-tree/idr-test.c @@ -523,8 +523,27 @@ static void *ida_random_fn(void *arg) return NULL; } +static void *ida_leak_fn(void *arg) +{ + struct ida *ida = arg; + time_t s = time(NULL); + int i, ret; + + rcu_register_thread(); + + do for (i = 0; i < 1000; i++) { + ret = ida_alloc_range(ida, 128, 128, GFP_KERNEL); + if (ret >= 0) + ida_free(ida, 128); + } while (time(NULL) < s + 2); + + rcu_unregister_thread(); + return NULL; +} + void ida_thread_tests(void) { + DEFINE_IDA(ida); pthread_t threads[20]; int i; @@ -536,6 +555,16 @@ void ida_thread_tests(void) while (i--) pthread_join(threads[i], NULL); + + for (i = 0; i < ARRAY_SIZE(threads); i++) + if (pthread_create(&threads[i], NULL, ida_leak_fn, &ida)) { + perror("creating ida thread"); + exit(1); + } + + while (i--) + pthread_join(threads[i], NULL); + assert(ida_is_empty(&ida)); } void ida_tests(void) -- 2.25.1