[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>
This commit is contained in:
Michael Brown
2009-06-03 10:13:29 +01:00
parent 5e51aaccaa
commit 4c5f00f879
5 changed files with 59 additions and 19 deletions

View File

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <gpxe/image.h>
@@ -90,8 +91,8 @@ static int script_exec ( struct image *image ) {
* @ret rc Return status code
*/
static int script_load ( struct image *image ) {
static const char magic[] = "#!gpxe\n";
char test[ sizeof ( magic ) - 1 ];
static const char magic[] = "#!gpxe";
char test[ sizeof ( magic ) - 1 /* NUL */ + 1 /* terminating space */];
/* Sanity check */
if ( image->len < sizeof ( test ) ) {
@@ -101,7 +102,8 @@ static int script_load ( struct image *image ) {
/* Check for magic signature */
copy_from_user ( test, image->data, 0, sizeof ( test ) );
if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
if ( ( memcmp ( test, magic, ( sizeof ( test ) - 1 ) ) != 0 ) ||
! isspace ( test[ sizeof ( test ) - 1 ] ) ) {
DBG ( "Invalid magic signature\n" );
return -ENOEXEC;
}