`chmod`: symbolic modes (`+x`, `u+w`, `a=r`, ...) silently do nothing; octal `chmod` also discards the file's type bits
Describe the bug
src/cowrie/commands/chmod.py validates a symbolic mode string (+x,
u+rwx, go-w, a=r, ...) against MODE_REGEX and accepts it, but then
only ever applies a mode via int(mode, 8). For any symbolic spec this
raises ValueError, which is caught and silently discarded:
file_mode_no_perm = f[fs.A_MODE] & NO_PERM_BITS_MASK
try:
f[fs.A_MODE] = file_mode_no_perm | int(mode, 8)
except ValueError:
passThe command reports success (no error message, matching real chmod's
silent-on-success behavior) but the file's mode is never actually changed.
Only numeric/octal modes (chmod 755 file) work at all.
Separately, in the same three lines: NO_PERM_BITS_MASK = 0o000, so
file_mode_no_perm is always 0 regardless of the file's previous mode.
A_MODE carries the file's S_IFMT type bits (regular file/directory/
symlink) in the same field as its permission bits (see
src/cowrie/shell/fs.py, which itself uses the correct
stat.S_IFMT(p[A_MODE]) | perm idiom elsewhere when constructing a mode).
chmod's masking discards those type bits on every call, including a
chmod that only ever exercised the already-working octal path. I could
not find anywhere in the current codebase that reads stat.S_IFMT(A_MODE)
back out at query time (ls -l's directory/symlink marker uses a separate
A_TYPE field), so this currently has no visible symptom, but it means
A_MODE silently stops matching a real file's mode after any chmod, and
any future code that inspects stat.S_IFMT(file[A_MODE]) would see it
corrupted.
To Reproduce
echo hi > fchmod +x fls -l f- Observe the file is still
-rw-r--r---- the+xhad no effect, thoughchmodreported no error. - Contrast with
chmod 755 f, which does work (but see above for what it silently does to the file's type bits).
Expected behavior
chmod +x (and other symbolic modes) should update the target's
permission bits the same way chmod 755 does, matching what chmod --help's
own printed mode grammar ('[ugoa]*([-+=]([rwxXst]*|[ugo]))+')
already documents as accepted syntax. Any chmod, octal or symbolic,
should not disturb the file's type bits.
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 implements symbolic mode parsing and application (apply_symbolic_mode()
in chmod.py): who-classes (u/g/o/a, defaulting to all three when
omitted), +/-/= operators, r/w/x per class, X (conditional
execute: only applied if the target is a directory or already has some
execute bit set), s (setuid on the u class, setgid on the g class),
t (sticky), permission-copy (u=g etc.), and comma-separated clauses
(u+x,go-w) -- the last of these MODE_REGEX itself did not previously
accept at all. The octal path now preserves stat.S_IFMT(...) instead of
discarding it.
Existing tests only checked that a valid chmod invocation produced no
error output, never that it actually changed anything, which is how this
went unnoticed; src/cowrie/test/test_chmod_symbolic.py (new) checks the
resulting mode bits directly, including that a directory's type bit
survives both an octal and a symbolic chmod.
Tested together with a related, separately-reported issue: with both applied,
chmod +x file && ./file now works end to end (previously +x did nothing,
but ./file ran regardless of the missing execute bit anyway, so the net behavior
looked correct for that one case by accident). Full existing suite (1104
tests with both patches applied) passes with no other regressions.
Patched/added files:
src/cowrie/commands/chmod.pysrc/cowrie/test/test_chmod_symbolic.py(new)
Source: cowrie/cowrie