Bug 1909988

Summary: ls -t option doesn't work to sort upon the modification time in sftp session
Product: Red Hat Enterprise Linux 8 Reporter: Masahiro Matsuya <mmatsuya>
Component: opensshAssignee: Dmitry Belyavskiy <dbelyavs>
Status: CLOSED ERRATA QA Contact: BaseOS QE Security Team <qe-baseos-security>
Severity: medium Docs Contact:
Priority: medium    
Version: 8.3CC: asosedki, dbelyavs, pkis
Target Milestone: rcKeywords: Patch, Triaged
Target Release: 8.0   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: openssh-8.0p1-7.el8 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2021-11-09 19:32:04 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Masahiro Matsuya 2020-12-22 09:13:50 UTC
Created attachment 1741313 [details]
proposed patch

Description of problem:

The ls -t option doesn't sort the globbed files upon the modification time.

sftp> ls -ltr SG*
-rw-r--r--    ? 0        0               0 Dec 22 17:43 SG0003
-rw-r--r--    ? 0        0               0 Dec 22 17:40 SG0001
-rw-r--r--    ? 0        0               0 Dec 22 17:45 SG0002

The root cause is in usage of the timespeccmp macro in sglob_comp().
The timespeccmp macro returns 0 (false) or 1 (true) as the result of comparison with "<". When the -r option is not used, sglob_comp() returns
1 or 0 only. When the -r option is used, it returns -1 or 0 only

sglob_comp(const void *aa, const void *bb)
{
...
        int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
...
        else if (sort_flag & LS_TIME_SORT) {
#if defined(HAVE_STRUCT_STAT_ST_MTIM)
                return (rmul * timespeccmp(&as->st_mtim, &bs->st_mtim, <));


The following is about the comparison function of qsort() in man qsort.

In man qsort
       The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be
       respectively less than, equal to, or greater than the second.  If two members compare as equal, their order in the sorted array is
       undefined.


So, the above sglob_comp() doesn't meet this requirement.
It should return -1, 0, or 1.


Version-Release number of selected component (if applicable):
openssh-8.0p1-5.el8.x86_64

How reproducible:
Always

Steps to Reproduce:
1. Create three files in /root on any ssh server

   # cd /root
   # touch SG0001
   wait more than one minute
   # touch SG0003
   wait more than one minute
   # touch SG0002

NOTE: We can see the difference of the modification time in the "ls -lt" output by waiting more than one minute.

2. connect with sftp from *RHEL8.3 client*

   # sftp root@<hostname>

NOTE: sorting is done in the sftp client side.

3. Run "ls -ltr SG*"

Actual results:
Sorting with -l option doesn't work.

sftp> ls -ltr SG*
-rw-r--r--    ? 0        0               0 Dec 22 17:43 SG0003
-rw-r--r--    ? 0        0               0 Dec 22 17:40 SG0001
-rw-r--r--    ? 0        0               0 Dec 22 17:45 SG0002

Expected results:
Sorting with -l option works.

sftp> ls -ltr SG*
-rw-r--r--    ? 0        0               0 Dec 22 17:40 SG0001
-rw-r--r--    ? 0        0               0 Dec 22 17:43 SG0003
-rw-r--r--    ? 0        0               0 Dec 22 17:45 SG0002

Comment 1 Jakub Jelen 2021-01-04 11:53:20 UTC
Thank you for the report. It is an issue in the upstream openssh too so I filled and attached an upstream bug.

It looks simple enough so we should be able to backport this for next release.

Comment 2 Masahiro Matsuya 2021-01-05 08:30:05 UTC
That's great. Thank you, Jakub!

Comment 10 errata-xmlrpc 2021-11-09 19:32:04 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: openssh security update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2021:4368