| @@ -733,82 +733,6 @@ const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb) · 732 unmodified lines |
| 733 | 733 | return has_metadata_uuid ? sb->metadata_uuid : sb->fsid; |
| 734 | 734 | } |
| 735 | 735 | |
| 736 | | −/* |
| 737 | | − * We can have very weird soft links passed in. |
| 738 | | − * One example is "/proc/self/fd/<fd>", which can be a soft link to |
| 739 | | − * a block device. |
| 740 | | − * |
| 741 | | − * But it's never a good idea to use those weird names. |
| 742 | | − * Here we check if the path (not following symlinks) is a good one inside |
| 743 | | − * "/dev/". |
| 744 | | − */ |
| 745 | | −static bool is_good_dev_path(const char *dev_path) |
| 746 | | −{ |
| 747 | | − struct path path = { .mnt = NULL, .dentry = NULL }; |
| 748 | | − char *path_buf = NULL; |
| 749 | | − char *resolved_path; |
| 750 | | − bool is_good = false; |
| 751 | | − int ret; |
| 752 | | − |
| 753 | | − if (!dev_path) |
| 754 | | − goto out; |
| 755 | | − |
| 756 | | − path_buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 757 | | − if (!path_buf) |
| 758 | | − goto out; |
| 759 | | − |
| 760 | | − /* |
| 761 | | − * Do not follow soft link, just check if the original path is inside |
| 762 | | − * "/dev/". |
| 763 | | − */ |
| 764 | | − ret = kern_path(dev_path, 0, &path); |
| 765 | | − if (ret) |
| 766 | | − goto out; |
| 767 | | − resolved_path = d_path(&path, path_buf, PATH_MAX); |
| 768 | | − if (IS_ERR(resolved_path)) |
| 769 | | − goto out; |
| 770 | | − if (strncmp(resolved_path, "/dev/", strlen("/dev/"))) |
| 771 | | − goto out; |
| 772 | | − is_good = true; |
| 773 | | −out: |
| 774 | | − kfree(path_buf); |
| 775 | | − path_put(&path); |
| 776 | | − return is_good; |
| 777 | | −} |
| 778 | | − |
| 779 | | −static int get_canonical_dev_path(const char *dev_path, char *canonical) |
| 780 | | −{ |
| 781 | | − struct path path = { .mnt = NULL, .dentry = NULL }; |
| 782 | | − char *path_buf = NULL; |
| 783 | | − char *resolved_path; |
| 784 | | − int ret; |
| 785 | | − |
| 786 | | − if (!dev_path) { |
| 787 | | − ret = -EINVAL; |
| 788 | | − goto out; |
| 789 | | − } |
| 790 | | − |
| 791 | | − path_buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 792 | | − if (!path_buf) { |
| 793 | | − ret = -ENOMEM; |
| 794 | | − goto out; |
| 795 | | − } |
| 796 | | − |
| 797 | | − ret = kern_path(dev_path, LOOKUP_FOLLOW, &path); |
| 798 | | − if (ret) |
| 799 | | − goto out; |
| 800 | | − resolved_path = d_path(&path, path_buf, PATH_MAX); |
| 801 | | − if (IS_ERR(resolved_path)) { |
| 802 | | − ret = PTR_ERR(resolved_path); |
| 803 | | − goto out; |
| 804 | | − } |
| 805 | | − ret = strscpy(canonical, resolved_path, PATH_MAX); |
| 806 | | −out: |
| 807 | | − kfree(path_buf); |
| 808 | | − path_put(&path); |
| 809 | | − return ret; |
| 810 | | −} |
| 811 | | − |
| 812 | 736 | static bool is_same_device(struct btrfs_device *device, const char *new_path) |
| 813 | 737 | { |
| 814 | 738 | struct path old = { .mnt = NULL, .dentry = NULL }; |
| @@ -1513,23 +1437,12 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags, · 698 unmodified lines |
| 1513 | 1437 | bool new_device_added = false; |
| 1514 | 1438 | struct btrfs_device *device = NULL; |
| 1515 | 1439 | struct file *bdev_file; |
| 1516 | | − char *canonical_path = NULL; |
| 1517 | 1440 | u64 bytenr; |
| 1518 | 1441 | dev_t devt; |
| 1519 | 1442 | int ret; |
| 1520 | 1443 | |
| 1521 | 1444 | lockdep_assert_held(&uuid_mutex); |
| 1522 | 1445 | |
| 1523 | | − if (!is_good_dev_path(path)) { |
| 1524 | | − canonical_path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1525 | | − if (canonical_path) { |
| 1526 | | − ret = get_canonical_dev_path(path, canonical_path); |
| 1527 | | − if (ret < 0) { |
| 1528 | | − kfree(canonical_path); |
| 1529 | | − canonical_path = NULL; |
| 1530 | | − } |
| 1531 | | − } |
| 1532 | | − } |
| 1533 | 1446 | /* |
| 1534 | 1447 | * Avoid an exclusive open here, as the systemd-udev may initiate the |
| 1535 | 1448 | * device scan which may race with the user's mount or mkfs command, |
| @@ -1574,8 +1487,7 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags, · 38 unmodified lines |
| 1574 | 1487 | goto free_disk_super; |
| 1575 | 1488 | } |
| 1576 | 1489 | |
| 1577 | | − device = device_list_add(canonical_path ? : path, disk_super, |
| 1578 | | − &new_device_added); |
| 1490 | + device = device_list_add(path, disk_super, &new_device_added); |
| 1579 | 1491 | if (!IS_ERR(device) && new_device_added) |
| 1580 | 1492 | btrfs_free_stale_devices(device->devt, device); |
| 1581 | 1493 | |
| @@ -1584,7 +1496,6 @@ free_disk_super: · 2 unmodified lines |
| 1584 | 1496 | |
| 1585 | 1497 | error_bdev_put: |
| 1586 | 1498 | fput(bdev_file); |
| 1587 | | − kfree(canonical_path); |
| 1588 | 1499 | |
| 1589 | 1500 | return device; |
| 1590 | 1501 | } |