Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1045122

Summary: cp -a messes up destination selinux contexts for existing directories
Product: Red Hat Enterprise Linux 7 Reporter: Michal Trunecka <mtruneck>
Component: coreutilsAssignee: Ondrej Vasik <ovasik>
Status: CLOSED CURRENTRELEASE QA Contact: Tomas Dolezal <todoleza>
Severity: urgent Docs Contact:
Priority: urgent    
Version: 7.0CC: dwalsh, ebenes, jscotka, ksrot, mfranc, mmalik, mtruneck, ovasik, pbrady, sgraf, todoleza
Target Milestone: rcKeywords: Regression, TestBlocker
Target Release: 7.0   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: coreutils-8.22-5.el7 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2014-06-13 11:16:49 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:
Bug Depends On:    
Bug Blocks: 798670    
Attachments:
Description Flags
Strace output of the reproducing cp command
none
proposed fix none

Description Michal Trunecka 2013-12-19 16:30:01 UTC
Description of problem:

This is a regression in coreutils version 8.22-1

In our tests, we have backups like this, with given (correctly backed up) contexts:

/var/tmp/beakerlib-meTuGkC/backup-krb5/var/kerberos/krb5kdc/kdc.conf
                  root_t---^           ^      ^        ^        ^
                             var_t-----^      ^        ^        ^
                             var_t------------^        ^        ^
                                       krb5kdc_conf_t--^--------^

But after restoring the backup, which is done by 

cp -fa /var/tmp/beakerlib-meTuGkC/backup-krb5/var  /

it assigns the krb5kdc_conf_t context to the whole destination path! It means that /var has this context and the whole system is broken by this.



Version-Release number of selected component (if applicable):
coreutils-8.22-1.el7.ppc64

How reproducible:
always


Expected results:

cp -fa should preserve the original contexts

