saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Install Salt from the Salt package repositories here:
https://docs.saltproject.io/salt/install-guide/en/latest/
Apache License 2.0
14.2k stars 5.48k forks source link

[master] Don't crash if dmesg contains non-utf8 characters #66764

Open asomers opened 3 months ago

asomers commented 3 months ago

On FreeBSD Salt scrapes /var/run/dmesg.boot to set the "cpu_flags" grain. But it's possible for that file to contain non-UTF-8 characters. Skipping over such characters is better than crashing.

What does this PR do?

Fixes a crash on startup on FreeBSD systems if non-UTF8 data is present in /var/run/dmesg.boot . That can happen, for example, if a connected SAS device has a serial number containing non-UTF8 characters.

Previous Behavior

On such systems, Salt would crash on startup with an error like this:

2022-06-30 13:54:03,038 [salt.log.setup   :911 ][ERROR   ][25928] An un-handled exception was caught by salt's global exception handler:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 470: invalid start byte
Traceback (most recent call last):
  File "/usr/local/bin/salt-call", line 33, in <module>
    sys.exit(load_entry_point('salt==3004', 'console_scripts', 'salt-call')())
  File "/usr/local/lib/python3.8/site-packages/salt/scripts.py", line 432, in salt_call
    client.run()
  File "/usr/local/lib/python3.8/site-packages/salt/cli/call.py", line 45, in run
    caller = salt.cli.caller.Caller.factory(self.config)
  File "/usr/local/lib/python3.8/site-packages/salt/cli/caller.py", line 55, in factory
    return ZeroMQCaller(opts, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/salt/cli/caller.py", line 319, in _init_
    super()._init_(opts)
  File "/usr/local/lib/python3.8/site-packages/salt/cli/caller.py", line 79, in _init_
    self.minion = salt.minion.SMinion(opts)
  File "/usr/local/lib/python3.8/site-packages/salt/minion.py", line 926, in _init_
    opts["grains"] = salt.loader.grains(opts)
  File "/usr/local/lib/python3.8/site-packages/salt/loader/_init_.py", line 909, in grains
    ret = funcs[key]()
  File "/usr/local/lib/python3.8/site-packages/salt/loader/lazy.py", line 149, in _call_
    return self.loader.run(run_func, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/salt/loader/lazy.py", line 1201, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/salt/loader/lazy.py", line 1216, in _run_as
    return _func_or_method(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/salt/grains/core.py", line 2251, in os_data
    grains.update(_bsd_cpudata(grains))
  File "/usr/local/lib/python3.8/site-packages/salt/grains/core.py", line 412, in _bsd_cpudata
    for line in _fp:
  File "/usr/local/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 470: invalid start byte

New Behavior

Salt will skip over the illegal bytes in /var/run/dmesg.boot .

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes