Skip to content

Commit c153a4e

Browse files
dtorLinus Walleij
authored andcommitted
pinctrl: avoid unsafe code pattern in find_pinctrl()
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: stable@vger.kernel.org Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Link: https://lore.kernel.org/r/ZQs1RgTKg6VJqmPs@google.com Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 64061b6 commit c153a4e

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

drivers/pinctrl/core.c

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

10231023
static struct pinctrl *find_pinctrl(struct device *dev)
10241024
{
1025-
struct pinctrl *p;
1025+
struct pinctrl *entry, *p = NULL;
10261026

10271027
mutex_lock(&pinctrl_list_mutex);
1028-
list_for_each_entry(p, &pinctrl_list, node)
1029-
if (p->dev == dev) {
1030-
mutex_unlock(&pinctrl_list_mutex);
1031-
return p;
1028+
1029+
list_for_each_entry(entry, &pinctrl_list, node) {
1030+
if (entry->dev == dev) {
1031+
p = entry;
1032+
kref_get(&p->users);
1033+
break;
10321034
}
1035+
}
10331036

10341037
mutex_unlock(&pinctrl_list_mutex);
1035-
return NULL;
1038+
return p;
10361039
}
10371040

10381041
static void pinctrl_free(struct pinctrl *p, bool inlist);
@@ -1140,7 +1143,6 @@ struct pinctrl *pinctrl_get(struct device *dev)
11401143
p = find_pinctrl(dev);
11411144
if (p) {
11421145
dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1143-
kref_get(&p->users);
11441146
return p;
11451147
}
11461148

0 commit comments

Comments
 (0)