[Bug 284513] Default bourne shell /bin/sh ignores or destroys terminal echoctl flag

From: <bugzilla-noreply_at_freebsd.org>
Date: Sat, 01 Feb 2025 21:37:43 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=284513

            Bug ID: 284513
           Summary: Default bourne shell /bin/sh ignores or destroys
                    terminal echoctl flag
           Product: Base System
           Version: 14.2-RELEASE
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: standards
          Assignee: standards@FreeBSD.org
          Reporter: dclarke@blastwave.org

One of the key ideas in the UNIX world is that we should not astonish the user.
This has taken a few years to finally just call it a bug.

The default FreeBSD bourne shell /bin/sh seems to ignore the terminal
config flag echoctl and does not give the user any visual feedback to
confirm that CTRL-C was issued. When one types anything on the input
line for the default FreeBSD /bin/sh and then you enter CTRL-c it is
silent.

Which is to say that the chars "^C" do not appear. This is wrong
behavior for normal terminal processing.

Here I create a new account and then login via a serial terminal :

hydra# uname -apKU
FreeBSD hydra 14.2-RELEASE FreeBSD 14.2-RELEASE
releng/14.2-n269506-c8918d6c7412 GENERIC amd64 amd64 1402000 1402000
hydra# 
hydra# pw useradd -n debug -m -c 'Some DEBUG User' -d /home/debug -e '' \
> -g devl -M 0700 \
> -s /bin/sh -u 16042 
hydra# 
hydra# passwd debug
Changing local password for debug
New Password:
Retype New Password:
hydra# 

   * * *  using a proper 1200 baud serial connection * * * 


Then login with a serial terminal connection : 

FreeBSD/amd64 (hydra) (ttyu0)

login: debug
Password:
FreeBSD 14.2-RELEASE (GENERIC) releng/14.2-n269506-c8918d6c7412

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/
Security Advisories:   https://www.FreeBSD.org/security/
FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
FreeBSD FAQ:           https://www.FreeBSD.org/faq/
Questions List:        https://www.FreeBSD.org/lists/questions/
FreeBSD Forums:        https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/
directory, or can be installed later with:  pkg install en-freebsd-doc
For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed:  freebsd-version ; uname -a
Please include that output and any error messages when posting questions.
Introduction to manual pages:  man man
FreeBSD directory layout:      man hier

To change this login announcement, see motd(5).
"man security" gives very good advice on how to tune the security of your
FreeBSD system.
debug@hydra:~ $ 
debug@hydra:~ $ uname -apKU
FreeBSD hydra 14.2-RELEASE FreeBSD 14.2-RELEASE
releng/14.2-n269506-c8918d6c7412 GENERIC amd64 amd64 1402000 1402000
debug@hydra:~ $ 
debug@hydra:~ $ echo $SHELL
/bin/sh
debug@hydra:~ $ ls -la 
total 45
drwx------   2 debug devl    9 Feb  1 20:12 .
drwxr-xr-x  13 root  devl   13 Feb  1 20:12 ..
-rw-r--r--   1 debug devl  950 Feb  1 20:12 .cshrc
-rw-r--r--   1 debug devl  311 Feb  1 20:12 .login
-rw-r--r--   1 debug devl   79 Feb  1 20:12 .login_conf
-rw-------   1 debug devl  289 Feb  1 20:12 .mail_aliases
-rw-r--r--   1 debug devl  255 Feb  1 20:12 .mailrc
-rw-r--r--   1 debug devl  966 Feb  1 20:12 .profile
-rw-r--r--   1 debug devl 1003 Feb  1 20:12 .shrc
debug@hydra:~ $ 
debug@hydra:~ $ stty -a 
speed 1200 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk
        brkint -inpck -ignpar -parmrk iutf8
oflags: opost onlcr -ocrnl tab3 -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf rtsdtr
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;
        lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
        status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;
debug@hydra:~ $ 
debug@hydra:~ $ ls -lapb
debug@hydra:~ $ That was a CTRL-c and it does not appear as ^C <-- bug !! 
debug@hydra:~ $ 

Here I am astonished. Where is the CTRL-C ? Perhaps I can type : 

