eBPF-based security platform
coccinelle: refuse an option compared by hand outside the parser
Daniel Borkmann210932cd51b0
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex b13fc658..2b8d6d2e 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -660,6 +660,12 @@ can rewrite what it finds does so with ``-D patch``:* - ``literal_len``- A length that does not match the string literal it compares orcopies.+ * - ``option_by_hand``+ - A value compared with an option name, a literal that starts with+ ``-`` and is longer than one character, outside+ :src:`util/options.c`, which parses every command's options from+ its table, and the help and image gate code that looks for+ ``--help`` and the options an image refuses.* - ``test_after_use``- A pointer tested for NULL after its use.* - ``null_deref``diff --git a/shirudo.c b/shirudo.cindex 2bbbbd20..0045fdb8 100644--- a/shirudo.c+++ b/shirudo.c@@ -84,7 +84,7 @@ static void print_group(enum cmd_group g, const char *header)static int cmd_help(int argc, const char **argv){- if (argc > 1 && strcmp(argv[1], "--list-cmds") == 0) {+ if (argc > 1 && is_list_cmds(argv[1])) {for (size_t i = 0; i < commands_n; i++)printf("%s\n", commands[i].name);return 0;diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 3b064027..00509803 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -601,6 +601,42 @@ test_expect_success 'dead_store reports a store stored over and spares the reads[ "$(grep -c "^dead_store: samples.c:" "$W/err")" = 1 ]'+# An option compared by hand, which option_by_hand reports, beside the+# standard output path "-", which is no option, and the parser itself,+# whose file the rule leaves alone.+write_option_samples() {+ cat >"$1/samples.c" <<'EOF'+int by_hand(int argc, const char **argv)+{+ return argc > 1 && !strcmp(argv[1], "--json");+}++int to_stdout(const char *path)+{+ return !strcmp(path, "-");+}+EOF+ mkdir -p "$1/util" &&+ cat >"$1/util/options.c" <<'EOF'+int is_end(const char *arg)+{+ return !strcmp(arg, "--");+}+EOF+}++test_expect_success 'option_by_hand reports an option compared outside the parser' '+ R7=$W/option &&+ mkdir -p "$R7/testing/coccinelle" &&+ git init -q "$R7" &&+ cp "$ROOT/testing/coccinelle/option_by_hand.cocci" \+ "$R7/testing/coccinelle/" &&+ write_option_samples "$R7" &&+ ! (cd "$R7" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^option_by_hand: samples.c:3:" "$W/err" &&+ [ "$(grep -c "^option_by_hand:" "$W/err")" = 1 ]+'+# A spatch that gives up, as a rule with --timeout makes it: the file it# gave up on is named and the check passes, while another failure fails.write_fake_spatch() {diff --git a/testing/coccinelle/option_by_hand.cocci b/testing/coccinelle/option_by_hand.coccinew file mode 100644index 00000000..0d94ac9c--- /dev/null+++ b/testing/coccinelle/option_by_hand.cocci@@ -0,0 +1,39 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A command reads its options from an opt_spec table through+// util/options.c, which holds every rule they follow; an argument+// compared with an option name by hand skips those rules.++virtual report++@r@+expression E, F;+constant char[] S;+identifier f = {strcmp, strncmp, strcasecmp, strncasecmp, str_has_prefix,+ str_skip_prefix};+position p;+@@+(+ f@p(E, S, ...)+|+ f@p(S, E, ...)+|+ span_equals@p(E, F, S)+)++@script:python depends on report@+p << r.p;+S << r.S;+@@+import ast+try:+ text = ast.literal_eval(S)+except (ValueError, SyntaxError):+ text = ""+exempt = ("util/options.c", "util/help.c", "util/image_runtime.c",+ "tools/gen-vmlinux-h/", "testing/")+if text.startswith("-") and len(text) > 1 and \+ not p[0].file.startswith(exempt):+ coccilib.report.print_report(p[0],+ "%s compared by hand; name it in the command's opt_spec table" % text)