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 1488814 - ftruncate on memfd_create'd fd fails in container, with AVC denial
Summary: ftruncate on memfd_create'd fd fails in container, with AVC denial
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: container-selinux
Version: 7.4
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: rc
: ---
Assignee: Lokesh Mandvekar
QA Contact: atomic-bugs@redhat.com
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2017-09-06 09:13 UTC by Jan Pazdziora (Red Hat)
Modified: 2018-01-25 12:56 UTC (History)
9 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-01-25 12:56:51 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Product Errata RHBA-2018:0195 0 normal SHIPPED_LIVE container-selinux bug fix update 2018-01-25 17:24:12 UTC

Description Jan Pazdziora (Red Hat) 2017-09-06 09:13:50 UTC
Description of problem:

Based on https://github.com/openshift/origin/issues/15723, we have a minimal reproducer where

  fd = memfd_create("Server memfd", MFD_ALLOW_SEALING);
  ret = ftruncate(fd, shm_size);

fails in container on RHEL 7.3 and 7.4 (in 7.1 and 7.2 memfd_create does not seem to be available).

AVC denial is logged and running in Permissive helps.

Version-Release number of selected component (if applicable):

docker-1.12.6-55.gitc4618fb.el7.x86_64
selinux-policy-3.13.1-166.el7_4.4.noarch
container-selinux-2.21-2.gitba103ac.el7.noarch

How reproducible:

Deterministic.

Steps to Reproduce:
1. Have docker running and git installed.
2. git clone https://github.com/voanhduy1512/memfd.git
3. docker build -t memfd memfd
4. docker run --rm memfd

Actual results:

ftruncate(): Permission denied

time->Wed Sep  6 04:48:35 2017
type=PROCTITLE msg=audit(1504687715.130:123): proctitle="/tmp/test_memfd/test"
type=SYSCALL msg=audit(1504687715.130:123): arch=c000003e syscall=77 success=no exit=-13 a0=3 a1=400 a2=400 a3=7ffc1dd94120 items=0 ppid=15307 pid=15322 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="test" exe="/tmp/test_memfd/test" subj=system_u:system_r:svirt_lxc_net_t:s0:c222,c472 key=(null)
type=AVC msg=audit(1504687715.130:123): avc:  denied  { open } for  pid=15322 comm="test" name=6D656D66643A536572766572206D656D6664 dev="tmpfs" ino=49543 scontext=system_u:system_r:svirt_lxc_net_t:s0:c222,c472 tcontext=system_u:object_r:tmpfs_t:s0 tclass=file

Expected results:

success on stdout and not AVC denial.

Additional info:

Running

   docker run --privileged --rm memfd

or

   docker run --security-opt=label=disable --rm memfd

passes.

On Fedora 25 and 26, docker run --rm memfd passes, with versions like

docker-1.13.1-22.gitb5e3294.fc26.x86_64
selinux-policy-3.13.1-260.8.fc26.noarch
container-selinux-2.21-1.fc26.noarch

Comment 3 Daniel Walsh 2017-10-09 12:37:45 UTC
Jan so this is a container creating a tmpfs file? called temp?

Comment 4 Daniel Walsh 2017-10-09 12:41:06 UTC
Stephen is there a way to automatically transition this file to container_tmpfs_t?

Comment 5 Jan Pazdziora (Red Hat) 2017-10-09 13:18:45 UTC
(In reply to Daniel Walsh from comment #3)
> Jan so this is a container creating a tmpfs file? called temp?

Not strictly tmpfs -- memfd_create(2) says

       The memfd_create() system call provides a simple alternative to
       manually mounting a tmpfs(5) filesystem and creating and opening a
       file in that filesystem.  The primary purpose of memfd_create() is to
       create files and associated file descriptors that are used with the
       file-sealing APIs provided by fcntl(2).

The file name given in that https://github.com/voanhduy1512/memfd example is "Server memfd" but it's for debugging purposes only, really.

Comment 6 Daniel Walsh 2017-10-09 13:58:18 UTC
Right, but I see this as a potential SELinux issue, since we really want these things to be labeled based on the creating process.  Currently it looks like they all get the same label, and no easy way to isolate them on creation based on a transition rule.

Comment 7 Stephen Smalley 2017-10-10 13:46:36 UTC
type transition rules should work; internally memfd_create(2) uses shmem_file_setup().

Comment 8 Daniel Walsh 2017-10-10 14:06:08 UTC
I guess if we add this rule then the tmpfs file will be container_file_t.


fs_tmpfs_filetrans(container_t, container_file_t, { dir file })

Jan could you add a test policy to see if this fixes the issue?

Comment 9 Jan Pazdziora (Red Hat) 2017-10-11 09:22:34 UTC
(In reply to Daniel Walsh from comment #8)
> I guess if we add this rule then the tmpfs file will be container_file_t.
> 
> 
> fs_tmpfs_filetrans(container_t, container_file_t, { dir file })
> 
> Jan could you add a test policy to see if this fixes the issue?

I have

container-selinux-2.21-2.gitba103ac.el7.noarch
selinux-policy-devel-3.13.1-166.el7_4.4.noarch
policycoreutils-2.5-17.1.el7.x86_64
selinux-policy-3.13.1-166.el7_4.4.noarch
checkpolicy-2.5-4.el7.x86_64
policycoreutils-python-2.5-17.1.el7.x86_64
policycoreutils-devel-2.5-17.1.el7.x86_64
selinux-policy-targeted-3.13.1-166.el7_4.4.noarch

and I've created bz1488814.te containing

policy_module(bz1488814, 1.0)
fs_tmpfs_filetrans(container_t, container_file_t, { dir file })

and I've run

ln -s /usr/share/selinux/devel/Makefile
make

and got

Compiling targeted bz1488814 module
/usr/bin/checkmodule:  loading policy configuration from tmp/bz1488814.tmp
bz1488814.te:2:ERROR 'unknown type container_file_t' at token ';' on line 3226:
	allow container_file_t tmpfs_t:filesystem associate;
#line 2
/usr/bin/checkmodule:  error(s) encountered while parsing configuration
make: *** [tmp/bz1488814.mod] Error 1

What's the best way to let the module / compile stack know about the container_file_t which I assume comes from container-selinux?

Comment 10 Jan Pazdziora (Red Hat) 2017-10-11 09:33:08 UTC
In the end, I've patched container-selinux-2.21-2.gitba103ac.el7.src.rpm with

# cat ~/rpmbuild/SOURCES/bz1488814.patch
diff -ru container-selinux-ba103acc2c20164e3b566e966c280364bc0d99f5.orig/container.te container-selinux-ba103acc2c20164e3b566e966c280364bc0d99f5/container.te
--- container-selinux-ba103acc2c20164e3b566e966c280364bc0d99f5.orig/container.te	2017-08-08 16:14:11.000000000 -0400
+++ container-selinux-ba103acc2c20164e3b566e966c280364bc0d99f5/container.te	2017-10-11 05:25:32.294362007 -0400
@@ -745,3 +745,5 @@
 	allow svirt_lxc_net_t self:capability mknod;
 #	allow svirt_lxc_net_t self:cap_userns mknod;
 ')
+
+fs_tmpfs_filetrans(svirt_lxc_net_t, svirt_sandbox_file_t, { dir file })

and recompiled and upgraded.

I confirm that after that, docker run passes.

Comment 11 Daniel Walsh 2017-10-11 12:04:14 UTC
Sorry about that. I can not wait until RHEL7.5 (Hopefully) and we can finally start calling svirt_sandbox_file_t container_file_t.

Comment 12 Daniel Walsh 2017-10-11 16:47:30 UTC
Fixed in container-selinux-2.30

Comment 13 Jan Pazdziora (Red Hat) 2017-10-11 18:35:44 UTC
Fixing the component change.

Comment 15 Luwen Su 2018-01-08 08:11:12 UTC
# docker run --rm memfd
success

# rpm -q container-selinux
container-selinux-2.38-1.git8b2b119.el7.noarch

Move to verified.

Comment 18 errata-xmlrpc 2018-01-25 12:56:51 UTC
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.

https://access.redhat.com/errata/RHBA-2018:0195


Note You need to log in before you can comment on or make changes to this bug.