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

Nix integer overflow is implemented by undefined behavior #10968

Closed
roberth opened this issue Jun 26, 2024 · 2 comments · Fixed by #11188
Closed

Nix integer overflow is implemented by undefined behavior #10968

roberth opened this issue Jun 26, 2024 · 2 comments · Fixed by #11188
Labels
bug language The Nix expression language; parser, interpreter, primops, evaluation, etc

Comments

@roberth
Copy link
Member

roberth commented Jun 26, 2024

Describe the bug

See https://git.lix.systems/lix-project/lix/issues/423

  1. Nix currently behaves by overflowing.
  2. Overflowing is UB, but predictable, so a few users might rely on it without difficulties such as impurities showing through hashes and substitution

Steps To Reproduce

nix-repl> 500000000*50000000000
6553255926290448384

Expected behavior

I'm inclined to make it throw instead.
If that is a problem, we may revisit. Have some thoughts on how; see https://git.lix.systems/lix-project/lix/issues/423#issuecomment-4604

nix-env --version output

Additional context

Priorities

Add 👍 to issues you find important.

@roberth roberth added bug language The Nix expression language; parser, interpreter, primops, evaluation, etc labels Jun 26, 2024
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-06-26-nix-team-meeting-minutes-156/47740/1

lf- added a commit to lf-/nix that referenced this issue Jul 25, 2024
This also bans various sneaking of negative numbers from the language
into unsuspecting builtins as was exposed while auditing the
consequences of changing the Nix language integer type to a newtype.

It's unlikely that this change comprehensively ensures correctness when
passing integers out of the Nix language and we should probably add a
checked-narrowing function or something similar, but that's out of scope
for the immediate change.

During the development of this I found a few fun facts about the
language:
- You could overflow integers by converting from unsigned JSON values.
- You could overflow unsigned integers by converting negative numbers
  into them when going into Nix config, into fetchTree, and into flake
  inputs.

  The flake inputs and Nix config cannot actually be tested properly
  since they both ban thunks, however, we put in checks anyway because
  it's possible these could somehow be used to do such shenanigans some
  other way.

Note that Lix has banned Nix language integer overflows since the very
first public beta, but threw a SIGILL about them because we run with
-fsanitize=signed-overflow -fsanitize-undefined-trap-on-error in
production builds. Since the Nix language uses signed integers, overflow
was simply undefined behaviour, and since we defined that to trap, it
did.

Trapping on it was a bad UX, but we didn't even entirely notice
that we had done this at all until it was reported as a bug a couple of
months later (which is, to be fair, that flag working as intended), and
it's got enough production time that, aside from code that is IMHO buggy
(and which is, in any case, not in nixpkgs) such as
https://git.lix.systems/lix-project/lix/issues/445, we don't think
anyone doing anything reasonable actually depends on wrapping overflow.

Even for weird use cases such as doing funny bit crimes, it doesn't make
sense IMO to have wrapping behaviour, since two's complement arithmetic
overflow behaviour is so *aggressively* not what you want for *any* kind
of mathematics/algorithms. The Nix language exists for package
management, a domain where bit crimes are already only dubiously in
scope to begin with, and it makes a lot more sense for that domain for
the integers to never lose precision, either by throwing errors if they
would, or by being arbitrary-precision.

Fixes: NixOS#10968
Original-CL: https://gerrit.lix.systems/c/lix/+/1596

Change-Id: I51f253840c4af2ea5422b8a420aa5fafbf8fae75
lf- added a commit to lf-/nix that referenced this issue Jul 26, 2024
This also bans various sneaking of negative numbers from the language
into unsuspecting builtins as was exposed while auditing the
consequences of changing the Nix language integer type to a newtype.

It's unlikely that this change comprehensively ensures correctness when
passing integers out of the Nix language and we should probably add a
checked-narrowing function or something similar, but that's out of scope
for the immediate change.

During the development of this I found a few fun facts about the
language:
- You could overflow integers by converting from unsigned JSON values.
- You could overflow unsigned integers by converting negative numbers
  into them when going into Nix config, into fetchTree, and into flake
  inputs.

  The flake inputs and Nix config cannot actually be tested properly
  since they both ban thunks, however, we put in checks anyway because
  it's possible these could somehow be used to do such shenanigans some
  other way.

Note that Lix has banned Nix language integer overflows since the very
first public beta, but threw a SIGILL about them because we run with
-fsanitize=signed-overflow -fsanitize-undefined-trap-on-error in
production builds. Since the Nix language uses signed integers, overflow
was simply undefined behaviour, and since we defined that to trap, it
did.

Trapping on it was a bad UX, but we didn't even entirely notice
that we had done this at all until it was reported as a bug a couple of
months later (which is, to be fair, that flag working as intended), and
it's got enough production time that, aside from code that is IMHO buggy
(and which is, in any case, not in nixpkgs) such as
https://git.lix.systems/lix-project/lix/issues/445, we don't think
anyone doing anything reasonable actually depends on wrapping overflow.

Even for weird use cases such as doing funny bit crimes, it doesn't make
sense IMO to have wrapping behaviour, since two's complement arithmetic
overflow behaviour is so *aggressively* not what you want for *any* kind
of mathematics/algorithms. The Nix language exists for package
management, a domain where bit crimes are already only dubiously in
scope to begin with, and it makes a lot more sense for that domain for
the integers to never lose precision, either by throwing errors if they
would, or by being arbitrary-precision.

Fixes: NixOS#10968
Original-CL: https://gerrit.lix.systems/c/lix/+/1596

Change-Id: I51f253840c4af2ea5422b8a420aa5fafbf8fae75
@anna328p
Copy link
Member

anna328p commented Oct 21, 2024

nix-repl> :p with lib.prng; iterate xoshiro256ss.generate (xoshiro256ss.initState 0) 5
[
  [
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
  ]
  [
    «error: integer overflow in multiplying 7046029256649317107 * -4658895280553007687»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
    «error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
  ]
]

i am very sad :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug language The Nix expression language; parser, interpreter, primops, evaluation, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants