]> git.alsa-project.org Git - alsa-lib.git/commitdiff
ucm: fix TOCTOU race in sysfs value substitution
authorJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 13:53:15 +0000 (14:53 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 14:01:19 +0000 (15:01 +0100)
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>
src/ucm/ucm_subs.c

index e4a238a9f8921b96cf76f76b1099b8a81947b4a1..96a1a44889129a3684303d56601bc41d4c367fbc 100644 (file)
@@ -744,28 +744,32 @@ static char *rval_sysfs_main(snd_use_case_mgr_t *uc_mgr, const char *top_path, c
                snprintf(path, sizeof(path), "%s/%s/%s", e, top_path, id);
        else
                snprintf(path, sizeof(path), "%s/%s", e, id);
-       if (lstat64(path, &sb) != 0)
-               return NULL;
-       if (S_ISLNK(sb.st_mode)) {
-               len = readlink(path, link, sizeof(link) - 1);
-               if (len <= 0) {
-                       snd_error(UCM, "sysfs: cannot read link '%s' (%d)", path, errno);
-                       return NULL;
-               }
+       /* Avoid a TOCTOU race: do not stat the path first and open it afterwards */
+       len = readlink(path, link, sizeof(link) - 1);
+       if (len >= 0) {
                link[len] = '\0';
                e = strrchr(link, '/');
                if (e)
                        return strdup(e + 1);
                return NULL;
        }
-       if (S_ISDIR(sb.st_mode))
-               return NULL;
-       if ((sb.st_mode & S_IRUSR) == 0)
+       if (errno != EINVAL)
+               /* EINVAL means "not a symlink" - proceed to open the file */
                return NULL;
 
-       fd = open(path, O_RDONLY);
+       /* Use O_NOFOLLOW to close the race window */
+       fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
        if (fd < 0) {
-               snd_error(UCM, "sysfs open failed for '%s' (%d)", path, errno);
+               if (errno != ENOENT && errno != ELOOP)
+                       snd_error(UCM, "sysfs open failed for '%s' (%d)", path, errno);
+               return NULL;
+       }
+       if (fstat64(fd, &sb) != 0) {
+               close(fd);
+               return NULL;
+       }
+       if (!S_ISREG(sb.st_mode) || (sb.st_mode & S_IRUSR) == 0) {
+               close(fd);
                return NULL;
        }
        len = sizeof(path) - 1;