eBPF-based security platform
upatch: expand a wildcard in the last component of a policy path
John Fastabend3657b7f69779
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 9ddea36c..4ff2cea2 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -641,8 +641,9 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Sections of 0, 1 and 2 labels and the lengths around them- An accepted section fills its length exactly, with its magic andversion, two distinct map indexes below the map count, one bit- per label and a terminated name and absolute path; a refusal- writes nothing+ per label, a terminated name and an absolute path of at most 255+ bytes that does not end in ``/`` and has no wildcard before its+ last component; a refusal writes nothing* - ``upatch-name``- ``upatch_name_valid()``, ``upatch_select()``,``upatch_prog_name()``diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex f5a7044e..18d4d187 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -264,7 +264,10 @@ signed config image.- A loaded upatch labels executable and ordinary files, ORingmultiple policy bits with a preexisting xattr; its signed policysection binds verdict programs to shared task and socket policy- maps+ maps; a path with a pattern labels the regular files of its+ directory that match, also over a preexisting xattr, and neither+ a directory nor the target of a symbolic link that matches, nor+ a file that does not match* - ``t3110-double-load``- A second load of a loaded upatch is refused as ``already_loaded``* - ``t3111-xdp``@@ -667,7 +670,9 @@ signed config image.refused when the tag serves another manifest* - ``t7006-policy-labels``- Beefile policy declarations are serialized into the signed- upatch section+ upatch section, a path with a pattern as it is written;+ ``bpm build`` refuses a wildcard in a directory component, a path+ that ends in ``/`` and a relative one* - ``t7007-bpm-tree-fixes``- ``bpm tree append`` writes ``fixes`` on an ``add`` in byte order,after ``artifact`` and outside ``operation_digest``, none on adiff --git a/Documentation/reference/formats/beefile.rst b/Documentation/reference/formats/beefile.rstindex 3373243a..9cc2a271 100644--- a/Documentation/reference/formats/beefile.rst+++ b/Documentation/reference/formats/beefile.rst@@ -344,7 +344,9 @@ embeds the entries in the signed ``SHPL`` section.* - ``paths``- array of strings- yes- - 1 to 16 absolute paths. A path may name a non-executable file.+ - 1 to 16 absolute paths of at most 255 bytes each, none ending in+ ``/``. A path may name a non-executable file. The last component+ may be a pattern, see below... code-block:: toml@@ -353,6 +355,42 @@ embeds the entries in the signed ``SHPL`` section.bit = 0x1paths = ["/usr/local/apache", "/usr/bin/apache"]+ [[upatches.policy]]+ id = "php-fpm"+ bit = 0x2+ paths = ["/usr/sbin/php-fpm*"]++In the last component of a path, ``*`` stands for any run of bytes and+``?`` for any one byte, and each may appear more than once. There are no+brackets and no escape, so a path cannot name a file with ``*`` or ``?``+in its name. A wildcard in a directory component, as in+``/opt/*/bin/app``, fails the build. The pattern is signed as written,+and each target matches it against its own directory at every load:++.. list-table::+ :header-rows: 1+ :widths: 30 35 35++ * - On the target+ - A path without a wildcard+ - A pattern+ * - Which files are labeled+ - the file the path names, through any symbolic link+ - every regular file of the directory whose name matches+ * - A symbolic link+ - followed+ - not followed and not labeled; its target is labeled when its own+ name matches+ * - A directory, device, socket or FIFO+ - labeled+ - not labeled+ * - Nothing to label+ - a warning in the journal; the load goes on+ - a warning in the journal; the load goes on++A file that appears in the directory after the load carries no label+until the upatch is loaded again.+A policy table opts the upatch into shirudo's shared task``shirudo_task_policy`` and socket ``shirudo_sock_policy`` maps. Thegenerated loader reuses both pinned maps rather than creating privatediff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 5d0ffd0f..f1f5b519 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -322,7 +322,7 @@ Section tags- ``0x4c504853``- variable- Signed policy labels, each naming a policy ID, one-hot bit and- absolute file path. Its presence opts into the shared task+ absolute file path or pattern. Its presence opts into the shared task``shirudo_task_policy`` and socket ``shirudo_sock_policy``bindings.* - ``PTYP``@@ -716,11 +716,16 @@ upatches without ``SHPL`` use their normal maps.- Distinct map descriptor index, less than ``nr_maps`` and 64.Each entry is 4168 bytes: a little-endian one-hot ``u64`` policy bit,-64 bytes of NUL-terminated policy ID, then 4096 bytes of NUL-terminated-absolute path. After a successful load, shirudo applies each entry by-ORing the policy bit into the inode's existing policy xattr. It takes the-entries from the copy of the artifact the kernel verified, not from the-file again.+64 bytes of NUL-terminated policy ID, then 4096 bytes that hold a+NUL-terminated absolute path of at most 255 bytes. The path does not end+in ``/``, and ``*`` and ``?`` appear in its last component alone, where+they make it a pattern (:doc:`beefile`); the target refuses a section+with any other path. After a successful load, shirudo applies each entry+by ORing the policy bit into the existing policy xattr of the inode the+path names, or of every regular file in the path's directory whose name+the pattern matches, without following a symbolic link there. It takes+the entries from the copy of the artifact the kernel verified, not from+the file again.Header fields outside the signature-----------------------------------diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 52412a24..ade78b74 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -784,7 +784,11 @@ A signed upatch with an ``SHPL`` path-to-policy section opts into theshared task and socket policy maps. The builder requires compatible mapdeclarations, and the target supplies both pinned map FDs to thegenerated loader. After the load, shirudo ORs each entry's bit into the-policy xattr of its path. It reads the artifact once and takes the+policy xattr of its path. A path whose last component is a pattern+labels the regular files of its directory that match, and no symbolic+link among them: root, who owns the directory, could otherwise point a+matching name at a file the signed pattern does not name. It reads the+artifact once and takes theentries from the copy it handed to the kernel, not from the file again,so a task that writes the artifact during the load cannot have shirudoset a label the signature does not cover. The upatch's signed BPF@@ -1958,8 +1962,11 @@ R23 Policy labels follow the inodeevery load and boot restore of the upatch that declares it. A copyof the executable, or the new inode a package upgrade or an editorleaves in its place, carries no policy until the upatch is loaded- again, and a hard link or a rename that root makes of an inode- without the seal label moves the policy to another path. A file+ again, and neither does a file that comes to match a pattern after+ the load. A hard link or a rename that root makes of an inode+ without the seal label moves the policy to another path, and a+ regular file root gives a name a pattern matches is labeled at the+ next load. A filesystem that root makes and mounts, a loop image say, carries thexattr on its own executables, written where no seal ran, and anexecution from it takes whatever bits root put there. A taskdiff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex 733146e9..4a469e46 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -135,7 +135,8 @@ The sections hold:``SHPL``The policy labels of a load whose programs share the``shirudo_task_policy`` and ``shirudo_sock_policy`` maps: for each- label, a policy ID, a bit and an absolute file path.+ label, a policy ID, a bit and an absolute file path, whose last+ component may be a pattern.The header's ``payload_sha256`` is the SHA-256 of the signed bytes. Thetarget recomputes it and refuses a mismatch, because the pin, thediff --git a/builtin-upatch.c b/builtin-upatch.cindex 4ecb529f..87d47a77 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2076,6 +2076,114 @@ static void close_loader_fds(const struct loader_ctx *cx,}}+/* OR the entry's bit into the policy label of the file fd is open on. */+static void label_fd(int fd, const char *path,+ const struct upatch_policy_label_entry *entry)+{+ __u64 current = 0;+ ssize_t n;++ n = fgetxattr(fd, XATTR_POLICY, ¤t, sizeof(current));+ if (n < 0 && (errno == ENODATA || errno == EOPNOTSUPP)) {+ current = 0;+ } else if (n != (ssize_t)sizeof(current)) {+ pr_warn("upatch: cannot read policy label on %s: %s", path,+ n < 0 ? strerror(errno) : "invalid length");+ return;+ }+ current |= le64_to_cpu(entry->bit);+ if (fsetxattr(fd, XATTR_POLICY, ¤t, sizeof(current), 0))+ pr_warn("upatch: cannot set policy label on %s: %s", path,+ strerror(errno));+}++static void label_path(const struct upatch_policy_label_entry *entry)+{+ int fd;++ fd = open(entry->path, O_RDONLY | O_CLOEXEC);+ if (fd < 0) {+ pr_warn("upatch: cannot label %s for policy %s: %s",+ entry->path, entry->policy, strerror(errno));+ return;+ }+ label_fd(fd, entry->path, entry);+ close(fd);+}++/*+ * Root owns the directory and can put a symbolic link under a name the+ * pattern matches, to a file the signed pattern does not name: a match+ * is labeled only where it is a regular file itself.+ */+static bool label_match(int dir_fd, const char *name,+ const struct upatch_policy_label_entry *entry)+{+ struct stat st;+ bool regular;+ int fd;++ fd = openat(dir_fd, name,+ O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);+ if (fd < 0)+ return false;+ regular = !fstat(fd, &st) && S_ISREG(st.st_mode);+ if (regular)+ label_fd(fd, name, entry);+ close(fd);+ return regular;+}++/*+ * Returns how many of the entries of dir, which reads dir_fd, the+ * pattern labeled, or a negative errno.+ */+static int label_matches(DIR *dir, int dir_fd, const char *pattern,+ const struct upatch_policy_label_entry *entry)+{+ struct dirent *de;+ int nr_labeled = 0;++ for (;;) {+ errno = 0;+ de = readdir(dir);+ if (!de)+ return errno ? -errno : nr_labeled;+ if (name_glob_match(pattern, de->d_name, strlen(de->d_name)) &&+ label_match(dir_fd, de->d_name, entry))+ nr_labeled++;+ }+}++static void label_pattern(const struct upatch_policy_label_entry *entry,+ const char *pattern)+{+ char dir_path[UPATCH_POLICY_PATH_MAX];+ int dir_fd, nr_labeled;+ DIR *dir;++ /* The directory with its '/', which a pattern in the root keeps. */+ memcpy(dir_path, entry->path, pattern - entry->path);+ dir_path[pattern - entry->path] = '\0';+ dir_fd = open(dir_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);+ dir = dir_fd < 0 ? NULL : fdopendir(dir_fd);+ if (!dir) {+ pr_warn("upatch: cannot label %s for policy %s: %s",+ entry->path, entry->policy, strerror(errno));+ if (dir_fd >= 0)+ close(dir_fd);+ return;+ }+ nr_labeled = label_matches(dir, dir_fd, pattern, entry);+ closedir(dir);+ if (nr_labeled < 0)+ pr_warn("upatch: cannot label %s for policy %s: %s",+ entry->path, entry->policy, strerror(-nr_labeled));+ else if (!nr_labeled)+ pr_warn("upatch: cannot label %s for policy %s: no regular file matches",+ entry->path, entry->policy);+}+/** From the copy the kernel's signature check covered, not from the file* again, which may hold other labels by now.@@ -2084,6 +2192,7 @@ static void apply_policy_labels(const struct upatch *u){struct upatch_policy_label_header header;struct upatch_policy_label_entry entry;+ const char *pattern;const __u8 *labels;__u32 len, count;@@ -2093,33 +2202,13 @@ static void apply_policy_labels(const struct upatch *u)memcpy(&header, labels, sizeof(header));count = le32_to_cpu(header.count);for (__u32 i = 0; i < count; i++) {- __u64 current = 0;- ssize_t n;- int fd;-memcpy(&entry, labels + sizeof(header) + i * sizeof(entry),sizeof(entry));- fd = open(entry.path, O_RDONLY | O_CLOEXEC);- if (fd < 0) {- pr_warn("upatch: cannot label %s for policy %s: %s",- entry.path, entry.policy, strerror(errno));- continue;- }- n = fgetxattr(fd, XATTR_POLICY, ¤t, sizeof(current));- if (n < 0 && (errno == ENODATA || errno == EOPNOTSUPP)) {- current = 0;- } else if (n != (ssize_t)sizeof(current)) {- pr_warn("upatch: cannot read policy label on %s: %s",- entry.path,- n < 0 ? strerror(errno) : "invalid length");- close(fd);- continue;- }- current |= le64_to_cpu(entry.bit);- if (fsetxattr(fd, XATTR_POLICY, ¤t, sizeof(current), 0))- pr_warn("upatch: cannot set policy label on %s: %s",- entry.path, strerror(errno));- close(fd);+ pattern = policy_path_pattern(entry.path);+ if (pattern)+ label_pattern(&entry, pattern);+ else+ label_path(&entry);}}diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex 43da1cb3..0fd8ce78 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -97,6 +97,8 @@ enum upatch_section_tag {#define UPATCH_MAPS_MAX 64u#define UPATCH_POLICY_NAME_MAX 64u#define UPATCH_POLICY_PATH_MAX 4096u+/* The longest path a label names, less than the field holds. */+#define UPATCH_POLICY_PATH_LEN_MAX 255ustruct upatch_policy_label_header {__le32 magic;diff --git a/testing/fixtures/policy-labels/Beefile b/testing/fixtures/policy-labels/Beefileindex da8a2349..20038768 100644--- a/testing/fixtures/policy-labels/Beefile+++ b/testing/fixtures/policy-labels/Beefile@@ -25,3 +25,8 @@ paths = ["/tmp/shirudo-policy-label-test/ordinary"]id = "socket-check"bit = 0x10paths = ["/tmp/shirudo-policy-label-test/socket-executable"]++[[upatches.policy]]+id = "matched"+bit = 0x20+paths = ["/tmp/shirudo-policy-label-test/match-*"]diff --git a/testing/functional/bpm/t7006-policy-labels.sh b/testing/functional/bpm/t7006-policy-labels.shindex 67959116..2c4f08fa 100755--- a/testing/functional/bpm/t7006-policy-labels.sh+++ b/testing/functional/bpm/t7006-policy-labels.sh@@ -3,7 +3,8 @@# Copyright (c) 2026 Authors of shirudo## t7006 -- bpm build turns Beefile policy declarations into the signed-# policy-label section consumed by upatch load.+# policy-label section consumed by upatch load, a pattern as it is+# written, and refuses a path that is none.cd "$(dirname "$0")/../..". ./test-lib.sh@@ -55,4 +56,26 @@ test_expect_success 'Beefile policy entries are carried in the signed upatch' '"$CHECK" "$WORK/policy-labels.upatch" policy-labels'+# Build the fixture with $1 in place of the path of its pattern.+build_with_path() {+ rm -rf "$WORK/bad" &&+ mkdir "$WORK/bad" &&+ cp -P "$WORK/pkg/"* "$WORK/bad/" &&+ sed "s|/tmp/shirudo-policy-label-test/match-\*|$1|" \+ fixtures/policy-labels/Beefile >"$WORK/bad/Beefile" &&+ grep -qF "\"$1\"" "$WORK/bad/Beefile" &&+ ( cd "$WORK" && "$S" bpm build bad --version 1.0.0 \+ --keys-dir "$WORK/keys" \+ --vmlinux "$REPO/vendor/linux/vmlinux.h" ) 2>"$WORK/bad.err"+}++test_expect_success 'bpm build takes a wildcard in the last component of a path alone' '+ build_with_path "/tmp/shirudo-policy-label-test/m?tch*" >&2 &&+ for path in "/tmp/*/match" "/tmp/shirudo-policy-label-?est/match-*" \+ "/tmp/shirudo-policy-label-test/" "tmp/match"; do+ ! build_with_path "$path" &&+ grep -q "is not a valid Beefile" "$WORK/bad.err" || return 1+ done+'+test_donediff --git a/testing/functional/upatch/t3109-policy-labels.sh b/testing/functional/upatch/t3109-policy-labels.shindex 6ac93437..b277d813 100755--- a/testing/functional/upatch/t3109-policy-labels.sh+++ b/testing/functional/upatch/t3109-policy-labels.sh@@ -3,7 +3,8 @@# Copyright (c) 2026 Authors of shirudo## t3109 -- a signed policy section labels executables and ordinary files,-# ORing multiple declarations with an existing file xattr.+# ORing multiple declarations with an existing file xattr, and a pattern+# labels the regular files of its directory that it matches.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -38,6 +39,14 @@ printf 'ordinary file\n' >"$WORK/ordinary"chmod 0755 "$WORK/executable" "$WORK/socket-executable""$PROBE" set-policy "$WORK/ordinary" 0x8 ||test_skip_all "scratch filesystem cannot store policy xattrs"+# What the fixture's pattern, match-*, meets in its directory.+printf 'first\n' >"$WORK/match-a"+printf 'second\n' >"$WORK/match-b"+printf 'other\n' >"$WORK/unmatched"+mkdir "$WORK/match-dir"+ln -s ordinary "$WORK/match-link"+"$PROBE" set-policy "$WORK/match-b" 0x40 ||+ test_skip_all "scratch filesystem cannot store policy xattrs"test_expect_success 'policy section binds task and socket maps and labels files' '"$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck &&@@ -61,4 +70,30 @@ assert policy(sys.argv[2]) == 0xePY'+# The symbolic link names a file the pattern does not: its target keeps+# the bits of the test above.+test_expect_success 'a pattern labels the regular files it matches and nothing else' '+ python3 - "$WORK" <<'"'"'PY'"'"'+import errno+import os+import sys++def policy(name):+ try:+ value = os.getxattr(os.path.join(sys.argv[1], name),+ "security.bpf.shirudo.policy")+ except OSError as error:+ assert error.errno == errno.ENODATA, (name, error)+ return None+ assert len(value) == 8, (name, len(value))+ return int.from_bytes(value, byteorder=sys.byteorder)++assert policy("match-a") == 0x20+assert policy("match-b") == 0x60+assert policy("unmatched") is None+assert policy("match-dir") is None+assert policy("ordinary") == 0xe+PY+'+test_donediff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.cindex 45616652..160d580c 100644--- a/testing/helper/test-upatch-prog-metadata.c+++ b/testing/helper/test-upatch-prog-metadata.c@@ -270,14 +270,16 @@ static int expect_policy_labels(const struct upatch *u){static const char *const policies[] = {"executable", "ordinary-a", "ordinary-b", "socket-check",+ "matched",};static const char *const paths[] = {"/tmp/shirudo-policy-label-test/executable","/tmp/shirudo-policy-label-test/ordinary","/tmp/shirudo-policy-label-test/ordinary","/tmp/shirudo-policy-label-test/socket-executable",+ "/tmp/shirudo-policy-label-test/match-*",};- static const __u64 bits[] = { 1, 2, 4, 16 };+ static const __u64 bits[] = { 1, 2, 4, 16, 32 };struct upatch_policy_label_entry entry;struct upatch_policy_label_header header;const __u8 *raw, *cursor;diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex c3171021..5565e1d1 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -18,7 +18,7 @@ attest-name-digest-long --unwind 72 --unwindset any_spec.0:323,harness.0:323,harpaths-bpffs-name --unwind 18upatch-header --unwind 2 --unwindset memchr.0:257upatch-decoders --unwind 2 --unwindset memchr.0:65,parse_decoders.0:3,harness.0:3-upatch-policy-labels --unwind 3 LEN=0,19,20,4187,4188,4189,8356+upatch-policy-labels --unwind 3 --unwindset policy_path_valid.0:257,path_valid.0:257 LEN=0,19,20,4187,4188,4189,8356upatch-name --unwind 33 --unwindset upatch_name_valid.0:251,long_name_valid.1:251upatch-signed-sections --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:4,directory_broken.0:4,parse_decoders.0:0,parse_policy_labels.0:0 LEN=15,16,40,64upatch-signed-name --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:2,directory_broken.0:2,parse_decoders.0:2,parse_policy_labels.0:0 LEN=288diff --git a/testing/proofs/cbmc/upatch-policy-labels.c b/testing/proofs/cbmc/upatch-policy-labels.cindex de21e3b3..8c8b8c6d 100644--- a/testing/proofs/cbmc/upatch-policy-labels.c+++ b/testing/proofs/cbmc/upatch-policy-labels.c@@ -5,7 +5,9 @@* The policy labels section of a .upatch, for sections of LEN bytes, each* length proofs.list names: util/upatch.c accepts one only when its labels* fill it, it names two distinct maps of the upatch, and every label has- * one bit, a name and a terminated absolute path; a refusal writes nothing.+ * one bit, a name and an absolute path of at most 255 bytes that does not+ * end in '/' and has no wildcard before its last component; a refusal+ * writes nothing.*/#define PROOF_MEMCHR_QUANTIFIED@@ -20,6 +22,23 @@ static bool terminated(const char *s, size_t size)return memchr(s, '\0', size);}+/* The rule of policy_path_valid(), stated for any one byte before the last '/'. */+static void path_valid(const char *path)+{+ size_t len, last_slash = 0, any;++ __CPROVER_assert(path[0] == '/' &&+ terminated(path, UPATCH_POLICY_PATH_LEN_MAX + 1),+ "an absolute path of at most 255 bytes");+ for (len = 0; path[len]; len++)+ if (path[len] == '/')+ last_slash = len;+ __CPROVER_assert(last_slash + 1 < len, "a last component");+ __CPROVER_assume(any < last_slash);+ __CPROVER_assert(path[any] != '*' && path[any] != '?',+ "no wildcard in a directory");+}+static void label_valid(const __u8 *p, __u32 i){struct upatch_policy_label_entry e;@@ -30,8 +49,7 @@ static void label_valid(const __u8 *p, __u32 i)__CPROVER_assert(bit && !(bit & (bit - 1)), "one bit");__CPROVER_assert(e.policy[0] && terminated(e.policy, sizeof(e.policy)),"a terminated policy name");- __CPROVER_assert(e.path[0] == '/' && terminated(e.path, sizeof(e.path)),- "a terminated absolute path");+ path_valid(e.path);}void harness(void)diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cindex 769f5623..2f04d263 100644--- a/testing/unit/upatch.c+++ b/testing/unit/upatch.c@@ -162,6 +162,56 @@ static void kprobe_multi_block_is_exact(void)EXPECT_EQ(parse_one(buf, len), -EINVAL);}+static bool path_valid(const char *path)+{+ return policy_path_valid(path, strlen(path) + 1);+}++static void policy_path_takes_a_wildcard_in_its_last_component(void)+{+ EXPECT_TRUE(path_valid("/usr/sbin/httpd"));+ EXPECT_TRUE(path_valid("/httpd"));+ EXPECT_TRUE(path_valid("/usr/sbin/php-fpm*"));+ EXPECT_TRUE(path_valid("/usr/sbin/*fpm?.?"));+ EXPECT_TRUE(path_valid("/*"));+ EXPECT_FALSE(path_valid(""));+ EXPECT_FALSE(path_valid("usr/sbin/httpd"));+ EXPECT_FALSE(path_valid("/"));+ EXPECT_FALSE(path_valid("/usr/sbin/"));+ EXPECT_FALSE(path_valid("/usr/sbin/php-fpm*/"));+ EXPECT_FALSE(path_valid("/opt/*/bin/app"));+ EXPECT_FALSE(path_valid("/opt/app?/bin/app"));+ EXPECT_FALSE(path_valid("/*/*"));+}++static void policy_path_ends_within_its_field_and_its_limit(void)+{+ char path[UPATCH_POLICY_PATH_MAX];++ memset(path, 'a', sizeof(path));+ path[0] = '/';+ EXPECT_FALSE(policy_path_valid(path, sizeof(path)));+ EXPECT_FALSE(policy_path_valid(path, 0));+ path[UPATCH_POLICY_PATH_LEN_MAX] = '\0';+ EXPECT_TRUE(policy_path_valid(path, sizeof(path)));+ EXPECT_FALSE(policy_path_valid(path, UPATCH_POLICY_PATH_LEN_MAX));+ path[UPATCH_POLICY_PATH_LEN_MAX] = 'a';+ path[UPATCH_POLICY_PATH_LEN_MAX + 1] = '\0';+ EXPECT_FALSE(policy_path_valid(path, sizeof(path)));+}++static void policy_path_pattern_is_the_last_component_with_a_wildcard(void)+{+ static const char literal[] = "/usr/sbin/httpd";+ static const char pattern[] = "/usr/sbin/php-fpm*";+ static const char in_root[] = "/v?";++ EXPECT_TRUE(!policy_path_pattern(literal));+ EXPECT_STREQ(policy_path_pattern(pattern), "php-fpm*");+ EXPECT_TRUE(policy_path_pattern(pattern) == pattern + 10);+ EXPECT_STREQ(policy_path_pattern(in_root), "v?");+}+static void kprobe_sym_is_one_plain_name(void){char name[UPATCH_KPROBE_SYM_MAX + 1];@@ -510,6 +560,9 @@ static const struct unit_case cases[] = {UNIT_CASE(kprobe_multi_names_are_parsed_in_order),UNIT_CASE(kprobe_multi_refuses_a_malformed_list),UNIT_CASE(kprobe_multi_block_is_exact),+ UNIT_CASE(policy_path_takes_a_wildcard_in_its_last_component),+ UNIT_CASE(policy_path_ends_within_its_field_and_its_limit),+ UNIT_CASE(policy_path_pattern_is_the_last_component_with_a_wildcard),UNIT_CASE(kprobe_sym_is_one_plain_name),UNIT_CASE(header_name_is_terminated),UNIT_CASE(prog_name_is_what_the_kernel_takes),diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex dd21a13e..ea34e78b 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -1182,7 +1182,8 @@ static int __build_validate(struct bpm_build_spec *spec,(p->bit & (p->bit - 1)))return -EINVAL;for (int path = 0; path < p->n_paths; path++)- if (p->paths[path][0] != '/')+ if (!policy_path_valid(p->paths[path],+ sizeof(p->paths[path])))return -EINVAL;for (size_t x = 0; x <= i; x++) {struct bpm_build_upatch *other = &spec->upatches[x];diff --git a/util/upatch.c b/util/upatch.cindex fe1d98df..0b953562 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -142,6 +142,30 @@ static bool has_nul(const char *s, size_t n)return memchr(s, '\0', n);}+bool policy_path_valid(const char *path, size_t size)+{+ size_t end = min(size, (size_t)UPATCH_POLICY_PATH_LEN_MAX + 1);+ bool wildcard = false;+ size_t i;++ if (!size || path[0] != '/')+ return false;+ for (i = 1; i < end && path[i]; i++) {+ if (path[i] == '*' || path[i] == '?')+ wildcard = true;+ else if (path[i] == '/' && wildcard)+ return false;+ }+ return i < end && path[i - 1] != '/';+}++const char *policy_path_pattern(const char *path)+{+ const char *name = strrchr(path, '/') + 1;++ return strchr(name, '*') || strchr(name, '?') ? name : NULL;+}+static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,__u32 *map_idx, __u32 *socket_map_idx){@@ -169,7 +193,7 @@ static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,bit = le64_to_cpu(e.bit);if (!bit || (bit & (bit - 1)) ||!has_nul(e.policy, sizeof(e.policy)) || !e.policy[0] ||- !has_nul(e.path, sizeof(e.path)) || e.path[0] != '/')+ !policy_path_valid(e.path, sizeof(e.path)))return false;}*map_idx = idx;diff --git a/util/upatch.h b/util/upatch.hindex fb446f8e..13c60677 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -197,6 +197,20 @@ static inline bool kind_is_kprobe_multi(__u32 kind)return kind == UPATCH_ATTACH_KPROBE_MULTI || kind == UPATCH_ATTACH_KRETPROBE_MULTI;}+/*+ * Whether the size bytes at path hold a string that a policy label can+ * name files with: an absolute path of at most UPATCH_POLICY_PATH_LEN_MAX+ * bytes that does not end in '/', with '*' and '?' in its last component+ * alone.+ */+bool policy_path_valid(const char *path, size_t size);++/*+ * The last component of a path policy_path_valid() took where it is a+ * pattern for name_glob_match(), else NULL: the path then names one file.+ */+const char *policy_path_pattern(const char *path);+/** Whether the len bytes at sym can be one name of a kprobe multi target:* 1 to UPATCH_KPROBE_SYM_MAX - 1 printable ASCII bytes, none of them a