2

I have a server that runs user submitted code, for evaluation. I wrote a systemd service that starts a python application, that application then run the submitted code using SElinux sandbox.

The sandbox fails to start with the following error

/usr/bin/sandbox: User account must be setup with an MCS Range 

However when I start my server from the command line as a normal user, instead of a systemd service launching it, it works without issues.

This is the output from semanage login -l

Login Name SELinux User MLS/MCS Range Service __default__ unconfined_u s0-s0:c0.c1023 * root unconfined_u s0-s0:c0.c1023 * system_u system_u s0-s0:c0.c1023 * 

My knowledge of SELinux is very limited, Since it works when launched by a normal user I am guessing this has something to do with system_u?

Running Fedora 20.

The python application is actually a celery worker.

1 Answer 1

4

There are two issues that cause this to fail.

TL;DR

To get this to work, you need to:

  1. Run sandbox with a --level option.
  2. Install the policy I show below.

Problem 1: MCS Ranges

When you start the service from the command line as a normal user, you are probably running in the following SELinux context. You can verify with id -Z.

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 

There are five parts to that context.

  1. Your SELinux user (unconfined_u)
  2. Your role (unconfined_r)
  3. Your domain (unconfined_t)
  4. Your MLS range (s0-s0)
  5. Your MCS range (c0.c1023)

By default, the latest version of sandbox requires that the user executing the sandbox command has an MCS range defined (see commit 78b077c and commit 6c2ad1c from 2011). When you're running as the normal user, everything's OK, because you have an MCS range defined. However, look at the context that systemd services run in by default. (I got this by making a script that printed out its SELinux context to the syslog.)

system_u:system_r:unconfined_service_t:s0 

Whoops! We don't have an MCS range! This is why you got your error when running sandbox in a systemd service.

Fortunately, sandbox has a command line flag that you can use to explicitly set the MLS and MCS parts of the execution context: --level. That is, when you run

sandbox --level "s0" /path/to/my/command 

then sandbox will no longer attempt to extract your current context's MCS range.

Problem 2: Sandbox Domain Pairings

If you make the above change and try re-running your service, however, you will get a new error.

/usr/bin/sandbox: Could not set exec context to system_u:system_r:sandbox_t:s0. Invalid argument

That error means that SELinux will not let you transition from the systemd context to the sandbox context. The reason for this is the pairings between the two different roles (system_r/unconfined_r) and the sandbox domain (sandbox_t).

The command seinfo -rXXXXX -x shows you a list of the domain pairings that are legal with the role "XXXXX". Let's look for sandbox_t.

$ sudo seinfo -runconfined_r -x | grep sandbox_t chrome_sandbox_t sandbox_t $ sudo seinfo -rsystem_r -x | grep sandbox_t sshd_sandbox_t chrome_sandbox_t 

So, "sandbox_t" is available for pairing with "unconfined_r" but not "system_r". I don't know why this is the case; my best guess is that the Red Hat people wrote sandbox with the intention of only normal users running it. Fortunately, it's reasonably easy to add a pairing between "system_r" and "sandbox_t". Create a policy file (*.te extension) with the following content.

policy_module(sandbox_system, 1.0); require { type sandbox_t; } role system_r types sandbox_t; 

If you name the file "sandbox_system.te", you can install it by running the following commands.

$ make -f /usr/share/selinux/devel/Makefile sandbox_system.pp $ sudo semodule -i sandbox_system.pp 

Now, if you re-run seinfo, you should see the correct pairing.

$ sudo seinfo -rsystem_r -x | grep sandbox_t sshd_sandbox_t chrome_sandbox_t sandbox_t 

Depending on your setup, you may need to add a few more rules to your policy file, but from this point forward, setroubleshoot and audit2allow will be able to do most of the work.

I hope this helps someone!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.