public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] idr: Prevent unintended underflow for the idr index
@ 2019-09-17 21:48 Jordan Crouse
  2019-09-18 11:50 ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Jordan Crouse @ 2019-09-17 21:48 UTC (permalink / raw)
  To: freedreno; +Cc: linux-arm-msm, linux-fsdevel, Matthew Wilcox, linux-kernel

It is possible for unaware callers of several idr functions to accidentally
underflow the index by specifying a id that is less than the idr base.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
---

 lib/idr.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/idr.c b/lib/idr.c
index 66a3748..d9e180c 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -151,6 +151,9 @@ EXPORT_SYMBOL(idr_alloc_cyclic);
  */
 void *idr_remove(struct idr *idr, unsigned long id)
 {
+	if (id < idr->idr_base)
+		return NULL;
+
 	return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL);
 }
 EXPORT_SYMBOL_GPL(idr_remove);
@@ -171,6 +174,9 @@ EXPORT_SYMBOL_GPL(idr_remove);
  */
 void *idr_find(const struct idr *idr, unsigned long id)
 {
+	if (id < idr->idr_base)
+		return NULL;
+
 	return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base);
 }
 EXPORT_SYMBOL_GPL(idr_find);
@@ -302,6 +308,9 @@ void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
 	void __rcu **slot = NULL;
 	void *entry;
 
+	if (id < idr->idr_base)
+		return ERR_PTR(-ENOENT);
+
 	id -= idr->idr_base;
 
 	entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-09-18 15:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-17 21:48 [PATCH] idr: Prevent unintended underflow for the idr index Jordan Crouse
2019-09-18 11:50 ` Matthew Wilcox
2019-09-18 15:32   ` Jordan Crouse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox