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

RFE: Support for setting seuser in selinux_fcontexts #106

Closed
benblasco opened this issue Apr 28, 2022 · 11 comments
Closed

RFE: Support for setting seuser in selinux_fcontexts #106

benblasco opened this issue Apr 28, 2022 · 11 comments

Comments

@benblasco
Copy link
Contributor

I see a comment in https://github.com/linux-system-roles/selinux/blob/master/tasks/main.yml suggesting that seuser and selevel handling functionality need to be added to this role:

    # FIXME: selevel, seuser

The community sefcontext module handles this by not touching the seuser if none is defined:
https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/sefcontext.py

     if seuser is None:
                seuser = orig_seuser

Is it straightforward enough to add a conditional to this role to not attempt to pass seuser (or selevel) to the sefcontext if the value is not defined? I would be happy to contribute a PR for this functionality if this is what we envisage that the role needs.

@richm
Copy link
Contributor

richm commented Apr 29, 2022

Will this fix the issue?

- name: Set SELinux file contexts
  sefcontext:
    target: "{{ item.target }}"
    setype: "{{ item.setype }}"
    ftype: "{{ item.ftype | default('a') }}"
    state: "{{ item.state | default('present') }}"
    selevel: "{{ item.selevel | default(omit) }}"
    seuser: "{{ item.seuser | default(omit) }}"
  with_items: "{{ selinux_fcontexts }}"

That is - if selevel is not present in item, do not pass it to the sefcontext module.

@benblasco
Copy link
Contributor Author

Yes, "omit" was what I had been looking at.

I can fork, test, and PR if you like!

@richm
Copy link
Contributor

richm commented Apr 29, 2022

Yes, "omit" was what I had been looking at.

I can fork, test, and PR if you like!

Sure - thanks - much appreciated!

@benblasco
Copy link
Contributor Author

According to this document:
"On a targeted system, all users are mapped to the unconfined_u SELinux user. "

https://wiki.gentoo.org/wiki/SELinux/Users_and_logins

If that is the case on Enterprise Linux systems, then there is no need for this RFE. I am exploring further and will advise once I know more.

@bachradsusi
Copy link
Member

When a file is created by a process it inherits user part from the process domain:

[staff@P1 ~]$ id -Z
staff_u:staff_r:staff_t:s0-s0:c0.c1023
[staff@P1 ~]$ touch file
[staff@P1 ~]$ ls -Z file
staff_u:object_r:user_home_t:s0 file

If there's a file contexts matching a file, the user part is reset on restorecon or during relabel:

[root@P1 ~]# touch /etc/remove_me
[root@P1 ~]# ls -Z /etc/remove_me
unconfined_u:object_r:etc_t:s0 /etc/remove_me
[root@P1 ~]# matchpathcon /etc/remove_me 
/etc/remove_me  system_u:object_r:etc_t:s0
[root@P1 ~]# restorecon -Fv /etc/remove_me 
Relabeled /etc/remove_me from unconfined_u:object_r:etc_t:s0 to system_u:object_r:etc_t:s0

In general files contexts define system_u user for files:

# ls -Z /etc/passwd
system_u:object_r:passwd_file_t:s0 /etc/passwd

# semanage fcontext -l | grep /etc/passwd      
/etc/passwd[-\+]?                                  regular file       system_u:object_r:passwd_file_t:s0 

sefcontext.py uses system_u when seuser is not defined as well - https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/sefcontext.py#L195

And it's also important to say that in SELinux policies based on Fedora selinux-policy-targeted, the user part is not used when allow rules are evaluated:

[staff@P1 ~]$ id -Z
staff_u:staff_r:staff_t:s0-s0:c0.c1023
[staff@P1 ~]$ ls -Z unconfined_file 
unconfined_u:object_r:user_home_t:s0 unconfined_file
[staff@P1 ~]$ cat unconfined_file 
unconfined user data

So you would need a policy based on mls and also use it for files like those inside users home.

