Skip to content

Enhance CERT rules with priority and level tags #895

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

Closed

Conversation

felickz
Copy link

@felickz felickz commented May 1, 2025

Description

please enter the description of your change here

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • rule number here
  • Queries have been modified for the following rules:
    • Metadata for all CERT C rules

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

  • Only adding metadata

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

Query development review checklist

For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:

Author

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

@Copilot Copilot AI review requested due to automatic review settings May 1, 2025 02:44
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Enhance existing CERT C rule queries by adding priority and level metadata tags.

  • Inserted external/cert/priority and external/cert/level tags into rule headers.
  • No changes to query logic or tests.

Reviewed Changes

Copilot reviewed 115 out of 115 changed files in this pull request and generated no comments.

Show a summary per file
File Description
c/cert/src/rules/CON40-C/AtomicVariableTwiceInExpression.ql Added priority and level metadata
c/cert/src/rules/CON39-C/ThreadWasPreviouslyJoinedOrDetached.ql Added priority and level metadata
c/cert/src/rules/CON38-C/PreserveSafetyWhenUsingConditionVariables.ql Added priority and level metadata
c/cert/src/rules/CON37-C/DoNotCallSignalInMultithreadedProgram.ql Added priority and level metadata
c/cert/src/rules/CON36-C/WrapFunctionsThatCanSpuriouslyWakeUpInLoop.ql Added priority and level metadata
c/cert/src/rules/CON35-C/DeadlockByLockingInPredefinedOrder.ql Added priority and level metadata
c/cert/src/rules/CON34-C/ThreadObjectStorageDurationsNotInitialized.ql Added priority and level metadata
c/cert/src/rules/CON34-C/AppropriateThreadObjectStorageDurations.ql Added priority and level metadata
c/cert/src/rules/CON33-C/RaceConditionsWhenUsingLibraryFunctions.ql Added priority and level metadata
c/cert/src/rules/CON32-C/PreventDataRacesWithMultipleThreads.ql Added priority and level metadata
c/cert/src/rules/CON31-C/DoNotDestroyAMutexWhileItIsLocked.ql Added priority and level metadata
c/cert/src/rules/CON31-C/DoNotAllowAMutexToGoOutOfScopeWhileLocked.ql Added priority and level metadata
c/cert/src/rules/CON30-C/CleanUpThreadSpecificStorage.ql Added priority and level metadata
c/cert/src/rules/ARR39-C/DoNotAddOrSubtractAScaledIntegerToAPointer.ql Added priority and level metadata
c/cert/src/rules/ARR38-C/LibraryFunctionArgumentOutOfBounds.ql Added priority and level metadata
c/cert/src/rules/ARR37-C/DoNotUsePointerArithmeticOnNonArrayObjectPointers.ql Added priority and level metadata
c/cert/src/rules/ARR36-C/DoNotSubtractPointersThatDoNotReferToTheSameArray.ql Added priority and level metadata
c/cert/src/rules/ARR36-C/DoNotRelatePointersThatDoNotReferToTheSameArray.ql Added priority and level metadata
c/cert/src/rules/ARR32-C/VariableLengthArraySizeNotInValidRange.ql Added priority and level metadata
c/cert/src/rules/ARR30-C/DoNotFormOutOfBoundsPointersOrArraySubscripts.ql Added priority and level metadata

@felickz
Copy link
Author

felickz commented May 1, 2025

I would eventually want to create a set of suites per level, but i see another PR that is in the middle of refactoring suites for CertC #880

ex: cert-l1.qls

- description: CERT C 2016 Level 1 Rules (Priority 12 - Priority 27)
- qlpack: codeql/cert-c-coding-standards
- include:
    kind:
    - problem
    - path-problem
    tags contain:
    - external/cert/level/l1

@felickz
Copy link
Author

felickz commented May 1, 2025

Looks like i have some work to do to automate the generation here 🫤 Was not aware this was a thing

