shithub: riscv

Download patch

ref: 019bb580da25ccfb414c9b71ce5c0743cf86d6fd
parent: 1889aa50c3727e7c09da16cacfbfecd72860511b
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Mar 26 12:45:34 EDT 2017

devsd: check return value of ifc->enable(), don't leak unit name/user strings

--- a/sys/src/9/port/devsd.c
+++ b/sys/src/9/port/devsd.c
@@ -293,12 +293,12 @@
 			qunlock(&sdev->unitlock);
 			return nil;
 		}
+
 		if((unit = malloc(sizeof(SDunit))) == nil){
 			qunlock(&sdev->unitlock);
 			return nil;
 		}
 		sdev->unitflg[subno] = 1;
-
 		snprint(buf, sizeof buf, "%s%x", sdev->name, subno);
 		kstrdup(&unit->name, buf);
 		kstrdup(&unit->user, eve);
@@ -305,10 +305,9 @@
 		unit->perm = 0555;
 		unit->subno = subno;
 		unit->dev = sdev;
-
+	
 		if(sdev->enabled == 0 && sdev->ifc->enable)
-			sdev->ifc->enable(sdev);
-		sdev->enabled = 1;
+			sdev->enabled = sdev->ifc->enable(sdev);
 
 		/*
 		 * No need to lock anything here as this is only
@@ -315,8 +314,10 @@
 		 * called before the unit is made available in the
 		 * sdunit[] array.
 		 */
-		if(unit->dev->ifc->verify(unit) == 0){
+		if(sdev->enabled == 0 || unit->dev->ifc->verify(unit) == 0){
 			qunlock(&sdev->unitlock);
+			free(unit->name);
+			free(unit->user);
 			free(unit);
 			return nil;
 		}