summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-24 03:43:34 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-10-24 03:43:34 +0200
commitba54aca3a47df0e8c7afde968bd5f7479372a404 (patch)
tree45c9a42cc13efaf3baa5aacb4297f52fd0adf734
parent131d8b1a79749ca0977a16532c0738fdfc767001 (diff)
factor out accesslist parser to fix a potential out of bound read and add tests to the accesslist unit
-rw-r--r--Makefile9
-rw-r--r--ot_accesslist.c63
-rw-r--r--ot_accesslist.h4
-rw-r--r--tests/runner.c1
-rw-r--r--tests/stubs.c20
-rw-r--r--tests/t_accesslist.c122
-rw-r--r--tests/test.h1
7 files changed, 187 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index a6b0b9b..e96269f 100644
--- a/Makefile
+++ b/Makefile
@@ -105,10 +105,13 @@ proxy.debug: $(OBJECTS_proxy_debug) $(HEADERS)
105# has to. tests/stubs.c supplies the few symbols those modules reference but 105# has to. tests/stubs.c supplies the few symbols those modules reference but
106# the tests never reach. 106# the tests never reach.
107TESTBIN=tests/runner 107TESTBIN=tests/runner
108TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c tests/t_hex.c 108TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c tests/t_hex.c tests/t_accesslist.c
109TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c ot_random.c ot_hex.c 109TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c ot_random.c ot_hex.c ot_accesslist.c
110TEST_HEADERS=tests/test.h $(HEADERS) 110TEST_HEADERS=tests/test.h $(HEADERS)
111CFLAGS_test = $(CFLAGS) -I. -g -O1 $(FEATURES) 111# Tests reach code that sits behind feature flags, so the units under test are
112# built with those features enabled. A test binary is not a production binary.
113FEATURES_test=$(FEATURES) -DWANT_ACCESSLIST_BLACK
114CFLAGS_test = $(CFLAGS) -I. -g -O1 $(FEATURES_test)
112 115
113$(TESTBIN): $(TEST_SOURCES) $(TEST_UNITS) $(TEST_HEADERS) 116$(TESTBIN): $(TEST_SOURCES) $(TEST_UNITS) $(TEST_HEADERS)
114 $(CC) -o $@ $(CFLAGS_test) $(TEST_SOURCES) $(TEST_UNITS) 117 $(CC) -o $@ $(CFLAGS_test) $(TEST_SOURCES) $(TEST_UNITS)
diff --git a/ot_accesslist.c b/ot_accesslist.c
index ee6adb2..9b0024c 100644
--- a/ot_accesslist.c
+++ b/ot_accesslist.c
@@ -98,12 +98,33 @@ static void accesslist_clean(ot_accesslist *accesslist) {
98 } 98 }
99} 99}
100 100
101/* Read initial access list */ 101/* A hash needs forty bytes, and all but a final one are followed by at least
102 one separator, so n hashes need at least 41n - 1 bytes. (len + 1) / 41 is
103 therefore an exact upper bound on what a buffer of len bytes can hold. */
104size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, size_t len) {
105 const char *end = buf + len;
106 const char *offs = buf;
107 size_t count = 0;
108
109 while (count < capacity && end - offs >= 40) {
110 if (hex_tohash(dest[count], offs)) {
111 offs += 40;
112
113 if (offs == end || !hex_isdigit((unsigned char)*offs))
114 ++count;
115 }
116
117 /* Find start of next line */
118 while (offs < end && *(offs++) != '\n') {}
119 }
120
121 return count;
122}
123
102static void accesslist_readfile(void) { 124static void accesslist_readfile(void) {
103 ot_accesslist *accesslist_new; 125 ot_accesslist *accesslist_new;
104 ot_hash *info_hash; 126 const char *map;
105 const char *map, *map_end, *read_offs; 127 size_t maplen, capacity, found;
106 size_t maplen;
107 128
108 if ((map = mmap_read(g_accesslist_filename, &maplen)) == NULL) { 129 if ((map = mmap_read(g_accesslist_filename, &maplen)) == NULL) {
109 char *wd = getcwd(NULL, 0); 130 char *wd = getcwd(NULL, 0);
@@ -112,42 +133,24 @@ static void accesslist_readfile(void) {
112 return; 133 return;
113 } 134 }
114 135
115 /* You need at least 41 bytes to pass an info_hash, make enough room 136 /* Make room for the most info_hashes this file could possibly hold */
116 for the maximum amount of them */ 137 capacity = (maplen + 1) / 41;
117 accesslist_new = accesslist_make(g_accesslist, maplen / 41); 138 accesslist_new = accesslist_make(g_accesslist, capacity);
118 if (!accesslist_new) { 139 if (!accesslist_new) {
119 fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", (maplen / 41) * 20); 140 fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash));
120 mmap_unmap(map, maplen); 141 mmap_unmap(map, maplen);
121 return; 142 return;
122 } 143 }
123 info_hash = accesslist_new->list;
124
125 /* No use to scan if there's not enough room for another full info_hash */
126 map_end = map + maplen - 40;
127 read_offs = map;
128
129 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
130 while (read_offs <= map_end) {
131 if (hex_tohash(*info_hash, read_offs)) {
132 read_offs += 40;
133
134 /* Append accesslist to accesslist vector */
135 if (read_offs == map_end || !hex_isdigit((unsigned char)*read_offs))
136 ++info_hash;
137 }
138 144
139 /* Find start of next line */ 145 found = accesslist_parse_hashes(accesslist_new->list, capacity, map, maplen);
140 while (read_offs <= map_end && *(read_offs++) != '\n')
141 ;
142 }
143#ifdef _DEBUG 146#ifdef _DEBUG
144 fprintf(stderr, "Added %zd info_hashes to accesslist\n", (size_t)(info_hash - accesslist_new->list)); 147 fprintf(stderr, "Added %zd info_hashes to accesslist\n", found);
145#endif 148#endif
146 149
147 mmap_unmap(map, maplen); 150 mmap_unmap(map, maplen);
148 151
149 qsort(accesslist_new->list, info_hash - accesslist_new->list, sizeof(*info_hash), vector_compare_hash); 152 qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash);
150 accesslist_new->size = info_hash - accesslist_new->list; 153 accesslist_new->size = found;
151 154
152 /* Now exchange the accesslist vector in the least race condition prone way */ 155 /* Now exchange the accesslist vector in the least race condition prone way */
153 pthread_mutex_lock(&g_accesslist_mutex); 156 pthread_mutex_lock(&g_accesslist_mutex);
diff --git a/ot_accesslist.h b/ot_accesslist.h
index d05e1f7..b3e979d 100644
--- a/ot_accesslist.h
+++ b/ot_accesslist.h
@@ -19,6 +19,10 @@ void accesslist_deinit(void);
19int accesslist_hashisvalid(ot_hash hash); 19int accesslist_hashisvalid(ot_hash hash);
20void accesslist_cleanup(void); 20void accesslist_cleanup(void);
21 21
22/* Scan buf for up to capacity info_hashes in hexadecimal notation, one per
23 line, and return how many were written to dest */
24size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, size_t len);
25
22extern char *g_accesslist_filename; 26extern char *g_accesslist_filename;
23#ifdef WANT_DYNAMIC_ACCESSLIST 27#ifdef WANT_DYNAMIC_ACCESSLIST
24extern char *g_accesslist_pipe_add; 28extern char *g_accesslist_pipe_add;
diff --git a/tests/runner.c b/tests/runner.c
index c9385f0..bf4fbde 100644
--- a/tests/runner.c
+++ b/tests/runner.c
@@ -19,6 +19,7 @@ static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urle
19 {"ot_vector", test_vector}, 19 {"ot_vector", test_vector},
20 {"ot_random", test_random}, 20 {"ot_random", test_random},
21 {"ot_hex", test_hex}, 21 {"ot_hex", test_hex},
22 {"ot_accesslist", test_accesslist},
22 {NULL, NULL}}; 23 {NULL, NULL}};
23 24
24/* Run every module, or only those whose name contains argv[1] */ 25/* Run every module, or only those whose name contains argv[1] */
diff --git a/tests/stubs.c b/tests/stubs.c
index ae40306..febdd7b 100644
--- a/tests/stubs.c
+++ b/tests/stubs.c
@@ -12,3 +12,23 @@
12 12
13/* ot_vector.c calls this from vector_clean_list, which no test drives yet. */ 13/* ot_vector.c calls this from vector_clean_list, which no test drives yet. */
14void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } 14void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; }
15
16/* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser
17 is reachable. These four belong to accesslist_readfile and the reload
18 worker, neither of which any test drives. */
19time_t g_now_seconds;
20volatile int g_opentracker_running;
21
22const char *mmap_read(const char *filename, size_t *filesize);
23const char *mmap_read(const char *filename, size_t *filesize) {
24 (void)filename;
25 *filesize = 0;
26 return NULL;
27}
28
29int mmap_unmap(const char *mapped, size_t maplen);
30int mmap_unmap(const char *mapped, size_t maplen) {
31 (void)mapped;
32 (void)maplen;
33 return 0;
34}
diff --git a/tests/t_accesslist.c b/tests/t_accesslist.c
new file mode 100644
index 0000000..9925acd
--- /dev/null
+++ b/tests/t_accesslist.c
@@ -0,0 +1,122 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6/* System */
7#include <stdlib.h>
8#include <string.h>
9
10/* Opentracker */
11#include "ot_accesslist.h"
12#include "test.h"
13
14#define A "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
15#define B "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
16#define C "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
17
18static ot_hash dest[8];
19
20/* The parser is handed a mapped file, so it must never look at a byte beyond
21 the length it was given. These cases place a hex digit immediately after
22 that length: a parser that reads one byte too far sees it, decides the hash
23 runs on, and silently drops the last entry. */
24
25static void case_empty(void) {
26 test_case("an empty buffer yields nothing");
27 CHECK_INT(accesslist_parse_hashes(dest, 8, "", 0), 0);
28}
29
30static void case_single_with_newline(void) {
31 const char buf[] = A "\n";
32
33 test_case("one hash followed by a newline");
34 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1);
35}
36
37static void case_single_without_newline(void) {
38 const char buf[] = A "A";
39
40 test_case("one hash ending the buffer, with a hex digit just past the end");
41 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 40), 1);
42}
43
44static void case_several_last_without_newline(void) {
45 const char buf[] = A "\n" B "\n" C "C";
46
47 test_case("a final hash without a trailing newline still counts");
48 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 3 * 40 + 2), 3);
49}
50
51static void case_run_on_hash_is_rejected(void) {
52 const char buf[] = A "A\n";
53
54 test_case("forty one hex digits are not a hash");
55 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 42), 0);
56}
57
58static void case_junk_lines_skipped(void) {
59 const char buf[] = "# a comment\n" A "\nnot a hash\n" B "\n";
60
61 test_case("lines that are not hashes are ignored");
62 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2);
63}
64
65static void case_trailing_junk_on_line(void) {
66 const char buf[] = A " some comment\n" B "\n";
67
68 test_case("a hash followed by other text on the same line still counts");
69 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, sizeof(buf) - 1), 2);
70}
71
72static void case_capacity_is_respected(void) {
73 const char buf[] = A "\n" B "\n" C "\n";
74
75 test_case("no more than capacity hashes are written");
76 CHECK_INT(accesslist_parse_hashes(dest, 2, buf, sizeof(buf) - 1), 2);
77}
78
79static void case_zero_capacity_never_writes(void) {
80 const char buf[] = A "\n";
81
82 test_case("a zero capacity tolerates a null destination");
83 CHECK_INT(accesslist_parse_hashes(NULL, 0, buf, 41), 0);
84}
85
86static void case_decoded_value(void) {
87 const char buf[] = "000102030405060708090A0B0C0D0E0F10111213\n";
88 const uint8_t want[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
89
90 test_case("the hash is decoded, not merely counted");
91 CHECK_INT(accesslist_parse_hashes(dest, 8, buf, 41), 1);
92 CHECK_MEM(dest[0], want, sizeof(want));
93}
94
95static void case_capacity_bound_holds(void) {
96 /* (len + 1) / 41 is what accesslist_readfile allocates. Pack a buffer as
97 densely as the parser allows and check the bound is not exceeded. */
98 char buf[41 * 4];
99 size_t len, capacity, found;
100
101 test_case("the capacity formula bounds what the parser can produce");
102 memcpy(buf, A "\n" B "\n" C "\n" A, 4 * 40 + 3);
103 len = 4 * 40 + 3;
104 capacity = (len + 1) / 41;
105 found = accesslist_parse_hashes(dest, 8, buf, len);
106 CHECK_INT(found, 4);
107 CHECK(found <= capacity);
108}
109
110void test_accesslist(void) {
111 case_empty();
112 case_single_with_newline();
113 case_single_without_newline();
114 case_several_last_without_newline();
115 case_run_on_hash_is_rejected();
116 case_junk_lines_skipped();
117 case_trailing_junk_on_line();
118 case_capacity_is_respected();
119 case_zero_capacity_never_writes();
120 case_decoded_value();
121 case_capacity_bound_holds();
122}
diff --git a/tests/test.h b/tests/test.h
index 6af0083..8be624b 100644
--- a/tests/test.h
+++ b/tests/test.h
@@ -81,5 +81,6 @@ void test_iovec(void);
81void test_vector(void); 81void test_vector(void);
82void test_random(void); 82void test_random(void);
83void test_hex(void); 83void test_hex(void);
84void test_accesslist(void);
84 85
85#endif 86#endif