Skip to contentSkip to conversation

drivers/net/fs_enet: remove null pointer dereference

PATCH Applied Julia Lawall 5 participants 5 messages May 12, 2008 – May 22, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 4/6 drivers/net/fs_enet: remove null pointer dereference

From: Julia Lawall <[email protected]>

The following code appears in the function fs_init_instance in the file drivers/net/fs_enet/fs_enet-main.c.

	if (fep->ops = NULL) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s No matching ops found (%d).\n",
		       ndev->name, fpi->fs_no);
		err = -EINVAL;
		goto err;
	}

This code implies that at the point of err, fep->ops can be NULL, so an extra test is needed before dereferencing this value.

This problem was found using the following semantic match
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@

* if (E = NULL)
{
... when != if (E = NULL) S1 else S2

      when != E = E1

* E->f
... when any
return ...;
}
else S3
// </smpl>

---

drivers/net/fs_enet/fs_enet-main.c +1 -1
diff -u -p a/drivers/net/fs_enet/fs_enet-main.c b/drivers/net/fs_enet/fs_enet-main.c
@@ -1093,7 +1093,7 @@ err:
 		if (registered)
 			unregister_netdev(ndev);
 
-		if (fep != NULL) {
+		if (fep && fep->ops) {
 			(*fep->ops->free_bd)(ndev);
 			(*fep->ops->cleanup_data)(ndev);
 		}
Acked-by on v1 4/6
#1 lore raw
Quoted 1 line · On Mon, 12 May 2008 15:38:26 +0200 (CEST) Julia Lawall wrote:

From: Julia Lawall <[email protected]>

Quoted 54 lines

The following code appears in the function fs_init_instance in the file drivers/net/fs_enet/fs_enet-main.c.

	if (fep->ops == NULL) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s No matching ops found (%d).\n",
		       ndev->name, fpi->fs_no);
		err = -EINVAL;
		goto err;
	}

This code implies that at the point of err, fep->ops can be NULL, so an extra test is needed before dereferencing this value.

This problem was found using the following semantic match
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@

* if (E == NULL)
{
... when != if (E == NULL) S1 else S2

      when != E = E1

* E->f
... when any
return ...;
}
else S3
// </smpl>

---

diff -u -p a/drivers/net/fs_enet/fs_enet-main.c b/drivers/net/fs_enet/fs_enet-main.c ---
a/drivers/net/fs_enet/fs_enet-main.c 2008-04-27
11:41:11.000000000 +0200 +++
b/drivers/net/fs_enet/fs_enet-main.c 2008-05-12
09:41:52.000000000 +0200 @@ -1093,7 +1093,7 @@ err: if (registered) unregister_netdev(ndev);

drivers/net/fs_enet/fs_enet-main.c +1 -1
-		if (fep != NULL) {
+		if (fep && fep->ops) {
 			(*fep->ops->free_bd)(ndev);
 			(*fep->ops->cleanup_data)(ndev);
 		}
#2 lore raw
In reply to Julia Lawall

Julia Lawall <[email protected]> :
[...]

drivers/net/fs_enet/fs_enet-main.c err: · line 1099

Quoted 12 lines

diff -u -p a/drivers/net/fs_enet/fs_enet-main.c b/drivers/net/fs_enet/fs_enet-main.c

drivers/net/fs_enet/fs_enet-main.c +1 -1
@@ -1093,7 +1093,7 @@ err:
 		if (registered)
 			unregister_netdev(ndev);
 
-		if (fep != NULL) {
+		if (fep && fep->ops) {
 			(*fep->ops->free_bd)(ndev);
 			(*fep->ops->cleanup_data)(ndev);
 		}

Extra cookies for the nice soul who:
- removes the 'if (registered)' test (it can not happen)
- uses different error lablels and unrolls the error path. I can not claim that the current error path is wrong but it would not hurt if it was more trivially balanced.

Signature · 1 line
Ueimor
#3 lore raw
Quoted 9 lines · Francois Romieu

Julia Lawall <[email protected]> :
[...]

Quoted 7 lines

diff -u -p a/drivers/net/fs_enet/fs_enet-main.c b/drivers/net/fs_enet/fs_enet-main.c

drivers/net/fs_enet/fs_enet-main.c +0 -0
@@ -1093,7 +1093,7 @@ err:
 		if (registered)
 			unregister_netdev(ndev);
 
Quoted 12 lines

drivers/net/fs_enet/fs_enet-main.c err: · line 1099

Quoted 5 lines
drivers/net/fs_enet/fs_enet-main.c +1 -1
-		if (fep != NULL) {
+		if (fep && fep->ops) {
 			(*fep->ops->free_bd)(ndev);
 			(*fep->ops->cleanup_data)(ndev);
 		}

Extra cookies for the nice soul who:
- removes the 'if (registered)' test (it can not happen)
- uses different error lablels and unrolls the error path. I can not claim that the current error path is wrong but it would not hurt if it was more trivially balanced.

Please note that this code is going away very soon, when arch/ppc dies.

-Scott

Applied
#4 lore raw
In reply to Julia Lawall
Quoted 30 lines · Julia Lawall

From: Julia Lawall <[email protected]>

The following code appears in the function fs_init_instance in the file drivers/net/fs_enet/fs_enet-main.c.

	if (fep->ops = NULL) {
		printk(KERN_ERR DRV_MODULE_NAME
		       ": %s No matching ops found (%d).\n",
		       ndev->name, fpi->fs_no);
		err = -EINVAL;
		goto err;
	}

This code implies that at the point of err, fep->ops can be NULL, so an extra test is needed before dereferencing this value.

This problem was found using the following semantic match
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
expression E, E1;
identifier f;
statement S1,S2,S3;
@@

* if (E = NULL)
{
... when != if (E = NULL) S1 else S2

      when != E = E1
Quoted 8 lines

* E->f
... when any
return ...;
}
else S3
// </smpl>

applied