Separately checking a file's state (lstat64) before opening it lets the
final path component be replaced in between, so the subsequent open() may
operate on a different object than was checked.
Probe and operate on the same object instead: read the link first and, if
the path is not a symlink, open it with O_NOFOLLOW and run the type and
permission checks on the opened descriptor via fstat64(). O_NOFOLLOW only
constrains the final component, so the symbolic links that sysfs uses for
the intermediate components are still resolved as before. Also tighten the
accepted value source to regular files only.
Based on a patch by Mingjie Shen.
Closes: https://github.com/alsa-project/alsa-lib/pull/367 Signed-off-by: Jaroslav Kysela <perex@perex.cz>