Explorar el Código

[script] Allow for DOS-style line endings in scripts

Windows text editors such as Notepad tend to use CRLF line endings,
which breaks gPXE's signature detection for script images.  Since
scripts are usually very small, they end up falling back to being
detected as valid PXE executable images (since there are no signature
checks for PXE executables).  Executing text files as x86 machine code
tends not to work well.

Fix by allowing for any isspace() character to terminate the "#!gpxe"
signature, and by ensuring that CR characters get stripped during
command line parsing.

Suggested-by: Shao Miller <Shao.Miller@yrdsb.edu.on.ca>
tags/v0.9.8
Michael Brown hace 15 años
padre
commit
4c5f00f879
Se han modificado 5 ficheros con 59 adiciones y 19 borrados
  1. 48
    0
      src/core/ctype.c
  2. 3
    2
      src/core/exec.c
  3. 1
    14
      src/core/misc.c
  4. 5
    3
      src/image/script.c
  5. 2
    0
      src/include/ctype.h

+ 48
- 0
src/core/ctype.c Ver fichero

1
+/*
2
+ * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/**
22
+ * @file
23
+ *
24
+ * Character types
25
+ *
26
+ */
27
+
28
+#include <ctype.h>
29
+
30
+/**
31
+ * Check to see if character is a space
32
+ *
33
+ * @v c			Character
34
+ * @ret isspace		Character is a space
35
+ */
36
+int isspace ( int c ) {
37
+	switch ( c ) {
38
+	case ' ' :
39
+	case '\f' :
40
+	case '\n' :
41
+	case '\r' :
42
+	case '\t' :
43
+	case '\v' :
44
+		return 1;
45
+	default:
46
+		return 0;
47
+	}
48
+}

+ 3
- 2
src/core/exec.c Ver fichero

22
 #include <string.h>
22
 #include <string.h>
23
 #include <stdlib.h>
23
 #include <stdlib.h>
24
 #include <stdio.h>
24
 #include <stdio.h>
25
+#include <ctype.h>
25
 #include <unistd.h>
26
 #include <unistd.h>
26
 #include <getopt.h>
27
 #include <getopt.h>
27
 #include <errno.h>
28
 #include <errno.h>
170
 
171
 
171
 	while ( 1 ) {
172
 	while ( 1 ) {
172
 		/* Skip over any whitespace / convert to NUL */
173
 		/* Skip over any whitespace / convert to NUL */
173
-		while ( *args == ' ' ) {
174
+		while ( isspace ( *args ) ) {
174
 			if ( argv )
175
 			if ( argv )
175
 				*args = '\0';
176
 				*args = '\0';
176
 			args++;
177
 			args++;
183
 			argv[argc] = args;
184
 			argv[argc] = args;
184
 		argc++;
185
 		argc++;
185
 		/* Skip to start of next whitespace, if any */
186
 		/* Skip to start of next whitespace, if any */
186
-		while ( *args && ( *args != ' ' ) ) {
187
+		while ( *args && ! isspace ( *args ) ) {
187
 			args++;
188
 			args++;
188
 		}
189
 		}
189
 	}
190
 	}

+ 1
- 14
src/core/misc.c Ver fichero

5
 FILE_LICENCE ( GPL2_OR_LATER );
5
 FILE_LICENCE ( GPL2_OR_LATER );
6
 
6
 
7
 #include <stdlib.h>
7
 #include <stdlib.h>
8
+#include <ctype.h>
8
 #include <byteswap.h>
9
 #include <byteswap.h>
9
 #include <gpxe/in.h>
10
 #include <gpxe/in.h>
10
 #include <gpxe/timer.h>
11
 #include <gpxe/timer.h>
32
 	return 0;
33
 	return 0;
33
 }
34
 }
34
 
35
 
35
-int isspace ( int c ) {
36
-	switch ( c ) {
37
-	case ' ':
38
-	case '\f':
39
-	case '\n':
40
-	case '\r':
41
-	case '\t':
42
-	case '\v':
43
-		return 1;
44
-	default:
45
-		return 0;
46
-	}
47
-}
48
-
49
 unsigned long strtoul ( const char *p, char **endp, int base ) {
36
 unsigned long strtoul ( const char *p, char **endp, int base ) {
50
 	unsigned long ret = 0;
37
 	unsigned long ret = 0;
51
 	unsigned int charval;
38
 	unsigned int charval;

+ 5
- 3
src/image/script.c Ver fichero

27
 
27
 
28
 #include <string.h>
28
 #include <string.h>
29
 #include <stdlib.h>
29
 #include <stdlib.h>
30
+#include <ctype.h>
30
 #include <errno.h>
31
 #include <errno.h>
31
 #include <gpxe/image.h>
32
 #include <gpxe/image.h>
32
 
33
 
90
  * @ret rc		Return status code
91
  * @ret rc		Return status code
91
  */
92
  */
92
 static int script_load ( struct image *image ) {
93
 static int script_load ( struct image *image ) {
93
-	static const char magic[] = "#!gpxe\n";
94
-	char test[ sizeof ( magic ) - 1 ];
94
+	static const char magic[] = "#!gpxe";
95
+	char test[ sizeof ( magic ) - 1 /* NUL */ + 1 /* terminating space */];
95
 
96
 
96
 	/* Sanity check */
97
 	/* Sanity check */
97
 	if ( image->len < sizeof ( test ) ) {
98
 	if ( image->len < sizeof ( test ) ) {
101
 
102
 
102
 	/* Check for magic signature */
103
 	/* Check for magic signature */
103
 	copy_from_user ( test, image->data, 0, sizeof ( test ) );
104
 	copy_from_user ( test, image->data, 0, sizeof ( test ) );
104
-	if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
105
+	if ( ( memcmp ( test, magic, ( sizeof ( test ) - 1 ) ) != 0 ) ||
106
+	     ! isspace ( test[ sizeof ( test ) - 1 ] ) ) {
105
 		DBG ( "Invalid magic signature\n" );
107
 		DBG ( "Invalid magic signature\n" );
106
 		return -ENOEXEC;
108
 		return -ENOEXEC;
107
 	}
109
 	}

+ 2
- 0
src/include/ctype.h Ver fichero

26
 	return c;
26
 	return c;
27
 }
27
 }
28
 
28
 
29
+extern int isspace ( int c );
30
+
29
 #endif /* _CTYPE_H */
31
 #endif /* _CTYPE_H */

Loading…
Cancelar
Guardar