Skip to content

cp: Fails to preserve fifo permissions, and symlink/fifo SELinux context #8402

@drinkcat

Description

@drinkcat

Repro case, similar to what GNU misc/selinux does:

rm -rf tmp tmp.gnu tmp.uu
mkdir tmp; touch tmp/f; mkfifo tmp/p; ln -s f tmp/l
chmod a+rw tmp/p
chcon -h 'root:object_r:tmp_t:s0' tmp/*
cp -r --preserve=all tmp tmp.gnu
cargo run --features=selinux -- cp -r --preserve=all tmp tmp.uu
ls -lZ tmp*
tmp:
total 4
-rw-r--r--. 1 drinkcat drinkcat root:object_r:tmp_t:s0 0 Jul 28 06:46 f
lrwxrwxrwx. 1 drinkcat drinkcat root:object_r:tmp_t:s0 1 Jul 28 06:46 l -> f
prw-rw-rw-. 1 drinkcat drinkcat root:object_r:tmp_t:s0 0 Jul 28 06:46 p

tmp.gnu:
total 4
-rw-r--r--. 1 drinkcat drinkcat root:object_r:tmp_t:s0 0 Jul 28 06:46 f
lrwxrwxrwx. 1 drinkcat drinkcat root:object_r:tmp_t:s0 1 Jul 28 06:46 l -> f
prw-rw-rw-. 1 drinkcat drinkcat root:object_r:tmp_t:s0 0 Jul 28 06:46 p

tmp.uu:
total 4
-rw-r--r--. 1 drinkcat drinkcat root:object_r:tmp_t:s0               0 Jul 28 06:46 f
lrwxrwxrwx. 1 drinkcat drinkcat unconfined_u:object_r:user_home_t:s0 1 Jul 28 06:46 l -> f
prw-r--r--. 1 drinkcat drinkcat unconfined_u:object_r:user_home_t:s0 0 Jul 28 06:46 p

3 issues:

  1. p permissions are wrong, should be prw-rw-rw-., not prw-r--r--..
  2. p SELinux context is wrong
  3. l SELinux context is wrong

For 1+2, this seems to be related to this line: https://github.com/uutils/coreutils/blob/main/src/uu/cp/src/cp.rs#L2594

    } else if source_is_stream && source.exists() {
        // Some stream files may not exist after we have copied it,
        // like anonymous pipes. Thus, we can't really copy its
        // attributes. However, this is already handled in the stream
        // copy function (see `copy_stream` under platform/linux.rs).
    } else {

This was added in #7061 by @DaringCuteSeal, but I think the comment and the if test code say the opposite? Should it be ! source.exists()? (I don't think we have a test for this)

Actually there used to be a copy_atttributes lines here, but that was removed by @sylvestre in #7878 (so now the code within the if block matches the comment: do nothing).

No clue what's happening with 3 (yet).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions