Skip to content

Commit ef1b6e2

Browse files
committed
fix(pe2): cover setgid and world-writable numeric chmod modes
Review of #678 found three coverage gaps against main's behaviour, all reproduced before fixing: - [4567] only selected setuid. A leading 2 or 3 sets setgid, which escalates an executable the same way because it then runs with the file's group, so `chmod 2755`/`chmod 3755` were reported on main and on this head by nothing. Use [2-7]; a bare leading 1 only adds sticky, which grants nobody new access, and stays unreported. - The terminator allowlist (?=\s|$|[;&|]) dropped valid spellings that main caught: leading zeros (chmod 04755, chmod 006755), and terminators such as a backtick, `)`, or ${IFS}. Accept 0* before the special digit and replace the allowlist with a trailing (?![0-9\w]) lookahead, which needs no allowlist and still rejects chmod 47554. - Requiring the leading digit also un-reported every world-writable numeric mode whose spelling does not contain a literal 777/666, so `chmod 646 /etc/sudoers` and `chmod 757 ...` were reported on main and on this head by no static pattern. Widen TM1, which already owned the literal 777/666 spellings, to any numeric mode whose others-triple carries the write bit (last digit 2, 3, 6 or 7). Anchoring on the mode argument also stops a 777/666 substring inside a later path or comment from being read as the mode. PE2 is "Sudo/Root Execution" and a world-writable file grants access to every user rather than to root, so the finding keeps its existing home. Also allow an option before the mode (chmod -R 4755 dir), which was missed before this change too, with negative cases proving the option group does not let a restrictive mode through. Reverting any one of the eight regex elements turns at least one test red; each was checked individually. Signed-off-by: Yi-111-a <[email protected]>
1 parent 5e599f8 commit ef1b6e2

3 files changed

Lines changed: 104 additions & 6 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_privilege_escalation.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@
9090
# digit, so the full four-digit form is what has to be present. Accepting a
9191
# bare 4/5/6/7 anywhere in the mode flagged every ordinary permission whose
9292
# rwx triple happened to contain one (chmod 600, chmod 644, chmod 755, ...).
93-
# The trailing boundary keeps a longer digit run from being read as a mode.
94-
(r"chmod\s+[\"']?[4567][0-7]{3}[\"']?(?=\s|$|[;&|])", 0.8),
93+
# Leading digit 2-7 selects setgid (2, 3, 6) or setuid (4, 5, 7). A bare 1
94+
# only adds sticky, which grants nobody new access, so it stays unreported.
95+
# The pieces around the mode: 0* absorbs the leading zeros GNU and BSD chmod
96+
# accept, the option group lets `chmod -R 4755 dir` reach the mode, the
97+
# optional quotes cover `chmod "6755"`, and the trailing lookahead rejects a
98+
# longer digit run (chmod 47554) as well as a malformed trailing word
99+
# (chmod 4755x) while still matching every non-alphanumeric terminator
100+
# (space, end of line, `;`, `&`, `|`, `)`, `$`, a quote or a backtick).
101+
(
102+
r"chmod\s+(?:--?[\w=-]*[ \t]+)*[\"']?0*[2-7][0-7]{3}[\"']?(?![0-9\w])",
103+
0.8,
104+
),
95105
]
96106
PE2_PROSE_PATTERNS = [
97107
(r"(?:run|execute)\s+(?:as|with)\s+root", 0.8),

‎src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,15 @@
212212
0.85,
213213
),
214214
(r"\bshutil\.rmtree\s*\(\s*['\"]\s*/", 0.85),
215-
(r"(?:chmod|chown)\s+[^|]*(?:777|666|a\+rwx)", 0.8),
215+
# A numeric chmod/chown mode is world-writable when the others-triple
216+
# carries the write bit, i.e. its last digit is 2, 3, 6 or 7. Matching the
217+
# mode argument itself rather than a bare `777`/`666` substring also keeps a
218+
# path or a trailing comment from being read as the mode (chmod 755 /tmp/6660
219+
# is a chmod 755). This owns the numeric forms that TM1 already owned for
220+
# 777/666, plus the 646/757/0662 spellings the privilege-escalation rule used
221+
# to catch as a side effect of matching 4/5/6/7 anywhere in the mode.
222+
(r"(?:chmod|chown)\s+(?:--?[\w=-]*[ \t]+)*[\"']?0*[0-7]{2,3}[2367](?![0-9\w])[\"']?", 0.8),
223+
(r"(?:chmod|chown)\s+[^|]*a\+rwx", 0.8),
216224
# Git force operations
217225
(r"git\s+push\s+[^|]*--force", 0.7),
218226
(r"git\s+reset\s+--hard", 0.65),

