eBPF-based security platform
coccinelle: refuse a path resolved by name where its descriptor is held
Daniel Borkmannd78cef109e64
diff --git a/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.mdindex 9857dae9..319a392e 100644--- a/.agents/skills/security-audit/SKILL.md+++ b/.agents/skills/security-audit/SKILL.md@@ -116,8 +116,8 @@ of that tree, as one about a guard does.`testing/coccinelle` has rules for the mechanical shapes of severalclasses below (`openssl_ret`, `errno_clobber`, `alloc_unchecked`,`fd_leak`, `double_close`, `use_after_free`, `read_unterminated`,-`file_as_string`, the `bpf_` rules): a clean `check-coccinelle.sh` says-those shapes are absent, not the class.+`file_as_string`, `name_after_fd`, the `bpf_` rules): a clean+`check-coccinelle.sh` says those shapes are absent, not the class.- **Parse before verify.** Anything read before its signature or digestis checked (`upatch info`, `bpm info`, manifests, indexes, thediff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 78ebc85e..1077e7df 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -761,6 +761,11 @@ can rewrite what it finds does so with ``-D patch``:lock of ``eventlog_lock()`` or ``seal_lock()`` among them.* - ``cloexec``- A descriptor or stream opened without close-on-exec.+ * - ``name_after_fd``+ - A path passed to ``realpath()`` or ``readlink()`` in a function+ that holds a descriptor opened from the same path or passed in+ with it: the name is looked up again and can lead to another+ file than the descriptor.* - ``banned``- ``rand()``, ``strtok()``, ``usleep()``, ``signal()``,``alloca()``, ``setjmp()``, the time and user database functionsdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 4de799a0..9e322ae6 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -60,10 +60,11 @@ signed config image.``positive_errno``, a case for each BPF hazard rule beside thesafe form it leaves alone, a case at its line for ``stale_success``,``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``,- ``strerror_negative`` and ``double_test`` beside the forms it- leaves alone, a dead store beside the reads ``dead_store`` leaves- alone, a buffer a read filled and a file read whole taken for a- string, beside the forms that terminate or check first, a count by+ ``strerror_negative``, ``double_test`` and ``name_after_fd``+ beside the forms it leaves alone, a dead store beside the reads+ ``dead_store`` leaves alone, a buffer a read filled and a file+ read whole taken for a string, beside the forms that terminate or+ check first, a count byhand in a macro and an ``ARRAY_SIZE()`` of a file's own for``array_size``, which rewrites the one and leaves the other, alocal's address, or that of its member or element, left in thediff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 6dd8b73d..0a746ac8 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -391,10 +391,11 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'# the forms the rule leaves alone: the count of reports shows they stay# quiet.SAMPLE_RULES="stale_success break_error sum_overwrite fd_leak getenv_null-strerror_negative double_test"+strerror_negative double_test name_after_fd"SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148getenv_null:170 getenv_null:177 strerror_negative:196 strerror_negative:202-double_test:215 double_test:222 double_test:231 double_test:243 double_test:254"+double_test:215 double_test:222 double_test:231 double_test:243 double_test:254+name_after_fd:276 name_after_fd:285 name_after_fd:296 name_after_fd:307"write_line_samples() {cat >"$1" <<'EOF'int search_left(int fd, int key)@@ -664,6 +665,96 @@ int counted_twice(struct lexer *l)return 2;return 0;}++int checked(int fd, const char *path)+{+ char real[PATH_MAX];+ struct stat st;++ if (fstat(fd, &st))+ return -errno;+ if (!realpath(path, real))+ return -errno;+ return walk(real);+}++static ssize_t target_of(const char *path, int dirfd, char *buf, size_t cap)+{+ if (owned(dirfd))+ return -EPERM;+ return readlink(path, buf, cap);+}++int opened(const char *path)+{+ char real[PATH_MAX];+ int fd, ret;++ fd = open(path, O_RDONLY | O_CLOEXEC);+ if (fd < 0)+ return -errno;+ ret = realpath(path, real) ? walk(real) : -errno;+ close(fd);+ return ret;+}++int opened_at(int dir, const char *link, char *buf, size_t cap)+{+ int fd = openat(dir, link, O_RDONLY | O_CLOEXEC);++ if (fd < 0)+ return -errno;+ if (readlink(link, buf, cap) < 0) {+ close(fd);+ return -errno;+ }+ return fd;+}++int resolved_first(const char *path)+{+ char real[PATH_MAX];++ if (!realpath(path, real))+ return -errno;+ return open(real, O_RDONLY | O_CLOEXEC);+}++int named_again(const char *path, const char *other, char *real)+{+ int fd;++ fd = open(path, O_RDONLY | O_CLOEXEC);+ if (fd < 0)+ return -errno;+ path = other;+ if (!realpath(path, real)) {+ close(fd);+ return -errno;+ }+ return fd;+}++int stated(int fd, const char *path)+{+ struct stat st;++ return stat(path, &st) ? -errno : 0;+}++int by_descriptor(int fd, char *buf, size_t cap)+{+ char proc[64];++ if (strsprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd) < 0)+ return -E2BIG;+ return readlink(proc, buf, cap) < 0 ? -errno : 0;+}++int no_descriptor(int flags, const char *path, char *real)+{+ return realpath(path, real) ? flags : -errno;+}EOF}diff --git a/testing/coccinelle/name_after_fd.cocci b/testing/coccinelle/name_after_fd.coccinew file mode 100644index 00000000..eec890e0--- /dev/null+++ b/testing/coccinelle/name_after_fd.cocci@@ -0,0 +1,78 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A descriptor names one file for good; a path is looked up again at+// every call that takes it. A function that holds a descriptor opened+// from a path, or passed in with it, and hands the same path to+// realpath() or readlink() learns where the name leads now, which a+// rename or a swapped link sends elsewhere than the descriptor: walk+// from the descriptor, or open each name once.++virtual report++@passed@+attribute name SEC;+identifier f, path;+identifier fd =~ "fd";+identifier lookup = {realpath, readlink};+type T;+position p;+@@+T f(..., int fd, ..., char *path, ...)+{+ <+...+ lookup@p(path, ...)+ ...+>+}++@passed_before@+identifier f, path;+identifier fd =~ "fd";+identifier lookup = {realpath, readlink};+type T;+position p;+@@+T f(..., char *path, ..., int fd, ...)+{+ <+...+ lookup@p(path, ...)+ ...+>+}++@opened exists@+expression fd, path, E, E1;+identifier open_fn = {open, openat, fopen};+identifier lookup = {realpath, readlink};+position p;+@@+(+ fd = open_fn(path, ...)+|+ fd = open_fn(E, path, ...)+)+ ... when != path = E1+ lookup@p(path, ...)++@script:python depends on report@+p << passed.p;+fd << passed.fd;+path << passed.path;+lookup << passed.lookup;+@@+coccilib.report.print_report(p[0], "%s() resolves %s by name again, where the descriptor %s is at hand" % (lookup, path, fd))++@script:python depends on report@+p << passed_before.p;+fd << passed_before.fd;+path << passed_before.path;+lookup << passed_before.lookup;+@@+coccilib.report.print_report(p[0], "%s() resolves %s by name again, where the descriptor %s is at hand" % (lookup, path, fd))++@script:python depends on report@+p << opened.p;+fd << opened.fd;+path << opened.path;+lookup << opened.lookup;+@@+coccilib.report.print_report(p[0], "%s() resolves %s by name again, where the descriptor %s is at hand" % (lookup, path, fd))