https://github.com/github/codeql-coding-standards/actions/runs/14768736793/job/41465018586?pr=895

@felickz felickz marked this pull request as draft May 1, 2025 03:08
@lcartey
Copy link
Collaborator

lcartey commented May 2, 2025

Thanks @felickz! I've put up a new PR here which fully automates this process, and validates the tags, as well as adding the relevant query suites:
#896

Feel free to add any comments to that PR, but it hopefully it addresses the requirements!

@felickz
Copy link
Author

felickz commented May 2, 2025

Shutting down this PR 💟

Also this work led to creating a few suites for a quick help - with the level tag metadata, we can make some much cleaner suites by filtering down on L1 / L2 / L3 🎉

codeql database analyze .codeql/cpp .codeql/codeql-suites/cert-p1.qls .codeql/codeql-suites/cert-p2.qls .codeql/codeql-suites/cert-p3.qls .codeql/codeql-suites/cert-p4.qls --format=sarif-latest --output=cpp.sarif --rerun

cert-p1.qls

#- description: "CERT C Priority P1 (lowest priority) Rules"
#        - c/cert/dcl39-c/information-leakage-across-trust-boundaries-c
#        - c/cert/sig35-c/do-not-return-from-a-computational-exception-handler
- qlpack: codeql/cert-c-coding-standards
- include:
    id:
    - c/cert/information-leakage-across-trust-boundaries-c
    - c/cert/do-not-return-from-a-computational-exception-handler

cert-p2.qls

#- description: "CERT C Priority P2 (low priority) Rules"
#          - c/cert/dcl40-c/incompatible-object-declarations
#          - c/cert/dcl40-c/incompatible-function-declarations
#          - c/cert/dcl40-c/excess-length-names-identifiers-not-distinct
#          - c/cert/exp39-c/do-not-access-variable-via-pointer-of-incompatible-type
#          - c/cert/exp40-c/do-not-modify-constant-objects
#          - c/cert/msc38-c/do-not-treat-a-predefined-identifier-as-object
#          - c/cert/msc40-c/do-not-violate-in-line-linkage-constraints
#          - c/cert/int34-c/expr-shiftedby-negative-or-greater-precision-operand
#          - c/cert/int35-c/use-correct-integer-precisions
#          - c/cert/int36-c/converting-a-pointer-to-integer-or-integer-to-pointer
#          - c/cert/con36-c/wrap-functions-that-can-spuriously-wake-up-in-loop
#          - c/cert/con38-c/preserve-safety-when-using-condition-variables
#          - c/cert/con41-c/wrap-functions-that-can-fail-spuriously-in-loop
#          - c/cert/pre32-c/macro-or-function-args-contain-hash-token

- qlpack: codeql/cert-c-coding-standards
- include:
    id:
        - c/cert/incompatible-object-declarations
        - c/cert/incompatible-function-declarations
        - c/cert/excess-length-names-identifiers-not-distinct
        - c/cert/do-not-access-variable-via-pointer-of-incompatible-type
        - c/cert/do-not-modify-constant-objects
        - c/cert/do-not-treat-a-predefined-identifier-as-object
        - c/cert/do-not-violate-in-line-linkage-constraints
        - c/cert/expr-shiftedby-negative-or-greater-precision-operand
        - c/cert/use-correct-integer-precisions
        - c/cert/converting-a-pointer-to-integer-or-integer-to-pointer
        - c/cert/wrap-functions-that-can-spuriously-wake-up-in-loop
        - c/cert/preserve-safety-when-using-condition-variables
        - c/cert/wrap-functions-that-can-fail-spuriously-in-loop
        - c/cert/macro-or-function-args-contain-hash-token

cert-p3.qls

