Skip to content

Commit 3fbefc5

Browse files
committed
Merge branch 'codeql'
This patch series has been long in the making, ever since Johannes Nicolai and myself spiked this in November/December 2020. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 01c7f92 + 6f7f86c commit 3fbefc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+309
-76
lines changed

.github/codeql/codeql-config.yml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
name: "CodeQL config"
2+
3+
queries:
4+
- uses: security-extended
5+
6+
paths-ignore:
7+
- gitweb/**/*.js # GitWeb is not distributed
8+
9+
query-filters:
10+
- exclude:
11+
# yes, this extra indentation is intentional
12+
# too common in Git's source code
13+
id: cpp/trivial-switch
14+
- exclude:
15+
id: cpp/loop-variable-changed
16+
- exclude:
17+
# we override this locally with a modified version
18+
id: cpp/non-constant-format
19+
- exclude:
20+
# Git does not consider this a problem
21+
id: cpp/irregular-enum-init
22+
- exclude:
23+
# Git has many long functions, this alert would match too many
24+
id: cpp/poorly-documented-function
25+
- exclude:
26+
# In Git, there is a lot of commented-out code
27+
id: cpp/commented-out-code
28+
- exclude:
29+
# While it is true that long switch cases are hard to read and
30+
# validate, Git has way too many for us to allow this query to
31+
# churn out alerts left and right
32+
id: cpp/long-switch
33+
- exclude:
34+
# CodeQL does not expect Git to heed the umask(), but it does
35+
id: cpp/world-writable-file-creation
36+
- exclude:
37+
# Git uses the construct `if (<not this>) ; else ...` often, to
38+
# avoid an extra indentation level. CodeQL does not like that.
39+
id: cpp/empty-block
40+
- exclude:
41+
# This rule unfortunately triggers some false positives, e.g.
42+
# where Git tries to redact URLs or where Git specifically
43+
# asks for a password upon GIT_SSL_CERT_PASSWORD_PROTECTED.
44+
id: cpp/user-controlled-bypass
45+
- exclude:
46+
# This rule fails to recognize that xmallocz() _specifically_
47+
# makes room for a trailing NUL, and instead assumes that this
48+
# function behaves like malloc(), which does not.
49+
id: cpp/invalid-pointer-deref
50+
- exclude:
51+
# CodeQL fails to recognize that xmallocz() accounts for the NUL,
52+
# instead assuming malloc() semantics.
53+
id: cpp/no-space-for-terminator
54+
- exclude:
55+
# Git does exchange plain-text passwords via stdin/stdout e.g.
56+
# with helpers in the credential protocol, or in credential-cache.
57+
# This rule, though, assumes that writing to _any_ file descriptor
58+
# is unsafe.
59+
id: cpp/cleartext-storage-file
60+
- exclude:
61+
# When storing the value of the environment variable `PWD` as the
62+
# current directory in absolute_pathdup(), or when allocating memory
63+
# for a binary patch where the size is specified in the patch itself,
64+
# CodeQL assumes that this can lead to a denial of service because
65+
# of an unbounded size, but Git's code works as designed here.
66+
id: cpp/uncontrolled-allocation-size
67+
- exclude:
68+
# lock_repo_for_gc() has admittedly obtuse logic to parse the
69+
# process ID out of the `gc.pid` file, which is correct, but
70+
# due to its construction throws a false positive here.
71+
id: cpp/missing-check-scanf
72+
- exclude:
73+
# discard_cache_entry() overwrites the name in a FLEX_ARRAY struct
74+
# if GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES is set, which CodeQL fails
75+
# to recognize as valid.
76+
id: cpp/overrun-write
77+
- exclude:
78+
# Since `time_t` can be signed or unsigned, there is unfortunately
79+
# no way to avoid letting this rule report a potential
80+
id: cpp/integer-multiplication-cast-to-long
81+
- exclude:
82+
# There are many, many legitimate code paths in Git where a path is
83+
# constructed from an environment variable, e.g. GIT_DIR. Let's suppress
84+
# this slightly overzealous query.
85+
id: cpp/path-injection
86+
- exclude:
87+
# Git has 99 instances of this at the time of writing :-(
88+
id: cpp/declaration-hides-variable
89+
- exclude:
90+
id: cpp/declaration-hides-parameter
91+
- exclude:
92+
id: cpp/local-variable-hides-global-variable
93+
- exclude:
94+
id: cpp/complex-condition
95+
- exclude:
96+
# Nested, long-winded switch statements are hard to read and hard
97+
# to reason about. Looking at you, `format_commit_one()`.
98+
id: cpp/complex-block
99+
- exclude:
100+
# There are four instances of this at time of writing, all intentional.
101+
# However, it is very easy to introduce unintentional re-use of loop
102+
# variable names, therefore we will most likely want to either change these
103+
# instances or add suppressions.
104+
id: cpp/nested-loops-with-same-variable
105+
- exclude:
106+
# zOMG so many FIXMEs
107+
id: cpp/fixme-comment
108+
- exclude:
109+
# Git assumes quite a bit about the user's control of the current worktree
110+
# Therefore, it kind of assumes that TOCTOU issues are not a thing when
111+
# it comes to files.
112+
id: cpp/toctou-race-condition
113+
- exclude:
114+
# Too many results in Git where the code was, however, intentionally written
115+
# the way it is.
116+
id: cpp/stack-address-escape
117+
- exclude:
118+
id: cpp/inconsistent-null-check
119+
- exclude:
120+
# This would trigger alerts in the functions in `help.c` that want to open
121+
# external programs to show manual pages.
122+
id: cpp/uncontrolled-process-operation
123+
- exclude:
124+
# The code in t/unit-tests/u-ctype.c implicitly exercises the `sane_istest()`
125+
# macro extensively, and CodeQL seems to miss the cast to `(unsigned char)`,
126+
# thereby mistaking the accesses for being past the end of the array (which
127+
# is incorrect).
128+
#
129+
# Ideally, we would exclude test programs from CodeQL anyways, but
130+
# unfortunately there is no Makefile rule in Git's code base to build only
131+
# the production code, and CodeQL's `paths-ignore` directive described at
132+
# https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning#specifying-directories-to-scan
133+
# unfortunately is _ignored_ for compiled languages.
134+
id: cpp/overflow-buffer

