Skip to content
Open
26 changes: 26 additions & 0 deletions .github/codeql/openssl-return-check.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @name Unchecked OpenSSL EVP return value
* @description Calls to OpenSSL EVP functions whose return value is not
* checked by the OQS_OPENSSL_GUARD macro may silently ignore
* errors, leading to undefined behaviour.
* @kind problem
* @problem.severity warning
* @precision medium
* @id cpp/openssl-unchecked-return
* @tags security
* correctness
*/

import cpp

from FunctionCall call, Function f
where
f = call.getTarget() and
f.getName().matches("EVP%") and
not f.getType() instanceof PointerType and
not f.getType() instanceof VoidType and
not exists(MacroAccess m |
m.getLocation().subsumes(call.getLocation()) and
m.getMacroName() = "OQS_OPENSSL_GUARD"
)
select call, "Return value of " + f.getName() + "() is not checked by OQS_OPENSSL_GUARD."
6 changes: 6 additions & 0 deletions .github/codeql/test/openssl-return-check-bad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
/* expect-fail: EVP return value not checked via OQS_OPENSSL_GUARD */
#include <openssl/evp.h>
void bad_example(EVP_MD_CTX *ctx, const EVP_MD *md) {
EVP_DigestInit_ex(ctx, md, NULL);
}
36 changes: 36 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CodeQL analysis
permissions:
contents: read
on:
workflow_call:
workflow_dispatch:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
codeql:
permissions:
contents: read
security-events: write
env:
CODEQL_EXTRACTOR_CPP_TRAP_CACHING: false
name: CodeQL
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Initialize CodeQL
uses: github/codeql-action/init@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3
with:
languages: cpp
queries: security-and-quality,./.github/codeql/openssl-return-check.ql
- name: Build liboqs
run: |
cmake -S . -B build -DOQS_MINIMAL_BUILD="KEM_ml_kem_768;SIG_ml_dsa_65"
cmake --build build --parallel $(nproc)
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3
with:
category: "/language:cpp"
upload: false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't look exactly right. With upload: false, the analyze step will not upload the scanning results to Code Scanning. I tried running it with upload set to true (and with default CodeQL setup disabled), the workflow reported successful upload, but I still could not find the corresponding entries in Code Scanning.

The liboqs repository currently enables the default CodeQL setup, which blocks code scanning results uploads from REST API (i.e. GitHub Actions) and CodeQL CLI. If we want to proceed with adding custom CodeQL queries, the default setup must be disabled, and to preserve existing code scanning we will need to manually add a codeql.yml workflow. Given how this touches privileged settings of the liboqs repository, I'm afraid that configuring custom CodeQL queries is beyond the authorization of external contributors.

cc: @dstebila

References:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @xuganyu96 for checking this out. Disabling the default CodeQL setup needs admin access so that's on your end. @dstebila @baentsch let me know how you want to proceed, happy either way.

8 changes: 8 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ jobs:
contents: read
id-token: write
security-events: write

codeql:
needs: basic-checks
uses: ./.github/workflows/codeql.yml
secrets: inherit
permissions:
contents: read
security-events: write
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,11 @@ If you feel like contributing but don't know what specific topic to work on,
please check the [open issues tagged "good first issue" or "help wanted"](https://github.com/open-quantum-safe/liboqs/issues).

You can also take a look at the [contribution wishlist](https://github.com/open-quantum-safe/liboqs/wiki/Contribution-wishlist) for more substantial contributions we are interested in.

## Verifying the CodeQL OpenSSL return-check query
The query detects EVP_* calls not wrapped in OQS_OPENSSL_GUARD.
A deliberately bad example lives in
.github/codeql/test/openssl-return-check-bad.c.
When CodeQL runs, any match against this file confirms the query
is scanning correctly. CI will flag new violations automatically
on every PR.
Loading