freebsd-nq/stand
John Hood 3a4b9e30d4 loader: support.4th resets the read buffer incorrectly
Large nextboot.conf files (over 80 bytes) are not read correctly by the
Forth loader, causing file parsing to abort, and nextboot configuration
fails to apply.

Simple repro:

nextboot -e foo=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
shutdown -r now

That will cause the bug to cause a parse failure but shouldn't otherwise
affect the boot.  Depending on your loader configuration, you may also
have to set beastie_disable and/or reduce the number of modules loaded
to see the error on a small console screen.  12.0 or CURRENT users will
also have to explicitly use the Forth loader instead of the Lua loader.
The error will look something like:

Warning: syntax error on file /boot/loader.conf.local
foo="xxxxxxxxxxxxxxnextboot_enable="YES"
                                    ^
/boot/support.4th has crude file I/O buffering, which uses a buffer
'read_buffer', defined to be 80 bytes by the 'read_buffer_size'
constant.  The loader first tastes nextboot.conf, reading and parsing
the first line in it for nextboot_enable="YES".  If this is true, then
it reopens the file and parses it like other loader .conf files.

Unfortunately, the file I/O buffering code does not fully reset the
buffer state in the reset_line_reading word.  If the last file was read
to the end, that doesn't matter; the file buffer is treated as empty
anyway.  But in the nextboot.conf case, the loader will not read to the
end of file if it is over 80 bytes, and the file buffer may be reused
when reading the next file.  When the file is reread, the corrupt text
may cause file parsing to abort on bad syntax (if the corrupt line has
<>2 quotes in it), the wrong variable to be set, no variable to be set
at all, or (if the splice happens to land at a line ending) something
approximating normal operation.

The bug is very old, dating back to at least 2000 if not before, and is
still present in 12.0 and CURRENT r345863 (though it is now hidden by
the Lua loader by default).

Suggested one-line attached.  This does change the behavior of the
reset_line_reading word, which is exported in the line-reading
dictionary (though the export is not documented in loader man pages).
But repo history shows it was probably exported for the PNP support
code, which was never included in the loader build, and was removed 5
months ago.

One thing that puzzles me: how has this bug gone unnoticed/unfixed for
nearly 2 decades?  I find it hard to believe that nobody's tried to do
something interesting with nextboot, like load a kernel and filesystem,
which is what I'm doing.

Tested by:		Gary Jennejohn
PR:			239315
MFC After:		3 weeks
Reviewed by:		imp (and correctly applied this time)
Differential Revision:	https://reviews.freebsd.org/D31328

(cherry picked from commit dbdf2b52f5)
2021-09-12 09:56:15 -06:00
..
arm Remove tests for obsolete compilers in the build system 2020-05-12 15:22:40 +00:00
arm64
common loader.efi: fix console output after BS off 2021-09-09 11:28:07 +03:00
defaults loader.conf(5): mention "efi" option for "console" parameter 2021-08-04 07:50:14 +08:00
efi loader.efi: fix console output after BS off 2021-09-09 11:28:07 +03:00
fdt stand/fdt: Scale blob size better as overlays apply 2020-01-09 04:34:42 +00:00
ficl loader: implement framebuffer console 2021-01-02 21:41:36 +02:00
ficl32 Enable veriexec for loader 2019-02-26 06:22:10 +00:00
fonts loader: really use bold font for vga text mode 2021-01-08 01:24:30 +02:00
forth loader: support.4th resets the read buffer incorrectly 2021-09-12 09:56:15 -06:00
i386 pxeboot: improve and simplify rx handling 2021-09-04 02:44:33 -05:00
images loader: implement framebuffer console 2021-01-02 21:41:36 +02:00
kshim Fix build of stand/usb . 2021-05-10 16:11:19 +02:00
liblua contrib/lua: update to 5.4.2 2021-01-13 23:56:18 -06:00
liblua32
libofw Fix 64-bit build of libofw. 2020-09-08 23:22:11 +00:00
libsa libsa: Fix a typo in source code comments 2021-09-01 06:34:20 +02:00
libsa32 Enable veriexec for loader 2019-02-26 06:22:10 +00:00
lua lualoader: use more concise verbiage for autoboot 2021-09-12 00:34:28 -05:00
man loader: update autoboot description and move to loader.conf.5 2021-07-16 12:28:44 -06:00
mips loader: fix mips build with gfx_fb 2021-01-04 16:57:50 +00:00
powerpc loader: fix powerpc build with gfx_fb 2021-01-04 16:57:58 +00:00
uboot loader: fix uboot build with gfx_fb 2021-01-03 20:45:37 +02:00
usb Fix build of stand/usb . 2020-01-23 10:40:34 +00:00
userboot loader: cstyle cleanup of userboot/devicename.c 2021-08-19 16:05:26 +03:00
defs.mk Prefer MK_SSP=no to SSP_CFLAGS= 2021-08-11 13:56:28 -03:00
fdt.mk
ficl.mk loader: implement framebuffer console 2021-01-02 21:41:36 +02:00
loader.mk loader: start kernel in text mode when there is no vbefb vt driver 2021-02-21 22:31:38 +02:00
lua.mk
Makefile stand: ensure that the efi directory's dependencies are correct 2021-01-29 23:46:17 -06:00
Makefile.amd64 stand: properly declare subdir deps or .WAIT, do parallel build 2020-12-31 11:15:45 -06:00
Makefile.inc stand: properly declare subdir deps or .WAIT, do parallel build 2020-12-31 11:15:45 -06:00
uboot.mk