Browse Source

[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 15 years ago
parent
commit
4c5f00f879
5 changed files with 59 additions and 19 deletions
  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 View File

@@ -0,0 +1,48 @@
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 View File

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

+ 1
- 14
src/core/misc.c View File

@@ -5,6 +5,7 @@ MISC Support Routines
5 5
 FILE_LICENCE ( GPL2_OR_LATER );
6 6
 
7 7
 #include <stdlib.h>
8
+#include <ctype.h>
8 9
 #include <byteswap.h>
9 10
 #include <gpxe/in.h>
10 11
 #include <gpxe/timer.h>
@@ -32,20 +33,6 @@ int inet_aton ( const char *cp, struct in_addr *inp ) {
32 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 36
 unsigned long strtoul ( const char *p, char **endp, int base ) {
50 37
 	unsigned long ret = 0;
51 38
 	unsigned int charval;

+ 5
- 3
src/image/script.c View File

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 
28 28
 #include <string.h>
29 29
 #include <stdlib.h>
30
+#include <ctype.h>
30 31
 #include <errno.h>
31 32
 #include <gpxe/image.h>
32 33
 
@@ -90,8 +91,8 @@ static int script_exec ( struct image *image ) {
90 91
  * @ret rc		Return status code
91 92
  */
92 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 97
 	/* Sanity check */
97 98
 	if ( image->len < sizeof ( test ) ) {
@@ -101,7 +102,8 @@ static int script_load ( struct image *image ) {
101 102
 
102 103
 	/* Check for magic signature */
103 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 107
 		DBG ( "Invalid magic signature\n" );
106 108
 		return -ENOEXEC;
107 109
 	}

+ 2
- 0
src/include/ctype.h View File

@@ -26,4 +26,6 @@ static inline unsigned char toupper(unsigned char c)
26 26
 	return c;
27 27
 }
28 28
 
29
+extern int isspace ( int c );
30
+
29 31
 #endif /* _CTYPE_H */

Loading…
Cancel
Save