Script execution via `./path` or `/path` never checks the executable permission bit
Describe the bug
src/cowrie/shell/protocol.py's getCommand() resolves a path-like
command (one starting with . or /) and, if it names an existing
regular file, always returns a runnable command for it:
if self.fs.isfile(path):
return self.scriptcmd(path)This never looks at the file's mode bits. Any regular file at that path
runs, regardless of whether it was ever made executable -- a file created
with a plain > redirect (mode 644, no execute bit at all) runs exactly
the same as one that has been chmod +x'd. A real kernel's exec()
requires an applicable execute bit and returns EACCES ("Permission
denied") otherwise, even for root: a file with no execute bit set for
anyone cannot be run by direct invocation, only by an interpreter that was
itself given the file as an argument (sh file, bash file), which only
needs read access.
To Reproduce
printf '#!/bin/sh\necho hi\n' > f./f- Observe it runs and prints
hi, thoughfis mode 644 and was never made executable. - Contrast with a real system, where step 2 gives
-bash: ./f: Permission denied(exit status 126) untilfischmod +x'd.
Note sh f / bash f are unaffected either way (correctly so): passing a
script as an argument to an interpreter only needs read access on a real
system too, and does not go through getCommand()'s command-name
resolution at all.
Expected behavior
./path and /path (direct invocation, as opposed to sh path/bash path)
should require an applicable execute bit, matching a real kernel's
exec()/EACCES behavior, including that root still cannot execute a
file with no execute bit set for anybody.
Server (please complete the following information):
- OS: Windows 10 Pro; also reproduced on Linux (Ubuntu 24.04)
- Python: 3.14.2; also reproduced on 3.12.3
- Twisted: 26.4.0
Additional context
- Cowrie Mode: Shell (default)
Patch adds getCommand(self, cmd, paths, cwd, uid=0, gid=0) (new
keyword-only-by-convention uid/gid parameters, defaulting to root, so
every existing call site keeps working unchanged except the one below) and
a _may_execute() helper implementing the standard owner/group/other
selection: root is exempt from the class check itself but still requires
some execute bit to be set somewhere, matching real kernel semantics;
otherwise the file's owner/group is compared against uid/gid to pick
which class's execute bit applies. A denied file now returns a new command
class (permission_denied_cmd()) that writes bash's own message and sets
exit status 126, matching a real shell.
The check is scoped to explicit invocation only (cmd[0] in (".", "/")):
a bare command name resolved via PATH search keeps its current behavior,
since a real shell's PATH search has its own different semantics (a
non-executable match is skipped and the search continues, rather than
failing outright) that this patch does not attempt to replicate.
src/cowrie/shell/honeypot.py's one call site that runs a resolved
command (as opposed to sudo.py/busybox.py's own calls, which keep the
uid=0/gid=0 default) now passes the session's actual self.user["uid"]/
self.user["gid"].
Tested together with a related, separately-reported issue: several existing tests in
test_script_execution.py and test_input_size.py created a script with
a plain > redirect and ran it directly via ./path without ever
chmod-ing it, which is what this patch now correctly refuses; those
tests were updated to chmod +x (or, for test_input_size.py's
plant_file helper, to plant the fixture file already executable) where
the test is about something else (size cap, shebang stripping, etc.) and
a new test_dotslash_without_exec_bit_is_refused test was added to cover
the refusal itself. Full existing suite (1104 tests with both patches
applied) passes with no other regressions.
Patched/added files:
src/cowrie/shell/honeypot.pysrc/cowrie/shell/protocol.pysrc/cowrie/test/test_input_size.pysrc/cowrie/test/test_script_execution.py
honeypot.py protocol.py test_input_size.py test_script_execution.py
Source: cowrie/cowrie