Blame posix/tst-glob-tilde.c

Packit 6c4009
/* Check for GLOB_TIDLE heap allocation issues (bugs 22320, 22325, 22332).
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <glob.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
/* Flag which indicates whether to pass the GLOB_ONLYDIR flag.  */
Packit 6c4009
static int do_onlydir;
Packit 6c4009
Packit 6c4009
/* Flag which indicates whether to pass the GLOB_NOCHECK flag.  */
Packit 6c4009
static int do_nocheck;
Packit 6c4009
Packit 6c4009
/* Flag which indicates whether to pass the GLOB_MARK flag.  */
Packit 6c4009
static int do_mark;
Packit 6c4009
Packit 6c4009
/* Flag which indicates whether to pass the GLOB_NOESCAPE flag.  */
Packit 6c4009
static int do_noescape;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
one_test (const char *prefix, const char *middle, const char *suffix)
Packit 6c4009
{
Packit 6c4009
  char *pattern = xasprintf ("%s%s%s", prefix, middle, suffix);
Packit 6c4009
  int flags = GLOB_TILDE;
Packit 6c4009
  if (do_onlydir)
Packit 6c4009
    flags |= GLOB_ONLYDIR;
Packit 6c4009
  if (do_nocheck)
Packit 6c4009
    flags |= GLOB_NOCHECK;
Packit 6c4009
  if (do_mark)
Packit 6c4009
    flags |= GLOB_MARK;
Packit 6c4009
  if (do_noescape)
Packit 6c4009
    flags |= GLOB_NOESCAPE;
Packit 6c4009
  glob_t gl;
Packit 6c4009
  /* This glob call might result in crashes or memory leaks.  */
Packit 6c4009
  if (glob (pattern, flags, NULL, &gl) == 0)
Packit 6c4009
    globfree (&gl);
Packit 6c4009
  free (pattern);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum
Packit 6c4009
  {
Packit 6c4009
    /* The largest base being tested.  */
Packit 6c4009
    largest_base_size = 500000,
Packit 6c4009
Packit 6c4009
    /* The actual size is the base size plus a variable whose absolute
Packit 6c4009
       value is not greater than this.  This helps malloc to trigger
Packit 6c4009
       overflows.  */
Packit 6c4009
    max_size_skew = 16,
Packit 6c4009
Packit 6c4009
    /* The maximum string length supported by repeating_string
Packit 6c4009
       below.  */
Packit 6c4009
    repeat_size = largest_base_size + max_size_skew,
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* Used to construct strings which repeat a single character 'x'.  */
Packit 6c4009
static char *repeat;
Packit 6c4009
Packit 6c4009
/* Return a string of SIZE characters.  */
Packit 6c4009
const char *
Packit 6c4009
repeating_string (int size)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY (size >= 0);
Packit 6c4009
  TEST_VERIFY (size <= repeat_size);
Packit 6c4009
  const char *repeated_shifted = repeat + repeat_size - size;
Packit 6c4009
  TEST_VERIFY (strlen (repeated_shifted) == size);
Packit 6c4009
  return repeated_shifted;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Avoid network-based NSS modules and initialize nss_files with a
Packit 6c4009
     dummy lookup.  This has to come before mtrace because NSS does
Packit 6c4009
     not free all memory.  */
Packit 6c4009
  __nss_configure_lookup ("passwd", "files");
Packit 6c4009
  (void) getpwnam ("root");
Packit 6c4009
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  repeat = xmalloc (repeat_size + 1);
Packit 6c4009
  memset (repeat, 'x', repeat_size);
Packit 6c4009
  repeat[repeat_size] = '\0';
Packit 6c4009
Packit 6c4009
  /* These numbers control the size of the user name.  The values
Packit 6c4009
     cover the minimum (0), a typical size (8), a large
Packit 6c4009
     stack-allocated size (100000), and a somewhat large
Packit 6c4009
     heap-allocated size (largest_base_size).  */
Packit 6c4009
  static const int base_sizes[] = { 0, 8, 100, 100000, largest_base_size, -1 };
Packit 6c4009
Packit 6c4009
  for (do_onlydir = 0; do_onlydir < 2; ++do_onlydir)
Packit 6c4009
    for (do_nocheck = 0; do_nocheck < 2; ++do_nocheck)
Packit 6c4009
      for (do_mark = 0; do_mark < 2; ++do_mark)
Packit 6c4009
	for (do_noescape = 0; do_noescape < 2; ++do_noescape)
Packit 6c4009
	  for (int base_idx = 0; base_sizes[base_idx] >= 0; ++base_idx)
Packit 6c4009
	    {
Packit 6c4009
	      for (int size_skew = -max_size_skew; size_skew <= max_size_skew;
Packit 6c4009
		   ++size_skew)
Packit 6c4009
		{
Packit 6c4009
		  int size = base_sizes[base_idx] + size_skew;
Packit 6c4009
		  if (size < 0)
Packit 6c4009
		    continue;
Packit 6c4009
Packit 6c4009
		  const char *user_name = repeating_string (size);
Packit 6c4009
		  one_test ("~", user_name, "/a/b");
Packit 6c4009
		  one_test ("~", user_name, "x\\x\\x////x\\a");
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      const char *user_name = repeating_string (base_sizes[base_idx]);
Packit 6c4009
	      one_test ("~", user_name, "");
Packit 6c4009
	      one_test ("~", user_name, "/");
Packit 6c4009
	      one_test ("~", user_name, "/a");
Packit 6c4009
	      one_test ("~", user_name, "/*/*");
Packit 6c4009
	      one_test ("~", user_name, "\\/");
Packit 6c4009
	      one_test ("/~", user_name, "");
Packit 6c4009
	      one_test ("*/~", user_name, "/a/b");
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
  free (repeat);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 200
Packit 6c4009
#include <support/test-driver.c>