[PATCH] metadata: automatically detect CVE via tags

Andrea Cervesato <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
From: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>

Automatically assign a 'cve' group to test metadata when we are tagging
it with a CVE number.

Signed-off-by: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>
---
 metadata/metaparse.c      | 43 ++++++++++++++++++++++++++++++++++++++-----
 metadata/tests/cve.c      | 12 ++++++++++++
 metadata/tests/cve.c.json | 15 +++++++++++++++
 3 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/metadata/metaparse.c b/metadata/metaparse.c
index a0f94511db36e42e6174774dfa8340cb4dce113a..ce9729122e40503b4bc6318f03e6b861f9d38a5c 100644
--- a/metadata/metaparse.c
+++ b/metadata/metaparse.c
@@ -52,12 +52,14 @@ static char *eat_asterisk_space(char *c)
 }
 
 /*
- * Add a group to the groups array, skipping 'kernel' as it's too generic.
- * Returns 0 if no group was added, 1 otherwise.
+ * Add a group to the groups array, skipping path components that are too
+ * generic ('kernel') or assigned from a more reliable source ('cve', which
+ * is derived from the CVE tag instead). Returns 0 if no group was added,
+ * 1 otherwise.
  */
 static int add_group(struct data_node *groups, const char *name)
 {
-	if (name && strcmp(name, "kernel")) {
+	if (name && strcmp(name, "kernel") && strcmp(name, "cve")) {
 		data_node_array_add(groups, data_node_string(name));
 		return 1;
 	}
@@ -971,11 +973,11 @@ static void load_internal_macros(void)
  * Add groups derived from the source file path.
  *
  * Groups are the two nearest parent directories (immediate parent
- * first), skipping 'kernel' as it's too generic:
+ * first), skipping 'kernel' (too generic) and 'cve' (assigned from the
+ * CVE tag instead, see add_tag_groups()):
  *
  *   testcases/kernel/syscalls/clone/clone01.c  -> clone, syscalls
  *   testcases/kernel/kvm/kvm_pagefault01.c     -> kvm
- *   testcases/cve/cve-2017-16939.c             -> cve
  */
 static void add_path_groups(struct data_node *groups, const char *fname)
 {
@@ -1010,6 +1012,35 @@ static void add_path_groups(struct data_node *groups, const char *fname)
 	free(buf);
 }
 
+/*
+ * Add group to specific test tags.
+ */
+static void add_tag_groups(struct data_node *groups, struct data_node *res)
+{
+	struct data_node *tags = data_node_hash_get(res, "tags");
+	unsigned int i;
+
+	if (!tags || tags->type != DATA_ARRAY)
+		return;
+
+	for (i = 0; i < data_node_array_len(tags); i++) {
+		struct data_node *tag = tags->array.array[i];
+		struct data_node *name;
+
+		if (tag->type != DATA_ARRAY || !data_node_array_len(tag))
+			continue;
+
+		name = tag->array.array[0];
+		if (name->type != DATA_STRING)
+			continue;
+
+		if (!strcmp(name->string.val, "CVE")) {
+			data_node_array_add(groups, data_node_string("cve"));
+			return;
+		}
+	}
+}
+
 static struct data_node *parse_file(const char *fname)
 {
 	int state = 0, found = 0;
@@ -1064,6 +1095,8 @@ static struct data_node *parse_file(const char *fname)
 		data_node_free(doc);
 	}
 
+	add_tag_groups(groups, res);
+
 	/*
 	 * Always emit groups, even when empty: tests outside testcases/
 	 * and files whose only parent dir is 'kernel' produce no groups.
diff --git a/metadata/tests/cve.c b/metadata/tests/cve.c
new file mode 100644
index 0000000000000000000000000000000000000000..c2b0e551f111ae87f3c6dcb50ef25c66967443f4
--- /dev/null
+++ b/metadata/tests/cve.c
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/*\
+ * Test for cve group derived from the CVE tag.
+ */
+
+static struct tst_test test = {
+	.tags = (const struct tst_tag[]) {
+		{"CVE", "2017-16939"},
+		{}
+	}
+};
diff --git a/metadata/tests/cve.c.json b/metadata/tests/cve.c.json
new file mode 100644
index 0000000000000000000000000000000000000000..543d9e58a3e78d296cc4247659d1b26f2a103245
--- /dev/null
+++ b/metadata/tests/cve.c.json
@@ -0,0 +1,15 @@
+  "cve": {
+   "tags": [
+    [
+     "CVE",
+     "2017-16939"
+    ]
+   ],
+   "doc": [
+    "Test for cve group derived from the CVE tag."
+   ],
+   "groups": [
+    "cve"
+   ],
+   "fname": "cve.c"
+  }
\ No newline at end of file

---
base-commit: b2184c0f2debb976414b294b8527298bdac0a877
change-id: 20260622-metadata_cve-25cf42b4f38d

Best regards,
-- 
Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.