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 1378311

Summary: virt-sysprep --ssh-inject failed to create the right selinux context for .ssh in the guest
Product: Red Hat Enterprise Linux 7 Reporter: Bin Han <bihan>
Component: libguestfsAssignee: Richard W.M. Jones <rjones>
Status: CLOSED NOTABUG QA Contact: Virtualization Bugs <virt-bugs>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 7.3CC: bihan, leiwang, ptoscano
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-09-27 07:41:14 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 Bin Han 2016-09-22 05:43:13 UTC
Description of problem:
Can't use SSH to login the guest without password when SELinux is enforcing

Version-Release number of selected component (if applicable):
libguestfs-1.32.7-2.el7.x86_64


How reproducible:
100%

Steps to Reproduce:
1. Prepare a RHEL guest image:
RHEL-Server-7.2-64-hvm.raw2
2. Create a tmp.qcow2 image to record only the differences from backing_file:
#qemu-img create -f qcow2 -o backing_file=RHEL-Server-7.2-64-hvm.raw tmp.qcow2
3.Inject your public key and a string to the guest:
# virt-sysprep -a tmp.qcow2 --ssh-inject root:file:/root/.ssh/id_rsa.pub  --ssh-inject root:string:"ssh-rsa AAtesttesttest"
Note:
You can also create new emptykey by:
# ssh-keygen -f emptykey
Press twice, you'll get:
Private key : emptkey (used to ssh login with -i)
Public key: emtykey.pub (used to instead of /root/.ssh/id_rsa.pub)

4.Boot the guest image , get the ip info, and ssh to it using you own private key:
# ssh root@$ip


Actual results:
Can't use SSH to login the guest without password when SELinux is enforcing.
If run the command "setenfoce 0" in guest to change the SELinux mode to permissive, then try to login again without password. It successes.

Expected results:
Can use SSH to login the guest without password when SELinux is enforcing


Additional info:
virt-customize has the same problem.

Comment 2 Pino Toscano 2016-09-22 07:41:49 UTC
Hi Bin Han,

(In reply to Bin Han from comment #0)
> 3.Inject your public key and a string to the guest:
> # virt-sysprep -a tmp.qcow2 --ssh-inject root:file:/root/.ssh/id_rsa.pub 
> --ssh-inject root:string:"ssh-rsa AAtesttesttest"

As also described in the documentation of virt-sysprep, you need to use --selinux-relabel when editing guests with SELinux enabled, at least with the invocation before the actual boot (in this case it's one, so here).
This applies to virt-customize, virt-builder, and virt-sysprep.

Comment 3 Richard W.M. Jones 2016-09-22 08:00:50 UTC
As well as doing what Pino says, please try the RHEL 7.3 packages since
some other bugs were fixed in both SSH key injection and SELinux
relabelling:

https://people.redhat.com/~rjones/libguestfs-RHEL-7.3-preview/

Comment 4 Richard W.M. Jones 2016-09-22 08:01:30 UTC
(In reply to Richard W.M. Jones from comment #3)
> As well as doing what Pino says, please try the RHEL 7.3 packages since
> some other bugs were fixed in both SSH key injection and SELinux
> relabelling:
> 
> https://people.redhat.com/~rjones/libguestfs-RHEL-7.3-preview/

Ah, I see that you're using these packages or RHEL 7.3
already ..   Carry on.

Comment 5 Pino Toscano 2016-09-26 12:57:49 UTC
Please check whether using --selinux-relabel works (it should).

Comment 6 Bin Han 2016-09-27 03:11:52 UTC
(In reply to Pino Toscano from comment #5)
> Please check whether using --selinux-relabel works (it should).

yes,if relabel the selinux context, it works. Can login without password. The changing steps show below.

steps:
#chcon -t ssh_home_t /root/.ssh/authorized_keys
# ls -Z /root/.ssh/authorized_keys
-rw-------. root root system_u:object_r:ssh_home_t:s0 /root/.ssh/authorized_keys

Comment 7 Bin Han 2016-09-27 06:01:24 UTC
(In reply to Pino Toscano from comment #5)
> Please check whether using --selinux-relabel works (it should).

I add --selinux-relabel into the command "virt-sysprep --selinux-relabel -a tmp.qcow2 --ssh-inject root:file:/root/.ssh/id_rsa.pub  --ssh-inject root:string:"ssh-rsa AAtesttesttest". It works well. It can relabel the selinux context. So the problem isn't a bug ? But I also have anther problem why not set the parameter --selinux-relabel as default setting.

Comment 8 Pino Toscano 2016-09-27 07:41:14 UTC
(In reply to Bin Han from comment #7)
> (In reply to Pino Toscano from comment #5)
> > Please check whether using --selinux-relabel works (it should).
> 
> I add --selinux-relabel into the command "virt-sysprep --selinux-relabel -a
> tmp.qcow2 --ssh-inject root:file:/root/.ssh/id_rsa.pub  --ssh-inject
> root:string:"ssh-rsa AAtesttesttest". It works well. It can relabel the
> selinux context.

Good thing, thanks for checking.

> So the problem isn't a bug ?

Indeed, this is not an issue. If you have are modifying a guest with SELinux enabled, you must use --selinux-relabel in the last libguestfs command before you boot it.

> But I also have anther problem
> why not set the parameter --selinux-relabel as default setting.

See bug 1340312, comment 3, for some data points regarding this.