‎tests/unit/test_patterns.py‎

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
from skillspector.nodes.analyzers import (
3737
static_patterns_supply_chain as supply_chain_module,
3838
)
39+
from skillspector.nodes.analyzers import (
40+
static_patterns_tool_misuse as tool_misuse_module,
41+
)
3942
from skillspector.nodes.analyzers import static_runner
4043
from skillspector.python_ast import prewarm_python_ast_cache
4144

@@ -1390,15 +1393,20 @@ def test_shared_documentation_filter_does_not_apply_to_executable_files(self) ->
13901393
pytest.param("chmod 0700 ./.ssh", id="restrictive_0700"),
13911394
pytest.param("chmod 0755 /usr/local/bin/tool", id="restrictive_0755"),
13921395
pytest.param("chmod 755 entrypoint.sh", id="restrictive_755"),
1393-
pytest.param("chmod 666 shared.log", id="restrictive_666"),
1394-
pytest.param("chmod 777 /tmp/out", id="restrictive_777"),
1396+
pytest.param("chmod 00755 tool", id="restrictive_00755"),
1397+
pytest.param("chmod 100644 file", id="git_file_mode_100644"),
1398+
pytest.param("chmod 1755 dir", id="sticky_only_1755"),
13951399
pytest.param("chmod 47554 helper", id="overlong_digit_run"),
1400+
pytest.param("chmod 4755x helper", id="malformed_trailing_word"),
1401+
pytest.param("chmod -R 0755 dir", id="restrictive_after_option"),
1402+
pytest.param("chmod --recursive 0644 dir", id="restrictive_after_long_option"),
1403+
pytest.param("chmod -v 700 key.pem", id="restrictive_700_after_option"),
13961404
],
13971405
)
13981406
def test_pe2_restrictive_numeric_chmod_modes_are_not_privilege_escalation(
13991407
self, source: str
14001408
) -> None:
1401-
"""Modes without setuid/setgid/sticky digits must not raise PE2."""
1409+
"""Modes without setuid/setgid digits must not raise PE2."""
14021410
findings = privilege_escalation_module.analyze(source, "setup.sh", "shell")
14031411
assert not any(finding.rule_id == "PE2" for finding in findings), findings
14041412

