summaryrefslogtreecommitdiff
path: root/ot_accesslist.c
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 /ot_accesslist.c
parent131d8b1a79749ca0977a16532c0738fdfc767001 (diff)
factor out accesslist parser to fix a potential out of bound read and add tests to the accesslist unit
Diffstat (limited to 'ot_accesslist.c')
-rw-r--r--ot_accesslist.c63
1 files changed, 33 insertions, 30 deletions
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);