.github/workflows/codeql.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: "CodeQL"
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
analyze:
10+
name: Analyze
11+
runs-on: ubuntu-latest
12+
permissions:
13+
actions: read
14+
contents: read
15+
security-events: write
16+
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
language: ["cpp", "javascript"]
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Install dependencies
27+
run: ci/install-dependencies.sh
28+
if: matrix.language == 'cpp'
29+
env:
30+
jobname: codeql
31+
32+
# Initializes the CodeQL tools for scanning.
33+
- name: Initialize CodeQL
34+
uses: github/codeql-action/init@v3
35+
with:
36+
languages: ${{ matrix.language }}
37+
config-file: ./.github/codeql/codeql-config.yml
38+
39+
- name: Build
40+
if: matrix.language == 'cpp'
41+
run: |
42+
cat /proc/cpuinfo
43+
make -j$(nproc)
44+
45+
- name: Perform CodeQL Analysis
46+
uses: github/codeql-action/analyze@v3
47+
with:
48+
upload: False
49+
output: sarif-results
50+
51+
- name: debug
52+
shell: bash
53+
run: ls -la sarif-results
54+
55+
- name: publish sarif for debugging
56+
uses: actions/upload-artifact@v4
57+
with:
58+
name: sarif-results-${{ matrix.language }}
59+
path: sarif-results
60+
61+
- name: Upload SARIF
62+
uses: github/codeql-action/upload-sarif@v3
63+
with:
64+
sarif_file: sarif-results/${{ matrix.language }}.sarif

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,5 @@ Release/
258258
CMakeSettings.json
259259
/contrib/libgit-rs/target
260260
/contrib/libgit-sys/target
261+
/.github/codeql/.cache/
262+
/.github/codeql/codeql-pack.lock.yml

branch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
224224
skip_prefix(orig_ref, "refs/heads/", &bare_ref);
225225

226226
branch = branch_get(bare_ref);
227+
if (!branch)
228+
BUG("could not get branch for '%s", bare_ref);
227229
if (!branch->remote_name) {
228230
warning(_("asked to inherit tracking from '%s', but no remote is set"),
229231
bare_ref);

builtin/am.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,33 +434,33 @@ static void am_load(struct am_state *state)
434434
}
435435

436436
read_state_file(&sb, state, "keep", 1);
437-
if (!strcmp(sb.buf, "t"))
437+
if (!strcmp(sb.buf, "t")) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
438438
state->keep = KEEP_TRUE;
439-
else if (!strcmp(sb.buf, "b"))
439+
else if (!strcmp(sb.buf, "b")) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
440440
state->keep = KEEP_NON_PATCH;
441441
else
442442
state->keep = KEEP_FALSE;
443443

444444
read_state_file(&sb, state, "messageid", 1);
445-
state->message_id = !strcmp(sb.buf, "t");
445+
state->message_id = !strcmp(sb.buf, "t"); // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
446446

