Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: fix get query caching #1449

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

crozzy
Copy link
Contributor

@crozzy crozzy commented Dec 9, 2024

This change replaces caching the DB pgx.Rows, which is an iterator, with the actual potential vulnerabilities. Previously, subsequent records manifesting in the same query were given a pgx.Rows object that had been exhausted.

@crozzy crozzy requested a review from a team as a code owner December 9, 2024 23:13
@crozzy crozzy requested review from hdonnay and removed request for a team December 9, 2024 23:13
@crozzy
Copy link
Contributor Author

crozzy commented Dec 9, 2024

Post-change:

    "186": {
      "id": "186",
      "name": "xz-libs",
      "version": "5.2.2-1.el7",
      "kind": "binary",
      "source": {
        "id": "185",
        "name": "xz",
        "version": "5.2.2-1.el7",
        "kind": "source"
      },
      "arch": "x86_64"
    },
...
    "1924": {
      "id": "1924",
      "name": "xz-libs",
      "version": "5.2.2-1.el7",
      "kind": "binary",
      "source": {
        "id": "185",
        "name": "xz",
        "version": "5.2.2-1.el7",
        "kind": "source"
      },
      "arch": "i686"
    },
...
    "2675687": {
      "id": "2675687",
      "updater": "rhel-vex",
      "name": "CVE-2022-1271",
      "description": "An arbitrary file write vulnerability was found in GNU gzip's zgrep utility. When zgrep is applied on the attacker's chosen file name (for example, a crafted file name), this can overwrite an attacker's content to an arbitrary attacker-selected file. This flaw occurs due to insufficient validation when processing filenames with two or more newlines where selected content and the target file names are embedded in crafted multi-line file names. This flaw allows a remote, low privileged attacker to force zgrep to write arbitrary files on the system.",
      "issued": "2022-04-07T00:00:00Z",
      "links": "https://access.redhat.com/security/cve/CVE-2022-1271 https://bugzilla.redhat.com/show_bug.cgi?id=2073310 https://www.cve.org/CVERecord?id=CVE-2022-1271 https://nvd.nist.gov/vuln/detail/CVE-2022-1271 https://security.access.redhat.com/data/csaf/v2/vex/2022/cve-2022-1271.json https://access.redhat.com/errata/RHSA-2022:5052",
      "severity": "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
      "normalized_severity": "High",
      "package": {
        "id": "",
        "name": "xz-libs",
        "version": "",
        "kind": "binary",
        "arch": "i686|ppc|ppc64|ppc64le|s390|s390x|amd64|x86_64"
      },
      "distribution": {
        "id": "",
        "did": "",
        "name": "",
        "version": "",
        "version_code_name": "",
        "version_id": "",
        "arch": "",
        "cpe": "",
        "pretty_name": ""
      },
      "repository": {
        "name": "cpe:2.3:o:redhat:enterprise_linux:7:*:server:*:*:*:*:*",
        "key": "rhel-cpe-repository",
        "cpe": "cpe:2.3:o:redhat:enterprise_linux:7:*:server:*:*:*:*:*"
      },
      "fixed_in_version": "0:5.2.2-2.el7_9",
      "arch_op": "pattern match"
    },
...
"package_vulnerabilities": {
    "186": [
      "2675687"
    ],
...
    "1924": [
      "2675687"
    ],
}

hdonnay
hdonnay previously approved these changes Dec 10, 2024
This change replaces caching the DB pgx.Rows, which is an iterator, with
the actual potential vulnerabilities. Previously, subsequent records
manifesting in the same query were given a pgx.Rows object that had been
exhausted.

Signed-off-by: crozzy <[email protected]>
@crozzy
Copy link
Contributor Author

crozzy commented Dec 10, 2024

@hdonnay sorry just had to update

continue
}
results[rid] = []*claircore.Vulnerability{}
rows, err := res.Query()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are missing a rows.Close() at the end of the for rows.Next() loop

if err != nil {
res.Close()
return nil, err
if vulns != nil { // We already have results we don't need to go back to the DB.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get why it was done this way (avoids more indentations for the larger block after this), but it is a bit unfortunate we do the same exact thing after the for rows.Next() loop.

I'm wondering if we should explore making a separate function for the outer loop and inner loop so we can also defer some Close calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants