aboutsummaryrefslogtreecommitdiff
path: root/tests/functional
diff options
context:
space:
mode:
authorJade Lovelace <lix@jade.fyi>2024-07-12 16:22:34 +0200
committerJade Lovelace <lix@jade.fyi>2024-07-13 00:59:33 +0200
commit917c9bdee76e4a9ad997c2503230c363a8bc5750 (patch)
tree445f55d8395d1d29aeab50f92f28d7a0c5d2f566 /tests/functional
parentf9641b9efd2478929e4811209111c3ca76836d68 (diff)
language: cleanly ban integer overflows
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. This change will be ported to CppNix as well, to maintain language consistency. Fixes: https://git.lix.systems/lix-project/lix/issues/423 Change-Id: I51f253840c4af2ea5422b8a420aa5fafbf8fae75
Diffstat (limited to 'tests/functional')
-rw-r--r--tests/functional/lang/eval-fail-fetchTree-negative.err.exp8
-rw-r--r--tests/functional/lang/eval-fail-fetchTree-negative.nix5
-rw-r--r--tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.err.exp14
-rw-r--r--tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.nix7
-rw-r--r--tests/functional/lang/eval-fail-fromJSON-overflowing.err.exp8
-rw-r--r--tests/functional/lang/eval-fail-fromJSON-overflowing.nix1
-rw-r--r--tests/functional/lang/eval-fail-overflowing-add.err.exp6
-rw-r--r--tests/functional/lang/eval-fail-overflowing-add.nix4
-rw-r--r--tests/functional/lang/eval-fail-overflowing-div.err.exp23
-rw-r--r--tests/functional/lang/eval-fail-overflowing-div.nix7
-rw-r--r--tests/functional/lang/eval-fail-overflowing-mul.err.exp16
-rw-r--r--tests/functional/lang/eval-fail-overflowing-mul.nix3
-rw-r--r--tests/functional/lang/eval-fail-overflowing-sub.err.exp9
-rw-r--r--tests/functional/lang/eval-fail-overflowing-sub.nix4
14 files changed, 115 insertions, 0 deletions
diff --git a/tests/functional/lang/eval-fail-fetchTree-negative.err.exp b/tests/functional/lang/eval-fail-fetchTree-negative.err.exp
new file mode 100644
index 000000000..d9ba1f0b2
--- /dev/null
+++ b/tests/functional/lang/eval-fail-fetchTree-negative.err.exp
@@ -0,0 +1,8 @@
+error:
+ … while calling the 'fetchTree' builtin
+ at /pwd/lang/eval-fail-fetchTree-negative.nix:1:1:
+ 1| builtins.fetchTree {
+ | ^
+ 2| type = "file";
+
+ error: negative value given for fetchTree attr owner: -1
diff --git a/tests/functional/lang/eval-fail-fetchTree-negative.nix b/tests/functional/lang/eval-fail-fetchTree-negative.nix
new file mode 100644
index 000000000..90bcab5d8
--- /dev/null
+++ b/tests/functional/lang/eval-fail-fetchTree-negative.nix
@@ -0,0 +1,5 @@
+builtins.fetchTree {
+ type = "file";
+ url = "file://eval-fail-fetchTree-negative.nix";
+ owner = -1;
+}
diff --git a/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.err.exp b/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.err.exp
new file mode 100644
index 000000000..25c8d7eaa
--- /dev/null
+++ b/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.err.exp
@@ -0,0 +1,14 @@
+error:
+ … while calling the 'seq' builtin
+ at /pwd/lang/eval-fail-flake-ref-to-string-negative-integer.nix:1:16:
+ 1| let n = -1; in builtins.seq n (builtins.flakeRefToString {
+ | ^
+ 2| type = "github";
+
+ … while calling the 'flakeRefToString' builtin
+ at /pwd/lang/eval-fail-flake-ref-to-string-negative-integer.nix:1:32:
+ 1| let n = -1; in builtins.seq n (builtins.flakeRefToString {
+ | ^
+ 2| type = "github";
+
+ error: negative value given for flake ref attr repo: -1
diff --git a/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.nix b/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.nix
new file mode 100644
index 000000000..e0208eb25
--- /dev/null
+++ b/tests/functional/lang/eval-fail-flake-ref-to-string-negative-integer.nix
@@ -0,0 +1,7 @@
+let n = -1; in builtins.seq n (builtins.flakeRefToString {
+ type = "github";
+ owner = "NixOS";
+ repo = n;
+ ref = "23.05";
+ dir = "lib";
+})
diff --git a/tests/functional/lang/eval-fail-fromJSON-overflowing.err.exp b/tests/functional/lang/eval-fail-fromJSON-overflowing.err.exp
new file mode 100644
index 000000000..a39082b45
--- /dev/null
+++ b/tests/functional/lang/eval-fail-fromJSON-overflowing.err.exp
@@ -0,0 +1,8 @@
+error:
+ … while calling the 'fromJSON' builtin
+ at /pwd/lang/eval-fail-fromJSON-overflowing.nix:1:1:
+ 1| builtins.fromJSON ''{"attr": 18446744073709551615}''
+ | ^
+ 2|
+
+ error: unsigned json number 18446744073709551615 outside of Nix integer range
diff --git a/tests/functional/lang/eval-fail-fromJSON-overflowing.nix b/tests/functional/lang/eval-fail-fromJSON-overflowing.nix
new file mode 100644
index 000000000..6dfbce3f6
--- /dev/null
+++ b/tests/functional/lang/eval-fail-fromJSON-overflowing.nix
@@ -0,0 +1 @@
+builtins.fromJSON ''{"attr": 18446744073709551615}''
diff --git a/tests/functional/lang/eval-fail-overflowing-add.err.exp b/tests/functional/lang/eval-fail-overflowing-add.err.exp
new file mode 100644
index 000000000..6458cf1c9
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-add.err.exp
@@ -0,0 +1,6 @@
+error: integer overflow in adding 9223372036854775807 + 1
+ at /pwd/lang/eval-fail-overflowing-add.nix:4:8:
+ 3| b = 1;
+ 4| in a + b
+ | ^
+ 5|
diff --git a/tests/functional/lang/eval-fail-overflowing-add.nix b/tests/functional/lang/eval-fail-overflowing-add.nix
new file mode 100644
index 000000000..24258fc20
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-add.nix
@@ -0,0 +1,4 @@
+let
+ a = 9223372036854775807;
+ b = 1;
+in a + b
diff --git a/tests/functional/lang/eval-fail-overflowing-div.err.exp b/tests/functional/lang/eval-fail-overflowing-div.err.exp
new file mode 100644
index 000000000..8ce07d4d6
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-div.err.exp
@@ -0,0 +1,23 @@
+error:
+ … while calling the 'seq' builtin
+ at /pwd/lang/eval-fail-overflowing-div.nix:7:4:
+ 6| b = -1;
+ 7| in builtins.seq intMin (builtins.seq b (intMin / b))
+ | ^
+ 8|
+
+ … while calling the 'seq' builtin
+ at /pwd/lang/eval-fail-overflowing-div.nix:7:25:
+ 6| b = -1;
+ 7| in builtins.seq intMin (builtins.seq b (intMin / b))
+ | ^
+ 8|
+
+ … while calling the 'div' builtin
+ at /pwd/lang/eval-fail-overflowing-div.nix:7:48:
+ 6| b = -1;
+ 7| in builtins.seq intMin (builtins.seq b (intMin / b))
+ | ^
+ 8|
+
+ error: integer overflow in dividing -9223372036854775808 / -1
diff --git a/tests/functional/lang/eval-fail-overflowing-div.nix b/tests/functional/lang/eval-fail-overflowing-div.nix
new file mode 100644
index 000000000..44fbe9d7e
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-div.nix
@@ -0,0 +1,7 @@
+let
+ # lol, this has to be written as an expression like this because negative
+ # numbers use unary negation rather than parsing directly, and 2**63 is out
+ # of range
+ intMin = -9223372036854775807 - 1;
+ b = -1;
+in builtins.seq intMin (builtins.seq b (intMin / b))
diff --git a/tests/functional/lang/eval-fail-overflowing-mul.err.exp b/tests/functional/lang/eval-fail-overflowing-mul.err.exp
new file mode 100644
index 000000000..f42b39d4d
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-mul.err.exp
@@ -0,0 +1,16 @@
+error:
+ … while calling the 'mul' builtin
+ at /pwd/lang/eval-fail-overflowing-mul.nix:3:10:
+ 2| a = 4294967297;
+ 3| in a * a * a
+ | ^
+ 4|
+
+ … while calling the 'mul' builtin
+ at /pwd/lang/eval-fail-overflowing-mul.nix:3:6:
+ 2| a = 4294967297;
+ 3| in a * a * a
+ | ^
+ 4|
+
+ error: integer overflow in multiplying 4294967297 * 4294967297
diff --git a/tests/functional/lang/eval-fail-overflowing-mul.nix b/tests/functional/lang/eval-fail-overflowing-mul.nix
new file mode 100644
index 000000000..6081d9c7b
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-mul.nix
@@ -0,0 +1,3 @@
+let
+ a = 4294967297;
+in a * a * a
diff --git a/tests/functional/lang/eval-fail-overflowing-sub.err.exp b/tests/functional/lang/eval-fail-overflowing-sub.err.exp
new file mode 100644
index 000000000..66a3a03f8
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-sub.err.exp
@@ -0,0 +1,9 @@
+error:
+ … while calling the 'sub' builtin
+ at /pwd/lang/eval-fail-overflowing-sub.nix:4:6:
+ 3| b = 2;
+ 4| in a - b
+ | ^
+ 5|
+
+ error: integer overflow in subtracting -9223372036854775807 - 2
diff --git a/tests/functional/lang/eval-fail-overflowing-sub.nix b/tests/functional/lang/eval-fail-overflowing-sub.nix
new file mode 100644
index 000000000..229b8c6d2
--- /dev/null
+++ b/tests/functional/lang/eval-fail-overflowing-sub.nix
@@ -0,0 +1,4 @@
+let
+ a = -9223372036854775807;
+ b = 2;
+in a - b