Skip to content

Commit b8a9ac1

Browse files
frowandrobherring
authored andcommitted
of: of_node_get()/of_node_put() nodes held in phandle cache
The phandle cache contains struct device_node pointers. The refcount of the pointers was not incremented while in the cache, allowing use after free error after kfree() of the node. Add the proper increment and decrement of the use count. Fixes: 0b3ce78 ("of: cache phandle nodes to reduce cost of of_find_node_by_phandle()") Cc: stable@vger.kernel.org # v4.17+ Signed-off-by: Frank Rowand <frank.rowand@sony.com> Signed-off-by: Rob Herring <robh@kernel.org>
1 parent 24ad02c commit b8a9ac1

1 file changed

Lines changed: 46 additions & 24 deletions

File tree

‎drivers/of/base.c‎

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ int __weak of_node_to_nid(struct device_node *np)
123123
}
124124
#endif
125125

126-
static struct device_node **phandle_cache;
127-
static u32 phandle_cache_mask;
128-
129126
/*
130127
* Assumptions behind phandle_cache implementation:
131128
* - phandle property values are in a contiguous range of 1..n
@@ -134,6 +131,44 @@ static u32 phandle_cache_mask;
134131
* - the phandle lookup overhead reduction provided by the cache
135132
* will likely be less
136133
*/
134+
135+
static struct device_node **phandle_cache;
136+
static u32 phandle_cache_mask;
137+
138+
/*
139+
* Caller must hold devtree_lock.
140+
*/
141+
static void __of_free_phandle_cache(void)
142+
{
143+
u32 cache_entries = phandle_cache_mask + 1;
144+
u32 k;
145+
146+
if (!phandle_cache)
147+
return;
148+
149+
for (k = 0; k < cache_entries; k++)
150+
of_node_put(phandle_cache[k]);
151+
152+
kfree(phandle_cache);
153+
phandle_cache = NULL;
154+
}
155+
156+
int of_free_phandle_cache(void)
157+
{
158+
unsigned long flags;
159+
160+
raw_spin_lock_irqsave(&devtree_lock, flags);
161+
162+
__of_free_phandle_cache();
163+
164+
raw_spin_unlock_irqrestore(&devtree_lock, flags);
165+
166+
return 0;
167+
}
168+
#if !defined(CONFIG_MODULES)
169+
late_initcall_sync(of_free_phandle_cache);
170+
#endif
171+
137172
void of_populate_phandle_cache(void)
138173
{
139174
unsigned long flags;
@@ -143,8 +178,7 @@ void of_populate_phandle_cache(void)
143178

144179
raw_spin_lock_irqsave(&devtree_lock, flags);
145180

146-
kfree(phandle_cache);
147-
phandle_cache = NULL;
181+
__of_free_phandle_cache();
148182

149183
for_each_of_allnodes(np)
150184
if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
@@ -162,30 +196,15 @@ void of_populate_phandle_cache(void)
162196
goto out;
163197

164198
for_each_of_allnodes(np)
165-
if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
199+
if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) {
200+
of_node_get(np);
166201
phandle_cache[np->phandle & phandle_cache_mask] = np;
202+
}
167203

168204
out:
169205
raw_spin_unlock_irqrestore(&devtree_lock, flags);
170206
}
171207

172-
int of_free_phandle_cache(void)
173-
{
174-
unsigned long flags;
175-
176-
raw_spin_lock_irqsave(&devtree_lock, flags);
177-
178-
kfree(phandle_cache);
179-
phandle_cache = NULL;
180-
181-
raw_spin_unlock_irqrestore(&devtree_lock, flags);
182-
183-
return 0;
184-
}
185-
#if !defined(CONFIG_MODULES)
186-
late_initcall_sync(of_free_phandle_cache);
187-
#endif
188-
189208
void __init of_core_init(void)
190209
{
191210
struct device_node *np;
@@ -1200,8 +1219,11 @@ struct device_node *of_find_node_by_phandle(phandle handle)
12001219
if (!np) {
12011220
for_each_of_allnodes(np)
12021221
if (np->phandle == handle) {
1203-
if (phandle_cache)
1222+
if (phandle_cache) {
1223+
/* will put when removed from cache */
1224+
of_node_get(np);
12041225
phandle_cache[masked_handle] = np;
1226+
}
12051227
break;
12061228
}
12071229
}

0 commit comments

Comments
 (0)