Skip to content

Commit b9784e5

Browse files
joehattorikrzk
authored andcommitted
memory: tegra20-emc: fix an OF node reference bug in tegra_emc_find_node_by_ram_code()
As of_find_node_by_name() release the reference of the argument device node, tegra_emc_find_node_by_ram_code() releases some device nodes while still in use, resulting in possible UAFs. According to the bindings and the in-tree DTS files, the "emc-tables" node is always device's child node with the property "nvidia,use-ram-code", and the "lpddr2" node is a child of the "emc-tables" node. Thus utilize the for_each_child_of_node() macro and of_get_child_by_name() instead of of_find_node_by_name() to simplify the code. This bug was found by an experimental verification tool that I am developing. Fixes: 96e5da7 ("memory: tegra: Introduce Tegra20 EMC driver") Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp> Link: https://lore.kernel.org/r/20241217091434.1993597-1-joe@pf.is.s.u-tokyo.ac.jp Link: https://lore.kernel.org/r/20241218024415.2494267-3-joe@pf.is.s.u-tokyo.ac.jp [krzysztof: applied v1, adjust the commit msg to incorporate v2 parts] Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
1 parent 5119e6b commit b9784e5

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

drivers/memory/tegra/tegra20-emc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,15 @@ tegra_emc_find_node_by_ram_code(struct tegra_emc *emc)
474474

475475
ram_code = tegra_read_ram_code();
476476

477-
for (np = of_find_node_by_name(dev->of_node, "emc-tables"); np;
478-
np = of_find_node_by_name(np, "emc-tables")) {
477+
for_each_child_of_node(dev->of_node, np) {
478+
if (!of_node_name_eq(np, "emc-tables"))
479+
continue;
479480
err = of_property_read_u32(np, "nvidia,ram-code", &value);
480481
if (err || value != ram_code) {
481482
struct device_node *lpddr2_np;
482483
bool cfg_mismatches = false;
483484

484-
lpddr2_np = of_find_node_by_name(np, "lpddr2");
485+
lpddr2_np = of_get_child_by_name(np, "lpddr2");
485486
if (lpddr2_np) {
486487
const struct lpddr2_info *info;
487488

@@ -518,7 +519,6 @@ tegra_emc_find_node_by_ram_code(struct tegra_emc *emc)
518519
}
519520

520521
if (cfg_mismatches) {
521-
of_node_put(np);
522522
continue;
523523
}
524524
}

0 commit comments

Comments
 (0)