Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore non utf8 characters while reading files with core grains module #62633

Merged
merged 7 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/62633.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent possible tracebacks in core grains module by ignoring non utf8 characters in /proc/1/environ, /proc/1/cmdline, /proc/cmdline
12 changes: 9 additions & 3 deletions salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,9 @@ def _virtual(osdata):
if ("virtual_subtype" not in grains) or (grains["virtual_subtype"] != "LXC"):
if os.path.isfile("/proc/1/environ"):
try:
with salt.utils.files.fopen("/proc/1/environ", "r") as fhr:
with salt.utils.files.fopen(
"/proc/1/environ", "r", errors="ignore"
) as fhr:
MKLeb marked this conversation as resolved.
Show resolved Hide resolved
fhr_contents = fhr.read()
if "container=lxc" in fhr_contents:
grains["virtual"] = "container"
Expand Down Expand Up @@ -1960,7 +1962,9 @@ def os_data():
grains["init"] = "systemd"
except OSError:
try:
with salt.utils.files.fopen("/proc/1/cmdline") as fhr:
with salt.utils.files.fopen(
"/proc/1/cmdline", "r", errors="ignore"
) as fhr:
init_cmdline = fhr.read().replace("\x00", " ").split()
except OSError:
pass
Expand Down Expand Up @@ -3166,7 +3170,9 @@ def kernelparams():
return {}
else:
try:
with salt.utils.files.fopen("/proc/cmdline", "r") as fhr:
with salt.utils.files.fopen(
"/proc/cmdline", "r", errors="surrogateescape"
) as fhr:
cmdline = fhr.read()
grains = {"kernelparams": []}
for data in [
Expand Down
118 changes: 118 additions & 0 deletions tests/pytests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pathlib
import platform
import socket
import tempfile
import textwrap
from collections import namedtuple

Expand Down Expand Up @@ -2740,6 +2741,38 @@ def test_kernelparams_return_linux(cmdline, expectation):
assert core.kernelparams() == expectation


@pytest.mark.skip_unless_on_linux
def test_kernelparams_return_linux_non_utf8():
_salt_utils_files_fopen = salt.utils.files.fopen

expected = {
"kernelparams": [
("TEST_KEY1", "VAL1"),
("TEST_KEY2", "VAL2"),
("BOOTABLE_FLAG", "\udc80"),
("TEST_KEY_NOVAL", None),
("TEST_KEY3", "3"),
]
}

with tempfile.TemporaryDirectory() as tempdir:

def _open_mock(file_name, *args, **kwargs):
return _salt_utils_files_fopen(
os.path.join(tempdir, "cmdline"), *args, **kwargs
)

with salt.utils.files.fopen(
os.path.join(tempdir, "cmdline"),
"wb",
) as cmdline_fh, patch("salt.utils.files.fopen", _open_mock):
cmdline_fh.write(
b'TEST_KEY1=VAL1 TEST_KEY2=VAL2 BOOTABLE_FLAG="\x80" TEST_KEY_NOVAL TEST_KEY3=3\n'
)
cmdline_fh.close()
assert core.kernelparams() == expected


def test_linux_gpus():
"""
Test GPU detection on Linux systems
Expand Down Expand Up @@ -2993,3 +3026,88 @@ def _raise_fnfe():
with patch("os.path.isfile", return_value=True):
with patch("salt.utils.files.fopen", new=_mock_open):
assert expected == core._linux_devicetree_platform_data()


@pytest.mark.skip_on_windows
def test_linux_proc_files_with_non_utf8_chars():
_salt_utils_files_fopen = salt.utils.files.fopen

empty_mock = MagicMock(return_value={})

with tempfile.TemporaryDirectory() as tempdir:

def _mock_open(filename, *args, **kwargs):
return _salt_utils_files_fopen(
os.path.join(tempdir, "cmdline-1"), *args, **kwargs
)

with salt.utils.files.fopen(
os.path.join(tempdir, "cmdline-1"),
"wb",
) as cmdline_fh, patch("os.path.isfile", return_value=False), patch(
"salt.utils.files.fopen", _mock_open
), patch.dict(
core.__salt__,
{
"cmd.retcode": salt.modules.cmdmod.retcode,
"cmd.run": MagicMock(return_value=""),
},
), patch.object(
core, "_linux_bin_exists", return_value=False
), patch.object(
core, "_parse_lsb_release", return_value=empty_mock
), patch.object(
core, "_parse_os_release", return_value=empty_mock
), patch.object(
core, "_hw_data", return_value=empty_mock
), patch.object(
core, "_virtual", return_value=empty_mock
), patch.object(
core, "_bsd_cpudata", return_value=empty_mock
), patch.object(
os, "stat", side_effect=OSError()
):
cmdline_fh.write(
b"/usr/lib/systemd/systemd\x00--switched-root\x00--system\x00--deserialize\x0028\x80\x00"
)
cmdline_fh.close()
os_grains = core.os_data()
assert os_grains != {}


@pytest.mark.skip_on_windows
def test_virtual_linux_proc_files_with_non_utf8_chars():
_salt_utils_files_fopen = salt.utils.files.fopen

def _is_file_mock(filename):
if filename == "/proc/1/environ":
return True
return False

with tempfile.TemporaryDirectory() as tempdir:

def _mock_open(filename, *args, **kwargs):
return _salt_utils_files_fopen(
os.path.join(tempdir, "environ"), *args, **kwargs
)

with salt.utils.files.fopen(
os.path.join(tempdir, "environ"),
"wb",
) as environ_fh, patch("os.path.isfile", _is_file_mock), patch(
"salt.utils.files.fopen", _mock_open
), patch.object(
salt.utils.path, "which", MagicMock(return_value=None)
), patch.dict(
core.__salt__,
{
"cmd.run_all": MagicMock(
return_value={"retcode": 1, "stderr": "", "stdout": ""}
),
"cmd.run": MagicMock(return_value=""),
},
):
environ_fh.write(b"KEY1=VAL1 KEY2=VAL2\x80 KEY2=VAL2")
environ_fh.close()
virt_grains = core._virtual({"kernel": "Linux"})
assert virt_grains == {"virtual": "physical"}