Skip to content

Commit a177771

Browse files
dtorgregkh
authored andcommitted
pinctrl: avoid unsafe code pattern in find_pinctrl()
commit c153a4e upstream. The code in find_pinctrl() takes a mutex and traverses a list of pinctrl structures. Later the caller bumps up reference count on the found structure. Such pattern is not safe as pinctrl that was found may get deleted before the caller gets around to increasing the reference count. Fix this by taking the reference count in find_pinctrl(), while it still holds the mutex. Cc: [email protected] Signed-off-by: Dmitry Torokhov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Linus Walleij <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2a4a828 commit a177771

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

drivers/pinctrl/core.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,17 +1007,20 @@ static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
10071007

10081008
static struct pinctrl *find_pinctrl(struct device *dev)
10091009
{
1010-
struct pinctrl *p;
1010+
struct pinctrl *entry, *p = NULL;
10111011

10121012
mutex_lock(&pinctrl_list_mutex);
1013-
list_for_each_entry(p, &pinctrl_list, node)
1014-
if (p->dev == dev) {
1015-
mutex_unlock(&pinctrl_list_mutex);
1016-
return p;
1013+
1014+
list_for_each_entry(entry, &pinctrl_list, node) {
1015+
if (entry->dev == dev) {
1016+
p = entry;
1017+
kref_get(&p->users);
1018+
break;
10171019
}
1020+
}
10181021

10191022
mutex_unlock(&pinctrl_list_mutex);
1020-
return NULL;
1023+
return p;
10211024
}
10221025

10231026
static void pinctrl_free(struct pinctrl *p, bool inlist);
@@ -1126,7 +1129,6 @@ struct pinctrl *pinctrl_get(struct device *dev)
11261129
p = find_pinctrl(dev);
11271130
if (p) {
11281131
dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1129-
kref_get(&p->users);
11301132
return p;
11311133
}
11321134

0 commit comments

Comments
 (0)