IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39393 for trunk


Ignore:
Timestamp:
Feb 26, 2016, 6:18:50 AM (10 years ago)
Author:
eugene
Message:

trying to catch some (possible) stack and heap overflow errors: adding padding to allocated config regions

Location:
trunk/Ohana/src/libohana
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/Makefile

    r39338 r39393  
    6666$(DESTLIB)/libohana.$(DLLTYPE): $(LIB)/libohana.$(ARCH).$(DLLTYPE)
    6767
    68 TESTPROG_XTRA = sprintf_floats
    69 TESTPROG = memtest typetest string
     68TESTPROG_XTRA = sprintf_floats memtest typetest string
     69TESTPROG = aliasing
    7070
    7171$(TESTPROG) : % : $(TESTBIN)/% $(OBJS)
  • trunk/Ohana/src/libohana/src/CommOps.c

    r27435 r39393  
    209209
    210210  if (buffer[0].Nbuffer < 16) return NULL;
    211   memcpy (command, buffer[0].buffer, 16);
     211  memcpy (command, buffer[0].buffer, 16); // SAFE
    212212  command[16] = 0;
    213213
     
    217217
    218218  ALLOCATE (line, char, Nbytes + 1);
    219   memcpy (line, &buffer[0].buffer[16], Nbytes);
     219  memcpy (line, &buffer[0].buffer[16], Nbytes); // SAFE
    220220  line[Nbytes] = 0;
    221221
  • trunk/Ohana/src/libohana/src/config.c

    r38553 r39393  
    22
    33# define D_NBYTES 4096
     4# define NXTRA 16
    45
    56static char *ConfigVariable = (char *) NULL;
     
    107108  }
    108109  free (find);
    109 
    110   /* we eliminate this option: this is just confusing */
    111 # if (0) 
    112   /* look for ./.prognamerc */
    113   sprintf (find, ".%src", progname);
    114   status = stat (find, &filestat);
    115   if (status == 0) {
    116     /* file exists, can we read it? */
    117     if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRUSR)) ||
    118         ((gid == filestat.st_gid) && (filestat.st_mode & S_IRGRP)) ||
    119         (                            (filestat.st_mode & S_IROTH))) {
    120       return (find);
    121     }
    122   }
    123 # endif
    124110
    125111  /* look for ~/.prognamerc */
     
    158144  Nbytes = 0;
    159145  NBYTES = D_NBYTES;
    160   ALLOCATE (ibuffer, char, NBYTES + 2);
    161   memset (ibuffer, 0, NBYTES + 2);
     146  ALLOCATE (ibuffer, char, NBYTES + NXTRA);
     147  memset (ibuffer, 0, NBYTES + NXTRA);
    162148   
    163149  /* load data from file */
     
    166152      Nbytes += nbytes;
    167153      NBYTES += D_NBYTES;
    168       REALLOCATE (ibuffer, char, NBYTES + 2);
     154      REALLOCATE (ibuffer, char, NBYTES + NXTRA);
    169155    }
    170156    Nbytes += nbytes;
     
    178164  }
    179165  if (ibuffer[Nbytes]) ibuffer[Nbytes] = 0;
     166  myAssert (Nbytes < NBYTES, "oops");
    180167
    181168  /* add the optional variables to the end of the input buffer */
     
    183170    snprintf (line, 256, "%s %s\n", "CONFIG", ConfigVariable);
    184171    Ncpy = strlen (line);
    185     if (Nbytes + Ncpy >= NBYTES - 1) {
     172    if (Nbytes + Ncpy >= NBYTES - NXTRA) {
    186173      NBYTES = Nbytes + Ncpy + D_NBYTES;
    187174      REALLOCATE (ibuffer, char, NBYTES);
     
    194181    snprintf (line, 256, "%s %s\n", DefineVariable[i], DefineValue[i]);
    195182    Ncpy = strlen (line);
    196     if (Nbytes + Ncpy >= NBYTES - 1) {
     183    if (Nbytes + Ncpy >= NBYTES - NXTRA) {
    197184      NBYTES = Nbytes + Ncpy + D_NBYTES;
    198185      REALLOCATE (ibuffer, char, NBYTES);
     
    202189    ibuffer[Nbytes] = 0;
    203190  }
     191  myAssert (Nbytes < NBYTES, "oops");
    204192 
    205193  /* loop over input buffer, interpolating 'input' lines until none are added */
     
    218206      /* copy data from last point to before 'input' */
    219207      Ncpy = next - last - 5;
    220       if (Nout + Ncpy >= NBYTES - 1) {
     208      if (Nout + Ncpy >= NBYTES - NXTRA) {
    221209        NBYTES = Nout + Ncpy + D_NBYTES;
    222210        REALLOCATE (obuffer, char, NBYTES);
     
    230218      if (tbuffer != (char *) NULL) {
    231219        Ncpy = strlen (tbuffer);
    232         if (Nout + Ncpy >= NBYTES - 1) {
     220        if (Nout + Ncpy >= NBYTES - NXTRA) {
    233221          NBYTES = Nout + Ncpy + D_NBYTES;
    234222          REALLOCATE (obuffer, char, NBYTES);
     
    248236    /* last set of bytes after last input */
    249237    Ncpy = strlen (last);
    250     if (Nout + Ncpy >= NBYTES - 1) {
     238    if (Nout + Ncpy >= NBYTES - NXTRA) {
    251239      NBYTES = Nout + Ncpy + D_NBYTES;
    252240      REALLOCATE (obuffer, char, NBYTES);
     
    277265  va_start (argp, Nentry);
    278266
    279   int Nchar = strlen (field) + 16;
     267  int Nchar = strlen (field) + NXTRA;
    280268  ALLOCATE (tfield, char, Nchar);
    281269  snprintf (tfield, Nchar, "\n%s", field);
     
    341329}
    342330
    343 
    344331char *expandline (char *line, char *config) {
    345332
    346   int Nin, Nout, Ncpy, NBYTES;
     333  int Nin, Nout, Ncpy;
    347334  char *p1, *p2, word[256], value[256];
    348335  char *outline;
    349336 
    350   NBYTES = 256;
     337  int DBYTES = 256;
     338  int NBYTES = 256;
    351339  ALLOCATE (outline, char, NBYTES);
    352340  Nout = 0;
     
    354342  while ((p1 = strchr (&line[Nin], '$')) != (char *) NULL) {
    355343    Ncpy = p1 - line - Nin;
    356     if (Nout + Ncpy >= NBYTES) {
    357       NBYTES = Nout + Ncpy + 256;
    358       REALLOCATE (outline, char, NBYTES - 2);
     344    if (Nout + Ncpy >= NBYTES - NXTRA) {
     345      NBYTES = Nout + Ncpy + DBYTES;
     346      REALLOCATE (outline, char, NBYTES);
    359347    }
    360348    memcpy (&outline[Nout], &line[Nin], Ncpy);
     
    373361    }
    374362    Ncpy = strlen(value);
    375     if (Nout + Ncpy >= NBYTES - 2) {
    376       NBYTES = Nout + Ncpy + 256;
     363    if (Nout + Ncpy >= NBYTES - NXTRA) {
     364      NBYTES = Nout + Ncpy + DBYTES;
    377365      REALLOCATE (outline, char, NBYTES);
    378366    }   
     
    381369  }
    382370  Ncpy = strlen(&line[Nin]);
    383   if (Nout + Ncpy >= NBYTES - 2) {
    384     NBYTES = Nout + Ncpy + 256;
     371  if (Nout + Ncpy >= NBYTES - NXTRA) {
     372    NBYTES = Nout + Ncpy + DBYTES;
    385373    REALLOCATE (outline, char, NBYTES);
    386374  } 
     
    409397  Nbytes = 0;
    410398  NBYTES = D_NBYTES;
    411   ALLOCATE_ZERO (ibuffer, char, NBYTES + 2);
     399  ALLOCATE_ZERO (ibuffer, char, NBYTES + NXTRA);
    412400
    413401  /* load data from file */
     
    415403    Nbytes += nbytes;
    416404    NBYTES += D_NBYTES;
    417     REALLOCATE (ibuffer, char, NBYTES + 2);
    418     memset (&ibuffer[NBYTES - D_NBYTES], 0, NBYTES - D_NBYTES + 2);
     405    REALLOCATE (ibuffer, char, NBYTES + NXTRA);
     406    memset (&ibuffer[NBYTES - D_NBYTES], 0, NBYTES - D_NBYTES);
    419407  }
    420408  Nbytes += nbytes;
     
    427415  }
    428416  if (ibuffer[Nbytes]) ibuffer[Nbytes] = 0;
     417  myAssert (Nbytes < NBYTES, "oops");
    429418
    430419  if (options) {
     
    433422      snprintf (line, 256, "%s %s\n", "CONFIG", ConfigVariable);
    434423      Ncpy = strlen (line);
    435       if (Nbytes + Ncpy >= NBYTES) {
     424      if (Nbytes + Ncpy >= NBYTES - NXTRA) {
    436425        int Nbytes_old = NBYTES;
    437426        NBYTES = Nbytes + Ncpy + D_NBYTES;
    438         REALLOCATE (ibuffer, char, NBYTES + 2);
    439         memset (&ibuffer[Nbytes_old], 0, NBYTES + 2 - Nbytes_old);
     427        REALLOCATE (ibuffer, char, NBYTES + NXTRA);
     428        memset (&ibuffer[Nbytes_old], 0, NBYTES - Nbytes_old);
    440429      }   
    441430      memcpy (&ibuffer[Nbytes], line, Ncpy);
     
    446435      snprintf (line, 256, "%s %s\n", DefineVariable[i], DefineValue[i]);
    447436      Ncpy = strlen (line);
    448       if (Nbytes + Ncpy >= NBYTES) {
     437      if (Nbytes + Ncpy >= NBYTES - NXTRA) {
    449438        int Nbytes_old = NBYTES;
    450439        NBYTES = Nbytes + Ncpy + D_NBYTES;
    451         REALLOCATE (ibuffer, char, NBYTES + 2);
    452         memset (&ibuffer[Nbytes_old], 0, NBYTES + 2 - Nbytes_old);
     440        REALLOCATE (ibuffer, char, NBYTES + NXTRA);
     441        memset (&ibuffer[Nbytes_old], 0, NBYTES - Nbytes_old);
    453442      }   
    454443      memcpy (&ibuffer[Nbytes], line, Ncpy);
  • trunk/Ohana/src/libohana/src/findexec.c

    r38553 r39393  
    7575  int status;
    7676  int valid;
     77
     78  myAssert (basefile, "oops");
     79  myAssert (basefile[0], "oops");
    7780
    7881  uid = getuid();
     
    152155  char *c, *file;
    153156
     157  myAssert (strlen(infile), "invalid zero-length infile");
     158
    154159  /* make working version */
    155160  file = strcreate (infile);
     
    238243  char *c, *file;
    239244
    240   ALLOCATE (file, char, strlen(name) + 1);
     245  ALLOCATE (file, char, strlen(name) + 16);
    241246
    242247  c = strrchr (name, '/');
  • trunk/Ohana/src/libohana/src/glockfile.c

    r38553 r39393  
    44 
    55  int status;
    6   char mode[3];
     6  char mode[16];
    77  int fd;
    88  FILE *f;
  • trunk/Ohana/src/libohana/src/ohana_allocate.c

    r39324 r39393  
    55# include <pthread.h>
    66# include <string.h>
     7# include <errno.h>
    78
    89# define TEST_SAVE_FREE_BLOCKS 0
     
    5354
    5455  // total size is : memblock + data + endpost
    55   new = (OhanaMemblock *) malloc (sizeof(OhanaMemblock) + size + sizeof(void *));
     56  new = (OhanaMemblock *) malloc (sizeof(OhanaMemblock) + size + 2*sizeof(void *));
    5657  if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d, %s)\n", file, line, func);
     58  if (errno == ENOMEM) abort();
    5759
    5860  // pointer to the start of the user memory
     
    7173  new->freed = FALSE;
    7274
    73   // to be clean, zero out the memory
    74   // memset (ptr, 0, new->size);
     75  // poison the whole memory: we should not rely of 0'ed memory
     76  memset (ptr, 0x7f, new->size);
    7577
    7678  // new memblock becomes the 'lastBlock':
     
    98100  Nblock ++;
    99101  pthread_mutex_unlock(&memBlockListMutex);
     102
     103  if (!new->nextBlock && !new->prevBlock && (Nblock > 1)) abort();
    100104
    101105  return (ptr);
     
    219223
    220224  ref = (OhanaMemblock *) in - 1;
     225  if (!ref->nextBlock && !ref->prevBlock && (Nblock > 1)) ohana_memabort ("orphan block?? (%s@%d : %s)\n", file, line, func);
    221226
    222227  if (ref->freed) ohana_memabort ("memory already freed (%s, %d, %s [%d bytes])\n", ref->file, ref->line, ref->func, ref->size);
     228  // if (!ref->nextBlock && !ref->prevBlock && (Nblock > 1)) ohana_memabort ("orphan block?? (%s@%d : %s)\n", file, line, func);
     229
    223230  ref->freed = TRUE;
     231  if (!ref->nextBlock && !ref->prevBlock && (Nblock > 1)) ohana_memabort ("orphan block?? (%s@%d : %s)\n", file, line, func);
    224232
    225233  // fprintf (stderr, "  file: %s, line: %d, func: %s, size: %zd, addr: %zx\n",
     
    227235
    228236  if (!lastBlock) ohana_memabort ("corruption? (%s@%d : %s)\n", file, line, func);
     237  if (!ref->nextBlock && !ref->prevBlock && (Nblock > 1)) ohana_memabort ("orphan block?? (%s@%d : %s)\n", file, line, func);
    229238
    230239  pthread_mutex_lock(&memBlockListMutex);
     
    260269  // pointer to the start of the user memory
    261270  void *ptr = (char *)(ref + 1);
    262   memset (ptr, 0x77, ref->size);
     271  memset (ptr, 0x7f, ref->size);
    263272
    264273  Nblock --;
     
    268277 
    269278# if (!TEST_SAVE_FREE_BLOCKS)
     279  memset (ref, 0x7f, sizeof(OhanaMemblock));
    270280  free (ref);
    271281# endif
  • trunk/Ohana/src/libohana/src/string.c

    r38441 r39393  
    186186    line[i] = c;
    187187  }
    188   line[i - 1] = 0;  /* this could make things crash! */
     188  if (i > 0) line[i - 1] = 0;  /* this could make things crash! */
    189189
    190190  if (i > 1) {
Note: See TracChangeset for help on using the changeset viewer.