Skip to content

Commit 2f57c36

Browse files
committed
work around comically tiny HOST_NAME_MAX on glibc system
glibc is clearly violating POSIX since they set HOST_NAME_MAX to 64, and they've known so for years. Unfortunately this means that, despite using the right interfaces, we have to work around bugs in their libc. ugh. Luckily, gmid doesn't need to do DNS, it just needs a define large enough to store a hostname, but not unlimited, to catch possible misconfigurations. We don't risk to round-trip this into an interface that expects smaller strings. Reported and fix tested by Anna “CyberTailor”, see <https://codeberg.org/op/gmid/issues/3>.
1 parent 2922e3f commit 2f57c36

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

configure

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,6 @@ cat <<__HEREDOC__
644644
# define LOGIN_NAME_MAX 32
645645
# endif
646646
#endif
647-
648-
#ifndef HOST_NAME_MAX
649-
# if defined(_POSIX_HOST_NAME_MAX)
650-
# define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
651-
# else
652-
# define HOST_NAME_MAX 255
653-
# endif
654-
#endif
655647
__HEREDOC__
656648

657649
echo "file config.h: written" 1>&2

gmid.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@
8686

8787
#define TLS_CERT_HASH_SIZE 128
8888

89+
/*
90+
* glibc is violating POSIX by defining HOST_NAME_MAX to a ridicully
91+
* small value, so we can't use it. Luckily, we don't have to do DNS
92+
* so we don't risk to pass buffers too big to functions that might
93+
* not expect them, we just need a fixed size buffer to catch possible
94+
* misconfigurations.
95+
*/
96+
#define GMID_HOST_NAME_MAX 255 /* without NUL */
97+
8998
/* forward declaration */
9099
struct privsep;
91100
struct privsep_proc;
@@ -144,19 +153,19 @@ struct envlist {
144153

145154
TAILQ_HEAD(aliashead, alist);
146155
struct alist {
147-
char alias[HOST_NAME_MAX + 1];
156+
char alias[GMID_HOST_NAME_MAX + 1];
148157
TAILQ_ENTRY(alist) aliases;
149158
};
150159

151160
TAILQ_HEAD(proxyhead, proxy);
152161
struct proxy {
153162
char match_proto[32];
154-
char match_host[HOST_NAME_MAX + 1];
163+
char match_host[GMID_HOST_NAME_MAX + 1];
155164
char match_port[32];
156165

157-
char host[HOST_NAME_MAX + 1];
166+
char host[GMID_HOST_NAME_MAX + 1];
158167
char port[32];
159-
char sni[HOST_NAME_MAX];
168+
char sni[GMID_HOST_NAME_MAX];
160169
int notls;
161170
uint32_t protocols;
162171
int noverifyname;
@@ -199,7 +208,7 @@ struct location {
199208

200209
TAILQ_HEAD(vhosthead, vhost);
201210
struct vhost {
202-
char domain[HOST_NAME_MAX + 1];
211+
char domain[GMID_HOST_NAME_MAX + 1];
203212
char *cert_path;
204213
char *key_path;
205214
char *ocsp_path;

regress/regress

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ run_test test_gemexp
4040

4141
# Run regression tests for the gmid binary.
4242
run_test test_static_files
43+
run_test test_alias_long_hostname
4344
run_test test_directory_redirect
4445
run_test test_serve_big_files
4546
run_test test_dont_execute_scripts

regress/tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ test_static_files() {
9090
check_reply "20 text/gemini" "# hello world" || return 1
9191
}
9292

93+
test_alias_long_hostname() {
94+
setup_simple_test '' '
95+
alias "laYkH0yyd7xDFO152Ubtm9Efxg8Gvt7wssNd8pPTVIIXVYbYrZERl38LrVY30WbrMrZxLFfhnmsfe1X2FUNAGMVYAxPspjl4"
96+
'
97+
98+
fetch /
99+
check_reply "20 text/gemini" "# hello world" || return 1
100+
}
101+
93102
test_directory_redirect() {
94103
setup_simple_test
95104

0 commit comments

Comments
 (0)