Skip to content

eBPF-based security platform

coccinelle: report a register lock left held

Daniel Borkmannb136bacd392d
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 7ba5f628..f0aee1c6 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -719,7 +719,8 @@ can rewrite what it finds does so with ``-D patch``:
path out.
* - ``fd_leak``
- A descriptor left open on a path out once its open succeeded,
- where it is not returned, stored or handed to what closes it.
+ where it is not returned, stored or handed to what closes it, a
+ lock of ``eventlog_lock()`` or ``seal_lock()`` among them.
* - ``cloexec``
- A descriptor or stream opened without close-on-exec.
* - ``banned``
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 51ca4285..595126a2 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -57,13 +57,13 @@ signed config image.
alone, the rewrite of ``-D patch``, a case for each rule taken
from CERT C, a case for each BPF hazard rule beside the safe form
it leaves alone, a case at its line for ``stale_success``,
- ``break_error`` and ``sum_overwrite`` beside the forms it leaves
- alone, a dead store beside the reads ``dead_store`` leaves alone,
- a count by hand in a macro and an ``ARRAY_SIZE()`` of a file's
- own for ``array_size``, which rewrites the one and leaves the
- other, a file a rule gives up on at its time limit named rather
- than failed, ``SEC`` declared an attribute in every rule, and the
- tree itself
+ ``break_error``, ``sum_overwrite`` and ``fd_leak`` beside the
+ forms it leaves alone, a dead store beside the reads
+ ``dead_store`` leaves alone, a count by hand in a macro and an
+ ``ARRAY_SIZE()`` of a file's own for ``array_size``, which
+ rewrites the one and leaves the other, a file a rule gives up on
+ at its time limit named rather than failed, ``SEC`` declared an
+ attribute in every rule, and the tree itself
* - ``t0007-check-operator-linkage``
- ``check-operator-linkage.sh``: the libraries an operator binary
may link and its own interpreter, another loader, a library
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index df07f4b2..8db85e34 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -375,8 +375,8 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'
# One case of each rule below at the line given with it, and beside it
# the forms the rule leaves alone: the count of reports shows they stay
# quiet.
-SAMPLE_RULES="stale_success break_error sum_overwrite"
-SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110"
+SAMPLE_RULES="stale_success break_error sum_overwrite fd_leak"
+SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148"
write_line_samples() {
cat >"$1" <<'EOF'
int search_left(int fd, int key)
@@ -516,6 +516,32 @@ size_t header_size(size_t fixed, size_t chosen, int custom)
n += 64;
return n;
}
+
+int measure_left(const struct op *op)
+{
+ int lock, ret;
+
+ lock = eventlog_lock();
+ if (lock < 0)
+ return lock;
+ ret = measure_locked(op);
+ if (ret)
+ return ret;
+ eventlog_unlock(lock);
+ return 0;
+}
+
+int measure_unlocked(const struct op *op)
+{
+ int lock, ret;
+
+ lock = eventlog_lock();
+ if (lock < 0)
+ return lock;
+ ret = measure_locked(op);
+ eventlog_unlock(lock);
+ return ret;
+}
EOF
}
diff --git a/testing/coccinelle/fd_leak.cocci b/testing/coccinelle/fd_leak.cocci
index 79d98904..4c877b92 100644
--- a/testing/coccinelle/fd_leak.cocci
+++ b/testing/coccinelle/fd_leak.cocci
@@ -6,7 +6,10 @@
// BIO_CLOSE) on every path out of the function once the open succeeded.
// A leak on an error path goes unnoticed in a one-shot command until a
// loop or a retry runs out of descriptors, and keeps open what it should
-// not.
+// not. The locks of eventlog_lock() and seal_lock() are descriptors too,
+// and eventlog_unlock() closes one: a lock left held makes the next
+// eventlog_lock() of the same command wait for itself, as flock() takes
+// each open of the file apart.
virtual report
@@ -22,7 +25,8 @@ expression E, E1, E2;
statement S, S1;
identifier opener = {open, openat, socket, accept4, dup, memfd_create,
eventfd, epoll_create1, inotify_init1, timerfd_create,
- pidfd_open, bpf_obj_get};
+ pidfd_open, bpf_obj_get, eventlog_lock,
+ seal_lock};
position p1, p2;
@@
(
@@ -33,6 +37,7 @@ position p1, p2;
... when != fd = E1
if (\(fd < 0\|fd == -1\)) S
... when != close(fd)
+ when != eventlog_unlock(fd)
when != fd = E1
when != E = fd
when != if (fd >= 0) S1
@@ -55,7 +60,8 @@ expression E, E1, E2;
statement S, S1;
identifier opener = {open, openat, socket, accept4, dup, memfd_create,
eventfd, epoll_create1, inotify_init1, timerfd_create,
- pidfd_open, bpf_obj_get};
+ pidfd_open, bpf_obj_get, eventlog_lock,
+ seal_lock};
position p1, p2;
@@
(
@@ -66,6 +72,7 @@ position p1, p2;
... when != fd = E1
if (\(fd < 0\|fd == -1\)) S
... when != close(fd)
+ when != eventlog_unlock(fd)
when != fd = E1
when != E = fd
when != if (fd >= 0) S1