* [PATCH net-next 01/14] lib: cpu_rmap: Avoid use after free on rmap->obj array entries
[not found] <20230220061442.403092-1-saeed@kernel.org>
@ 2023-02-20 6:14 ` Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 02/14] lib: cpu_rmap: Use allocator for rmap entries Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 03/14] lib: cpu_rmap: Add irq_cpu_rmap_remove to complement irq_cpu_rmap_add Saeed Mahameed
2 siblings, 0 replies; 3+ messages in thread
From: Saeed Mahameed @ 2023-02-20 6:14 UTC (permalink / raw)
To: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
Cc: Saeed Mahameed, netdev, Tariq Toukan, Eli Cohen, Ben Hutchings,
Andrew Morton, David Decotigny, linux-kernel
From: Eli Cohen <elic@nvidia.com>
When calling irq_set_affinity_notifier() with NULL at the notify
argument, it will cause freeing of the glue pointer in the
corresponding array entry but will leave the pointer in the array. A
subsequent call to free_irq_cpu_rmap() will try to free this entry again
leading to possible use after free.
Fix that by setting NULL to the array entry and checking that we have
non-zero at the array entry when iterating over the array in
free_irq_cpu_rmap().
Fixes: c39649c331c7 ("lib: cpu_rmap: CPU affinity reverse-mapping")
CC: Ben Hutchings <bhutchings@solarflare.com>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: David Decotigny <decot@googlers.com>
CC: Eric Dumazet <edumazet@google.com>
CC: linux-kernel@vger.kernel.org
Signed-off-by: Eli Cohen <elic@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
lib/cpu_rmap.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
index f08d9c56f712..e77f12bb3c77 100644
--- a/lib/cpu_rmap.c
+++ b/lib/cpu_rmap.c
@@ -232,7 +232,8 @@ void free_irq_cpu_rmap(struct cpu_rmap *rmap)
for (index = 0; index < rmap->used; index++) {
glue = rmap->obj[index];
- irq_set_affinity_notifier(glue->notify.irq, NULL);
+ if (glue)
+ irq_set_affinity_notifier(glue->notify.irq, NULL);
}
cpu_rmap_put(rmap);
@@ -268,6 +269,7 @@ static void irq_cpu_rmap_release(struct kref *ref)
container_of(ref, struct irq_glue, notify.kref);
cpu_rmap_put(glue->rmap);
+ glue->rmap->obj[glue->index] = NULL;
kfree(glue);
}
@@ -297,6 +299,7 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
rc = irq_set_affinity_notifier(irq, &glue->notify);
if (rc) {
cpu_rmap_put(glue->rmap);
+ rmap->obj[glue->index] = NULL;
kfree(glue);
}
return rc;
--
2.39.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH net-next 02/14] lib: cpu_rmap: Use allocator for rmap entries
[not found] <20230220061442.403092-1-saeed@kernel.org>
2023-02-20 6:14 ` [PATCH net-next 01/14] lib: cpu_rmap: Avoid use after free on rmap->obj array entries Saeed Mahameed
@ 2023-02-20 6:14 ` Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 03/14] lib: cpu_rmap: Add irq_cpu_rmap_remove to complement irq_cpu_rmap_add Saeed Mahameed
2 siblings, 0 replies; 3+ messages in thread
From: Saeed Mahameed @ 2023-02-20 6:14 UTC (permalink / raw)
To: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
Cc: Saeed Mahameed, netdev, Tariq Toukan, Eli Cohen, Ben Hutchings,
Andrew Morton, David Decotigny, linux-kernel
From: Eli Cohen <elic@nvidia.com>
Use a proper allocator for rmap entries using a naive for loop. The
allocator relies on whether an entry is NULL to be considered free.
Remove the used field of rmap which is not needed.
Also, avoid crashing the kernel if an entry is not available.
CC: Ben Hutchings <bhutchings@solarflare.com>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: David Decotigny <decot@googlers.com>
CC: Eric Dumazet <edumazet@google.com>
CC: linux-kernel@vger.kernel.org
Signed-off-by: Eli Cohen <elic@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
include/linux/cpu_rmap.h | 3 +--
lib/cpu_rmap.c | 23 +++++++++++++++++++----
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/include/linux/cpu_rmap.h b/include/linux/cpu_rmap.h
index be8aea04d023..0ec745e6cd36 100644
--- a/include/linux/cpu_rmap.h
+++ b/include/linux/cpu_rmap.h
@@ -16,14 +16,13 @@
* struct cpu_rmap - CPU affinity reverse-map
* @refcount: kref for object
* @size: Number of objects to be reverse-mapped
- * @used: Number of objects added
* @obj: Pointer to array of object pointers
* @near: For each CPU, the index and distance to the nearest object,
* based on affinity masks
*/
struct cpu_rmap {
struct kref refcount;
- u16 size, used;
+ u16 size;
void **obj;
struct {
u16 index;
diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
index e77f12bb3c77..e95d018e01c2 100644
--- a/lib/cpu_rmap.c
+++ b/lib/cpu_rmap.c
@@ -128,6 +128,17 @@ debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
}
#endif
+static int get_free_index(struct cpu_rmap *rmap)
+{
+ int i;
+
+ for (i = 0; i < rmap->size; i++)
+ if (!rmap->obj[i])
+ return i;
+
+ return -1;
+}
+
/**
* cpu_rmap_add - add object to a rmap
* @rmap: CPU rmap allocated with alloc_cpu_rmap()
@@ -137,10 +148,11 @@ debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
*/
int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
{
- u16 index;
+ u16 index = get_free_index(rmap);
+
+ if (index == -1)
+ return index;
- BUG_ON(rmap->used >= rmap->size);
- index = rmap->used++;
rmap->obj[index] = obj;
return index;
}
@@ -230,7 +242,7 @@ void free_irq_cpu_rmap(struct cpu_rmap *rmap)
if (!rmap)
return;
- for (index = 0; index < rmap->used; index++) {
+ for (index = 0; index < rmap->size; index++) {
glue = rmap->obj[index];
if (glue)
irq_set_affinity_notifier(glue->notify.irq, NULL);
@@ -296,6 +308,9 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
glue->rmap = rmap;
cpu_rmap_get(rmap);
glue->index = cpu_rmap_add(rmap, glue);
+ if (glue->index == -1)
+ return -ENOSPC;
+
rc = irq_set_affinity_notifier(irq, &glue->notify);
if (rc) {
cpu_rmap_put(glue->rmap);
--
2.39.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH net-next 03/14] lib: cpu_rmap: Add irq_cpu_rmap_remove to complement irq_cpu_rmap_add
[not found] <20230220061442.403092-1-saeed@kernel.org>
2023-02-20 6:14 ` [PATCH net-next 01/14] lib: cpu_rmap: Avoid use after free on rmap->obj array entries Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 02/14] lib: cpu_rmap: Use allocator for rmap entries Saeed Mahameed
@ 2023-02-20 6:14 ` Saeed Mahameed
2 siblings, 0 replies; 3+ messages in thread
From: Saeed Mahameed @ 2023-02-20 6:14 UTC (permalink / raw)
To: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
Cc: Saeed Mahameed, netdev, Tariq Toukan, Eli Cohen, Ben Hutchings,
Andrew Morton, David Decotigny, linux-kernel
From: Eli Cohen <elic@nvidia.com>
Add a function to complement irq_cpu_rmap_add(). It removes the irq from
the reverse mapping by setting the notifier to NULL.
CC: Ben Hutchings <bhutchings@solarflare.com>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: David Decotigny <decot@googlers.com>
CC: Eric Dumazet <edumazet@google.com>
CC: linux-kernel@vger.kernel.org
Signed-off-by: Eli Cohen <elic@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
include/linux/cpu_rmap.h | 2 ++
lib/cpu_rmap.c | 11 +++++++++++
2 files changed, 13 insertions(+)
diff --git a/include/linux/cpu_rmap.h b/include/linux/cpu_rmap.h
index 0ec745e6cd36..58284f1f3a58 100644
--- a/include/linux/cpu_rmap.h
+++ b/include/linux/cpu_rmap.h
@@ -60,6 +60,8 @@ static inline struct cpu_rmap *alloc_irq_cpu_rmap(unsigned int size)
}
extern void free_irq_cpu_rmap(struct cpu_rmap *rmap);
+extern int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq);
extern int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq);
+
#endif /* __LINUX_CPU_RMAP_H */
diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
index e95d018e01c2..7c67e9382845 100644
--- a/lib/cpu_rmap.c
+++ b/lib/cpu_rmap.c
@@ -285,6 +285,17 @@ static void irq_cpu_rmap_release(struct kref *ref)
kfree(glue);
}
+/**
+ * irq_cpu_rmap_remove - remove an IRQ from a CPU affinity reverse-map
+ * @rmap: The reverse-map
+ * @irq: The IRQ number
+ */
+int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq)
+{
+ return irq_set_affinity_notifier(irq, NULL);
+}
+EXPORT_SYMBOL(irq_cpu_rmap_remove);
+
/**
* irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
* @rmap: The reverse-map
--
2.39.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-02-20 6:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20230220061442.403092-1-saeed@kernel.org>
2023-02-20 6:14 ` [PATCH net-next 01/14] lib: cpu_rmap: Avoid use after free on rmap->obj array entries Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 02/14] lib: cpu_rmap: Use allocator for rmap entries Saeed Mahameed
2023-02-20 6:14 ` [PATCH net-next 03/14] lib: cpu_rmap: Add irq_cpu_rmap_remove to complement irq_cpu_rmap_add Saeed Mahameed
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox