Changeset 6935428 in rtems


Ignore:
Timestamp:
Nov 14, 2014, 10:30:50 AM (5 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
002f351
Parents:
80055514
git-author:
Sebastian Huber <sebastian.huber@…> (11/14/14 10:30:50)
git-committer:
Sebastian Huber <sebastian.huber@…> (11/20/14 09:30:22)
Message:

libcsupport: Avoid TOCTOU and format errors

Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libcsupport/src/pwdgrp.c

    r80055514 r6935428  
    4040static pthread_once_t pwdgrp_once = PTHREAD_ONCE_INIT;
    4141
     42static void init_file(const char *name, const char *content)
     43{
     44  FILE *fp = fopen(name, "wx");
     45
     46  if (fp != NULL) {
     47    fputs(content, fp);
     48    fclose(fp);
     49  }
     50}
     51
    4252/**
    4353 *  Initialize useable but dummy databases
     
    4555static void pwdgrp_init(void)
    4656{
    47   FILE *fp;
    48 
    4957  mkdir("/etc", 0777);
    5058
     
    5260   *  Initialize /etc/passwd
    5361   */
    54   if ((fp = fopen("/etc/passwd", "r")) != NULL) {
    55     fclose(fp);
    56   }
    57   else if ((fp = fopen("/etc/passwd", "w")) != NULL) {
    58     fprintf(fp, "root:*:0:0:root::/:/bin/sh\n"
    59                  "rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
    60                  "tty:!:2:2:tty owner::/:/bin/false\n" );
    61     fclose(fp);
    62   }
     62  init_file(
     63    "/etc/passwd",
     64    "root:*:0:0:root::/:/bin/sh\n"
     65      "rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
     66      "tty:!:2:2:tty owner::/:/bin/false\n"
     67  );
    6368
    6469  /*
    6570   *  Initialize /etc/group
    6671   */
    67   if ((fp = fopen("/etc/group", "r")) != NULL) {
    68     fclose(fp);
    69   }
    70   else if ((fp = fopen("/etc/group", "w")) != NULL) {
    71     fprintf( fp, "root:x:0:root\n"
    72                  "rtems:x:1:rtems\n"
    73                  "tty:x:2:tty\n" );
    74     fclose(fp);
    75   }
     72  init_file(
     73    "/etc/group",
     74    "root:x:0:root\n"
     75      "rtems:x:1:rtems\n"
     76      "tty:x:2:tty\n"
     77  );
    7678}
    7779
  • testsuites/libtests/Makefile.am

    r80055514 r6935428  
    22
    33_SUBDIRS = POSIX
     4_SUBDIRS += pwdgrp01
    45_SUBDIRS += crypt01
    56_SUBDIRS += sha
  • testsuites/libtests/configure.ac

    r80055514 r6935428  
    6767# Explicitly list all Makefiles here
    6868AC_CONFIG_FILES([Makefile
     69pwdgrp01/Makefile
    6970crypt01/Makefile
    7071sha/Makefile
Note: See TracChangeset for help on using the changeset viewer.