From 82de296fc707203047097cc38aa5ca158e277435 Mon Sep 17 00:00:00 2001 From: schwarze Date: Sat, 23 Apr 2022 13:58:09 +0000 Subject: [PATCH] If the last data row of a tbl(7) contains nothing but a horizontal line, do not skip closing the table and cleaning up memory at the end of the table in the HTML output module. This bug resulted in skipping the tblcalc() function and reusing the existing roffcol array for the next tbl(7) processed. If the next table had more columns than the one ending with a horizontal line in the last data row, uninitialized memory was read, potentially resulting in near-infinite output. The bug was introduced in rev. 1.24 (2018/11/26) but only fully exposed by rev. 1.33 (2021/09/09). Until rev. 1.32, it could only cause misformatting and invalid HTML output syntax but not huge output because up to that point, the function did not use the roffcol array. Nasty bug found the hard way by Michael Stapelberg on the production server manpages.debian.org. Michael also supplied example files and excellent instructions how to reproduce the bug, which was very difficult because no real-world manual page is known that triggers the bug by itself, so to reproduce the bug, mandoc(1) had to be invoked with at least two file name arguments. --- usr.bin/mandoc/tbl_html.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/usr.bin/mandoc/tbl_html.c b/usr.bin/mandoc/tbl_html.c index f2a3c2d51c9..68ae83548d1 100644 --- a/usr.bin/mandoc/tbl_html.c +++ b/usr.bin/mandoc/tbl_html.c @@ -1,7 +1,8 @@ -/* $OpenBSD: tbl_html.c,v 1.34 2022/01/12 04:43:21 schwarze Exp $ */ +/* $OpenBSD: tbl_html.c,v 1.35 2022/04/23 13:58:09 schwarze Exp $ */ /* + * Copyright (c) 2014, 2015, 2017, 2018, 2021, 2022 + * Ingo Schwarze * Copyright (c) 2011 Kristaps Dzonsons - * Copyright (c) 2014,2015,2017,2018,2021 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -132,7 +133,7 @@ print_tbl(struct html *h, const struct tbl_span *sp) */ if (sp->pos != TBL_SPAN_DATA) - return; + goto out; /* Inhibit printing of spaces: we do padding ourselves. */ @@ -284,6 +285,7 @@ print_tbl(struct html *h, const struct tbl_span *sp) h->flags &= ~HTML_NONOSPACE; +out: if (sp->next == NULL) { assert(h->tbl.cols); free(h->tbl.cols); -- 2.20.1