debug@hydra:~ $ rm -rf /*

How can you tell if that was ever entered or what?  The echoctl was ignored.

Even worse the default shell destroys the state of the terminal flag echoctl.

debug@hydra:~ $ stty -echoctl
debug@hydra:~ $ ls -lapb 
debug@hydra:~ $ the echoctl is destroyed or ignored !  
debug@hydra:~ $ stty -a 
speed 1200 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk
        brkint -inpck -ignpar -parmrk iutf8
oflags: opost onlcr -ocrnl tab3 -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf rtsdtr
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;
        lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
        status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;
debug@hydra:~ $ exit

FreeBSD/amd64 (hydra) (ttyu0)

login:




OKay so that is the poor behavior of the default FreeBSD /bin/sh bourne
shell which seems to handle CTRL chars poorly.  Certainly CTRL-C.

If I build a GNU BASH shell with minimal options. In fact, the configuration
is ruthlessly harsh wherein it has no line editing and no processing of
trivial control chars for things like the arrow keys on a keyboard. These
are taken for granted by most users in the past three or four decades.

Same machine where I now create another user to demonstrate correct behavior :

hydra# pw useradd -n debug2 -m -c 'Another DEBUG User' -d /home/debug2 \
> -e '' -g devl -M 0700 \
> -s /usr/local/shell/bash -u 16041
hydra# passwd debug2
Changing local password for debug2
New Password:
Retype New Password:
hydra# 


Then login as that user on a serial terminal connection.

FreeBSD/amd64 (hydra) (ttyu0)

login: debug2
Password:
FreeBSD 14.2-RELEASE (GENERIC) releng/14.2-n269506-c8918d6c7412

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/
Security Advisories:   https://www.FreeBSD.org/security/
FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
FreeBSD FAQ:           https://www.FreeBSD.org/faq/
Questions List:        https://www.FreeBSD.org/lists/questions/
FreeBSD Forums:        https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/
directory, or can be installed later with:  pkg install en-freebsd-doc
For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed:  freebsd-version ; uname -a
Please include that output and any error messages when posting questions.
Introduction to manual pages:  man man
FreeBSD directory layout:      man hier

To change this login announcement, see motd(5).
$ 
$ echo $SHELL
/usr/local/shell/bash
$ 
$ stty -a 
speed 1200 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk
        brkint -inpck -ignpar -parmrk iutf8
oflags: opost onlcr -ocrnl tab3 -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf rtsdtr
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;
        lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
        status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;
$ 
$ ls -la ^C
$ 
$   That was a CTRL-c and it works ! also we see ^C chars on the line ^C
$

      * * *   now we flip the flag for echoctl   * * 


$ stty -echoctl
$ 
$  That disables the output of CTRL chars like this --> 
$ 
$  that is an invisible CTRL-c and that is correct for -echoctl terminal flag
$ 
$ stty echoctl
$ 
$ Now we see the CTRL-c again ^C
$ that is correct behavior .... fully reasonable and no surprise to the user ^C
$ 
$ $SHELL --version 
GNU bash, version 5.2.37(2)-release (x86_64-unknown-freebsd14.2)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ exit
logout

FreeBSD/amd64 (hydra) (ttyu0)

login: 


Why the serial terminal config?  Simply to remove any possible network
layer from the problem.  There is no telnet here. No SSHd daemon. This
is a correct login to one of the machines serial ports.

To be more clear here the GNU BASH shell that I tested with is merely
a test case and the configuration was very harsh : 

$ ./configure --prefix=/home/dclarke/local --enable-threads=posix \
> --enable-xpg-echo-default --enable-minimal-config \
> --enable-strict-posix-default

That is all. Nothing more. 

That shell needs almost next to nothing :

$ echo $SHELL
/home/dclarke/local/bin/bash
$ 
$ readelf -delV /home/dclarke/local/bin/bash | grep -E 'NEED|NAME|PATH'
 0x0000000000000001 NEEDED               Shared library: [libintl.so.8]
 0x0000000000000001 NEEDED               Shared library: [libiconv.so.2]
 0x0000000000000001 NEEDED               Shared library: [libdl.so.1]
 0x0000000000000001 NEEDED               Shared library: [libc.so.7]
 0x000000006ffffffe VERNEED              0x402238
 0x000000006fffffff VERNEEDNUM           2
$ 

This is the exact shell used by the debug2 user : 

$ openssl dgst -sha256 -r $SHELL /usr/local/shell/bash 
4e9ce6b33a9e909ab8d6df746898c8148a388f82eb333b7d577df9b2fa97a0d0
*/home/dclarke/local/bin/bash
4e9ce6b33a9e909ab8d6df746898c8148a388f82eb333b7d577df9b2fa97a0d0
*/usr/local/shell/bash
$ 

The FreeBSD default bourne shell needs to respect the terminal echoctl flag
correctly and output a "^C" char sequence correctly. There should be some
reasonable feedback to let the user know what happened.

It is wrong to astonish the user.


--
Dennis Clarke
RISC-V/SPARC/PPC/ARM/CISC
UNIX and Linux spoken

ps: some discussions were had with others and I think there is agreement
    that this is a bug : 

    https://lists.gnu.org/archive/html/bug-ncurses/2025-02/msg00001.html

-- 
You are receiving this mail because:
You are the assignee for the bug.