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

Fix a segfault in the git fetcher #6582

Merged
merged 1 commit into from
May 31, 2022
Merged

Fix a segfault in the git fetcher #6582

merged 1 commit into from
May 31, 2022

Conversation

thufschmitt
Copy link
Member

The git fetcher code used to dereference the (potentially empty) ref
input attribute. This was magically working, probably because the
compiler somehow outsmarted us, but is now blowing up with newer nixpkgs
versions.

Fix that by not trying to access this field while we don't know for sure
that it has been defined.

Fix #6554

@harrisonthorne if you have a way to reproduce the original issue, can you confirm that the fix is correct?

Related to #6509

The git fetcher code used to dereference the (potentially empty) `ref`
input attribute. This was magically working, probably because the
compiler somehow outsmarted us, but is now blowing up with newer nixpkgs
versions.

Fix that by not trying to access this field while we don't know for sure
that it has been defined.

Fix #6554
@edolstra
Copy link
Member

I fixed this differently in #6530 by making sure that we always have a ref at that point:

{"ref", ref},

@fogti
Copy link
Contributor

fogti commented May 27, 2022

why don't we move this part above the "is-local" check? the code duplication seems unnecessary, especially because the isLocal condition appears to be a simple boolean branch...
e.g.

        if (!input.getRef()) {
            auto head = isLocal ? readHead(actualUrl) : readHeadCached(actualUrl);
            if (!head) {
                warn("could not read HEAD ref from repo at '%s', using 'master'", actualUrl);
                head = "master";
            }
            input.attrs.insert_or_assign("ref", *head);
            unlockedAttrs.insert_or_assign("ref", *head);
        }

Also, what's going on with the following (although probably unrelated to this PR):

if (!input.getRef() && !input.getRev() && isLocal) {

The following also doesn't really make any sense... (when getRef() returned nullopt or such, we explicitly dereference it afterwards, instantly UB)

nix/src/libfetchers/git.cc

Lines 471 to 473 in ec07a70

if (!input.getRev())
input.attrs.insert_or_assign("rev",
Hash::parseAny(chomp(runProgram("git", true, { "-C", actualUrl, "--git-dir", gitDir, "rev-parse", *input.getRef() })), htSHA1).gitRev());

the meaning/usage of getRef() and what it returns are really confusing... like what's this?

runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "cat-file", "-e", input.getRev()->gitRev() });

@FlorianFranzen
Copy link
Contributor

FlorianFranzen commented May 31, 2022

I ran into this issue with nixUnstable on nixos-unstable, e.g. with the following expression:

nix eval --expr 'builtins.fetchGit { url = "https://github.com/mozilla/nixpkgs-mozilla.git"; rev = "15b7a05f20aab51c4ffbefddb1b448e862dccb7d"; }'

@fogti
Copy link
Contributor

fogti commented May 31, 2022

I guess this should be merged because the breakage on nixpkgs master/unstable of nixUnstable is extremely annoying... we can improve the code afterwards if necessary; it appears good enough for now, the more problematic parts of git fetcher are pretty much unrelated to this.

@thufschmitt
Copy link
Member Author

Yup' I agree with @zseri, #6530 doesn't look like it can be merged in a matter of days, so it seems important to have this fix land since it's pretty bad

@edolstra
Copy link
Member

I can't reproduce this on current master (2.10.0pre20220531_04a699b).

./result/bin/nix eval --expr 'builtins.fetchGit { url = "https://github.com/mozilla/nixpkgs-mozilla.git"; rev = "15b7a05f20aab51c4ffbefddb1b448e862dccb7d"; }'
{ lastModified = 1645464064; lastModifiedDate = "20220221172104"; narHash = "sha256-YeN4bpPvHkVOpQzb8APTAfE7/R+MFMwJUMkqmfvytSk="; outPath = "/nix/store/5snmi8nb601c90swp17b96g3a1622366-source"; rev = "15b7a05f20aab51c4ffbefddb1b448e862dccb7d"; revCount = 242; shortRev = "15b7a05"; submodules = false; }

@thufschmitt
Copy link
Member Author

I can't reproduce this on current master (2.10.0pre20220531_04a699b).

Yes, as stated in the issue, this only shows for newer nixpkgs versions. nix build nix --override-input nixpkgs nixpkgs/nixos-unstable will fail the fetchGit test because of that segfault

@edolstra edolstra merged commit 078c80f into master May 31, 2022
@edolstra edolstra deleted the debug-fetchgit-sigsev branch May 31, 2022 15:17
@edolstra edolstra added the backport 2.9-maintenance Automatically creates a PR against the branch label May 31, 2022
@github-actions
Copy link

Successfully created backport PR #6597 for 2.9-maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.9-maintenance Automatically creates a PR against the branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nix build for first-time home-manager build fails with address boundary error (SIGSEGV)
4 participants