Bug 3523

Summary: standard output file descriptor was set with: O_NONBLOCK, but restore with flag: 0
Product: Portable OpenSSH Reporter: mzhan017 <mark.zhang>
Component: sshAssignee: Assigned to nobody <unassigned-bugs>
Status: CLOSED FIXED    
Severity: normal CC: djm, dtucker
Priority: P5    
Version: 9.1p1   
Hardware: Other   
OS: Linux   
Bug Depends on:    
Bug Blocks: 3480    
Attachments:
Description Flags
restore exact flags at exit dtucker: ok+

Description mzhan017 2023-01-17 19:22:06 AEDT
Hello,
We have one script to call ssh command to do some action on other server.
then output some string to the standard output, then append to one file.
But the append flag doesn't take effect.

# ssh -V
OpenSSH_9.1p1, OpenSSL 1.1.1k  FIPS 25 Mar 2021

-- scripts a.sh
ssh -l root <other ssh server> "ls 1>/dev/null 2>&1" &
sleep 2
echo "abc"
====

Reproduce with ksh a.sh >> ddd
the "abc" will be written at the first line of file ddd;

Expected,
"abc" was at the last line of file ddd.

Following commit, seems make enhancement for the non-blocking action;
https://github.com/openssh/openssh-portable/commit/7be4ac813662f68e89f23c50de058a49aa32f7e4

Please help if the commit will cause the problem.

output of systemtap
1673941921913: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8401
1673941921913: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8401
1673941921915: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8401
1673941921915: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8401  /// with flag: o_append
1673941922004: setfl: process=ssh, pid=4177330: file name =ddd, flag=8401, arg=8c01  //set with flag non-block
1673941922135: setfl: process=ssh, pid=4177330: file name =ddd, flag=8c01, arg=0     // why set to 0;
1673941923917: ext4_write_checks: inode->i_size: 8455228, name: ksh, pid=4177329, off=0, file_off=0, count=4, flag=0, file_flag=8001, filename=ddd
1673941923917: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8001
1673941923917: vfs_llseek: process=ksh, pid=4177329: file name =ddd, flag=8001

Thanks,
Mark
Comment 1 Damien Miller 2023-01-17 21:30:37 AEDT
Created attachment 3651 [details]
restore exact flags at exit

Thanks for the report. Could you try this patch?
Comment 2 mzhan017 2023-01-17 23:49:21 AEDT
Hello Miller,
Thanks for your quick response and fix.
The patch is good by my test.

Thanks,
Mark

systemtap's result, the flag could be restored.
time: 1673959685099: vfs_llseek: process=ksh, pid=1168362: file name =ddd, flag=8401
time: 1673959685099: vfs_llseek: process=ksh, pid=1168362: file name =ddd, flag=8401
time: 1673959685100: vfs_llseek: process=ksh, pid=1168362: file name =ddd, flag=8401
time: 1673959685101: vfs_llseek: process=ksh, pid=1168362: file name =ddd, flag=8401
time: 1673959685192: setfl: process=ssh, pid=1168363: file name =ddd, flag=8401, arg=8c01
time: 1673959685319: setfl: process=ssh, pid=1168363: file name =ddd, flag=8c01, arg=8401
time= 1673959687102: inode->i_size: 8455256, name: ksh, pid=1168362, off=0, file_off=0, count=4, flag=2, file_flag=8401, filename=ddd
Comment 3 Damien Miller 2023-01-18 13:32:51 AEDT
Thanks this fix has been committed and will be in OpenSSH 9.2, due for release next month.
Comment 4 Damien Miller 2023-03-17 13:38:33 AEDT
OpenSSH 9.3 has been released. Close resolved bugs