Skip to content

Commit 6678acc

Browse files
authored
Merge pull request #362 from abhinavagarwal07/advisory-fix-1
reject hostname option injection via bracketed mount source
2 parents 6893c3a + 29bb565 commit 6678acc

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

sshfs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,11 @@ static char *find_base_path(void)
40624062
*d++ = '\0';
40634063
s++;
40644064

4065+
if (sshfs.host[0] == '-') {
4066+
fprintf(stderr, "invalid hostname '%s'\n", sshfs.host);
4067+
exit(1);
4068+
}
4069+
40654070
return s;
40664071
}
40674072

@@ -4460,7 +4465,6 @@ int main(int argc, char *argv[])
44604465
tmp = g_strdup_printf("-%i", sshfs.ssh_ver);
44614466
ssh_add_arg(tmp);
44624467
g_free(tmp);
4463-
ssh_add_arg(sshfs.host);
44644468
if (sshfs.sftp_server)
44654469
sftp_server = sshfs.sftp_server;
44664470
else if (sshfs.ssh_ver == 1)
@@ -4471,6 +4475,8 @@ int main(int argc, char *argv[])
44714475
if (sshfs.ssh_ver != 1 && strchr(sftp_server, '/') == NULL)
44724476
ssh_add_arg("-s");
44734477

4478+
ssh_add_arg("--");
4479+
ssh_add_arg(sshfs.host);
44744480
ssh_add_arg(sftp_server);
44754481
free(sshfs.sftp_server);
44764482

test/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_scripts = [ 'conftest.py', 'pytest.ini', 'test_sshfs.py',
2-
'util.py' ]
2+
'test_hostname_validation.py', 'util.py' ]
33
custom_target('test_scripts', input: test_scripts,
44
output: test_scripts, build_by_default: true,
55
command: ['cp', '-fPp',

test/test_hostname_validation.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
"""Tests for hostname validation — no FUSE mount required."""
3+
4+
if __name__ == "__main__":
5+
import pytest
6+
import sys
7+
8+
sys.exit(pytest.main([__file__] + sys.argv[1:]))
9+
10+
import subprocess
11+
from util import base_cmdline, basename
12+
from os.path import join as pjoin
13+
14+
15+
def test_reject_option_injection_in_hostname(tmpdir):
16+
"""Bracketed source that resolves to a dash-prefixed host must be rejected."""
17+
18+
mnt_dir = str(tmpdir.mkdir("mnt"))
19+
malicious = "[-oProxyCommand=echo pwned]:/path"
20+
21+
cmdline = base_cmdline + [
22+
pjoin(basename, "sshfs"),
23+
"-f",
24+
malicious,
25+
mnt_dir,
26+
]
27+
res = subprocess.run(
28+
cmdline,
29+
stdin=subprocess.DEVNULL,
30+
stdout=subprocess.PIPE,
31+
stderr=subprocess.PIPE,
32+
timeout=10,
33+
text=True,
34+
)
35+
assert res.returncode != 0
36+
assert "invalid hostname" in res.stderr
37+
38+
39+
def test_reject_dash_host_after_doubledash(tmpdir):
40+
"""Non-bracketed dash-prefixed source after -- must also be rejected."""
41+
42+
mnt_dir = str(tmpdir.mkdir("mnt"))
43+
44+
cmdline = base_cmdline + [
45+
pjoin(basename, "sshfs"),
46+
"-f",
47+
"--",
48+
"-oProxyCommand=echo pwned:/path",
49+
mnt_dir,
50+
]
51+
res = subprocess.run(
52+
cmdline,
53+
stdin=subprocess.DEVNULL,
54+
stdout=subprocess.PIPE,
55+
stderr=subprocess.PIPE,
56+
timeout=10,
57+
text=True,
58+
)
59+
assert res.returncode != 0
60+
assert "invalid hostname" in res.stderr

0 commit comments

Comments
 (0)