Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp358728pxx; Wed, 28 Oct 2020 06:39:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOP4LHxaUgz/MCvSgkytX8agTUdKflNUlbppmf1nlQNPN5PuD/yaV07z8UoiiujcHI6kmK X-Received: by 2002:a50:88e5:: with SMTP id d92mr7915153edd.145.1603892380199; Wed, 28 Oct 2020 06:39:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603892380; cv=none; d=google.com; s=arc-20160816; b=wmUp0vZl2X7jsgNEK4SoMY/vTmvz6N+0WmkBWiAehWwoxPGiOn1VnE1z24bGqoKgLB x+l7jUqZ3MeOuY894nH5ZrGidaBjot5FmiHoYz9nPwzXZVNQlfO5V3dAkhdxDxm3XA7z xFltJ6CLftKCe9130wwdyFE2CAVRFYIDZAdAed+4c/Q+jRido5Mvc8IL+zBMLcRhpZ5Z pwJpOEQrPg4hxqv2Th9h6Z47Xr1uf1O2iBHdtKR3bqrz5zdDZWqtcyb45rhXgY+Z44kz dD92yQlc3ZNvjngSSA3J90azwp2owoOCJRLN7rswoSsDrOHOd/9+f2mrM9jPmI3bx6RP +o/A== 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=d6cmuaGFVkkr2leQ3/2Hk/QzSH7JNaIxW0PltStrZzc6neoSP9orXBEMVjyKyGfDKD xzMfYIa5Gfleez+N7XsODHTJ3ODwjeC1BbxXsVLDyjCoTdazQjb5zpExv/MhARX3EKcW BpOsdB7b0LuVFYh5qZ+ntY9iCfUJUcpeo4My9DUzmX2TfGUUCIwqFZF5P6b27+sVAvUK 6BKJFpfelY96IE0vZDucCtEriRx91FKZtWD35XegHPQog/1h3dnS7st54r/N5xlKqjel ifGGKq9zsPdR8o+It0xDC23kRFFxd/YSR5Q17RwjhJC0BZOou5ITSqU6mzuzp6jaEkrd PHQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xsSp5BiS; 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 w11si2719069ejq.533.2020.10.28.06.39.17; Wed, 28 Oct 2020 06:39:40 -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=xsSp5BiS; 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 S1802722AbgJ0PvG (ORCPT + 99 others); Tue, 27 Oct 2020 11:51:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:56608 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1801044AbgJ0Phd (ORCPT ); Tue, 27 Oct 2020 11:37:33 -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 B9C00204EF; Tue, 27 Oct 2020 15:37:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603813052; bh=pgU48Lf+PiMP7v6usSDcWLCT0cPhAWdBgvnWQMGxPRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xsSp5BiSEhBlT4KXtoMKeqafNtYEWA55DuJDyVQhtUKlV4lJzVx7cFluSnTo05XWG nXLcxy7QynvRKzNEM6IwkGnbvViLM+5sszQyn4QHJZipJ5yA5Cr/tbvia4vcU79Tdb 2O3GTmhWJSq8NlwAE1G1OGxkcFNfLHgrbofhYVJA= 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.9 422/757] ida: Free allocated bitmap in error path Date: Tue, 27 Oct 2020 14:51:12 +0100 Message-Id: <20201027135510.359998751@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135450.497324313@linuxfoundation.org> References: <20201027135450.497324313@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