Bug 275009 - panic: Assertion ti->ti_linestart < ti->ti_end failed
Summary: panic: Assertion ti->ti_linestart < ti->ti_end failed
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 15.0-CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Christos Margiolis
URL:
Keywords: regression
Depends on:
Blocks: 14.0-erratas
  Show dependency treegraph
 
Reported: 2023-11-10 09:39 UTC by Peter Holm
Modified: 2023-12-05 18:40 UTC (History)
4 users (show)

See Also:


Attachments
Patch for tty_rubchar (592 bytes, patch)
2023-11-12 18:03 UTC, Bojan Novković
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Holm freebsd_committer freebsd_triage 2023-11-10 09:39:07 UTC
write(2) sys call fuzzing. Easy to reproduce.

20231110 02:08:35 all (547/926): write2.sh
panic: Assertion ti->ti_linestart < ti->ti_end failed at ../../../kern/tty_inq.c:414
cpuid = 9
time = 1699578545
KDB: stack backtrace:
db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe0676128970
vpanic() at vpanic+0x132/frame 0xfffffe0676128aa0
panic() at panic+0x43/frame 0xfffffe0676128b00
ttyinq_unputchar() at ttyinq_unputchar+0x52/frame 0xfffffe0676128b10
ttydisc_rubchar() at ttydisc_rubchar+0x200/frame 0xfffffe0676128b70
ttydisc_rint() at ttydisc_rint+0x58d/frame 0xfffffe0676128bb0
ttydisc_rint_simple() at ttydisc_rint_simple+0x3f/frame 0xfffffe0676128be0
ptsdev_write() at ptsdev_write+0xde/frame 0xfffffe0676128d40
dofilewrite() at dofilewrite+0x82/frame 0xfffffe0676128d90
sys_write() at sys_write+0xc2/frame 0xfffffe0676128e00
amd64_syscall() at amd64_syscall+0x153/frame 0xfffffe0676128f30
fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe0676128f30
--- syscall (0, FreeBSD ELF64, syscall), rip = 0x822c4d2ca, rsp = 0x83b34ef48, rbp = 0x83b34efc0 ---

Details @ https://people.freebsd.org/~pho/stress/log/log0495.txt
Comment 1 Peter Holm freebsd_committer freebsd_triage 2023-11-11 10:20:47 UTC
I'm not able to reproduce the panic with bb830e346bd5 reverted.
Comment 2 Christos Margiolis freebsd_committer freebsd_triage 2023-11-11 17:24:15 UTC
All bb830e346bd5 does is enable the IUTF8 flag by default, so this should be a regression introduced in 9e589b093857 [1], which implements the flag's functionality. I will CC Bojan, who's the author of the commit.

