fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
PATCH 1/1 fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
Commit 98a6f1fc02e41 (fs/cpio: make initramfs init script survive 'console=' kernel argument) dropped the explicit /dev/console execs for fd 0,1,2, as they fail when booted with console= and aren't really needed as the kernel will setup fd 0,1,2 from /dev/console before executing the initramfs anyway.
Not doing this unfortunately confuses glibc's ttyname_r(3) implementation (used by E.G. busybox/coreutils 'tty'), causing it to fail with ENOENT as it does a fstat on fd 0 and tries to match up st_ino / st_dev against the entries in /dev (since glibc 2.26):
commit 15e9a4f378c8607c2ae1aa465436af4321db0e23
Author: Christian Brauner <[email protected]>
Date: Fri Jan 27 15:59:59 2017 +0100
linux ttyname and ttyname_r: do not return wrong results
If a link (say /proc/self/fd/0) pointing to a device, say /dev/pts/2, in a parent mount namespace is passed to ttyname, and a /dev/pts/2 exists (in a different devpts) in the current namespace, then it returns /dev/pts/2. But /dev/pts/2 is NOT the current tty, it is a different file and device.
Detect this case and return ENODEV. Userspace can choose to take this as a hint that the fd points to a tty device but to act on the fd rather than the link.
The reason it fails is that we manually mount devtmpfs on /dev in /init, so the /dev/console used by the kernel (in rootfs) is not the same file as /dev/console at runtime (in devtmpfs).
Notice: Once logged in, tty does work correctly. Presumably login reopens stdin/stdout/stderr.
To fix this, re-add the exec of /dev/console for fd 0,1,2, but only do so if possible. Because of the above mentioned shell behaviour (specified by POSIX [0]), perform this check in a subshell.
[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_20_01
--- fs/cpio/init | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/cpio/init +11 -0
@@ -1,4 +1,15 @@ #!/bin/sh # devtmpfs does not get automounted for initramfs /bin/mount -t devtmpfs devtmpfs /dev + +# use the /dev/console device node from devtmpfs if possible to not +# confuse glibc's ttyname_r(). +# This may fail (E.G. booted with console=), and errors from exec will +# terminate the shell, so use a subshell for the test +if (exec 0</dev/console) 2>/dev/null; then + exec 0</dev/console + exec 1>/dev/console + exec 2>/dev/console +fi + exec /sbin/init "$@"
Signature · 1 line
2.20.1
PATCH 1/1 fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
Peter, All,
On 2020-08-29 15:09 +0200, Peter Korsgaard spake thusly:
Quoted 53 lines
Commit
98a6f1fc02e41(fs/cpio: make initramfs init script survive 'console=' kernel argument) dropped the explicit/dev/consoleexecs for fd 0,1,2, as they fail when booted with console= and aren't really needed as the kernel will setup fd 0,1,2 from/dev/consolebefore executing the initramfs anyway.Not doing this unfortunately confuses glibc's
ttyname_r(3)implementation (used by E.G. busybox/coreutils 'tty'), causing it to fail with ENOENT as it does a fstat on fd 0 and tries to match upst_ino/st_devagainst the entries in /dev (since glibc 2.26):commit
15e9a4f378c8607c2ae1aa465436af4321db0e23
Author: Christian Brauner <[email protected]>
Date: Fri Jan 27 15:59:59 2017 +0100linux ttyname and
ttyname_r: do not return wrong resultsIf a link (say
/proc/self/fd/0) pointing to a device, say/dev/pts/2, in a parent mount namespace is passed to ttyname, and a/dev/pts/2exists (in a different devpts) in the current namespace, then it returns/dev/pts/2. But/dev/pts/2is NOT the current tty, it is a different file and device.Detect this case and return ENODEV. Userspace can choose to take this as a hint that the fd points to a tty device but to act on the fd rather than the link.
Signed-off-by Serge Hallyn <[email protected]>Signed-off-by Christian Brauner <[email protected]>The reason it fails is that we manually mount devtmpfs on /dev in /init, so the
/dev/consoleused by the kernel (in rootfs) is not the same file as/dev/consoleat runtime (in devtmpfs).Notice: Once logged in, tty does work correctly. Presumably login reopens stdin/stdout/stderr.
To fix this, re-add the exec of
/dev/consolefor fd 0,1,2, but only do so if possible. Because of the above mentioned shell behaviour (specified by POSIX [0]), perform this check in a subshell.[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_20_01
Signed-off-by Peter Korsgaard <[email protected]>--- fs/cpio/init | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/cpio/init +0
-0@@ -1,4 +1,15 @@ #!/bin/sh # devtmpfs does not get automounted for initramfs /bin/mount -t devtmpfs devtmpfs /dev
fs/cpio/init line 13
Quoted 10 lines
fs/cpio/init +10
-0+ +# use the /dev/console device node from devtmpfs if possible to not +# confuse glibc's ttyname_r(). +# This may fail (E.G. booted with console=), and errors from exec will +# terminate the shell, so use a subshell for the test +if (exec 0</dev/console) 2>/dev/null; then + exec 0</dev/console + exec 1>/dev/console + exec 2>/dev/console +fi
You forgot the else part, which should redirect to /dev/null, so that we do have valid fds in the case /dev/console does not really exist, no?
Regards,
Yann E. MORIN.
Quoted 4 lines
fs/cpio/init +0
-0exec /sbin/init "$@"Signature · 1 line
2.20.1
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------. | Yann E. MORIN | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: | | +33 662 376 056 | Software Designer | \ / CAMPAIGN | ___ | | +33 561 099 427 `------------.-------: X AGAINST | \e/ There is no | | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL | v conspiracy. | '------------------------------^-------^------------------^--------------------'
PATCH 1/1 fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Yann" == Yann E MORIN <[email protected]> writes:
Hi,
Quoted 4 lines
Quoted 4 lines
fs/cpio/init +4
-0+# use the /dev/console device node from devtmpfs if possible to not +# confuse glibc's ttyname_r(). +# This may fail (E.G. booted with console=), and errors from exec will +# terminate the shell, so use a subshell for the test
Quoted 8 lines
fs/cpio/init line 13
Quoted 5 lines
fs/cpio/init +5
-0+if (exec 0</dev/console) 2>/dev/null; then + exec 0</dev/console + exec 1>/dev/console + exec 2>/dev/console +fiYou forgot the else part, which should redirect to
/dev/null, so that we do have valid fds in the case/dev/consoledoes not really exist, no?
Well, the kernel already takes care of that since commit 2bd3a997befc2 (E.G. as explained by buildroot commit 98a6f1fc02e41), so I am not sure what explicitly reopening /dev/null would achive?
ttyname_r() / tty already returns ENOENT (not a tty) for this case without reopening /dev/null:
tty </dev/null
not a tty
Signature · 1 line
Bye, Peter Korsgaard
Yann E. MORIN
AppliedPATCH 1/1 fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
In reply to Peter KorsgaardPeter, All,
On 2020-08-29 15:09 +0200, Peter Korsgaard spake thusly:
Quoted 34 lines
Commit
98a6f1fc02e41(fs/cpio: make initramfs init script survive 'console=' kernel argument) dropped the explicit/dev/consoleexecs for fd 0,1,2, as they fail when booted with console= and aren't really needed as the kernel will setup fd 0,1,2 from/dev/consolebefore executing the initramfs anyway.Not doing this unfortunately confuses glibc's
ttyname_r(3)implementation (used by E.G. busybox/coreutils 'tty'), causing it to fail with ENOENT as it does a fstat on fd 0 and tries to match upst_ino/st_devagainst the entries in /dev (since glibc 2.26):commit
15e9a4f378c8607c2ae1aa465436af4321db0e23
Author: Christian Brauner <[email protected]>
Date: Fri Jan 27 15:59:59 2017 +0100linux ttyname and
ttyname_r: do not return wrong resultsIf a link (say
/proc/self/fd/0) pointing to a device, say/dev/pts/2, in a parent mount namespace is passed to ttyname, and a/dev/pts/2exists (in a different devpts) in the current namespace, then it returns/dev/pts/2. But/dev/pts/2is NOT the current tty, it is a different file and device.Detect this case and return ENODEV. Userspace can choose to take this as a hint that the fd points to a tty device but to act on the fd rather than the link.
Signed-off-by Serge Hallyn <[email protected]>Signed-off-by Christian Brauner <[email protected]>The reason it fails is that we manually mount devtmpfs on /dev in /init, so the
/dev/consoleused by the kernel (in rootfs) is not the same file as/dev/consoleat runtime (in devtmpfs).Notice: Once logged in, tty does work correctly. Presumably login reopens stdin/stdout/stderr.
Quoted 7 lines
To fix this, re-add the exec of
/dev/consolefor fd 0,1,2, but only do so if possible. Because of the above mentioned shell behaviour (specified by POSIX [0]), perform this check in a subshell.[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_20_01
Signed-off-by Peter Korsgaard <[email protected]>
Applied to master, thanks.
Regards,
Yann E. MORIN.
Quoted 27 lines
--- fs/cpio/init | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/cpio/init +11
-0@@ -1,4 +1,15 @@ #!/bin/sh # devtmpfs does not get automounted for initramfs /bin/mount -t devtmpfs devtmpfs /dev + +# use the /dev/console device node from devtmpfs if possible to not +# confuse glibc's ttyname_r(). +# This may fail (E.G. booted with console=), and errors from exec will +# terminate the shell, so use a subshell for the test +if (exec 0</dev/console) 2>/dev/null; then + exec 0</dev/console + exec 1>/dev/console + exec 2>/dev/console +fi + exec /sbin/init "$@"Signature · 1 line
2.20.1
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------. | Yann E. MORIN | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: | | +33 662 376 056 | Software Designer | \ / CAMPAIGN | ___ | | +33 561 099 427 `------------.-------: X AGAINST | \e/ There is no | | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL | v conspiracy. | '------------------------------^-------^------------------^--------------------'
PATCH 1/1 fs/cpio/init: unbreak ttyname_r() on glibc after dropping /dev/console exec
In reply to Peter KorsgaardQuoted 36 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Peter" == Peter Korsgaard <[email protected]> writes:
Commit
98a6f1fc02e41(fs/cpio: make initramfs init script survive 'console=' kernel argument) dropped the explicit/dev/consoleexecs for fd 0,1,2, as they fail when booted with console= and aren't really needed as the kernel will setup fd 0,1,2 from/dev/consolebefore executing the initramfs anyway.Not doing this unfortunately confuses glibc's
ttyname_r(3)implementation (used by E.G. busybox/coreutils 'tty'), causing it to fail with ENOENT as it does a fstat on fd 0 and tries to match upst_ino/st_devagainst the entries in /dev (since glibc 2.26):commit
15e9a4f378c8607c2ae1aa465436af4321db0e23
Author: Christian Brauner <[email protected]>
Date: Fri Jan 27 15:59:59 2017 +0100linux ttyname and
ttyname_r: do not return wrong resultsIf a link (say
/proc/self/fd/0) pointing to a device, say/dev/pts/2, in a parent mount namespace is passed to ttyname, and a/dev/pts/2exists (in a different devpts) in the current namespace, then it returns/dev/pts/2. But/dev/pts/2is NOT the current tty, it is a different file and device.Detect this case and return ENODEV. Userspace can choose to take this as a hint that the fd points to a tty device but to act on the fd rather than the link.
Signed-off-by Serge Hallyn <[email protected]>Signed-off-by Christian Brauner <[email protected]>The reason it fails is that we manually mount devtmpfs on /dev in /init, so the
/dev/consoleused by the kernel (in rootfs) is not the same file as/dev/consoleat runtime (in devtmpfs).Notice: Once logged in, tty does work correctly. Presumably login reopens stdin/stdout/stderr.
Quoted 7 lines
To fix this, re-add the exec of
/dev/consolefor fd 0,1,2, but only do so if possible. Because of the above mentioned shell behaviour (specified by POSIX [0]), perform this check in a subshell.[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_20_01
Signed-off-by Peter Korsgaard <[email protected]>
Committed to 2020.02.x and 2020.05.x, thanks.
Signature · 1 line
Bye, Peter Korsgaard