447447
read_state_file(&sb, state, "scissors", 1);
448-
if (!strcmp(sb.buf, "t"))
448+
if (!strcmp(sb.buf, "t")) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
449449
state->scissors = SCISSORS_TRUE;
450-
else if (!strcmp(sb.buf, "f"))
450+
else if (!strcmp(sb.buf, "f")) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
451451
state->scissors = SCISSORS_FALSE;
452452
else
453453
state->scissors = SCISSORS_UNSET;
454454

455455
read_state_file(&sb, state, "quoted-cr", 1);
456456
if (!*sb.buf)
457457
state->quoted_cr = quoted_cr_unset;
458-
else if (mailinfo_parse_quoted_cr_action(sb.buf, &state->quoted_cr) != 0)
458+
else if (mailinfo_parse_quoted_cr_action(sb.buf, &state->quoted_cr) != 0) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
459459
die(_("could not parse %s"), am_path(state, "quoted-cr"));
460460

461461
read_state_file(&sb, state, "apply-opt", 1);
462462
strvec_clear(&state->git_apply_opts);
463-
if (sq_dequote_to_strvec(sb.buf, &state->git_apply_opts) < 0)
463+
if (sq_dequote_to_strvec(sb.buf, &state->git_apply_opts) < 0) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
464464
die(_("could not parse %s"), am_path(state, "apply-opt"));
465465

466466
state->rebasing = !!file_exists(am_path(state, "rebasing"));

builtin/cat-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
106106
struct object_id oid;
107107
enum object_type type;
108108
char *buf;
109-
unsigned long size;
109+
unsigned long size = 0;
110110
struct object_context obj_context = {0};
111111
struct object_info oi = OBJECT_INFO_INIT;
112112
unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
120120
continue;
121121
len = read_in_full(fd, signature, 8);
122122
close(fd);
123-
if (len != 8 || strncmp(signature, "gitdir: ", 8))
123+
if (len != 8 || strncmp(signature, "gitdir: ", 8)) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
124124
continue;
125125
dst = read_gitfile(path->buf);
126126
if (dst) {

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ int cmd_commit(int argc,
21042104
if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
21052105
if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
21062106
die_errno(_("could not read MERGE_MODE"));
2107-
if (!strcmp(sb.buf, "no-ff"))
2107+
if (!strcmp(sb.buf, "no-ff")) // CodeQL [SM01932] justification: CodeQL is wrong here because the value is read from a file via strbuf_read() which does NUL-terminate the string, something CodeQL fails to understand
21082108
allow_fast_forward = 0;
21092109
}
21102110
if (allow_fast_forward)

builtin/describe.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
324324
unsigned int unannotated_cnt = 0;
325325

326326
cmit = lookup_commit_reference(the_repository, oid);
327+
if (!cmit)
328+
die(_("could not look up commit '%s'"), oid_to_hex(oid));
327329

328330
n = find_commit_name(&cmit->object.oid);
329331
if (n && (tags || all || n->prio == 2)) {

builtin/fetch.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ static struct ref *get_ref_map(struct remote *remote,
555555
if (remote &&
556556
(remote->fetch.nr ||
557557
/* Note: has_merge implies non-NULL branch->remote_name */
558-
(has_merge && !strcmp(branch->remote_name, remote->name)))) {
558+
(has_merge && branch && !strcmp(branch->remote_name, remote->name)))) {
559559
for (i = 0; i < remote->fetch.nr; i++) {
560560
get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
561561
if (remote->fetch.items[i].dst &&
@@ -573,6 +573,7 @@ static struct ref *get_ref_map(struct remote *remote,
573573
* Note: has_merge implies non-NULL branch->remote_name
574574
*/
575575
if (has_merge &&
576+
branch &&
576577
!strcmp(branch->remote_name, remote->name))
577578
add_merge_config(&ref_map, remote_refs, branch, &tail);
578579
} else if (!prefetch) {
@@ -2580,6 +2581,11 @@ int cmd_fetch(int argc,
25802581
die(_("must supply remote when using --negotiate-only"));
25812582
gtransport = prepare_transport(remote, 1);
25822583
if (gtransport->smart_options) {
2584+
/*
2585+
* Intentionally assign the address of a local variable
2586+
* to a non-local struct's field.
2587+
* codeql[cpp/stack-address-escape]
2588+
*/
25832589
gtransport->smart_options->acked_commits = &acked_commits;
25842590
} else {
25852591
warning(_("protocol does not support --negotiate-only, exiting"));

0 commit comments

Comments
 (0)