Browse Source

[iscsi] Eliminate variable-length stack allocation in URI parsing

Signed-off-by: Michael Brown <mcb30@ipxe.org>
master
Michael Brown 4 years ago
parent
commit
e3ca211071
1 changed files with 28 additions and 10 deletions
  1. 28
    10
      src/net/tcp/iscsi.c

+ 28
- 10
src/net/tcp/iscsi.c View File

1948
  */
1948
  */
1949
 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1949
 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1950
 				   const char *root_path ) {
1950
 				   const char *root_path ) {
1951
-	char rp_copy[ strlen ( root_path ) + 1 ];
1951
+	char *rp_copy;
1952
 	char *rp_comp[NUM_RP_COMPONENTS];
1952
 	char *rp_comp[NUM_RP_COMPONENTS];
1953
-	char *rp = rp_copy;
1953
+	char *rp;
1954
 	int skip = 0;
1954
 	int skip = 0;
1955
 	int i = 0;
1955
 	int i = 0;
1956
 	int rc;
1956
 	int rc;
1957
 
1957
 
1958
+	/* Create modifiable copy of root path */
1959
+	rp_copy = strdup ( root_path );
1960
+	if ( ! rp_copy ) {
1961
+		rc = -ENOMEM;
1962
+		goto err_strdup;
1963
+	}
1964
+	rp = rp_copy;
1965
+
1958
 	/* Split root path into component parts */
1966
 	/* Split root path into component parts */
1959
-	strcpy ( rp_copy, root_path );
1960
 	while ( 1 ) {
1967
 	while ( 1 ) {
1961
 		rp_comp[i++] = rp;
1968
 		rp_comp[i++] = rp;
1962
 		if ( i == NUM_RP_COMPONENTS )
1969
 		if ( i == NUM_RP_COMPONENTS )
1965
 			if ( ! *rp ) {
1972
 			if ( ! *rp ) {
1966
 				DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1973
 				DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1967
 				       "too short\n", iscsi, root_path );
1974
 				       "too short\n", iscsi, root_path );
1968
-				return -EINVAL_ROOT_PATH_TOO_SHORT;
1975
+				rc = -EINVAL_ROOT_PATH_TOO_SHORT;
1976
+				goto err_split;
1969
 			} else if ( *rp == '[' ) {
1977
 			} else if ( *rp == '[' ) {
1970
 				skip = 1;
1978
 				skip = 1;
1971
 			} else if ( *rp == ']' ) {
1979
 			} else if ( *rp == ']' ) {
1977
 
1985
 
1978
 	/* Use root path components to configure iSCSI session */
1986
 	/* Use root path components to configure iSCSI session */
1979
 	iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
1987
 	iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
1980
-	if ( ! iscsi->target_address )
1981
-		return -ENOMEM;
1988
+	if ( ! iscsi->target_address ) {
1989
+		rc = -ENOMEM;
1990
+		goto err_servername;
1991
+	}
1982
 	iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1992
 	iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1983
 	if ( ! iscsi->target_port )
1993
 	if ( ! iscsi->target_port )
1984
 		iscsi->target_port = ISCSI_PORT;
1994
 		iscsi->target_port = ISCSI_PORT;
1985
 	if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
1995
 	if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
1986
 		DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1996
 		DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1987
 		       iscsi, rp_comp[RP_LUN] );
1997
 		       iscsi, rp_comp[RP_LUN] );
1988
-		return rc;
1998
+		goto err_lun;
1989
 	}
1999
 	}
1990
 	iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
2000
 	iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
1991
-	if ( ! iscsi->target_iqn )
1992
-		return -ENOMEM;
2001
+	if ( ! iscsi->target_iqn ) {
2002
+		rc = -ENOMEM;
2003
+		goto err_targetname;
2004
+	}
1993
 
2005
 
1994
-	return 0;
2006
+ err_targetname:
2007
+ err_lun:
2008
+ err_servername:
2009
+ err_split:
2010
+	free ( rp_copy );
2011
+ err_strdup:
2012
+	return rc;
1995
 }
2013
 }
1996
 
2014
 
1997
 /**
2015
 /**

Loading…
Cancel
Save