fix(mod_whp_symlink): deny-by-default Options remap, refuse quoted lines; add probe test

Security review (2 HIGH):
- Incomplete remap: only bare `All` was handled; `+All`/`-All`, `None +X`,
  `RunScripts` etc. passed through. The remap now matches every word against
  the full core keyword list and emits only known-safe names (prefix kept,
  core +/- mixing rules respected): any symlink-follow form ->
  SymLinksIfOwnerMatch, any exec-SSI form -> IncludesNOEXEC (only if allowed,
  else dropped), positive ExecCGI dropped, unknown words dropped. The line is
  always replaced by the normalised form, so the core never sees input words.
- Parser differential: ap_getword_conf() unquotes, and the re-joined line was
  re-tokenised by the core. A line containing quote or backslash characters is
  now refused (config error -> 500, nothing enabled); emitted tokens are fixed
  keywords, so the join is unambiguous.

scripts/tests/whp-symlink-module.test.sh probes 29 Options forms (+All, -All,
All mixed with +/-, case and FollowSymlinks spelling variants, quoted,
escaped, tab-separated) for cross-tenant symlink reads, SSI/CGI exec, refusal
of quoted lines, and the swapped public_html owner guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-23 06:29:48 -08:00
1 parent 5fbc2e315e
commit 31e42ecbc2
2 files changed
+207 -32

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
#!/usr/bin/env bash
## whp-symlink-module.test.sh - live probes for src/mod_whp_symlink against a built
## shared-httpd image: no .htaccess Options form may re-enable un-owner-checked
## symlinks, exec-SSI or CGI; quoted/escaped Options lines are refused; stock CMS
## shapes (`Options +FollowSymlinks`) no longer answer 500; the owner guard refuses
## a swapped public_html.
## whp-symlink-module.test.sh [image] (default shared-httpd:latest)
## SKIPs (exit 0) without docker or the image.
set -u
IMAGE="${1:-shared-httpd:latest}"
NAME="whp-symtest-$$"; T="$(mktemp -d)"
trap 'docker rm -f "$NAME" >/dev/null 2>&1; rm -rf "$T"' EXIT
command -v docker >/dev/null 2>&1 || { echo "SKIP: no docker"; exit 0; }
docker image inspect "$IMAGE" >/dev/null 2>&1 || { echo "SKIP: no image $IMAGE"; exit 0; }
fail=0; pass=0
ok() { pass=$((pass+1)); echo "ok $*"; }
bad() { fail=$((fail+1)); echo "FAIL $*"; }
# A per-site vhost shaped like WHP's shared-vhost-template.tpl (2026-11-22).
cat > "$T/site.conf" <<'EOF'
<Directory "/mnt/users">
Options SymLinksIfOwnerMatch
</Directory>
<Directory "/mnt/users/a/a.test/public_html">
Options Indexes SymLinksIfOwnerMatch MultiViews
AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,MultiViews,SymLinksIfOwnerMatch,ExecCGI
Require all granted
</Directory>
<Directory "/mnt/users/a/a2.test/public_html">
Options Indexes SymLinksIfOwnerMatch MultiViews
Require all granted
</Directory>
<VirtualHost *:80>
ServerName a.test
DocumentRoot "/mnt/users/a/a.test/public_html"
</VirtualHost>
<VirtualHost *:80>
ServerName a2.test
DocumentRoot "/mnt/users/a/a2.test/public_html"
</VirtualHost>
<IfModule whp_symlink_module>
WHPOwnerGuard On
</IfModule>
EOF
cat > "$T/lines" <<'EOF'
+All
-All
All
+all
All +Indexes
All Indexes
None +FollowSymLinks
-None
FollowSymLinks
FOLLOWSYMLINKS
+FollowSymlinks
+followSYMlinks
+FollowSymLinks -SymLinksIfOwnerMatch
-SymLinksIfOwnerMatch +FollowSymLinks
Indexes +FollowSymLinks
+FollowSymLinks\t+Indexes
"FollowSymLinks Indexes"
"+FollowSymLinks"
'FollowSymLinks'
Follow\\SymLinks
"Follow"SymLinks
+FollowSymLinks "x y"
+Includes
Includes
+RunScripts
+ExecCGI
ExecCGI
+Bogus +FollowSymLinks
+FollowSymLinks +ExecCGI +Includes
EOF
{
echo 'set -e; A=/mnt/users/a/a.test/public_html; B=/mnt/users/b/b.test/public_html'
echo 'mkdir -p $A $B /mnt/users/a/a2.test; echo B-SECRET > $B/secret.txt; ln -s $B /mnt/users/a/a2.test/public_html'
n=0
while IFS= read -r line; do
n=$((n+1)); d="\$A/t$n"
printf 'mkdir -p %s; printf %%s %q > %s/.line\n' "$d" "$line" "$d"
printf "printf 'Options %%b\\\\nAddOutputFilter INCLUDES .shtml\\\\nAddHandler cgi-script .cgi\\\\n' %q > %s/.htaccess\n" "$line" "$d"
printf 'echo OWN > %s/own.txt; ln -s $B/secret.txt %s/steal.txt\n' "$d" "$d"
printf "printf 'S<!--#exec cmd=\"id\" -->E\\\\n' > %s/p.shtml; printf '#!/bin/sh\\\\necho Content-type: text/plain\\\\necho\\\\nid\\\\n' > %s/p.cgi; chmod 755 %s/p.cgi\n" "$d" "$d" "$d"
done < "$T/lines"
echo "echo $n > /ntests"
echo 'chmod -R a+rX /mnt/users; chown -R -h 1006:1006 /mnt/users/a; chown -R -h 1003:1003 /mnt/users/b'
} > "$T/fx.sh"
docker run -d --name "$NAME" "$IMAGE" >/dev/null || { echo "SKIP: cannot start $IMAGE"; exit 0; }
sleep 3
docker cp "$T/site.conf" "$NAME:/etc/httpd/conf.d/vhosts/zz-symtest.conf" >/dev/null
docker cp "$T/fx.sh" "$NAME:/fx.sh" >/dev/null
docker exec "$NAME" bash /fx.sh || { bad "fixtures"; exit 1; }
docker exec "$NAME" httpd -M 2>/dev/null | grep -q ' whp_symlink_module ' && ok "module loaded" || bad "module not loaded"
docker exec "$NAME" httpd -t >/dev/null 2>&1 && ok "httpd -t" || { bad "httpd -t"; exit 1; }
docker exec "$NAME" httpd -k graceful; sleep 3
get() { docker exec "$NAME" sh -c "curl -s --max-time 10 --resolve $1:80:127.0.0.1 -o /tmp/o -w '%{http_code}' 'http://$1$2'; printf ' '; head -c 200 /tmp/o | tr '\n' '~'"; }
N=$(docker exec "$NAME" cat /ntests)
for i in $(seq 1 "$N"); do
line="$(docker exec "$NAME" cat /mnt/users/a/a.test/public_html/t$i/.line)"
st=$(get a.test /t$i/steal.txt); x=$(get a.test /t$i/p.shtml); c=$(get a.test /t$i/p.cgi)
case "$st" in *B-SECRET*) bad "Options $line: cross-tenant symlink read" ;; *) ok "Options $line: no cross-tenant read (${st:0:3})" ;; esac
case "$x$c" in *uid=*) bad "Options $line: code executed" ;; *) ok "Options $line: no exec" ;; esac
case "$line" in
*\"*|*\'*|*\\\\*) [ "$(get a.test /t$i/own.txt | cut -c1-3)" = 500 ] && ok "Options $line: quoted/escaped line refused" || bad "Options $line: quoted/escaped line not refused" ;;
+FollowSymlinks|FollowSymLinks|All|+All) o=$(get a.test /t$i/own.txt); [ "${o:0:3}" = 200 ] && ok "Options $line: directory works (200)" || bad "Options $line: own file ${o:0:3}" ;;
esac
done
r=$(get a2.test /secret.txt)
case "$r" in *B-SECRET*) bad "swapped public_html served another tenant's file" ;; *) ok "swapped public_html refused (${r:0:3})" ;; esac
echo "whp-symlink-module.test: $pass passed, $fail failed"
[ "$fail" -eq 0 ]
+91 -32
View File
@@ -120,11 +120,25 @@ static void push(apr_array_header_t *out, char action, const char *w)
action ? apr_pstrcat(out->pool, buf, w, NULL) : w; action ? apr_pstrcat(out->pool, buf, w, NULL) : w;
} }
/* Push `name` with prefix `action` only when AllowOverride Options= permits
* the option bits `bits`; used for every name this module synthesises. */
static void push_if(apr_array_header_t *out, cmd_parms *cmd, char action,
const char *name, int bits)
{
if ((cmd->override_opts & bits) == bits)
push(out, action, name);
}
/* Deny by default: every input word is matched against the complete list of
* core Options keywords (server/core.c set_options); only known-safe names
* are ever emitted, unknown words are dropped, and a line containing quote
* or escape characters is refused outright, because ap_getword_conf()
* unquotes and a re-joined line could tokenize differently in the core. */
static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l) static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l)
{ {
whp_srv_conf *sc; whp_srv_conf *sc;
apr_array_header_t *words, *out; apr_array_header_t *words, *out;
int i, follow_on = 0, merge = 0, changed = 0, owner_ok, noexec_ok; int i, follow_on = 0, changed = 0, any_merge = 0, absolute = 0;
const char *orig = l; const char *orig = l;
char *res; char *res;
@@ -134,24 +148,31 @@ static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l)
if (!sc || sc->remap != MODE_ON) if (!sc || sc->remap != MODE_ON)
return NULL; return NULL;
if (strpbrk(l, "\"'\\")) {
return "Options: quoted or escaped option names are not supported "
"on this server (whp_symlink)";
}
words = apr_array_make(cmd->temp_pool, 8, sizeof(char *)); words = apr_array_make(cmd->temp_pool, 8, sizeof(char *));
while (*l) { while (*l) {
char *w = ap_getword_conf(cmd->temp_pool, &l); char *w = ap_getword_conf(cmd->temp_pool, &l);
const char *n = w;
if (!*w) if (!*w)
continue; continue;
if (*n == '+' || *n == '-')
n++;
if (*n == '\0' || strpbrk(n, " \t\r\n\"'\\"))
return "Options: malformed option word (whp_symlink)";
if (words->nelts == 0 && n == w)
absolute = 1;
if (n != w)
any_merge = 1;
*(char **)apr_array_push(words) = w; *(char **)apr_array_push(words) = w;
if ((w[0] == '+' && !ap_cstr_casecmp(w + 1, "FollowSymLinks")) if (*w != '-' && (!ap_cstr_casecmp(n, "FollowSymLinks")
|| !ap_cstr_casecmp(w, "FollowSymLinks")) || !ap_cstr_casecmp(n, "All")))
follow_on = 1; follow_on = 1;
if (w[0] == '+' || w[0] == '-')
merge = 1;
} }
/* Synthesised names are only emitted when AllowOverride Options=
* permits them, so the rewritten line never fails the core's check. */
owner_ok = (cmd->override_opts & OPT_SYM_OWNER) != 0;
noexec_ok = (cmd->override_opts & OPT_INCLUDES) != 0;
out = apr_array_make(cmd->temp_pool, 8, sizeof(char *)); out = apr_array_make(cmd->temp_pool, 8, sizeof(char *));
for (i = 0; i < words->nelts; i++) { for (i = 0; i < words->nelts; i++) {
const char *w = ((char **)words->elts)[i]; const char *w = ((char **)words->elts)[i];
@@ -159,51 +180,89 @@ static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l)
if (*w == '+' || *w == '-') if (*w == '+' || *w == '-')
action = *w++; action = *w++;
if (!ap_cstr_casecmp(w, "FollowSymLinks")) { if (!ap_cstr_casecmp(w, "Indexes")) {
changed = 1; push(out, action, "Indexes");
if (action != '-' && owner_ok) }
else if (!ap_cstr_casecmp(w, "MultiViews")) {
push(out, action, "MultiViews");
}
else if (!ap_cstr_casecmp(w, "IncludesNOEXEC")) {
push(out, action, "IncludesNOEXEC");
}
else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch")) {
if (action == '-' && follow_on)
changed = 1; /* "-SymLinksIfOwnerMatch +FollowSymLinks" */
else
push(out, action, "SymLinksIfOwnerMatch"); push(out, action, "SymLinksIfOwnerMatch");
} }
else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch") else if (!ap_cstr_casecmp(w, "FollowSymLinks")) {
&& action == '-' && follow_on) {
changed = 1; changed = 1;
if (action != '-')
push_if(out, cmd, action, "SymLinksIfOwnerMatch", OPT_SYM_OWNER);
} }
else if (!ap_cstr_casecmp(w, "Includes")) { else if (!ap_cstr_casecmp(w, "Includes")) {
changed = 1; changed = 1; /* exec-SSI never; NOEXEC if allowed */
if (noexec_ok) push_if(out, cmd, action, "IncludesNOEXEC", OPT_INCLUDES);
push(out, action, "IncludesNOEXEC");
} }
else if (!ap_cstr_casecmp(w, "ExecCGI")) { else if (!ap_cstr_casecmp(w, "ExecCGI")) {
if (action == '-') if (action == '-')
push(out, action, w); push(out, action, "ExecCGI");
else else
changed = 1; changed = 1; /* tier policy: never enable CGI */
} }
else if (!ap_cstr_casecmp(w, "RunScripts")) { else if (!ap_cstr_casecmp(w, "RunScripts")) {
changed = 1; changed = 1; /* = MultiViews + ExecCGI */
push(out, action, "MultiViews"); push(out, action, "MultiViews");
if (action == '-')
push_if(out, cmd, '-', "ExecCGI", OPT_EXECCGI);
} }
else if (!ap_cstr_casecmp(w, "All") && !action) { else if (!ap_cstr_casecmp(w, "None")) {
if (action || i != 0)
changed = 1; /* invalid in the core; drop */
else
push(out, 0, "None");
}
else if (!ap_cstr_casecmp(w, "All")) {
changed = 1; changed = 1;
if (cmd->override_opts & OPT_INDEXES) if (!action && i != 0) {
push(out, 0, "Indexes"); /* the core rejects this; drop it */
if (owner_ok) }
push(out, 0, "SymLinksIfOwnerMatch"); else if (action == '-') {
if (noexec_ok) push_if(out, cmd, '-', "Indexes", OPT_INDEXES);
push(out, 0, "IncludesNOEXEC"); push_if(out, cmd, '-', "SymLinksIfOwnerMatch", OPT_SYM_OWNER);
push_if(out, cmd, '-', "IncludesNOEXEC", OPT_INCLUDES);
push_if(out, cmd, '-', "ExecCGI", OPT_EXECCGI);
} }
else { else {
push(out, action, w); /* bare All (first) or +All: the safe subset of All. A bare
* All followed by +/- words becomes "None +x +y" (the core
* accepts +/- after None); otherwise plain bare names. */
char a = action;
if (!action && any_merge) {
push(out, 0, "None");
a = '+';
}
push_if(out, cmd, a, "Indexes", OPT_INDEXES);
push_if(out, cmd, a, "SymLinksIfOwnerMatch", OPT_SYM_OWNER);
push_if(out, cmd, a, "IncludesNOEXEC", OPT_INCLUDES);
}
}
else {
changed = 1; /* unknown word: deny by default */
ap_log_error(APLOG_MARK, APLOG_INFO, 0, cmd->server,
"whp_symlink: %s:%d: dropped unknown Options word %s",
cmd->directive->filename, cmd->directive->line_num, w);
} }
} }
if (!changed)
return NULL;
/* Always replace the line with the normalised, known-keyword-only form
* (also when nothing was remapped), so the core never sees input words. */
if (out->nelts == 0) if (out->nelts == 0)
res = merge ? "" : "None"; /* "" = no-op in core set_options */ res = absolute ? "None" : ""; /* "" = no-op in core set_options */
else else
res = apr_array_pstrcat(cmd->temp_pool, out, ' '); res = apr_array_pstrcat(cmd->temp_pool, out, ' ');
if (changed)
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server,
"whp_symlink: %s:%d: Options \"%s\" -> \"%s\"", "whp_symlink: %s:%d: Options \"%s\" -> \"%s\"",
cmd->directive->filename, cmd->directive->line_num, cmd->directive->filename, cmd->directive->line_num,