Changeset 373ccbb9 in rtems


Ignore:
Timestamp:
Nov 17, 2014, 1:55:38 PM (5 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
fa028bb
Parents:
065d72ce
git-author:
Sebastian Huber <sebastian.huber@…> (11/17/14 13:55:38)
git-committer:
Sebastian Huber <sebastian.huber@…> (11/20/14 09:30:24)
Message:

shell: Use exiting once initialization

Avoid TOCTOU issues. Avoid pull in of global buffers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/shell/shell.c

    r065d72ce r373ccbb9  
    5757};
    5858
    59 static pthread_once_t rtems_shell_current_env_once = PTHREAD_ONCE_INIT;
     59static pthread_once_t rtems_shell_once = PTHREAD_ONCE_INIT;
     60
    6061static pthread_key_t rtems_shell_current_env_key;
    6162
     
    102103}
    103104
    104 /*
    105  *  Create the posix key.
    106  */
    107 static void rtems_shell_current_env_make_key(void)
    108 {
    109   (void) pthread_key_create(&rtems_shell_current_env_key, rtems_shell_env_free);
     105static void rtems_shell_create_file(const char *name, const char *content)
     106{
     107  FILE *fp = fopen(name, "wx");
     108
     109  if (fp != NULL) {
     110    fputs(content, fp);
     111    fclose(fp);
     112  }
     113}
     114
     115static void rtems_shell_init_once(void)
     116{
     117  struct passwd pwd;
     118  struct passwd *pwd_res;
     119
     120  pthread_key_create(&rtems_shell_current_env_key, rtems_shell_env_free);
     121
     122  /* dummy call to init /etc dir */
     123  getpwuid_r(0, &pwd, NULL, 0, &pwd_res);
     124
     125  rtems_shell_create_file("etc/issue",
     126                          "\n"
     127                          "Welcome to @V\\n"
     128                          "Login into @S\\n");
     129
     130  rtems_shell_create_file("/etc/issue.net",
     131                          "\n"
     132                          "Welcome to %v\n"
     133                          "running on %m\n");
    110134}
    111135
     
    505529 * ----------------------------------------------- */
    506530
    507 static void rtems_shell_init_issue(void)
    508 {
    509   static bool issue_inited=false;
    510   struct stat buf;
    511 
    512   if (issue_inited)
    513     return;
    514   issue_inited = true;
    515 
    516   /* dummy call to init /etc dir */
    517   getpwnam("root");
    518 
    519   if (stat("/etc/issue",&buf)) {
    520     rtems_shell_write_file("/etc/issue",
    521                            "\n"
    522                            "Welcome to @V\\n"
    523                            "Login into @S\\n");
    524   }
    525 
    526   if (stat("/etc/issue.net",&buf)) {
    527      rtems_shell_write_file("/etc/issue.net",
    528                            "\n"
    529                             "Welcome to %v\n"
    530                             "running on %m\n");
    531   }
    532 }
    533 
    534531static bool rtems_shell_login(FILE * in,FILE * out) {
    535532  rtems_shell_env_t *env;
     
    541538  assert(env != NULL);
    542539
    543   rtems_shell_init_issue();
    544540  setuid(0);
    545541  setgid(0);
     
    710706  rtems_shell_initialize_command_set();
    711707
    712   eno = pthread_once(
    713     &rtems_shell_current_env_once,
    714     rtems_shell_current_env_make_key
    715   );
     708  eno = pthread_once(&rtems_shell_once, rtems_shell_init_once);
    716709  assert(eno == 0);
    717710
Note: See TracChangeset for help on using the changeset viewer.