test/bad_chars: ensure our tests could fail
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Tue, 16 Nov 2021 21:23:02 +0000 (10:23 +1300)
committerRalph Boehme <slow@samba.org>
Wed, 17 Nov 2021 04:36:36 +0000 (04:36 +0000)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Ralph Boehme <slow@samba.org>
python/samba/tests/source_chars.py
testdata/source-chars-bad.c [new file with mode: 0644]

index 5d107e095749149cca7b4abe71b155886c7c633d..bfaf6f34ee1cb92c8d7f852f38d951fc1f0027d4 100644 (file)
@@ -63,6 +63,7 @@ IGNORED_FILES = {
     'source3/selftest/ktest-krb5_ccache-2',
     'source3/selftest/ktest-krb5_ccache-3',
     'third_party/pep8/testsuite/latin-1.py',
+    'testdata/source-chars-bad.c',
 }
 
 IGNORED_EXTENSIONS = {
@@ -205,6 +206,18 @@ class CharacterTests(TestCase):
                 if is_bad_char(c):
                     self.fail(f"{name} has potentially bad format characters!")
 
+    def test_unexpected_format_chars_do_fail(self):
+        """Test the test"""
+        for name, n_bad in [
+                ('testdata/source-chars-bad.c', 3)
+        ]:
+            fullname = os.path.join(ROOT, name)
+            with open(fullname) as f:
+                s = f.read()
+            chars = set(s)
+            bad_chars = [c for c in chars if is_bad_char(c)]
+            self.assertEqual(len(bad_chars), n_bad)
+
 
 def check_file_text():
     """If called directly as a script, count the found characters."""
diff --git a/testdata/source-chars-bad.c b/testdata/source-chars-bad.c
new file mode 100644 (file)
index 0000000..9b43572
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * "Trojan Source"  CVE-2021-42574 test.
+ *
+ * Based on an example from https://lwn.net/Articles/874951/
+ */
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+       int isAdmin = 0;
+
+#if 0
+       /* This is what is really there. */
+
+       /*«RLO» } «LRI»if (isAdmin)«PDI» «LRI» begin admins only */
+       puts("hello admin");
+       /* end admin only «RLO» { «LRI»*/
+#else
+       /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
+       puts("hello admin");
+       /* end admin only ‮ { ⁦*/
+#endif
+}