Skip to contentSkip to conversation

strak: add cve listing tests

PATCH Sasha Levin 1 participant 6 messages Apr 29 4 messages not in the archive

lore
Share & tools
lore
#2 lore raw
In reply to a message not in the archive
Quoted 8 lines · Jorge Marques

Map some expected results in different scenarios, in particular, commits merged on a stable tag, but created on a previous stable tag. The tests also allow for assessing querying performance.

---
 tools/strak/tests/strak.rs | 57 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

The five new tests pin the bug nicely; the negative cases (cve_fix_in_history, cve_newer_than_commit) are exactly what's needed to guard against an overcorrection. I confirmed locally that these tests fail on master and pass with patches 2 and 3 applied.

One missing case that would have caught a regression in patch 3: a stable tag input asking about a fix released in that same major. See my comments on patch 3/3 for details, but please add something equivalent to:

	#[test]
	fn cve_fix_in_same_major_release() {
	    let mut cmd = Command::new(cargo::cargo_bin!("strak"));
	    cmd.arg("v6.12")
	        .assert()
	        .success()
	        // CVE-2024-46869 fix is at 6.12 (7ffaa20025187).
	        .stdout(predicate::str::contains("is vulnerable to CVE-2024-46869").not());
	}

Review

Quoted 2 lines
(quoted diff) +2 -0
+/// Commit based on v6.11-rc2, merged into v6.12.
+const TOPIC_BRANCH_COMMIT: &str = "bbf3c7ff9dfa45be51500d23a1276991a7cd8c6e";

Worth a brief note in the file (or commit message) that these are integration tests against the live published/ tree and a working CVEKERNELTREE, so they will silently break if a referenced CVE's dyad ever changes. Pinning by SHA helps; pinning by CVE id less so.

With the extra test (and the fix in 3/3) this looks good to me.

--
Thanks,
Sasha

#5 lore raw

Re: [PATCH 2/3] strak: use commit window in ambiguous checks

In reply to a message not in the archive
Quoted 6 lines · Jorge Marques

Strak would fail to report a vulnerabilities from a commit in a topic branch that got merged after the version with the fix was released. For example, commit 0c3 was created before 4f3, with 4f3 first at 6.11 and 0c3 first at 6.12, strack reports commit 0c3 not vulnerable by cve fixed-by 4f3. Because it would compare 6.11 < 6.12 and assume commit A is newer than release tag 6.11.

s/strack/strak/. Diagnosis is correct; I reproduced the false negative on master and confirmed the fix works for the cover-letter cases:

  $ time strak bbf3c7ff9dfa45be51500d23a1276991a7cd8c6e
  ... is vulnerable to CVE-2024-43884
  ... is vulnerable to CVE-2024-46823
  ... NOT vulnerable to CVE-2024-26600 (fix in v6.8 history)
  ... NOT vulnerable to CVE-2024-50084 (vuln introduced after base)
  real 0m0.76s

Few comments on the implementation:

Review

Quoted 3 lines
(quoted diff) +3 -0
+/// Find the previous stable (non-rc) release tag before the given base tag.
+/// v6.11-rc2 -> v6.10
+/// v6.11 -> v6.11

The second example is wrong:

  $ git describe --abbrev=0 --match='v[0-9]*' --exclude='*-rc*' v6.11~1
  v6.10

It always walks back at least one stable. This is harmless because the caller takes an early return for stable-tag inputs before calling previous_stable_tag(), but please update the doc comment (and the matching paragraph in the cover letter that states "v6.11 -> v6.11").

Review

Quoted 6 lines
(quoted diff) +6 -0
+    let rev_list_output = run_command(
+        "git",
+        &["rev-list", &rev_list_range],
+        Some(&kernel_tree_str),
+    )
+    .unwrap_or_default();

Please don't swallow this. cve_utils::run_command returns Err on any non-zero exit, and unwrap_or_default() turns that into an empty HashSet, which then makes do_is_ancestor()'s ambiguous branch silently return "fix not in ancestry" -> false-vulnerable. Either propagate the error (it's already a Result-returning fn) or at minimum log it loudly:

	let rev_list_output = run_command(...)?;          // preferred

	// or if you really want to be lenient:
	let rev_list_output = run_command(...).unwrap_or_else(|e| {
	    error!("rev-list failed: {e}");
	    String::new()
	});

Review

Quoted 8 lines
(quoted diff) +8 -0
+    let describe_output = run_command(
+        "git",
+        &["describe", "--always", git_sha],
+        Some(&kernel_tree_str),
+    )?;
+
+    // "v6.11-rc2-8-gbbf3c7ff9dfa" -> base tag "v6.11-rc2"
+    let base_tag = if let Some(pos) = describe_output.rfind("-g") {

Two related concerns here:

1. --always falls back to a bare abbreviated SHA when no tag is reachable. Linux mainline always has a reachable tag so this is theoretical, but the no-"-g" early-return path then treats a bare SHA as if it were a stable tag (empty ancestor_set), which causes exactly the kind of false negative this patch is trying to fix. Either drop --always (let it fail loudly on tag-less commits), or reject outputs that don't start with 'v'.

2. rfind("-g") matches anywhere in the string. Mainline tags don't contain "-g", but a defensive pattern would be:

	if let Some(rest) = describe_output.strip_prefix('v')
	    && let Some((tag, _abbrev)) = rest.rsplit_once("-g")
	    && tag.rsplit_once('-').is_some_and(|(_, n)| n.parse::<u64>().is_ok())
	{
	    ...
	}

Or document the assumption with a /// comment.

Review

Quoted 6 lines
(quoted diff) +6 -0
+    } else {
+        let base_tag_version = describe_output
+            .strip_prefix('v')
+            .unwrap_or(&describe_output)
+            .to_string();
+        // No ambiguiety when the commit is a stable tag.

s/ambiguiety/ambiguity/ (also at line ~127 in the diff).

This early return is the source of the patch-3 regression I describe in the 3/3 reply: it produces an empty ancestor_set, which patch 3's new same-major branch then treats as authoritative. Easiest fix is to *always* run the rev-list (using describe_output itself as the base tag here), so that ancestor_set is correct regardless of input form.

Quoted 1 line

+ let prev_stable = previous_stable_tag(kernel_tree, &base_tag)?;

Bounded-window assumption (one stable release back) deserves a one-line note in the commit message that explicitly calls out the failure mode:
a topic-branch commit that took >2 stable releases to merge will be treated as if its base tag were prev_stable, missing fixes that landed in the skipped window. Cover letter mentions extending N if needed - worth restating in this patch's commit message so it isn't lost.

Review

Quoted 3 lines
(quoted diff) +2 -1
+/// Wrapper for is_ancestor() so we can get some debugging output easier.
-fn do_is_ancestor(first: &Kernel, second: &Kernel) -> bool {
+fn do_is_ancestor(first: &Kernel, second: &Kernel, ctx: &AncestryWindow) -> bool {

The &first.git_id()[..12.min(first.git_id().len())] pattern shows up in three new debug! calls; consider a small helper like short_sha(s: &str) -> &str to keep the call sites tidy.

With the rev-list error path tightened up and the early-return reworked so ancestor_set is built unconditionally, this patch is in good shape.

--
Thanks,
Sasha

#8 lore raw

Re: [PATCH 3/3] strak: use hash set to compare in the same major

In reply to a message not in the archive
Quoted 1 line · Jorge Marques

Subject: [PATCH 3/3] strack: use hash set to compare in the same major

s/strack/strak/ in the subject.

Quoted 3 lines

Since strak fast-path is fix.released() < commit.released() && fix.released() < commit.base_tag(), we can use the cached hashset for the ancestor check.

Commit message describes patch 2/3's gate, not what this patch changes.

The correctness side, though, has a regression:

Review

Quoted 11 lines
(quoted diff) +11 -0
+    if first.version_major_match(second) {
+        if first.version() == second.version() {
+            let in_ancestry = ctx.ancestor_set.contains(&first.git_id());
+            debug!(
+                "    [ancestor set] same version {}, sha {} in ancestry: {}",
+                first.version(),
+                &first.git_id()[..12.min(first.git_id().len())],
+                in_ancestry
+            );
+            return in_ancestry;
+        }

When the strak input is a stable tag (e.g. v6.12), git describe returns just v6.12 with no -g, so build_ancestry_context() takes its early-return path (patch 2 main.rs:124-131) and produces an EMPTY ancestor_set. This branch then unconditionally returns false for any fix whose released version equals the test_kernel's version - i.e. for any fix that landed in the queried mainline release.

The OLD code at this point was:

	if first.version_major_match(second) && first < second {
		return true;
	}

where first < second for equal version strings falls into Kernel::compare()'s git_sort_ids() path - slow, but correct. With patch 3 we replace that with a HashSet lookup against an empty set.

Please also add a test along the lines of:

	#[test]
	fn cve_fix_in_same_major_release() {
	    Command::new(cargo::cargo_bin!("strak"))
	        .arg("v6.12")
	        .assert()
	        .success()
	        .stdout(predicate::str::contains("is vulnerable to CVE-2024-46869").not());
	}

A regression of this size with no failing test is exactly what the test-first patch in 1/3 was supposed to prevent. Worth covering it.

Review

Quoted 5 lines
(quoted diff) +5 -0
+        if first < second {
+            debug!("    [fast path] major match, {} < {}", first.version(), second.version());
+            return true;
+        }
+        return false;

Note: this changes behaviour even on populated ancestor_sets where versions differ but git_sort_ids would disagree with the version ordering. In practice version-major-match implies version-string ordering matches commit ordering, so this should be fine, but worth mentioning in the commit message.

--
Thanks,
Sasha

#3 lore raw
In reply to a message not in the archive
Quoted 8 lines · Jorge Marques

Map some expected results in different scenarios, in particular, commits merged on a stable tag, but created on a previous stable tag. The tests also allow for assessing querying performance.

---
 tools/strak/tests/strak.rs | 57 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

The five new tests pin the bug nicely; the negative cases (cve_fix_in_history, cve_newer_than_commit) are exactly what's needed to guard against an overcorrection. I confirmed locally that these tests fail on master and pass with patches 2 and 3 applied.

One missing case that would have caught a regression in patch 3: a stable tag input asking about a fix released in that same major. See my comments on patch 3/3 for details, but please add something equivalent to:

	#[test]
	fn cve_fix_in_same_major_release() {
	    let mut cmd = Command::new(cargo::cargo_bin!("strak"));
	    cmd.arg("v6.12")
	        .assert()
	        .success()
	        // CVE-2024-46869 fix is at 6.12 (7ffaa20025187).
	        .stdout(predicate::str::contains("is vulnerable to CVE-2024-46869").not());
	}

Review

Quoted 2 lines
(quoted diff) +2 -0
+/// Commit based on v6.11-rc2, merged into v6.12.
+const TOPIC_BRANCH_COMMIT: &str = "bbf3c7ff9dfa45be51500d23a1276991a7cd8c6e";

Worth a brief note in the file (or commit message) that these are integration tests against the live published/ tree and a working CVEKERNELTREE, so they will silently break if a referenced CVE's dyad ever changes. Pinning by SHA helps; pinning by CVE id less so.

With the extra test (and the fix in 3/3) this looks good to me.

--
Thanks,
Sasha

#6 lore raw

Re: [PATCH 2/3] strak: use commit window in ambiguous checks

In reply to a message not in the archive
Quoted 6 lines · Jorge Marques

Strak would fail to report a vulnerabilities from a commit in a topic branch that got merged after the version with the fix was released. For example, commit 0c3 was created before 4f3, with 4f3 first at 6.11 and 0c3 first at 6.12, strack reports commit 0c3 not vulnerable by cve fixed-by 4f3. Because it would compare 6.11 < 6.12 and assume commit A is newer than release tag 6.11.

s/strack/strak/. Diagnosis is correct; I reproduced the false negative on master and confirmed the fix works for the cover-letter cases:

  $ time strak bbf3c7ff9dfa45be51500d23a1276991a7cd8c6e
  ... is vulnerable to CVE-2024-43884
  ... is vulnerable to CVE-2024-46823
  ... NOT vulnerable to CVE-2024-26600 (fix in v6.8 history)
  ... NOT vulnerable to CVE-2024-50084 (vuln introduced after base)
  real 0m0.76s

Few comments on the implementation:

Review

Quoted 3 lines
(quoted diff) +3 -0
+/// Find the previous stable (non-rc) release tag before the given base tag.
+/// v6.11-rc2 -> v6.10
+/// v6.11 -> v6.11

The second example is wrong:

  $ git describe --abbrev=0 --match='v[0-9]*' --exclude='*-rc*' v6.11~1
  v6.10

It always walks back at least one stable. This is harmless because the caller takes an early return for stable-tag inputs before calling previous_stable_tag(), but please update the doc comment (and the matching paragraph in the cover letter that states "v6.11 -> v6.11").

Review

Quoted 6 lines
(quoted diff) +6 -0
+    let rev_list_output = run_command(
+        "git",
+        &["rev-list", &rev_list_range],
+        Some(&kernel_tree_str),
+    )
+    .unwrap_or_default();

Please don't swallow this. cve_utils::run_command returns Err on any non-zero exit, and unwrap_or_default() turns that into an empty HashSet, which then makes do_is_ancestor()'s ambiguous branch silently return "fix not in ancestry" -> false-vulnerable. Either propagate the error (it's already a Result-returning fn) or at minimum log it loudly:

	let rev_list_output = run_command(...)?;          // preferred

	// or if you really want to be lenient:
	let rev_list_output = run_command(...).unwrap_or_else(|e| {
	    error!("rev-list failed: {e}");
	    String::new()
	});

Review

Quoted 8 lines
(quoted diff) +8 -0
+    let describe_output = run_command(
+        "git",
+        &["describe", "--always", git_sha],
+        Some(&kernel_tree_str),
+    )?;
+
+    // "v6.11-rc2-8-gbbf3c7ff9dfa" -> base tag "v6.11-rc2"
+    let base_tag = if let Some(pos) = describe_output.rfind("-g") {

Two related concerns here:

1. --always falls back to a bare abbreviated SHA when no tag is reachable. Linux mainline always has a reachable tag so this is theoretical, but the no-"-g" early-return path then treats a bare SHA as if it were a stable tag (empty ancestor_set), which causes exactly the kind of false negative this patch is trying to fix. Either drop --always (let it fail loudly on tag-less commits), or reject outputs that don't start with 'v'.

2. rfind("-g") matches anywhere in the string. Mainline tags don't contain "-g", but a defensive pattern would be:

	if let Some(rest) = describe_output.strip_prefix('v')
	    && let Some((tag, _abbrev)) = rest.rsplit_once("-g")
	    && tag.rsplit_once('-').is_some_and(|(_, n)| n.parse::<u64>().is_ok())
	{
	    ...
	}

Or document the assumption with a /// comment.

Review

Quoted 6 lines
(quoted diff) +6 -0
+    } else {
+        let base_tag_version = describe_output
+            .strip_prefix('v')
+            .unwrap_or(&describe_output)
+            .to_string();
+        // No ambiguiety when the commit is a stable tag.

s/ambiguiety/ambiguity/ (also at line ~127 in the diff).

This early return is the source of the patch-3 regression I describe in the 3/3 reply: it produces an empty ancestor_set, which patch 3's new same-major branch then treats as authoritative. Easiest fix is to *always* run the rev-list (using describe_output itself as the base tag here), so that ancestor_set is correct regardless of input form.

Quoted 1 line

+ let prev_stable = previous_stable_tag(kernel_tree, &base_tag)?;

Bounded-window assumption (one stable release back) deserves a one-line note in the commit message that explicitly calls out the failure mode:
a topic-branch commit that took >2 stable releases to merge will be treated as if its base tag were prev_stable, missing fixes that landed in the skipped window. Cover letter mentions extending N if needed - worth restating in this patch's commit message so it isn't lost.

Review

Quoted 3 lines
(quoted diff) +2 -1
+/// Wrapper for is_ancestor() so we can get some debugging output easier.
-fn do_is_ancestor(first: &Kernel, second: &Kernel) -> bool {
+fn do_is_ancestor(first: &Kernel, second: &Kernel, ctx: &AncestryWindow) -> bool {

The &first.git_id()[..12.min(first.git_id().len())] pattern shows up in three new debug! calls; consider a small helper like short_sha(s: &str) -> &str to keep the call sites tidy.

With the rev-list error path tightened up and the early-return reworked so ancestor_set is built unconditionally, this patch is in good shape.

--
Thanks,
Sasha

#9 lore raw

Re: [PATCH 3/3] strak: use hash set to compare in the same major

In reply to a message not in the archive
Quoted 1 line · Jorge Marques

Subject: [PATCH 3/3] strack: use hash set to compare in the same major

s/strack/strak/ in the subject.

Quoted 3 lines

Since strak fast-path is fix.released() < commit.released() && fix.released() < commit.base_tag(), we can use the cached hashset for the ancestor check.

Commit message describes patch 2/3's gate, not what this patch changes.

The correctness side, though, has a regression:

Review

Quoted 11 lines
(quoted diff) +11 -0
+    if first.version_major_match(second) {
+        if first.version() == second.version() {
+            let in_ancestry = ctx.ancestor_set.contains(&first.git_id());
+            debug!(
+                "    [ancestor set] same version {}, sha {} in ancestry: {}",
+                first.version(),
+                &first.git_id()[..12.min(first.git_id().len())],
+                in_ancestry
+            );
+            return in_ancestry;
+        }

When the strak input is a stable tag (e.g. v6.12), git describe returns just v6.12 with no -g, so build_ancestry_context() takes its early-return path (patch 2 main.rs:124-131) and produces an EMPTY ancestor_set. This branch then unconditionally returns false for any fix whose released version equals the test_kernel's version - i.e. for any fix that landed in the queried mainline release.

The OLD code at this point was:

	if first.version_major_match(second) && first < second {
		return true;
	}

where first < second for equal version strings falls into Kernel::compare()'s git_sort_ids() path - slow, but correct. With patch 3 we replace that with a HashSet lookup against an empty set.

Please also add a test along the lines of:

	#[test]
	fn cve_fix_in_same_major_release() {
	    Command::new(cargo::cargo_bin!("strak"))
	        .arg("v6.12")
	        .assert()
	        .success()
	        .stdout(predicate::str::contains("is vulnerable to CVE-2024-46869").not());
	}

A regression of this size with no failing test is exactly what the test-first patch in 1/3 was supposed to prevent. Worth covering it.

Review

Quoted 5 lines
(quoted diff) +5 -0
+        if first < second {
+            debug!("    [fast path] major match, {} < {}", first.version(), second.version());
+            return true;
+        }
+        return false;

Note: this changes behaviour even on populated ancestor_sets where versions differ but git_sort_ids would disagree with the version ordering. In practice version-major-match implies version-string ordering matches commit ordering, so this should be fine, but worth mentioning in the commit message.

--
Thanks,
Sasha