Skip to contentSkip to conversation

question about drivers/serial/sunsu.c

DISCUSSION Julia Lawall 2 participants 3 messages Mar 28, 2010 – May 27, 2010

lore
Share & tools
lore
#0 lore raw

The function su_probe in the file drivers/serial/sunsu.c contains the code:

	if (up->su_type = SU_PORT_KBD || up->su_type = SU_PORT_MS) {
		err = sunsu_kbd_ms_init(up);
	        if (err) {
                        kfree(up);
                        goto out_unmap;
		}
                dev_set_drvdata(&op->dev, up);

		return 0;
	}

And then the label out_unmap contains:

out_unmap:

	of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
	return err;

which represents a potential use of up after the free. There are however other gotos to out_unmap that don't seem to want to free up. Should the of_iounmap just be copied up before the kfree(up)?

julia

#1 lore raw

From: Julia Lawall <[email protected]>
Date: Sun, 28 Mar 2010 17:40:23 +0200 (CEST)

Quoted 16 lines

The function su_probe in the file drivers/serial/sunsu.c contains the code:

	if (up->su_type = SU_PORT_KBD || up->su_type = SU_PORT_MS) {
		err = sunsu_kbd_ms_init(up);
	        if (err) {
                        kfree(up);
                        goto out_unmap;
		}
                dev_set_drvdata(&op->dev, up);

		return 0;
	}

And then the label out_unmap contains:

Quoted 7 lines

out_unmap:

	of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
	return err;

which represents a potential use of up after the free. There are however other gotos to out_unmap that don't seem to want to free up. Should the of_iounmap just be copied up before the kfree(up)?

This is a bug, but the fix is tricky.

Ports which are not keyboard or mice ports are statically allocated from the sunsu_ports[] array, so we can't unconditionally free 'up' at the out_unmap label.

So yes copying the of_iounmap() statement up to the keyboard/mouse failure case and doing a straight return from there seems the best way to handle this.

#2 lore raw
In reply to Julia Lawall

From: Julia Lawall <[email protected]>
Date: Sat, 15 May 2010 10:15:17 +0200 (CEST)

Quoted 2 lines

The following function in drivers/serial/sunsu.c contains a possible use after free:

...

Quoted 2 lines

Is it safe to simply move the if (up->port.membase) of_iounmap(...); up above the other ifs, or should something else be done?

No, that isn't safe. We have to unregister the device from the SERIO layer before we do that.

Practically speaking, these remove methods never really ever execute on sparc64 since 1) sunsu is usually built statically so we can get a console and 2) hotplug of these devices is basically a non-occurance as well.

Anyways, I'm going to fix this bug as follows, thanks for spotting it!

--------------------
sunsu: Fix use after free in su_remove().

Real serial port 'up' objects are statically allocated from an array in the driver. Keyboard and mouse ports, on the other hand, are dynamically allocated.

Unfortunately, we free these dynamic 'up' objects before we unmap the I/O registers.

Rearrange su_remove() so that this does not happen.

Noticed by Julia Lawall.

---
 drivers/serial/sunsu.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)
drivers/serial/sunsu.c +9 -4
index 234459c..ffbf455 100644
@@ -1500,20 +1500,25 @@ out_unmap:
 static int __devexit su_remove(struct of_device *op)
 {
 	struct uart_sunsu_port *up = dev_get_drvdata(&op->dev);
+	bool kbdms = false;
 
 	if (up->su_type = SU_PORT_MS ||
-	    up->su_type = SU_PORT_KBD) {
+	    up->su_type = SU_PORT_KBD)
+		kbdms = true;
+
+	if (kbdms) {
 #ifdef CONFIG_SERIO
 		serio_unregister_port(&up->serio);
 #endif
-		kfree(up);
-	} else if (up->port.type != PORT_UNKNOWN) {
+	} else if (up->port.type != PORT_UNKNOWN)
 		uart_remove_one_port(&sunsu_reg, &up->port);
-	}
 
 	if (up->port.membase)
 		of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
 
+	if (kbdms)
+		kfree(up);
+
 	dev_set_drvdata(&op->dev, NULL);
 
 	return 0;
Signature · 1 line
1.7.0.4