Skip to content

eBPF-based security platform

upatches/app-sandbox: name the executables of PHP-FPM by a pattern

John Fastabenda883e7999ad1
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 18d4d187..c45f3013 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -945,7 +945,8 @@ signed config image.
sandbox without a ``[[upatches.policy]]`` table, ``put-shields``
and ``shields-put``, ``bpm tree append`` by reference and
``--package-digest``, the node loading it at seq 1 and labeling
- the sandbox's files, a labeled program denied the executions,
+ the sandbox's files, those a pattern of the Beefile matches and
+ not the target of a symbolic link it matches, a labeled program denied the executions,
connections, datagrams, raw socket and ``.php`` creations,
renames and writes its sandbox rules out and allowed the rest, an
unlabeled program denied nothing, then ``--op delete`` and the
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 8e883671..1643fa8f 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -1432,16 +1432,19 @@ it publishes is the shipped one with ``probe.Beefile`` as its Beefile
and the rules of :src:`testing/fixtures/app-sandbox/`, whose one
sandbox, ``probe``,
labels a file of the test: a copy of the node's ``python3`` at
-``/tmp/shirudo-app-sandbox/app``. It may execute the file labeled
-``probe-helpers`` beside it, reach the loopback networks, and not
-create, rename or write ``*.php``. That copy runs
-:src:`testing/end2end/app_sandbox_probe.py`, which tries 26 operations
+``/tmp/shirudo-app-sandbox/app``. It may execute the files labeled
+``probe-helpers`` beside it, which the Beefile names by the pattern
+``helper*``, reach the loopback networks, and not create, rename or
+write ``*.php``. That copy runs
+:src:`testing/end2end/app_sandbox_probe.py`, which tries 28 operations
and prints which of them the kernel denied. The test:
#. checks that the node has no tree state and that the registry holds
neither the tree nor an index, and empties the keepalive backend;
-#. copies the application, a program it may execute and one it may not
- to the node, and runs the probe: nothing is denied;
+#. copies the application, two programs it may execute, ``helper-a``
+ and ``helper-b``, and one it may not, ``other``, to the node, with a
+ symbolic link ``helper-link`` to the last, and runs the probe:
+ nothing is denied;
#. builds the package as it ships and reads the rules of its two
sandboxes back with ``bpm info data``, and checks that ``bpm build``
fails for rules that name a sandbox the Beefile has no policy for;
@@ -1453,13 +1456,15 @@ and prints which of them the kernel denied. The test:
reads it back with ``bpm tree show`` and loads it with the ingest
job;
#. syncs the node, which reaches seq 1, pins the upatch and labels the
- application with policy bit ``0x1`` and the program it may execute
- with ``0x2``;
-#. runs the probe in the application: 16 operations are denied, the
- other program, the connections and datagrams to addresses outside
- the loopback networks, a raw socket and each way to create, rename,
- write or truncate a ``.php`` file, and 10 are allowed, among them the
- helper, the loopback and reading a ``.php`` file;
+ application with policy bit ``0x1`` and both programs the pattern
+ matches with ``0x2``, and not ``other``, though the link to it
+ matches too;
+#. runs the probe in the application: 17 operations are denied, the
+ other program by its name and by the link, the connections and
+ datagrams to addresses outside the loopback networks, a raw socket
+ and each way to create, rename, write or truncate a ``.php`` file,
+ and 11 are allowed, among them both helpers, the loopback and reading
+ a ``.php`` file;
#. runs the probe with the node's own ``python3``, which no policy
labels: nothing is denied;
#. sends a keepalive and checks ``5 head: pass, seq 1 of 1`` and ``6
diff --git a/Documentation/upatches/learn/examples.rst b/Documentation/upatches/learn/examples.rst
index 68dd538c..27476b97 100644
--- a/Documentation/upatches/learn/examples.rst
+++ b/Documentation/upatches/learn/examples.rst
@@ -143,7 +143,8 @@ app-sandbox
* - Policy labels
- one ``[[upatches.policy]]`` table per sandbox, ``apache`` and
``php-fpm``, each a bit and the executables that put a task in
- the sandbox
+ the sandbox, those of PHP-FPM by the pattern
+ ``/usr/sbin/php-fpm*``
* - Generated data
- ``sandbox_policy``, which ``gen_policy.py`` writes from
``sandbox.toml`` and the policy bits of the Beefile
diff --git a/testing/end2end/app_sandbox_probe.py b/testing/end2end/app_sandbox_probe.py
index 4610626f..0dd185cd 100644
--- a/testing/end2end/app_sandbox_probe.py
+++ b/testing/end2end/app_sandbox_probe.py
@@ -69,7 +69,10 @@ def checks(work):
return os.path.join(work, "files", name)
return [
- ("exec-helper", lambda: execute(os.path.join(work, "helper"))),
+ ("exec-helper-a", lambda: execute(os.path.join(work, "helper-a"))),
+ ("exec-helper-b", lambda: execute(os.path.join(work, "helper-b"))),
+ ("exec-helper-link",
+ lambda: execute(os.path.join(work, "helper-link"))),
("exec-other", lambda: execute(os.path.join(work, "other"))),
("connect-loopback", lambda: connect(socket.AF_INET, "127.0.0.1")),
("connect-loopback-v6", lambda: connect(socket.AF_INET6, "::1")),
diff --git a/testing/end2end/t9114-app-sandbox.sh b/testing/end2end/t9114-app-sandbox.sh
index 9faf5480..04d6c7ed 100755
--- a/testing/end2end/t9114-app-sandbox.sh
+++ b/testing/end2end/t9114-app-sandbox.sh
@@ -5,7 +5,7 @@
# t9114 -- the app-sandbox upatch through the whole pipeline. The
# operator builds the package of upatches/app-sandbox, with probe.Beefile
# and the rules of fixtures/app-sandbox, which sandbox a program of this
-# test, publishes it to a namespace, where the backend lists it as
+# test and name the programs it may execute by a pattern, publishes it to a namespace, where the backend lists it as
# available, and appends it to a new tree by its reference and digest.
# The node pulls it, and the program then executes, connects and writes
# only what its sandbox allows, while a program outside it is left
@@ -65,11 +65,11 @@ stage() {
# Run the probe in the node with the interpreter $1 and write the checks
# the kernel denied it to $W/denied, one name per line. The probe makes
-# 26 checks.
+# 28 checks.
probe() {
on edge "python3 $PROBE setup $APP_DIR && $1 $PROBE run $APP_DIR" \
>"$W/probe" &&
- test "$(wc -l <"$W/probe")" = 26 &&
+ test "$(wc -l <"$W/probe")" = 28 &&
sed -n "s/ denied\$//p" "$W/probe" >"$W/denied"
}
@@ -110,12 +110,16 @@ test_expect_success 'the node trusts the CA and reaches the registry' '
'
# The application is a copy of the interpreter, so that the label goes on
-# a file of this test and python3 itself stays outside the sandbox.
+# a file of this test and python3 itself stays outside the sandbox. The
+# pattern helper* matches the name of the link too, whose file it must
+# not label.
test_expect_success 'the node has the application and the programs it may and may not execute' '
on edge "rm -rf $APP_DIR && mkdir $APP_DIR &&
cp -L /usr/bin/python3 $APP_DIR/app &&
- cp -L /usr/bin/true $APP_DIR/helper &&
- cp -L /usr/bin/true $APP_DIR/other"
+ cp -L /usr/bin/true $APP_DIR/helper-a &&
+ cp -L /usr/bin/true $APP_DIR/helper-b &&
+ cp -L /usr/bin/true $APP_DIR/other &&
+ ln -s other $APP_DIR/helper-link"
'
test_expect_success 'without the sandbox, the application is denied nothing' '
@@ -169,17 +173,19 @@ test_expect_success 'the operator creates the tree with the package' '
ORDER BY seq" "$W/ops"
'
-test_expect_success 'the node pulls the tree, loads the upatch and labels the files of the sandbox' '
+test_expect_success 'the node pulls the tree, loads the upatch and labels the files its paths and pattern name' '
sync_to 1 1 &&
on edge "[ -d $PINS ]" &&
test "$(policy_bits "$APP_DIR/app")" = 0x1 &&
- test "$(policy_bits "$APP_DIR/helper")" = 0x2 &&
+ test "$(policy_bits "$APP_DIR/helper-a")" = 0x2 &&
+ test "$(policy_bits "$APP_DIR/helper-b")" = 0x2 &&
test "$(policy_bits "$APP_DIR/other")" = none
'
test_expect_success 'the application executes, connects and writes only what its sandbox allows' '
probe "$APP_DIR/app" &&
cat >"$W/expected" <<-\EOT &&
+ exec-helper-link
exec-other
connect-remote
connect-remote-v6
diff --git a/testing/fixtures/app-sandbox/probe.Beefile b/testing/fixtures/app-sandbox/probe.Beefile
index f380e991..fc627441 100644
--- a/testing/fixtures/app-sandbox/probe.Beefile
+++ b/testing/fixtures/app-sandbox/probe.Beefile
@@ -21,7 +21,7 @@ paths = ["/tmp/shirudo-app-sandbox/app"]
[[upatches.policy]]
id = "probe-helpers"
bit = 0x2
-paths = ["/tmp/shirudo-app-sandbox/helper"]
+paths = ["/tmp/shirudo-app-sandbox/helper*"]
[upatches.tree]
id = "app-sandbox"
diff --git a/upatches/app-sandbox/Beefile b/upatches/app-sandbox/Beefile
index 7b465d8a..277076e2 100644
--- a/upatches/app-sandbox/Beefile
+++ b/upatches/app-sandbox/Beefile
@@ -29,7 +29,7 @@ paths = ["/usr/sbin/apache2", "/usr/sbin/httpd", "/usr/local/apache2/bin/httpd"]
[[upatches.policy]]
id = "php-fpm"
bit = 0x2
-paths = ["/usr/sbin/php-fpm8.2", "/usr/sbin/php-fpm8.3", "/usr/sbin/php-fpm8.4", "/usr/local/sbin/php-fpm"]
+paths = ["/usr/sbin/php-fpm*", "/usr/local/sbin/php-fpm"]
[upatches.tree]
id = "app-sandbox"
diff --git a/upatches/app-sandbox/README.md b/upatches/app-sandbox/README.md
index 10e78fd2..8cd60ae7 100644
--- a/upatches/app-sandbox/README.md
+++ b/upatches/app-sandbox/README.md
@@ -32,6 +32,10 @@ bit = 0x1
paths = ["/usr/sbin/apache2", "/usr/sbin/httpd"]
```
+The last component of a path may be a pattern, as in
+`/usr/sbin/php-fpm*` for the PHP-FPM of any version: the target labels
+every regular file of the directory that matches it.
+
`sandbox.toml` gives the rules of the sandbox of that name:
```toml
@@ -121,7 +125,7 @@ Every refusal is `EACCES`.
- A path that does not exist at the load is not labeled, and a file
that replaces a labeled one, as a package upgrade does, is a new file
- without the label. Load the upatch again after the application is
+ without the label, whether a path or a pattern named it. Load the upatch again after the application is
installed or upgraded.
- The file rules name files, not their contents: a descriptor that was
open for writing before the task entered the sandbox, or that another