Bug 254661 - Debug registers seem to be inherited in forked processes
Summary: Debug registers seem to be inherited in forked processes
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Mitchell Horne
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-30 15:31 UTC by Michał Górny
Modified: 2021-05-21 00:21 UTC (History)
2 users (show)

See Also:


Attachments
fbsd-fork-dbreg.c (2.49 KB, text/plain)
2021-03-30 15:31 UTC, Michał Górny
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Michał Górny 2021-03-30 15:31:03 UTC
Created attachment 223712 [details]
fbsd-fork-dbreg.c

It seems that when a process forks, the new child process inherits debug registers from the parent.  Not sure if this is desirable but it's certainly different than e.g. Linux and NetBSD do (both clear dbregs in the new process).

This currently breaks e.g. GDB that doesn't account for this.  If the process has any watchpoints set before forking, the watchpoints leak to child process when it's being detached, and it crashes with SIGTRAP afterwards.

I'm attaching yet another variant of my cheap reproducer script™.  It sets dbreg on the parent process before fork, and then reads dbreg from the child after fork.

The results I get are:

dr0 = 0x204690   // set in parent process
dr7 = 00000013
dr0 = 0x204690   // read in child process
dr7 = 00000413
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2021-03-30 16:00:53 UTC
See https://reviews.freebsd.org/D29496
In particular, please note the question in the follow-up, about behavior for
the new thread.
Comment 2 commit-hook freebsd_committer freebsd_triage 2021-03-30 23:28:01 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8223717ce62c1ad0becc34ce69fe2d1771f3ba05

commit 8223717ce62c1ad0becc34ce69fe2d1771f3ba05
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-03-30 15:40:02 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-03-30 23:07:35 +0000

    x86: clear %db registers in new process

    Reported by:     MichaŠGórny <mgorny@gentoo.org>
    PR:     254661
    Reviewed by:    emaste, jhb
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential revision:  https://reviews.freebsd.org/D29496

 sys/amd64/amd64/vm_machdep.c | 8 ++++++++
 sys/i386/i386/vm_machdep.c   | 8 ++++++++
 2 files changed, 16 insertions(+)
Comment 3 Michał Górny 2021-03-30 23:41:54 UTC
I haven't tested but could you double-check whether the same problem doesn't affect arm64?
Comment 4 commit-hook freebsd_committer freebsd_triage 2021-04-06 00:49:10 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5524122ee3b78b3a9bba1d0a0d9b8ac080a8e6d8

commit 5524122ee3b78b3a9bba1d0a0d9b8ac080a8e6d8
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-03-30 15:40:02 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-04-06 00:47:34 +0000

    x86: clear %db registers in new process

    PR:     254661

    (cherry picked from commit 8223717ce62c1ad0becc34ce69fe2d1771f3ba05)

 sys/amd64/amd64/vm_machdep.c | 8 ++++++++
 sys/i386/i386/vm_machdep.c   | 8 ++++++++
 2 files changed, 16 insertions(+)
Comment 5 commit-hook freebsd_committer freebsd_triage 2021-04-08 12:44:33 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=1fd001db9c330f133708f3c04c8852f8b07cfed9

commit 1fd001db9c330f133708f3c04c8852f8b07cfed9
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2021-04-07 19:23:46 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2021-04-08 12:41:41 +0000

    arm64: clear debug register state on fork

    Following the analogous change for amd64 and i386 in 8223717ce62c,
    ensure that new processes start with these registers inactive.

    PR:             254661
    Reported by:    MichaŠGórny
    Reviewed by:    kib, emaste
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D29630

 sys/arm64/arm64/vm_machdep.c | 3 +++
 1 file changed, 3 insertions(+)
Comment 6 commit-hook freebsd_committer freebsd_triage 2021-04-16 13:53:08 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=17b0a38d96feb77d4b019ab9108b2c5050273823

commit 17b0a38d96feb77d4b019ab9108b2c5050273823
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2021-04-07 19:23:46 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2021-04-16 13:47:32 +0000

    arm64: clear debug register state on fork

    Following the analogous change for amd64 and i386 in 8223717ce62c,
    ensure that new processes start with these registers inactive.

    PR:             254661
    Reported by:    MichaŠGórny
    Reviewed by:    kib, emaste
    Sponsored by:   The FreeBSD Foundation

    (cherry picked from commit 1fd001db9c330f133708f3c04c8852f8b07cfed9)

 sys/arm64/arm64/vm_machdep.c | 3 +++
 1 file changed, 3 insertions(+)
Comment 7 Mark Linimon freebsd_committer freebsd_triage 2021-05-21 00:21:13 UTC
^Triage: assign to committer that resolved.