This service will be undergoing maintenance at 00:00 UTC, 2016-08-01. It is expected to last about 1 hours

Bug 747345

Summary: semanage is changing permissions of /etc/selinux files
Product: Red Hat Enterprise Linux 6 Reporter: Miroslav Vadkerti <mvadkert>
Component: libsemanageAssignee: Daniel Walsh <dwalsh>
Status: CLOSED ERRATA QA Contact: Miroslav Vadkerti <mvadkert>
Severity: urgent Docs Contact:
Priority: urgent    
Version: 6.2CC: dwalsh, ebenes, eparis, jrieden, ksrot, mgrepl, mmalik, sds, sgrubb, tmraz
Target Milestone: rcKeywords: TestBlocker
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: libsemanage-2.0.43-4.1.el6 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2011-12-06 14:04:13 EST Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Bug Depends On:    
Bug Blocks: 584498, 750914, 846801, 846802    
Attachments:
Description Flags
Save, clear, and restore umask around file creation by libsemanage. none

Description Miroslav Vadkerti 2011-10-19 10:51:56 EDT
Description of problem:
During development of CC tests we found an issue with semanage. It is changing permissions of /etc/selinux files according to the set umask. This has large side effects, e.g. with umask 027 it makes restorecon unusable for ordinary users. This bug was found on an MLS machine applies generally.

Reproducer:
# umask
0027
# ll /etc/selinux/mls/contexts/files/file_contexts
-rw-r--r--. 1 root root 224235 Oct 19 09:29 /etc/selinux/mls/contexts/files/file_contexts
# mkdir /d
# semanage fcontext -a -t home_root_t /d
# ll /etc/selinux/mls/contexts/files/file_contexts
-rw-r-----. 1 root root 224235 Oct 19 09:38 /etc/selinux/mls/contexts/files/file_contexts

Version-Release number of selected component (if applicable):
policycoreutils-python-2.0.83-19.16.el6

How reproducible:
100%

Steps to Reproduce:
see description
  
Actual results:
semanage changes file permissions

Expected results:
semanage doesn't change file permissions

Additional info:
I can reproduce this also in policycoreutils-python from RHEL6.0:
policycoreutils-python-2.0.83-19.1.el6.x86_64
Comment 1 Daniel Walsh 2011-10-19 11:08:53 EDT
I am not sure if this is a bug or not.  Users generally should not be running restorecon. Currently we do not have hard coded permissions for these files.
Comment 2 Miroslav Vadkerti 2011-10-19 11:19:48 EDT
This is an issue with semanage changing permissions on several files in /etc/selinux according to umask. Is this on purpose?

For example if user wants to use ssh-copy-id he ends up with permission denied because it runs restorecon:
# grep -n restorecon $(which ssh-copy-id)
41:{ eval "$GET_ID" ; } | ssh $1 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys; test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys" || exit 1
Comment 3 Miroslav Grepl 2011-10-19 11:35:02 EDT
I would say, if an admin sets umask, then probably is expected the restorecon won't work for users.
Comment 4 Miroslav Vadkerti 2011-10-19 11:48:58 EDT
If this won't be fixed in semanage, we will need to fix openssh.
Comment 5 Stephen Smalley 2011-10-19 12:22:12 EDT
Maybe libsemanage should save and restore umask() when creating files?  It already allows file mode to be specified via semanage.conf, but doesn't save/restore umask and thus is subject to caller's umask.
Comment 6 Stephen Smalley 2011-10-19 12:40:41 EDT
I'd say this is similar to the situation when someone runs adduser and /etc/passwd gets re-created; the permissions of /etc/passwd aren't subject to the caller's umask, right?  So libsemanage ought to save and clear the umask before creating files and restore it afterward.  Simple patch to semanage_copy_file().
Comment 7 Eduard Benes 2011-10-19 12:43:04 EDT
(In reply to comment #5)
> Maybe libsemanage should save and restore umask() when creating files?  It
> already allows file mode to be specified via semanage.conf, but doesn't
> save/restore umask and thus is subject to caller's umask.

I agree, you are reading my thoughts. I think that unless there is another way how to preserve the correct contexts for ssh-copy-id this is the most appropriate solution.
Comment 8 Stephen Smalley 2011-10-19 12:57:26 EDT
Created attachment 529059 [details]
Save, clear, and restore umask around file creation by libsemanage.
Comment 9 Tomas Mraz 2011-10-19 14:20:13 EDT
Or it should copy the permissions from the original file with chmod() call.
Comment 14 Daniel Walsh 2011-10-20 15:34:15 EDT
Patch applied in libsemanage-2.0.45-1.1.el6
Comment 19 errata-xmlrpc 2011-12-06 14:04:13 EST
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

http://rhn.redhat.com/errata/RHBA-2011-1770.html