@@ -1407,7 +1415,25 @@ def test_pe2_restrictive_numeric_chmod_modes_are_not_privilege_escalation(
14071415
[
14081416
pytest.param("chmod 4755 helper", id="setuid_4755"),
14091417
pytest.param("chmod 6755 helper", id="setuid_setgid_6755"),
1418+
# A leading 2 or 3 sets setgid, which is the same escalation on an
1419+
# executable: it then runs with the file's group.
1420+
pytest.param("chmod 2755 helper", id="setgid_only_2755"),
1421+
pytest.param("chmod 3755 helper", id="setgid_sticky_3755"),
14101422
pytest.param("chmod 7777 tmp", id="all_special_bits_7777"),
1423+
pytest.param("chmod 4755 dir", id="setgid_group_write_4755"),
1424+
# GNU and BSD chmod both accept leading zeros on a four-digit mode.
1425+
pytest.param("chmod 04755 helper", id="leading_zero_04755"),
1426+
pytest.param("chmod 006755 helper", id="two_leading_zeros_006755"),
1427+
# Terminators other than whitespace/EOL/[;&|].
1428+
pytest.param("Run `chmod 4755` on the helper", id="backtick_terminated"),
1429+
pytest.param("$(chmod 4755)", id="command_substitution_terminated"),
1430+
pytest.param("os.system('chmod 4755')", id="call_parenthesis_terminated"),
1431+
pytest.param("chmod 4755${IFS}/tmp/x", id="brace_expansion_terminated"),
1432+
pytest.param("chmod 4755|xargs ls", id="pipe_terminated"),
1433+
# An option before the mode reaches the same digits.
1434+
pytest.param("chmod -R 4755 dir", id="short_option_before_mode"),
1435+
pytest.param("chmod -v 4755 x", id="verbose_option_before_mode"),
1436+
pytest.param("chmod --recursive 6755 dir", id="long_option_before_mode"),
14111437
pytest.param('chmod "4755" helper', id="quoted_4755"),
14121438
pytest.param("chmod '6755' helper", id="quoted_6755"),
14131439
pytest.param("chmod 4755 helper; echo done", id="command_separator"),
@@ -1421,6 +1447,60 @@ def test_pe2_keeps_special_bit_chmod_modes(self, source: str) -> None:
14211447
findings = privilege_escalation_module.analyze(source, "setup.sh", "shell")
14221448
assert any(finding.rule_id == "PE2" for finding in findings), findings
14231449

1450+
@pytest.mark.parametrize(
1451+
"source",
1452+
[
1453+
pytest.param("chmod 646 /etc/sudoers", id="others_write_646"),
1454+
pytest.param("chmod 757 /usr/local/bin/backup.sh", id="others_write_757"),
1455+
pytest.param("chmod 0662 /etc/group", id="leading_zero_0662"),
1456+
pytest.param("chmod 666 shared.log", id="others_rw_666"),
1457+
pytest.param("chmod 777 /tmp/out", id="others_rwx_777"),
1458+
pytest.param("chmod 0777 dir", id="leading_zero_0777"),
1459+
pytest.param("chmod 1777 dir", id="sticky_others_rwx_1777"),
1460+
pytest.param("chmod -R 777 dir", id="option_before_777"),
1461+
pytest.param('chmod "777" dir', id="quoted_777"),
1462+
pytest.param("chmod 626 f", id="others_write_626"),
1463+
pytest.param("chmod 237 f", id="setgid_others_rwx_237"),
1464+
],
1465+
)
1466+
def test_pe2_defers_world_writable_numeric_modes_to_tm1(self, source: str) -> None:
1467+
"""World-writable modes keep a finding, but not a root-execution one.
1468+
1469+
PE2 is "Sudo/Root Execution"; a world-writable file grants access to every
1470+
user rather than to root, so labelling it a root-execution escalation
1471+
misstates the operation. TM1 ("Tool Parameter Abuse") already owned the
1472+
literal 777/666 spellings, so it owns the rest of the family too and the
1473+
coverage #671 asks to preserve stays in one rule instead of two.
1474+
"""
1475+
pe2 = privilege_escalation_module.analyze(source, "setup.sh", "shell")
1476+
assert not any(finding.rule_id == "PE2" for finding in pe2), pe2
1477+
tm1 = tool_misuse_module.analyze(source, "setup.sh", "shell")
1478+
assert any(finding.rule_id == "TM1" for finding in tm1), tm1
1479+
1480+
def test_pe2_and_tm1_both_fire_for_a_setuid_world_writable_mode(self) -> None:
1481+
"""2755/4666-style modes are genuinely two different findings."""
1482+
source = "chmod 4666 /usr/local/bin/wrapper"
1483+
assert any(
1484+
f.rule_id == "PE2"
1485+
for f in privilege_escalation_module.analyze(source, "setup.sh", "shell")
1486+
)
1487+
assert any(
1488+
f.rule_id == "TM1" for f in tool_misuse_module.analyze(source, "setup.sh", "shell")
1489+
)
1490+
1491+
@pytest.mark.parametrize(
1492+
"source",
1493+
[
1494+
pytest.param("chmod 755 /tmp/6660.txt", id="666_inside_a_path"),
1495+
pytest.param("chmod 644 notes.txt # keep 0777 style", id="777_inside_a_comment"),
1496+
pytest.param("chmod 755 entrypoint.sh", id="plain_restrictive_mode"),
1497+
],
1498+
)
1499+
def test_tm1_world_writable_pattern_reads_the_mode_not_a_later_token(self, source: str) -> None:
1500+
"""TM1 must key on the mode argument, not a 777/666 substring anywhere."""
1501+
findings = tool_misuse_module.analyze(source, "setup.sh", "shell")
1502+
assert not any(finding.rule_id == "TM1" for finding in findings), findings
1503+
14241504
@pytest.mark.parametrize(
14251505
"source",
14261506
[

0 commit comments

Comments
 (0)