Skip to content

Commit 8309234

Browse files
warthog618Bartosz Golaszewski
authored andcommitted
gpio: cdev: fix missed label sanitizing in debounce_setup()
When adding sanitization of the label, the path through edge_detector_setup() that leads to debounce_setup() was overlooked. A request taking this path does not allocate a new label and the request label is freed twice when the request is released, resulting in memory corruption. Add label sanitization to debounce_setup(). Cc: [email protected] Fixes: b344908 ("gpio: cdev: sanitize the label before requesting the interrupt") Signed-off-by: Kent Gibson <[email protected]> [Bartosz: rebased on top of the fix for empty GPIO labels] Co-developed-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent b3b9596 commit 8309234

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

drivers/gpio/gpiolib-cdev.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,25 @@ static u32 line_event_id(int level)
728728
GPIO_V2_LINE_EVENT_FALLING_EDGE;
729729
}
730730

731+
static inline char *make_irq_label(const char *orig)
732+
{
733+
char *new;
734+
735+
if (!orig)
736+
return NULL;
737+
738+
new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
739+
if (!new)
740+
return ERR_PTR(-ENOMEM);
741+
742+
return new;
743+
}
744+
745+
static inline void free_irq_label(const char *label)
746+
{
747+
kfree(label);
748+
}
749+
731750
#ifdef CONFIG_HTE
732751

733752
static enum hte_return process_hw_ts_thread(void *p)
@@ -1015,6 +1034,7 @@ static int debounce_setup(struct line *line, unsigned int debounce_period_us)
10151034
{
10161035
unsigned long irqflags;
10171036
int ret, level, irq;
1037+
char *label;
10181038

10191039
/* try hardware */
10201040
ret = gpiod_set_debounce(line->desc, debounce_period_us);
@@ -1037,11 +1057,17 @@ static int debounce_setup(struct line *line, unsigned int debounce_period_us)
10371057
if (irq < 0)
10381058
return -ENXIO;
10391059

1060+
label = make_irq_label(line->req->label);
1061+
if (IS_ERR(label))
1062+
return -ENOMEM;
1063+
10401064
irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
10411065
ret = request_irq(irq, debounce_irq_handler, irqflags,
1042-
line->req->label, line);
1043-
if (ret)
1066+
label, line);
1067+
if (ret) {
1068+
free_irq_label(label);
10441069
return ret;
1070+
}
10451071
line->irq = irq;
10461072
} else {
10471073
ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
@@ -1083,25 +1109,6 @@ static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
10831109
return 0;
10841110
}
10851111

1086-
static inline char *make_irq_label(const char *orig)
1087-
{
1088-
char *new;
1089-
1090-
if (!orig)
1091-
return NULL;
1092-
1093-
new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
1094-
if (!new)
1095-
return ERR_PTR(-ENOMEM);
1096-
1097-
return new;
1098-
}
1099-
1100-
static inline void free_irq_label(const char *label)
1101-
{
1102-
kfree(label);
1103-
}
1104-
11051112
static void edge_detector_stop(struct line *line)
11061113
{
11071114
if (line->irq) {

0 commit comments

Comments
 (0)