But given that how simple the patch would be, I would say lets add this to have a complete set of options available.

@richm
Copy link
Contributor

richm commented May 4, 2022

any luck with this?

benblasco pushed a commit to benblasco/selinux that referenced this issue May 4, 2022
See Issue linux-system-roles#106 "RFE: Support for setting seuser in selinux_fcontexts"
linux-system-roles#106
@benblasco
Copy link
Contributor Author

PR submitted for review

#108

@bachradsusi @richm review welcome!

@benblasco
Copy link
Contributor Author

@bachradsusi does it matter at all that this role performs a restorecon rather than a restorecon -F?

@bachradsusi
Copy link
Member

Yes, it should use -F to force the reset of user part.

@benblasco
Copy link
Contributor Author

@bachradsusi Should we be concerned about the implications of switching to -F more broadly?

@bachradsusi
Copy link
Member

I don't think there's any negative implication of switching restorecon -F. targeted policy in Fedora based systems don't use the user and level part. But for Fedora mls policy it's important to reset the full context.

richm pushed a commit to benblasco/selinux that referenced this issue Jul 28, 2022
Added setting of seuser and selevel for completeness
See Issue linux-system-roles#106 "RFE: Support for setting seuser in selinux_fcontexts"
linux-system-roles#106

Added explanation of seuser and selevel parameters

Added -F flag to restorecon to force reset
See "man restorecon" for more detail on -F flag
richm pushed a commit that referenced this issue Jul 28, 2022
Added setting of seuser and selevel for completeness
See Issue #106 "RFE: Support for setting seuser in selinux_fcontexts"
#106

Added explanation of seuser and selevel parameters

Added -F flag to restorecon to force reset
See "man restorecon" for more detail on -F flag

Authored-by: Benjamin Blasco <bblasco@redhat.com>
richm added a commit to richm/linux-system-roles-selinux that referenced this issue Jul 28, 2022
[1.4.0] - 2022-07-28
--------------------

### New Features

- Added setting of seuser and selevel for completeness (linux-system-roles#108)

Added setting of seuser and selevel for completeness
See Issue linux-system-roles#106 "RFE: Support for setting seuser in selinux_fcontexts"
linux-system-roles#106

Added explanation of seuser and selevel parameters

Added -F flag to restorecon to force reset
See "man restorecon" for more detail on -F flag

Authored-by: Benjamin Blasco <bblasco@redhat.com>

### Bug Fixes

- none

### Other Changes

- changelog_to_tag action - support other than "master" for the main branch name, as well (linux-system-roles#117)

- Use GITHUB_REF_NAME as name of push branch; fix error in branch detection [citest skip] (linux-system-roles#118)

We need to get the name of the branch to which CHANGELOG.md was pushed.
For now, it looks as though `GITHUB_REF_NAME` is that name.  But don't
trust it - first, check that it is `main` or `master`.  If not, then use
a couple of other methods to determine what is the push branch.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
richm added a commit that referenced this issue Jul 28, 2022
[1.4.0] - 2022-07-28
--------------------

### New Features

- Added setting of seuser and selevel for completeness (#108)

Added setting of seuser and selevel for completeness
See Issue #106 "RFE: Support for setting seuser in selinux_fcontexts"
#106

Added explanation of seuser and selevel parameters

Added -F flag to restorecon to force reset
See "man restorecon" for more detail on -F flag

Authored-by: Benjamin Blasco <bblasco@redhat.com>

### Bug Fixes

- none

### Other Changes

- changelog_to_tag action - support other than "master" for the main branch name, as well (#117)

- Use GITHUB_REF_NAME as name of push branch; fix error in branch detection [citest skip] (#118)

We need to get the name of the branch to which CHANGELOG.md was pushed.
For now, it looks as though `GITHUB_REF_NAME` is that name.  But don't
trust it - first, check that it is `main` or `master`.  If not, then use
a couple of other methods to determine what is the push branch.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@richm richm closed this as completed Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants