Skip to contentSkip to conversation

Daemon cannot execute python

DISCUSSION Ian Pilcher 2 participants 8 messages Apr 29, 2020 – Apr 30, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

Over the years, I've written several Python-based daemons for my home network. I've also written SELinux policies for these daemons.

After a recent CentOS 7 update, which includes selinux-policy-targeted-3.13.1-266.el7.noarch, these daemons are failing to start:

type=AVC msg=audit(1588171416.424:157): avc: denied { execute } for pid=3359 comm="denatc" path="/usr/bin/python2.7" dev="dm-0" ino=12679476 scontext=system_u:system_r:denatc_t:s0 tcontext=system_u:object_r:bin_t:s0 tclass=file permissive=0

For some reason, these policies worked in the past without including specific permission to execute bin_t files (something that I'd prefer to avoid, as it's awfully broad).

Does anyone have any idea what changed (i.e. why did this work before)?

Is there any way to make things work other than giving any Python-based daemon permission to execute *any* bin_t file?

Thanks!

Signature · 3 lines
========================================================================
                 In Soviet Russia, Google searches you!
========================================================================
maintainer
#1 lore raw
Quoted 13 lines · Ian Pilcher

Over the years, I've written several Python-based daemons for my home network. I've also written SELinux policies for these daemons.

After a recent CentOS 7 update, which includes selinux-policy-targeted-3.13.1-266.el7.noarch, these daemons are failing to start:

   type=AVC msg=audit(1588171416.424:157): avc:  denied  { execute } for
   pid=3359 comm="denatc" path="/usr/bin/python2.7" dev="dm-0"
   ino=12679476 scontext=system_u:system_r:denatc_t:s0
   tcontext=system_u:object_r:bin_t:s0 tclass=file permissive=0
Quoted 8 lines

For some reason, these policies worked in the past without including specific permission to execute bin_t files (something that I'd prefer to avoid, as it's awfully broad).

Does anyone have any idea what changed (i.e. why did this work before)?

Is there any way to make things work other than giving any Python-based daemon permission to execute *any* bin_t file?

Sounds similar to
https://lore.kernel.org/selinux/[email protected]/,
which may be due to a kernel change outside SELinux as per that thread.

It is logically correct since the new domain is executing from the interpreter. Note that it cannot execve() arbitrary bin_t files without execute_no_trans permission,
although it can open/mmap PROT_EXEC them with execute permission.

You can reduce the scope by defining and assigning a specific type to /usr/bin/python2.7
but obviously that will have a rippling impact on the rest of the policy.

#2 lore raw
Quoted 3 lines · Stephen Smalley

Sounds similar to
https://lore.kernel.org/selinux/[email protected]/,
which may be due to a kernel change outside SELinux as per that thread.

Yes. That's exactly it.

Quoted 1 line

It is logically correct since the new domain is executing from the interpreter.

Indeed. I was quite puzzled about how it ever worked.

Quoted 3 lines

You can reduce the scope by defining and assigning a specific type to /usr/bin/python2.7
but obviously that will have a rippling impact on the rest of the policy.

That would undoubtedly be painful! For now, I've modified my systemd service file to make a copy of the Python executable with the required context, i.e.:

[Service]
Type=simple
PrivateTmp=true
ExecStartPre=/usr/bin/cp /usr/bin/python2 /tmp/python.denatc ExecStartPre=/usr/bin/chcon -t denatc_exec_t /tmp/python.denatc ExecStart=/tmp/python.denatc /usr/local/bin/denatc -d ExecStartPost=/usr/bin/rm /tmp/python.denatc

Thanks!

Signature · 3 lines
========================================================================
                 In Soviet Russia, Google searches you!
========================================================================
#3 lore raw
Quoted 11 lines · Ian Pilcher

That would undoubtedly be painful!  For now, I've modified my systemd service file to make a copy of the Python executable with the required context, i.e.:

  [Service]
  Type=simple
  PrivateTmp=true
  ExecStartPre=/usr/bin/cp /usr/bin/python2 /tmp/python.denatc ExecStartPre=/usr/bin/chcon -t denatc_exec_t /tmp/python.denatc ExecStart=/tmp/python.denatc /usr/local/bin/denatc -d ExecStartPost=/usr/bin/rm /tmp/python.denatc

