Pārlūkot izejas kodu

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

Signed-off-by: Michael Brown <mcb30@ipxe.org>
master
Michael Brown 4 gadus atpakaļ
vecāks
revīzija
e3ca211071
1 mainītis faili ar 28 papildinājumiem un 10 dzēšanām
  1. 28
    10
      src/net/tcp/iscsi.c

+ 28
- 10
src/net/tcp/iscsi.c Parādīt failu

@@ -1948,15 +1948,22 @@ const struct setting reverse_password_setting __setting ( SETTING_AUTH_EXTRA,
1948 1948
  */
1949 1949
 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1950 1950
 				   const char *root_path ) {
1951
-	char rp_copy[ strlen ( root_path ) + 1 ];
1951
+	char *rp_copy;
1952 1952
 	char *rp_comp[NUM_RP_COMPONENTS];
1953
-	char *rp = rp_copy;
1953
+	char *rp;
1954 1954
 	int skip = 0;
1955 1955
 	int i = 0;
1956 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 1966
 	/* Split root path into component parts */
1959
-	strcpy ( rp_copy, root_path );
1960 1967
 	while ( 1 ) {
1961 1968
 		rp_comp[i++] = rp;
1962 1969
 		if ( i == NUM_RP_COMPONENTS )
@@ -1965,7 +1972,8 @@ static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1965 1972
 			if ( ! *rp ) {
1966 1973
 				DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1967 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 1977
 			} else if ( *rp == '[' ) {
1970 1978
 				skip = 1;
1971 1979
 			} else if ( *rp == ']' ) {
@@ -1977,21 +1985,31 @@ static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1977 1985
 
1978 1986
 	/* Use root path components to configure iSCSI session */
1979 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 1992
 	iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1983 1993
 	if ( ! iscsi->target_port )
1984 1994
 		iscsi->target_port = ISCSI_PORT;
1985 1995
 	if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
1986 1996
 		DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1987 1997
 		       iscsi, rp_comp[RP_LUN] );
1988
-		return rc;
1998
+		goto err_lun;
1989 1999
 	}
1990 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
 /**

Notiek ielāde…
Atcelt
Saglabāt