[1] https://cgit.freebsd.org/src/commit/?id=9e589b0938579f3f4d89fa5c051f845bf754184d
Comment 3 Christos Margiolis freebsd_committer freebsd_triage 2023-11-11 17:33:19 UTC
Peter, can you reproduce this with bb830e346bd5 reverted, but with `stty iutf8` enabled during the test?
Comment 4 Peter Holm freebsd_committer freebsd_triage 2023-11-12 06:31:49 UTC
(In reply to Christos Margiolis from comment #3)
I'll see what I can find out.
Comment 5 Bojan Novković 2023-11-12 18:03:57 UTC
Created attachment 246260 [details]
Patch for tty_rubchar

(In reply to Peter Holm from comment #0)
I've found the underlying issue - the code didn't check whether it reached the start of the current line, causing the next 'unputchar' to panic.

I've attached a patch with the fix. Everything was stable throughout multiple "write2.sh" runs (30 or so minutes of runtime). I can easily reproduce the issue without the fix. 

Please let me know if you can still reproduce the issue with the patch applied.
Comment 6 Peter Holm freebsd_committer freebsd_triage 2023-11-13 04:14:07 UTC
(In reply to Bojan Novković from comment #5)
I ran the problem test for 8 hours with your patch. I was not able to reproduce the panic.
Comment 7 Christos Margiolis freebsd_committer freebsd_triage 2023-11-13 18:06:47 UTC
Pushed the change to upstream:
https://cgit.freebsd.org/src/commit/?id=c6d7be214811c315d234d64c6cbaa92d4f55d2c1
Comment 8 Mark Johnston freebsd_committer freebsd_triage 2023-11-13 18:13:30 UTC
Is the bug in stable/14 and releng/14.0 as well?  Looks like it, in which case the PR is not finished yet.

Does this bug let an unprivileged user panic the kernel?
Comment 9 Christos Margiolis freebsd_committer freebsd_triage 2023-11-13 18:18:08 UTC
(In reply to Mark Johnston from comment #8)
Since the tty patches have been MFC'd to stable/14.0 and releng/14.0, the bug should be present there as well.
Comment 10 Peter Holm freebsd_committer freebsd_triage 2023-11-13 19:59:33 UTC
(In reply to Mark Johnston from comment #8)
Yes, the syscall fuzz test is run as an unprivileged user (nobody).
Comment 11 Mark Johnston freebsd_committer freebsd_triage 2023-11-15 18:50:42 UTC
I spent some time running write2.sh with a kernel that has INVARIANTS disabled but KASAN enabled, and didn't observe any problems.  In any case, I think we should ship an EN for 14.0 for this bug.
Comment 12 commit-hook freebsd_committer freebsd_triage 2023-11-20 16:56:15 UTC
A commit in branch stable/14 references this bug:

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

commit ae8387cc818a0d6a2229ee049b671482e1549519
Author:     Bojan Novković <bojan.novkovic@fer.hr>
AuthorDate: 2023-11-13 18:02:30 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2023-11-20 16:54:54 +0000

    tty: properly check character position when handling IUTF8 backspaces

    The tty_rubchar() code handling backspaces for UTF-8 characters didn't
    properly check whether the beginning of the current line was reached.
    This resulted in a kernel panic in ttyinq_unputchar() when prodded with
    certain malformed UTF-8 sequences.

    PR:             275009
    Reviewed by:    christos
    Differential Revision:  https://reviews.freebsd.org/D42564

    (cherry picked from commit c6d7be214811c315d234d64c6cbaa92d4f55d2c1)

 sys/kern/tty_ttydisc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2023-11-20 16:58:17 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8647fe60b8c3cc03cf1d53d281f9306b7043543b

commit 8647fe60b8c3cc03cf1d53d281f9306b7043543b
Author:     Bojan Novković <bojan.novkovic@fer.hr>
AuthorDate: 2023-11-13 18:02:30 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2023-11-20 16:57:49 +0000

    tty: properly check character position when handling IUTF8 backspaces

    The tty_rubchar() code handling backspaces for UTF-8 characters didn't
    properly check whether the beginning of the current line was reached.
    This resulted in a kernel panic in ttyinq_unputchar() when prodded with
    certain malformed UTF-8 sequences.

    PR:             275009
    Reviewed by:    christos
    Differential Revision:  https://reviews.freebsd.org/D42564

    (cherry picked from commit c6d7be214811c315d234d64c6cbaa92d4f55d2c1)

 sys/kern/tty_ttydisc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Comment 14 Ed Maste freebsd_committer freebsd_triage 2023-12-01 17:54:19 UTC
I see the MFC to stable/13 but 8647fe60b8c3 does not apply to releng/13.2. For the purposes of an EN only 14.0 is affected?
Comment 15 commit-hook freebsd_committer freebsd_triage 2023-12-05 18:28:38 UTC
A commit in branch releng/14.0 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=31f6cfca851f20099162352dac8d044857b2e1f9

commit 31f6cfca851f20099162352dac8d044857b2e1f9
Author:     Bojan Novković <bojan.novkovic@fer.hr>
AuthorDate: 2023-11-13 18:02:30 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-12-04 14:06:29 +0000

    tty: properly check character position when handling IUTF8 backspaces

    The tty_rubchar() code handling backspaces for UTF-8 characters didn't
    properly check whether the beginning of the current line was reached.
    This resulted in a kernel panic in ttyinq_unputchar() when prodded with
    certain malformed UTF-8 sequences.

    PR:             275009
    Reviewed by:    christos
    Differential Revision:  https://reviews.freebsd.org/D42564
    Approved by:    so
    Security:       FreeBSD-EN-23:21.tty

    (cherry picked from commit c6d7be214811c315d234d64c6cbaa92d4f55d2c1)
    (cherry picked from commit ae8387cc818a0d6a2229ee049b671482e1549519)

 sys/kern/tty_ttydisc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Comment 16 Mark Johnston freebsd_committer freebsd_triage 2023-12-05 18:40:36 UTC
Fixed in 14.0-RELEASE-p2.