Comment 1 Ondrej Vasik 2013-12-19 19:06:38 UTC
Thanks Michal for looking at this - adding Dan Walsh to CC - as he is the author of most of the upstream changes...
There is one divergency in SELinux copy mechanism in downstream SELinux coreutils-8.22 patch - so pasting it here to be sure. I'll try to analyze it while on Christmas break.
diff -urNp coreutils-8.21-orig/src/copy.c coreutils-8.21/src/copy.c
--- coreutils-8.21-orig/src/copy.c	2013-02-07 10:37:05.000000000 +0100
+++ coreutils-8.21/src/copy.c	2013-02-15 14:31:58.941467872 +0100
@@ -2315,6 +2315,8 @@ copy_internal (char const *src_name, cha
         {
           /* Here, we are crossing a file system boundary and cp's -x option
              is in effect: so don't copy the contents of this directory. */
+        if (x->preserve_security_context)
+           restore_default_fscreatecon_or_die ();
         }
       else
         {

Comment 3 Daniel Walsh 2013-12-20 16:13:06 UTC
Are you saying we added or removed

+        if (x->preserve_security_context)
+           restore_default_fscreatecon_or_die ();

Comment 4 Ondrej Vasik 2013-12-20 16:28:52 UTC
No, this is part of our downstream patch which is in Fedora/RHEL for ages. As most of the things are already accepted upstream, this one is the only remaining chunk in copy.c - probably it is not connected to the issue.

Comment 5 Ondrej Vasik 2013-12-20 17:58:04 UTC
Is this specific for ppc64? It seems to work properly on my machine (ok, compiled coreutils-8.22 on F20, I haven't tried the rhel7 machine yet). Can you please provide strace of the failure? TIA.

Comment 6 Daniel Walsh 2013-12-20 20:33:27 UTC
Then the problem looks like it is calling setfscreatecon(krb5kdc_conf_t) before to create the target file but not for the intermediate directories.  



/var/tmp/beakerlib-meTuGkC/backup-krb5/var/kerberos/krb5kdc/kdc.conf
                  root_t---^           ^      ^        ^        ^
                             var_t-----^      ^        ^        ^
                             var_t------------^        ^        ^
                                       krb5kdc_conf_t--^--------^
IE is should be calling

setfsreatecon(root_t); mkdir backup-krb5
 setfsreatecon(var_t); mkdir var
 setfsreatecon(var_t); mkdir kerberos
 setfsreatecon(krb5kdc_conf_t); mkdir krb5kdc
 setfsreatecon(krb5kdc_conf_t); create kdc.conf

Comment 7 Michal Trunecka 2014-01-02 09:04:15 UTC
Created attachment 844440 [details]
Strace output of the reproducing cp command

Yes, it is reproducible on x86_64 too, attached strace of the command.

# rpm -q coreutils
coreutils-8.22-1.el7.x86_64

Comment 8 Ondrej Vasik 2014-01-02 09:28:22 UTC
Hmmms, from strace it clearly calls setxattr "unconfined_u:object_r:krb5kdc_conf_t:s0" on /var ... strange - it really works just fine on my F20 x86_64 machine with the same coreutils build. I'm going to compare the straces. One suspicious thing - warnings about missing /var/run/setrans/.setrans-unix socket file - is mcstrans service running?

Comment 9 Michal Trunecka 2014-01-02 09:41:35 UTC
No, mcstrans was not even installed. But the bug is still reproducible after installing and running mcstrans daemon.

Comment 10 Ondrej Vasik 2014-01-02 09:55:25 UTC
Ok, now I see - my reproducer on F20 was "slightly modified" - copying the output to non-existing directory - that worked (-f was not in action). I'm able to reproduce it now - it seems to correctly copy the context if the file/directory doesn't exist before the command, it screws up the context if the directory existed before the command. Further investigating.

Comment 11 Ondrej Vasik 2014-01-02 10:19:04 UTC
I believe this is caused by this chunk from the http://git.savannah.gnu.org/cgit/coreutils.git/commit/src/copy.c?id=7958a4a4fe234f9787daf178a60bc83449605dac ... as -a turns on the x->preserve_security_context ... 

@@ -2521,6 +2596,19 @@ copy_internal (char const *src_name, char const *dst_name,
goto un_backup;
}
+ /* With -Z or --preserve=context, set the context for existing files.
+ Note this is done already for copy_reg() for reasons described therein. */
+ if (!new_dst && !x->copy_as_regular
+ && (x->set_security_context || x->preserve_security_context))
+ {
+ if (! set_file_security_ctx (dst_name, x->preserve_security_context,
+ false, x))
+ {
+ if (x->require_preserve_context)
+ goto un_backup;
+ }
+ }
+
if (command_line_arg && x->dest_info)
{
/* Now that the destination file is very likely to exist,

Adding upstream maintainer to CC to let him notified about this regression.
I believe "|| x->require_preserve_context" should be used there. Anyway - this chunk is probably wrong in general, as it sets the context of all recursive subdirs to the context of destination file.

Comment 12 Pádraig Brady 2014-01-02 14:22:47 UTC
Ugh. What created the backup BTW? I'm not sure that any version of `cp -a` copies intermediate directory contexts. Anyway looking at this now...

Comment 13 Michal Trunecka 2014-01-02 14:26:37 UTC
The backup was not created by single cp command, but by Beaker rlFileBackup function, which creates the path using mkdir and assigns the contexts using chcon.

Comment 14 Pádraig Brady 2014-01-02 17:02:38 UTC
Created attachment 844620 [details]
proposed fix

I need to add a test for this before pushing upstream, but it should fix this issue.

I also noticed the related issue that the context is not set for new
dirs when using --parents

Comment 15 Ondrej Vasik 2014-01-02 17:53:32 UTC
Thanks Pádraig...

Comment 18 Michal Trunecka 2014-01-03 12:15:56 UTC
I can confirm that the scenario in the Bug Description works as expected now with coreutils-8.22-5.el7.x86_64.

Comment 25 Pádraig Brady 2014-01-07 18:09:39 UTC
The proposed upstream commit also includes a test
http://lists.gnu.org/archive/html/coreutils/2014-01/msg00012.html

Comment 27 Ludek Smid 2014-06-13 11:16:49 UTC
This request was resolved in Red Hat Enterprise Linux 7.0.

Contact your manager or support representative in case you have further questions about the request.