Skip to content

Commit 83574b3

Browse files
committed
firmware: arm_scpi: Ensure scpi_info is not assigned if the probe fails
jira VULN-70082 cve CVE-2022-50087 commit-author Sudeep Holla <[email protected]> commit 689640e upstream-diff | Adjusted context in the scpi_probe func due to missing commit 43b9ac9 ("firmware: arm_scpi: convert platform driver to use dev_groups") 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/[email protected] Cc: [email protected] # 4.19+ Reported-by: huhai <[email protected]> Reviewed-by: Jackie Liu <[email protected]> Signed-off-by: Sudeep Holla <[email protected]> (cherry picked from commit 689640e) Signed-off-by: Roxana Nicolescu <[email protected]>
1 parent 4aaae6f commit 83574b3

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

drivers/firmware/arm_scpi.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ static int scpi_init_versions(struct scpi_drvinfo *info)
824824
info->firmware_version = le32_to_cpu(caps.platform_version);
825825
}
826826
/* Ignore error if not implemented */
827-
if (scpi_info->is_legacy && ret == -EOPNOTSUPP)
827+
if (info->is_legacy && ret == -EOPNOTSUPP)
828828
return 0;
829829

830830
return ret;
@@ -914,33 +914,34 @@ static int scpi_probe(struct platform_device *pdev)
914914
struct resource res;
915915
struct device *dev = &pdev->dev;
916916
struct device_node *np = dev->of_node;
917+
struct scpi_drvinfo *scpi_drvinfo;
917918

918-
scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
919-
if (!scpi_info)
919+
scpi_drvinfo = devm_kzalloc(dev, sizeof(*scpi_drvinfo), GFP_KERNEL);
920+
if (!scpi_drvinfo)
920921
return -ENOMEM;
921922

922923
if (of_match_device(legacy_scpi_of_match, &pdev->dev))
923-
scpi_info->is_legacy = true;
924+
scpi_drvinfo->is_legacy = true;
924925

925926
count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
926927
if (count < 0) {
927928
dev_err(dev, "no mboxes property in '%pOF'\n", np);
928929
return -ENODEV;
929930
}
930931

931-
scpi_info->channels = devm_kcalloc(dev, count, sizeof(struct scpi_chan),
932-
GFP_KERNEL);
933-
if (!scpi_info->channels)
932+
scpi_drvinfo->channels =
933+
devm_kcalloc(dev, count, sizeof(struct scpi_chan), GFP_KERNEL);
934+
if (!scpi_drvinfo->channels)
934935
return -ENOMEM;
935936

936-
ret = devm_add_action(dev, scpi_free_channels, scpi_info);
937+
ret = devm_add_action(dev, scpi_free_channels, scpi_drvinfo);
937938
if (ret)
938939
return ret;
939940

940-
for (; scpi_info->num_chans < count; scpi_info->num_chans++) {
941+
for (; scpi_drvinfo->num_chans < count; scpi_drvinfo->num_chans++) {
941942
resource_size_t size;
942-
int idx = scpi_info->num_chans;
943-
struct scpi_chan *pchan = scpi_info->channels + idx;
943+
int idx = scpi_drvinfo->num_chans;
944+
struct scpi_chan *pchan = scpi_drvinfo->channels + idx;
944945
struct mbox_client *cl = &pchan->cl;
945946
struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
946947

@@ -984,49 +985,57 @@ static int scpi_probe(struct platform_device *pdev)
984985
return ret;
985986
}
986987

987-
scpi_info->commands = scpi_std_commands;
988+
scpi_drvinfo->commands = scpi_std_commands;
988989

989-
platform_set_drvdata(pdev, scpi_info);
990+
platform_set_drvdata(pdev, scpi_drvinfo);
990991

991-
if (scpi_info->is_legacy) {
992+
if (scpi_drvinfo->is_legacy) {
992993
/* Replace with legacy variants */
993994
scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
994-
scpi_info->commands = scpi_legacy_commands;
995+
scpi_drvinfo->commands = scpi_legacy_commands;
995996

996997
/* Fill priority bitmap */
997998
for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
998999
set_bit(legacy_hpriority_cmds[idx],
999-
scpi_info->cmd_priority);
1000+
scpi_drvinfo->cmd_priority);
10001001
}
10011002

1002-
ret = scpi_init_versions(scpi_info);
1003+
scpi_info = scpi_drvinfo;
1004+
1005+
ret = scpi_init_versions(scpi_drvinfo);
10031006
if (ret) {
10041007
dev_err(dev, "incorrect or no SCP firmware found\n");
1008+
scpi_info = NULL;
10051009
return ret;
10061010
}
10071011

1008-
if (scpi_info->is_legacy && !scpi_info->protocol_version &&
1009-
!scpi_info->firmware_version)
1012+
if (scpi_drvinfo->is_legacy && !scpi_drvinfo->protocol_version &&
1013+
!scpi_drvinfo->firmware_version)
10101014
dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
10111015
else
10121016
dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
10131017
FIELD_GET(PROTO_REV_MAJOR_MASK,
1014-
scpi_info->protocol_version),
1018+
scpi_drvinfo->protocol_version),
10151019
FIELD_GET(PROTO_REV_MINOR_MASK,
1016-
scpi_info->protocol_version),
1020+
scpi_drvinfo->protocol_version),
10171021
FIELD_GET(FW_REV_MAJOR_MASK,
1018-
scpi_info->firmware_version),
1022+
scpi_drvinfo->firmware_version),
10191023
FIELD_GET(FW_REV_MINOR_MASK,
1020-
scpi_info->firmware_version),
1024+
scpi_drvinfo->firmware_version),
10211025
FIELD_GET(FW_REV_PATCH_MASK,
1022-
scpi_info->firmware_version));
1023-
scpi_info->scpi_ops = &scpi_ops;
1026+
scpi_drvinfo->firmware_version));
1027+
1028+
scpi_drvinfo->scpi_ops = &scpi_ops;
10241029

10251030
ret = devm_device_add_groups(dev, versions_groups);
10261031
if (ret)
10271032
dev_err(dev, "unable to create sysfs version group\n");
10281033

1029-
return devm_of_platform_populate(dev);
1034+
ret = devm_of_platform_populate(dev);
1035+
if (ret)
1036+
scpi_info = NULL;
1037+
1038+
return ret;
10301039
}
10311040

10321041
static const struct of_device_id scpi_of_match[] = {

0 commit comments

Comments
 (0)