Subversion Repositories SvarDOS

Rev

Rev 494 | Rev 505 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 494 Rev 500
Line 50... Line 50...
50
#define CMDLINE_MAXLEN 255
50
#define CMDLINE_MAXLEN 255
51
 
51
 
52
 
52
 
53
/* sets guard values at a few places in memory for later detection of
53
/* sets guard values at a few places in memory for later detection of
54
 * overflows via memguard_check() */
54
 * overflows via memguard_check() */
55
static void memguard_set(void) {
55
static void memguard_set(char *cmdlinebuf) {
56
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
56
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
57
  BUFFER[sizeof(BUFFER) - (CMDLINE_MAXLEN + 3)] = 0xC7;
57
  cmdlinebuf[CMDLINE_MAXLEN] = 0xC7;
58
}
58
}
59
 
59
 
60
 
60
 
61
/* checks for valguards at specific memory locations, returns 0 on success */
61
/* checks for valguards at specific memory locations, returns 0 on success */
62
static int memguard_check(unsigned short rmodseg) {
62
static int memguard_check(unsigned short rmodseg, char *cmdlinebuf) {
63
  /* check RMOD signature (would be overwritten in case of stack overflow */
63
  /* check RMOD signature (would be overwritten in case of stack overflow */
64
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
64
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
65
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
65
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
66
  if (*rmodsig != 0x2019) {
66
  if (*rmodsig != 0x2019) {
67
    msg[22] = '1';
67
    msg[22] = '1';
68
    outputnl(msg);
68
    outputnl(msg);
69
    printf("rmodseg = %04X ; *rmodsig = %04X\r\n", rmodseg, *rmodsig);
69
    printf("rmodseg = %04X ; *rmodsig = %04X\r\n", rmodseg, *rmodsig);
70
    return(1);
70
    return(1);
71
  }
71
  }
72
  /* check last BUFFER byte (could be overwritten by cmdline) */
72
  /* check last BUFFER byte */
73
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
73
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
74
    msg[22] = '2';
74
    msg[22] = '2';
75
    outputnl(msg);
75
    outputnl(msg);
76
    return(2);
76
    return(2);
77
  }
77
  }
78
  /* check that cmdline BUFFER's end hasn't been touched by something else */
78
  /* check last cmdlinebuf byte */
79
  if (BUFFER[sizeof(BUFFER) - (CMDLINE_MAXLEN + 3)] != 0xC7) {
79
  if (cmdlinebuf[CMDLINE_MAXLEN] != 0xC7) {
80
    msg[22] = '3';
80
    msg[22] = '3';
81
    outputnl(msg);
81
    outputnl(msg);
82
    return(3);
82
    return(3);
83
  }
83
  }
84
  /* all good */
84
  /* all good */
Line 612... Line 612...
612
int main(void) {
612
int main(void) {
613
  static struct config cfg;
613
  static struct config cfg;
614
  static unsigned short far *rmod_envseg;
614
  static unsigned short far *rmod_envseg;
615
  static unsigned short far *lastexitcode;
615
  static unsigned short far *lastexitcode;
616
  static struct rmod_props far *rmod;
616
  static struct rmod_props far *rmod;
-
 
617
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
617
  static char *cmdline;
618
  static char *cmdline;
618
 
619
 
619
  rmod = rmod_find(BUFFER_len);
620
  rmod = rmod_find(BUFFER_len);
620
  if (rmod == NULL) {
621
  if (rmod == NULL) {
621
    /* look at command line parameters (in case env size if set there) */
622
    /* look at command line parameters (in case env size if set there) */
Line 635... Line 636...
635
     * have a purpose in life */
636
     * have a purpose in life */
636
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
637
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
637
  }
638
  }
638
 
639
 
639
  /* install a few guardvals in memory to detect some cases of overflows */
640
  /* install a few guardvals in memory to detect some cases of overflows */
640
  memguard_set();
641
  memguard_set(cmdlinebuf);
641
 
642
 
642
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
643
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
643
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
644
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
644
 
645
 
645
  /* make COMPSEC point to myself */
646
  /* make COMPSEC point to myself */
Line 667... Line 668...
667
 
668
 
668
    /* cancel any redirections that may have been set up before */
669
    /* cancel any redirections that may have been set up before */
669
    redir_revert();
670
    redir_revert();
670
 
671
 
671
    /* memory check */
672
    /* memory check */
672
    memguard_check(rmod->rmodseg);
673
    memguard_check(rmod->rmodseg, cmdlinebuf);
673
 
674
 
674
    /* preset cmdline to point at the end of my general-purpose buffer (with
675
    /* preset cmdline to point at the dedicated buffer */
675
     * one extra byte for the NULL terminator and another for memguard val) */
-
 
676
    cmdline = BUFFER + sizeof(BUFFER) - (CMDLINE_MAXLEN + 2);
676
    cmdline = cmdlinebuf;
677
 
677
 
678
    /* (re)load translation strings if needed */
678
    /* (re)load translation strings if needed */
679
    nls_langreload(BUFFER, *rmod_envseg);
679
    nls_langreload(BUFFER, *rmod_envseg);
680
 
680
 
681
    /* skip user input if I have a command to exec (/C or /K) */
681
    /* skip user input if I have a command to exec (/C or /K) */
Line 731... Line 731...
731
      outputnl("");
731
      outputnl("");
732
      continue;
732
      continue;
733
    }
733
    }
734
 
734
 
735
    /* try matching (and executing) an internal command */
735
    /* try matching (and executing) an internal command */
736
    if (cmd_process(rmod, *rmod_envseg, cmdline, BUFFER) >= -1) {
736
    if (cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER)) >= -1) {
737
      /* internal command executed */
737
      /* internal command executed */
738
      redir_revert(); /* revert stdout (in case it was redirected) */
738
      redir_revert(); /* revert stdout (in case it was redirected) */
739
      continue;
739
      continue;
740
    }
740
    }
741
 
741