10000 Wrong value of S_IFMT mask when converting sftp filemode bits to os.FileMode · Issue #291 · pkg/sftp · GitHub
[go: up one dir, main page]

Skip to content
Wrong value of S_IFMT mask when converting sftp filemode bits to os.FileMode #291
@cyrilcourvoisier

Description

@cyrilcourvoisier

I encountered this issue when synchronizing a directory using rclone (with sftp) to a server running AIX. Rclone was sending a SSH_FXP_STAT request to test if a directory is, well, a directory.

The STAT request was giving the directory's status as 'not a directory' despite being one.
The problem was in the toFileMode function in attrs.go

input filemode bits are                  1 0100 0011 1111 1111‬
syscall.S_IFMT mask bits are             1 1111 0000 0000 0000

The different values tested are:

 S_IFBLK                                 0 0110 0000 0000 0000
 S_IFCHR                                 0 0010 0000 0000 0000
 S_IFDIR                                 0 0100 0000 0000 0000
 S_IFIFO                                 0 0001 0000 0000 0000
 S_IFLNK                                 0 1010 0000 0000 0000
 S_IFREG                                 0 1000 0000 0000 0000
 S_IFSOCK                                0 1100 0000 0000 0000

As you can see, none of the values matchs the input, and the leftmost bit is not used by any value.
The input would match S_IFDIR if the S_IFMT mask didn't test the leftmost bit.

I have searched the value of S_IFMT mask in other sftp implentations and in the ones I have found, they define S_IFMT as: 1111 0000 0000 0000

for example, the python implementation of stat:
https://github.com/python/cpython/blob/2.7/Lib/stat.py

Or this javascript implementation of sftp:
http://maverick-ng-javadocs.s3-website-eu-west-1.amazonaws.com/constant-values.html#com.sshtools.client.sftp.SftpFileAttributes.S_IFMT

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0