stanislav-tkach / os_info

Rust library to detect the operating system type
MIT License
175 stars 52 forks source link

Bug: OracleLinux false detections #315

Closed exincore closed 2 years ago

exincore commented 2 years ago

I believe I have found how this module has been falsely identifying so many operating systems as OracleLinux. See:

Failure Mode

  1. linux/mod.rs is run
  2. lsb_release is not installed and fails properly; linux/mod.rs falls back to file_release
  3. Using an array of 6 predefined DISTRIBUTIONS, file_release will initially attempt to read a {mariner,centos,fedora,alpine}-release, each of which fail properly to the next in the array since those files do not exist in this scenario.
  4. file_release arrives at os-release with a fallback Type as OracleLinux as defined in DISTRIBUTIONS. The os-release file DOES exist and a parsing attempt is made.
  5. The parser Matcher::KeyValue searches for "NAME"
  6. The get_type function attempts to convert the identified name string into a Type, but fails to match due to not being robust enough: lacking any sort of fuzzy matching or regex, the NAME field not existing, or having a limited list of potential matches.
  7. With None returned by get_type, file_release uses the fallback Type OracleLinux from the current array element, and proceeds to identify the version as it would any other os-release.
  8. With a Type and Version in hand, file_release returns, short-circuiting any other array elements defined in DISTRIBUTIONS as long as os-release exists.
  9. The wrong Type of OracleLinux is returned, while Version is correct as it is specific to os-release and not the returned OracleLinux.

Notes

davidkna commented 2 years ago

Instead of fuzzy matching, looking at ID (and ID_LIKE) likely works better than NAME for parsing.

exincore commented 2 years ago

I'll go ahead and fork and see if I can take a swing at improving refactoring get_type slightly significantly. Currently I am was looking at accumulating all the detected *-release files together, then regex capturing for ID. If that fails it will fallback to the DISTRIBUTIONS method as that presumably covers older cases before os-release became mainstream or if there are some other distribution quirks.

exincore commented 2 years ago

I found a resource for the os-release files to cross reference with what ID fields I know: https://github.com/chef/os_release

stanislav-tkach commented 2 years ago

Thank you for the very detailed bug report! And sorry for the late reply - I have recently changed a job, so not it takes most of my free time. I see you drafted the pull request and will try to look into it.