Slight update for posterity. It looks like it's possible to use a symbolic link, so ...

[Service]
Type=simple
PrivateTmp=true
ExecStartPre=/usr/bin/ln -s /usr/bin/python2 /tmp/python.denatc ExecStartPre=/usr/bin/chcon --reference=/usr/local/bin/denatc
--no-dereference /tmp/python.denatc
ExecStart=/tmp/python.denatc /usr/local/bin/denatc -d

(Deleting the copy of the interpreter seems to be racy, so it's probably best to rely on systemd to delete the whole private temporary directory.)

Signature · 4 lines
========================================================================
Ian Pilcher                                         [email protected]
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================
maintainer
#4 lore raw
Quoted 17 lines · Ian Pilcher
Quoted 11 lines · Ian Pilcher

That would undoubtedly be painful! For now, I've modified my systemd service file to make a copy of the Python executable with the required context, i.e.:

[Service]
Type=simple
PrivateTmp=true
ExecStartPre=/usr/bin/cp /usr/bin/python2 /tmp/python.denatc ExecStartPre=/usr/bin/chcon -t denatc_exec_t /tmp/python.denatc ExecStart=/tmp/python.denatc /usr/local/bin/denatc -d ExecStartPost=/usr/bin/rm /tmp/python.denatc

Slight update for posterity. It looks like it's possible to use a symbolic link, so ...

Quoted 7 lines

[Service]
Type=simple
PrivateTmp=true
ExecStartPre=/usr/bin/ln -s /usr/bin/python2 /tmp/python.denatc ExecStartPre=/usr/bin/chcon --reference=/usr/local/bin/denatc
--no-dereference /tmp/python.denatc
ExecStart=/tmp/python.denatc /usr/local/bin/denatc -d

I don't see how that could work. Symbolic link should be resolved and its context only ever used to determine whether you could follow/read it. Not for the execute check.

#5 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 2 lines · Ian Pilcher

Slight update for posterity. It looks like it's possible to use a symbolic link, so ...

I don't see how that could work. Symbolic link should be resolved and its context only ever used to determine whether you could follow/read it. Not for the execute check.

I can't speak to how it works, but it does work on CentOS 7.8. I suppose it's entirely possible that it would fail on a more up-to-date distribution.

Signature · 3 lines
========================================================================
                 In Soviet Russia, Google searches you!
========================================================================
#6 lore raw
Quoted 5 lines · Ian Pilcher
Quoted 4 lines · Stephen Smalley
Quoted 2 lines · Ian Pilcher

Slight update for posterity.  It looks like it's possible to use a symbolic link, so ...

Quoted 9 lines
Quoted 4 lines

I don't see how that could work.  Symbolic link should be resolved and its context only ever used to determine whether you could follow/read it. Not for the execute check.

I can't speak to how it works, but it does work on CentOS 7.8.  I suppose it's entirely possible that it would fail on a more up-to-date distribution.

I was incorrect. It doesn't work.

The service does start, but it's running as unconfined_service_t (which makes even less sense to me). So back to making an actual copy of the interpreter.

Signature · 4 lines
========================================================================
Ian Pilcher                                         [email protected]
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================
maintainer
#7 lore raw
Quoted 19 lines · Ian Pilcher
Quoted 12 lines · Ian Pilcher
Quoted 10 lines · Stephen Smalley
Quoted 2 lines · Ian Pilcher

Slight update for posterity. It looks like it's possible to use a symbolic link, so ...

I don't see how that could work. Symbolic link should be resolved and its
context only ever used to determine whether you could follow/read it. Not
for the execute check.

Quoted 4 lines

I can't speak to how it works, but it does work on CentOS 7.8. I suppose it's entirely possible that it would fail on a more up-to-date distribution.

Quoted 5 lines

I was incorrect. It doesn't work.

The service does start, but it's running as unconfined_service_t (which makes even less sense to me). So back to making an actual copy of the interpreter.

That makes sense to me. The targeted policy in CentOS defaults to transitioning to unconfined_service_t
for services that lack a specific domain/policy. The context of the symbolic link is irrelevant for the execve
because the link is read and resolved to the regular executable file to which it refers before we even look at its context for transition purposes, just as with setuid/setgid bits or file capabilities.