#- description: "CERT C Priority P3 (medium-low priority) Rules"
#          - c/cert/dcl31-c/declare-identifiers-before-using-them
#          - c/cert/dcl37-c/do-not-declare-or-define-a-reserved-identifier
#          - c/cert/dcl38-c/declaring-a-flexible-array-member
#          - c/cert/exp44-c/unevaluated-operand-with-side-effect
#          - c/cert/pre31-c/side-effects-in-arguments-to-unsafe-macros
- qlpack: codeql/cert-c-coding-standards
- include:
    id:
        - c/cert/declare-identifiers-before-using-them
        - c/cert/do-not-declare-or-define-a-reserved-identifier
        - c/cert/declaring-a-flexible-array-member
        - c/cert/unevaluated-operand-with-side-effect
        - c/cert/side-effects-in-arguments-to-unsafe-macros

cert-p4.qls

#- description: "CERT C Priority P4 (medium priority) Rules"
#          - c/cert/con30-c/clean-up-thread-specific-storage
#          - c/cert/con31-c/do-not-destroy-a-mutex-while-it-is-locked
#          - c/cert/con31-c/do-not-allow-a-mutex-to-go-out-of-scope-while-locked
#          - c/cert/con33-c/race-conditions-when-using-library-functions
#          - c/cert/con34-c/appropriate-thread-object-storage-durations
#          - c/cert/con35-c/deadlock-by-locking-in-predefined-order
#          - c/cert/dcl41-c/variables-inside-switch-statement
#          - c/cert/env30-c/do-not-modify-the-return-value-of-certain-functions
#          - c/cert/env31-c/env-pointer-is-invalid-after-certain-operations
#          - c/cert/env34-c/do-not-store-pointers-returned-by-environment-fun-warn
#          - c/cert/exp35-c/do-not-modify-objects-with-temporary-lifetime
#          - c/cert/exp36-c/do-not-cast-pointer-to-more-strictly-aligned-pointer-type
#          - c/cert/exp37-c/do-not-call-functions-with-incompatible-arguments
#          - c/cert/exp43-c/do-not-pass-aliased-pointer-to-restrict-qualified-param
#          - c/cert/exp43-c/restrict-pointer-references-overlapping-object
#          - c/cert/fio32-c/do-not-perform-file-operations-on-devices
#          - c/cert/fio38-c/do-not-copy-a-file-object
#          - c/cert/fio40-c/reset-strings-on-fgets-or-fgetws-failure
#          - c/cert/fio42-c/close-files-when-they-are-no-longer-needed
#          - c/cert/fio44-c/only-use-values-for-fsetpos-that-are-returned-from-fgetpos
#          - c/cert/fio46-c/undefined-behavior-accessing-a-closed-file
- qlpack: codeql/cert-c-coding-standards
- include:
    id:
          - c/cert/clean-up-thread-specific-storage
          - c/cert/do-not-destroy-a-mutex-while-it-is-locked
          - c/cert/do-not-allow-a-mutex-to-go-out-of-scope-while-locked
          - c/cert/race-conditions-when-using-library-functions
          - c/cert/appropriate-thread-object-storage-durations
          - c/cert/deadlock-by-locking-in-predefined-order
          - c/cert/variables-inside-switch-statement
          - c/cert/do-not-modify-the-return-value-of-certain-functions
          - c/cert/env-pointer-is-invalid-after-certain-operations
          - c/cert/do-not-store-pointers-returned-by-environment-fun-warn
          - c/cert/do-not-modify-objects-with-temporary-lifetime
          - c/cert/do-not-cast-pointer-to-more-strictly-aligned-pointer-type
          - c/cert/do-not-call-functions-with-incompatible-arguments
          - c/cert/do-not-pass-aliased-pointer-to-restrict-qualified-param
          - c/cert/restrict-pointer-references-overlapping-object
          - c/cert/do-not-perform-file-operations-on-devices
          - c/cert/do-not-copy-a-file-object
          - c/cert/reset-strings-on-fgets-or-fgetws-failure
          - c/cert/close-files-when-they-are-no-longer-needed
          - c/cert/only-use-values-for-fsetpos-that-are-returned-from-fgetpos
          - c/cert/undefined-behavior-accessing-a-closed-file

@felickz felickz closed this May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants