CVE-2021-47634

Updated on 26 Feb 2025

Severity

6.4 Medium severity

Details

CVSS score
6.4
CVSS vector
CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H

Overview

About vulnerability

In the Linux kernel, the following vulnerability has been resolved:

ubi: Fix race condition between ctrl_cdev_ioctl and ubi_cdev_ioctl

Hulk Robot reported a KASAN report about use-after-free:

BUG: KASAN: use-after-free in __list_del_entry_valid+0x13d/0x160 Read of size 8 at addr ffff888035e37d98 by task ubiattach/1385 […] Call Trace: klist_dec_and_del+0xa7/0x4a0 klist_put+0xc7/0x1a0 device_del+0x4d4/0xed0 cdev_device_del+0x1a/0x80 ubi_attach_mtd_dev+0x2951/0x34b0 [ubi] ctrl_cdev_ioctl+0x286/0x2f0 [ubi]

Allocated by task 1414: device_add+0x60a/0x18b0 cdev_device_add+0x103/0x170 ubi_create_volume+0x1118/0x1a10 [ubi] ubi_cdev_ioctl+0xb7f/0x1ba0 [ubi]

Freed by task 1385: cdev_device_del+0x1a/0x80 ubi_remove_volume+0x438/0x6c0 [ubi] ubi_cdev_ioctl+0xbf4/0x1ba0 [ubi] […]

The lock held by ctrl_cdev_ioctl is ubi_devices_mutex, but the lock held by ubi_cdev_ioctl is ubi->device_mutex. Therefore, the two locks can be concurrent.

ctrl_cdev_ioctl contains two operations: ubi_attach and ubi_detach. ubi_detach is bug-free because it uses reference counting to prevent concurrency. However, uif_init and uif_close in ubi_attach may race with ubi_cdev_ioctl.

uif_init will race with ubi_cdev_ioctl as in the following stack. cpu1 cpu2 cpu3 ||_____________________ ctrl_cdev_ioctl ubi_attach_mtd_dev uif_init ubi_cdev_ioctl ubi_create_volume cdev_device_add ubi_add_volume // sysfs exist kill_volumes ubi_cdev_ioctl ubi_remove_volume cdev_device_del // first free ubi_free_volume cdev_del // double free cdev_device_del

And uif_close will race with ubi_cdev_ioctl as in the following stack. cpu1 cpu2 cpu3 ||_____________________ ctrl_cdev_ioctl ubi_attach_mtd_dev uif_init ubi_cdev_ioctl ubi_create_volume cdev_device_add ubi_debugfs_init_dev //error goto out_uif; uif_close kill_volumes ubi_cdev_ioctl ubi_remove_volume cdev_device_del // first free ubi_free_volume // double free

The cause of this problem is that commit 714fb87e8bc0 make device “available” before it becomes accessible via sysfs. Therefore, we roll back the modification. We will fix the race condition between ubi device creation and udev by removing ubi_get_device in vol_attribute_show and dev_attribute_show.This avoids accessing uninitialized ubi_devices[ubi_num].

ubi_get_device is used to prevent devices from being deleted during sysfs execution. However, now kernfs ensures that devices will not be deleted before all reference counting are released. The key process is shown in the following stack.

device_del device_remove_attrs device_remove_groups sysfs_remove_groups sysfs_remove_group remove_files kernfs_remove_by_name kernfs_remove_by_name_ns __kernfs_remove kernfs_drain

Details

Fixes

KernelCare state

Live-patch status from KernelCare for each operating system.

Operating system Status Covered kernels
AlmaLinux 9.2 ESU Planned
Amazon Linux 2 Released
44 kernels
  • 4.14.152-127.182.amzn2
  • 4.14.154-128.181.amzn2
  • 4.14.165-131.185.amzn2
  • 4.14.146-120.181.amzn2
  • 4.14.152-124.171.amzn2
  • 4.14.171-136.231.amzn2
  • 4.14.158-129.185.amzn2
  • 4.14.165-133.209.amzn2
  • 4.14.173-137.228.amzn2
  • 4.14.177-139.254.amzn2
  • 4.14.173-137.229.amzn2
  • 4.14.177-139.253.amzn2
  • 4.14.181-140.257.amzn2
  • 4.14.186-146.268.amzn2
  • 4.14.181-142.260.amzn2
  • 4.14.192-147.314.amzn2
  • 4.14.193-149.317.amzn2
  • 4.14.198-152.320.amzn2
  • 4.14.200-155.322.amzn2
  • 4.14.203-156.332.amzn2
  • 4.14.209-160.335.amzn2
  • 4.14.214-160.339.amzn2
  • 4.14.209-160.339.amzn2
  • 4.14.219-161.340.amzn2
  • 4.14.225-168.357.amzn2
  • 4.14.231-173.360.amzn2
  • 4.14.219-164.354.amzn2
  • 4.14.225-169.362.amzn2
  • 4.14.232-176.381.amzn2
  • 4.14.232-177.418.amzn2
  • 4.14.238-182.421.amzn2
  • 4.14.238-182.422.amzn2
  • 4.14.231-173.361.amzn2
  • 4.14.241-184.433.amzn2
  • 4.14.243-185.433.amzn2
  • 4.14.246-187.474.amzn2
  • 4.14.248-189.473.amzn2
  • 4.14.252-195.481.amzn2
  • 4.14.252-195.483.amzn2
  • 4.14.256-197.484.amzn2
  • 4.14.262-200.489.amzn2
  • 4.14.268-205.500.amzn2
  • 4.14.273-207.502.amzn2
  • 4.14.275-207.503.amzn2
Amazon Linux 2 5.10 Will Not Fix
Amazon Linux 2 5.4 Will Not Fix
Debian 11 Planned
Ubuntu 16.04 AWS HWE ESM Planned
Ubuntu 16.04 Azure ESM Will Not Fix
Ubuntu 16.04 GCP ESM Planned
Ubuntu 16.04 HWE ESM Planned
Ubuntu 18.04 Planned
Ubuntu 18.04 AWS Focal Planned
Ubuntu 20.04 Planned
Ubuntu 22.04 Planned
Ubuntu 22.04 AWS Planned