Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/kern ksyms(4): Fix race in ksymsread iteration.



details:   https://anonhg.NetBSD.org/src/rev/9f88787504c7
branches:  trunk
changeset: 983713:9f88787504c7
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Thu Jun 03 01:00:15 2021 +0000

description:
ksyms(4): Fix race in ksymsread iteration.

TAILQ_NEXT(ksyms_last_snapshot) might change while we are iterating,
but ksyms_last_snapshot itself cannot, so invert the loop structure.

Discussed with rin@.

diffstat:

 sys/kern/kern_ksyms.c |  12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diffs (54 lines):

diff -r 5fda3c2b0dc6 -r 9f88787504c7 sys/kern/kern_ksyms.c
--- a/sys/kern/kern_ksyms.c     Wed Jun 02 22:32:29 2021 +0000
+++ b/sys/kern/kern_ksyms.c     Thu Jun 03 01:00:15 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $      */
+/*     $NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $        */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.94 2021/06/02 15:43:33 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.95 2021/06/03 01:00:15 riastradh Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -1087,7 +1087,7 @@
         */
        filepos = sizeof(struct ksyms_hdr);
        for (st = TAILQ_FIRST(&ksyms_symtabs);
-            st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
+            ;
             st = TAILQ_NEXT(st, sd_queue)) {
                if (__predict_false(st->sd_gone))
                        continue;
@@ -1101,6 +1101,8 @@
                                return error;
                }
                filepos += st->sd_symsize;
+               if (st == ksyms_last_snapshot)
+                       break;
        }
 
        /*
@@ -1109,7 +1111,7 @@
        KASSERT(filepos <= sizeof(struct ksyms_hdr) +
            ksyms_hdr.kh_shdr[SYMTAB].sh_size);
        for (st = TAILQ_FIRST(&ksyms_symtabs);
-            st != TAILQ_NEXT(ksyms_last_snapshot, sd_queue);
+            ;
             st = TAILQ_NEXT(st, sd_queue)) {
                if (__predict_false(st->sd_gone))
                        continue;
@@ -1123,6 +1125,8 @@
                                return error;
                }
                filepos += st->sd_strsize;
+               if (st == ksyms_last_snapshot)
+                       break;
        }
 
        /*



Home | Main Index | Thread Index | Old Index