Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to add an executable file to a 7z & zip archive (part of a bigger problem) #4558

Open
mc-butler opened this issue Jul 7, 2024 · 7 comments
Labels
area: vfs Virtual File System support prio: medium Has the potential to affect progress

Comments

@mc-butler
Copy link

Important

This issue was migrated from Trac:

Origin https://midnight-commander.org/ticket/4558
Reporter ponko (ponkotsume@….com)

I have a 7z archive: /run/shm/1.7z. I open it in the left panel, open /bin in the other panel and copy base64 to the archive. The file gets added to the archive, but as a regular file, NOT as an executable one.

The evident problem is such command used in the u7z extfs script to add the file:

7z a -si"$2" "$1" <"$3" >/dev/null 2>&1

This command takes file data from stdin and, evidently, information about file permissions/mtime is lost.

But there is one more problem. On copying the file into the archive, u7z gets such args:

copyin /run/shm/1.7z base64 /tmp/user/1000/mc-user/extfsOVJKQ2base64

The temp file has perms 600 and is touched with the current time. So, no matter if u7z feeds 7z from stdin or not, there is no way for it to see the original file perms and mtime.

I think, when the extfs (or vfs?) core creates a temp file to feed it to the copyin action, the temp file should have the same perms and mtime as the original.

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on Jul 8, 2024 at 10:52 UTC (comment 1)

This is weird, in some other scripts care is taken to preserve the permissions while copying or using a symbolic link (see uar.in for a good example). Are you sure that the temp file has wrong permissions and it doesn't work with other archivers as well?

@mc-butler
Copy link
Author

Changed by ponko (ponkotsume@….com) on Jul 9, 2024 at 9:37 UTC (comment 2)

uar.in

mcarfs_copyin ()
{
    TMPDIR=`mktemp -d "${MC_TMPDIR:-/tmp}/mctmpdir-uar.XXXXXX"` || exit 1
    name=`basename "$2"`
    (cd "$TMPDIR" && cp -fp "$3" "$name" && $XAR r "$1" "$name")
    rm -rf "$TMPDIR"
}

It works directly on $3, but $3 is a temp file created by extfs/vfs core and it always has perms 600, no matter the original perms. I just tried it in an archlinux docker container. I added echo "$@" >> /tmp/log into the copyin functions in u7z and uar.

After adding a file to a 7z and an ar archives I've got this under $MC_TMPDIR:

# ls -l
total 8
-rw------- 1 root root 36 Jul  9 09:22 extfs21SQQ27z
-rw------- 1 root root 36 Jul  9 09:22 extfsQFRIQ27z

and this in the log

# cat /tmp/log
/tmp/1/1.7z 7z /tmp/mc-root/extfsQFRIQ27z
/tmp/1/archive.a 7z /tmp/mc-root/extfs21SQQ27z

See? In both cases, $3, fed to the copyin action, has perms 600. The original file was 755.

@mc-butler
Copy link
Author

Changed by ponko (ponkotsume@….com) on Jul 9, 2024 at 10:02 UTC (comment 3)

In the /tmp/log above "7z" is the file I added to the archives (/bin/7z). Should have used another binary to copy in, it looks confusing as such in the log

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on Jan 9, 2025 at 10:28 UTC (comment 4)

  • Summary changed from Part of a bigger problem: impossible to add an executable file to a 7z archive to Impossible to add an executable file to a 7z & zip archive (part of a bigger problem)

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on Jan 9, 2025 at 10:28 UTC (comment 5)

Ticket #4628 has been marked as a duplicate of this ticket.

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on Jan 9, 2025 at 10:39 UTC (comment 6)

In #4628 the same problem was observed with ZIP archives.

Unfortunately, it seems to be caused by a fundamental issue in extfs design. If I understand it correctly, extfs core creates a temporary file for copyin with permissions set to 600 (and probably other properties reset / changed ownership).

I guess that this was done initially for security reasons. Not sure how we can fix this properly. We can't completely avoid temporary files, unless the source is a local file system.

  • Is this a good idea to copy permissions to $3, or does it really create some problems?
  • Can we just use original file for copyin if "local" (non-extfs) or is it too dangerous?

Thinking and patches are welcome.

@mc-butler
Copy link
Author

Changed by ossi (@ossilator) on Jan 9, 2025 at 11:31 UTC (comment 7)

/tmp may be mounted noexec. setuid and xattrs are even less likely to survive.

i'd definitely try to avoid the temporary if possible.

gnu tar supports overriding the stored attributes, though it might be "challenging" to control individual files.
from a quick look it doesn't seem like 7z or zip would support that.

one could write a helper that does that after archive creation, which would also enable supporting mc's chmod, etc. functions directly. https://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zip might be of use.

a different approach would be using bindfs to create a virtual view for the packer, but that's kinda crazy land as far as mc is concerned.

@mc-butler mc-butler marked this as a duplicate of #4628 Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: vfs Virtual File System support prio: medium Has the potential to affect progress
Development

No branches or pull requests

1 participant