When duplicating a process' virtual memory mappings also duplicate all
of its registered .eh_frame_hdr sections stored in the per-mm maple tree
to enable stacktracing using eh_frame of the child process.

Signed-off-by: Jens Remus <[email protected]>
---
 include/linux/eh_frame.h       |  7 +++++
 kernel/unwind/eh_frame.c       | 48 ++++++++++++++++++++++++++++++++++
 kernel/unwind/eh_frame_debug.h |  7 +++++
 mm/mmap.c                      |  5 ++++
 4 files changed, 67 insertions(+)

diff --git a/include/linux/eh_frame.h b/include/linux/eh_frame.h
index b2f98cd6166f..65f87c2714d8 100644
--- a/include/linux/eh_frame.h
+++ b/include/linux/eh_frame.h
@@ -30,6 +30,7 @@ struct eh_frame_section {
 };
 
 #define INIT_MM_EH_FRAME .eh_frame_mt = MTREE_INIT(eh_frame_mt, 0),
+extern int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm);
 extern void eh_frame_free_mm(struct mm_struct *mm);
 
 extern int eh_frame_add_section(unsigned long eh_frame_hdr_start,
@@ -49,6 +50,12 @@ static inline bool current_has_eh_frame(void)
 #else /* !CONFIG_HAVE_UNWIND_USER_EH_FRAME */
 
 #define INIT_MM_EH_FRAME
+
+static inline int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct 
*oldmm)
+{
+       return 0;
+}
+
 static inline void eh_frame_free_mm(struct mm_struct *mm) {}
 
 static inline int eh_frame_add_section(unsigned long eh_frame_hdr_start,
diff --git a/kernel/unwind/eh_frame.c b/kernel/unwind/eh_frame.c
index c9161229196c..7f572d1711d3 100644
--- a/kernel/unwind/eh_frame.c
+++ b/kernel/unwind/eh_frame.c
@@ -1450,6 +1450,54 @@ int eh_frame_remove_section(unsigned long 
eh_frame_hdr_start)
        return 0;
 }
 
+static void __eh_frame_dup_section(struct eh_frame_section *sec,
+                                  struct eh_frame_section *oldsec)
+{
+       sec->eh_frame_hdr_start = oldsec->eh_frame_hdr_start;
+       sec->eh_frame_hdr_end   = oldsec->eh_frame_hdr_end;
+       sec->text_start         = oldsec->text_start;
+       sec->text_end           = oldsec->text_end;
+
+       sec->eh_frame_start             = oldsec->eh_frame_start;
+       sec->eh_frame_vma_end           = oldsec->eh_frame_vma_end;
+       sec->binary_search_table_start  = oldsec->binary_search_table_start;
+       sec->binary_search_table_end    = oldsec->binary_search_table_end;
+       sec->fde_count                  = oldsec->fde_count;
+       sec->binary_search_table_enc    = oldsec->binary_search_table_enc;
+
+       dbg_dup(sec, oldsec);
+}
+
+int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+       struct eh_frame_section *sec, *oldsec;
+       unsigned long index = 0;
+       int ret;
+
+       guard(srcu)(&eh_frame_srcu);
+
+       mt_for_each(&oldmm->eh_frame_mt, oldsec, index, ULONG_MAX) {
+               sec = kzalloc(sizeof(*sec), GFP_KERNEL_ACCOUNT);
+               if (!sec)
+                       return -ENOMEM;
+
+               __eh_frame_dup_section(sec, oldsec);
+
+               ret = mtree_insert_range(&mm->eh_frame_mt,
+                                        sec->text_start,
+                                        sec->text_end - 1,
+                                        sec, GFP_KERNEL_ACCOUNT);
+               if (ret)
+                       goto err_free;
+       }
+
+       return 0;
+
+err_free:
+       free_section(sec);
+       return ret;
+}
+
 void eh_frame_free_mm(struct mm_struct *mm)
 {
        struct eh_frame_section *sec;
diff --git a/kernel/unwind/eh_frame_debug.h b/kernel/unwind/eh_frame_debug.h
index bcb2d03ab9ab..e72e011ba539 100644
--- a/kernel/unwind/eh_frame_debug.h
+++ b/kernel/unwind/eh_frame_debug.h
@@ -41,6 +41,12 @@ static inline void dbg_init(struct eh_frame_section *sec)
                sec->filename = kstrdup("(vma unknown???)", GFP_KERNEL_ACCOUNT);
 }
 
+static inline void dbg_dup(struct eh_frame_section *sec, struct 
eh_frame_section *oldsec)
+{
+       if (oldsec->filename)
+               sec->filename = kstrdup(oldsec->filename, GFP_KERNEL_ACCOUNT);
+}
+
 static inline void dbg_free(struct eh_frame_section *sec)
 {
        kfree(sec->filename);
@@ -53,6 +59,7 @@ static inline void dbg_free(struct eh_frame_section *sec)
 #define dbg_sec_ehfh(args...)          no_printk(args)
 
 static inline void dbg_init(struct eh_frame_section *sec) {}
+static inline void dbg_dup(struct eh_frame_section *sec, struct 
eh_frame_section *oldsec) {}
 static inline void dbg_free(struct eh_frame_section *sec) {}
 
 #endif /* !CONFIG_DYNAMIC_DEBUG */
diff --git a/mm/mmap.c b/mm/mmap.c
index 2311ae7c2ff4..7715a799810e 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -48,6 +48,7 @@
 #include <linux/sched/mm.h>
 #include <linux/ksm.h>
 #include <linux/memfd.h>
+#include <linux/eh_frame.h>
 
 #include <linux/uaccess.h>
 #include <asm/cacheflush.h>
@@ -1844,6 +1845,9 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, 
struct mm_struct *oldmm)
                        goto loop_out;
                }
        }
+       retval = eh_frame_dup_mm(mm, oldmm);
+       if (retval)
+               goto loop_out;
        /* a new mm has just been created */
        retval = arch_dup_mmap(oldmm, mm);
 loop_out:
@@ -1893,6 +1897,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, 
struct mm_struct *oldmm)
                        vm_unacct_memory(charge);
                }
                __mt_destroy(&mm->mm_mt);
+               eh_frame_free_mm(mm);
                /*
                 * The mm_struct is going to exit, but the locks will be dropped
                 * first.  Set the mm_struct as unstable is advisable as it is
-- 
2.53.0


Reply via email to