Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

firmware: arm_scpi: Ensure scpi_info is not assigned if the probe fails

When scpi probe fails, at any point, we need to ensure that the scpi_info
is not set and will remain NULL until the probe succeeds. If it is not
taken care, then it could result use-after-free as the value is exported
via get_scpi_ops() and could refer to a memory allocated via devm_kzalloc()
but freed when the probe fails.

Link: https://lore.kernel.org/r/20220701160310.148344-1-sudeep.holla@arm.com
Cc: stable@vger.kernel.org # 4.19+
Reported-by: huhai <huhai@kylinos.cn>
Reviewed-by: Jackie Liu <liuyun01@kylinos.cn>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>

+35 -26
+35 -26
drivers/firmware/arm_scpi.c
··· 815 815 info->firmware_version = le32_to_cpu(caps.platform_version); 816 816 } 817 817 /* Ignore error if not implemented */ 818 - if (scpi_info->is_legacy && ret == -EOPNOTSUPP) 818 + if (info->is_legacy && ret == -EOPNOTSUPP) 819 819 return 0; 820 820 821 821 return ret; ··· 913 913 struct resource res; 914 914 struct device *dev = &pdev->dev; 915 915 struct device_node *np = dev->of_node; 916 + struct scpi_drvinfo *scpi_drvinfo; 916 917 917 - scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL); 918 - if (!scpi_info) 918 + scpi_drvinfo = devm_kzalloc(dev, sizeof(*scpi_drvinfo), GFP_KERNEL); 919 + if (!scpi_drvinfo) 919 920 return -ENOMEM; 920 921 921 922 if (of_match_device(legacy_scpi_of_match, &pdev->dev)) 922 - scpi_info->is_legacy = true; 923 + scpi_drvinfo->is_legacy = true; 923 924 924 925 count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); 925 926 if (count < 0) { ··· 928 927 return -ENODEV; 929 928 } 930 929 931 - scpi_info->channels = devm_kcalloc(dev, count, sizeof(struct scpi_chan), 932 - GFP_KERNEL); 933 - if (!scpi_info->channels) 930 + scpi_drvinfo->channels = 931 + devm_kcalloc(dev, count, sizeof(struct scpi_chan), GFP_KERNEL); 932 + if (!scpi_drvinfo->channels) 934 933 return -ENOMEM; 935 934 936 - ret = devm_add_action(dev, scpi_free_channels, scpi_info); 935 + ret = devm_add_action(dev, scpi_free_channels, scpi_drvinfo); 937 936 if (ret) 938 937 return ret; 939 938 940 - for (; scpi_info->num_chans < count; scpi_info->num_chans++) { 939 + for (; scpi_drvinfo->num_chans < count; scpi_drvinfo->num_chans++) { 941 940 resource_size_t size; 942 - int idx = scpi_info->num_chans; 943 - struct scpi_chan *pchan = scpi_info->channels + idx; 941 + int idx = scpi_drvinfo->num_chans; 942 + struct scpi_chan *pchan = scpi_drvinfo->channels + idx; 944 943 struct mbox_client *cl = &pchan->cl; 945 944 struct device_node *shmem = of_parse_phandle(np, "shmem", idx); 946 945 ··· 987 986 return ret; 988 987 } 989 988 990 - scpi_info->commands = scpi_std_commands; 989 + scpi_drvinfo->commands = scpi_std_commands; 991 990 992 - platform_set_drvdata(pdev, scpi_info); 991 + platform_set_drvdata(pdev, scpi_drvinfo); 993 992 994 - if (scpi_info->is_legacy) { 993 + if (scpi_drvinfo->is_legacy) { 995 994 /* Replace with legacy variants */ 996 995 scpi_ops.clk_set_val = legacy_scpi_clk_set_val; 997 - scpi_info->commands = scpi_legacy_commands; 996 + scpi_drvinfo->commands = scpi_legacy_commands; 998 997 999 998 /* Fill priority bitmap */ 1000 999 for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++) 1001 1000 set_bit(legacy_hpriority_cmds[idx], 1002 - scpi_info->cmd_priority); 1001 + scpi_drvinfo->cmd_priority); 1003 1002 } 1004 1003 1005 - ret = scpi_init_versions(scpi_info); 1004 + scpi_info = scpi_drvinfo; 1005 + 1006 + ret = scpi_init_versions(scpi_drvinfo); 1006 1007 if (ret) { 1007 1008 dev_err(dev, "incorrect or no SCP firmware found\n"); 1009 + scpi_info = NULL; 1008 1010 return ret; 1009 1011 } 1010 1012 1011 - if (scpi_info->is_legacy && !scpi_info->protocol_version && 1012 - !scpi_info->firmware_version) 1013 + if (scpi_drvinfo->is_legacy && !scpi_drvinfo->protocol_version && 1014 + !scpi_drvinfo->firmware_version) 1013 1015 dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n"); 1014 1016 else 1015 1017 dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n", 1016 1018 FIELD_GET(PROTO_REV_MAJOR_MASK, 1017 - scpi_info->protocol_version), 1019 + scpi_drvinfo->protocol_version), 1018 1020 FIELD_GET(PROTO_REV_MINOR_MASK, 1019 - scpi_info->protocol_version), 1021 + scpi_drvinfo->protocol_version), 1020 1022 FIELD_GET(FW_REV_MAJOR_MASK, 1021 - scpi_info->firmware_version), 1023 + scpi_drvinfo->firmware_version), 1022 1024 FIELD_GET(FW_REV_MINOR_MASK, 1023 - scpi_info->firmware_version), 1025 + scpi_drvinfo->firmware_version), 1024 1026 FIELD_GET(FW_REV_PATCH_MASK, 1025 - scpi_info->firmware_version)); 1026 - scpi_info->scpi_ops = &scpi_ops; 1027 + scpi_drvinfo->firmware_version)); 1027 1028 1028 - return devm_of_platform_populate(dev); 1029 + scpi_drvinfo->scpi_ops = &scpi_ops; 1030 + 1031 + ret = devm_of_platform_populate(dev); 1032 + if (ret) 1033 + scpi_info = NULL; 1034 + 1035 + return ret; 1029 1036 } 1030 1037 1031 1038 static const struct of_device_id scpi_of_match[] = {